Skip to content

Commit

Permalink
Add support for ledger_getEvents in RpcClient
Browse files Browse the repository at this point in the history
Related GH issue: <#1066>.

Signed-off-by: Filippo Costa <[email protected]>
Co-authored-by: Filippo Costa <[email protected]>
Co-authored-by: Roman Krasiuk <[email protected]>
  • Loading branch information
neysofu and rkrasiuk committed Oct 18, 2023
1 parent 606d7b8 commit 3f2925b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
11 changes: 7 additions & 4 deletions full-node/sov-ledger-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! See [`RpcClient`].

use jsonrpsee::proc_macros::rpc;
use sov_rollup_interface::rpc::{BatchIdentifier, QueryMode, SlotIdentifier, TxIdentifier};
use sov_rollup_interface::rpc::{
BatchIdentifier, EventIdentifier, QueryMode, SlotIdentifier, TxIdentifier,
};
use sov_rollup_interface::stf::Event;

use crate::HexHash;
Expand All @@ -16,9 +18,6 @@ use crate::HexHash;
///
/// For more information about the specific methods, see the
/// [`sov_rollup_interface::rpc`] module.
///
/// TODO: `getEvents`, which has a `Vec<T>` as a single parameter. That's not
/// supported by `jsonrpsee`, surprisingly.
#[rpc(client, namespace = "ledger")]
pub trait Rpc<Slot, Batch, Tx>
where
Expand Down Expand Up @@ -54,6 +53,10 @@ where
query_mode: QueryMode,
) -> RpcResult<Vec<Option<Tx>>>;

/// Gets a list of events by ID. The IDs need not be ordered.
#[method(name = "getEvents")]
async fn get_events(&self, event_ids: Vec<EventIdentifier>) -> RpcResult<Vec<Option<Event>>>;

/// Gets a single slot by hash.
#[method(name = "getSlotByHash")]
async fn get_slot_by_hash(
Expand Down
2 changes: 1 addition & 1 deletion full-node/sov-ledger-rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
.map_err(|e| to_jsonrpsee_error_object(e, LEDGER_RPC_ERROR))
})?;
rpc.register_method("ledger_getEvents", move |params, db| {
let ids: Vec<EventIdentifier> = params.parse()?;
let ids: Vec<EventIdentifier> = params.parse().or_else(|_| params.one())?;
db.get_events(&ids)
.map_err(|e| to_jsonrpsee_error_object(e, LEDGER_RPC_ERROR))
})?;
Expand Down
45 changes: 44 additions & 1 deletion full-node/sov-ledger-rpc/tests/empty_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use sov_db::ledger_db::LedgerDB;
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::server::rpc_module;
use sov_ledger_rpc::HexHash;
use sov_rollup_interface::rpc::{BatchResponse, QueryMode, SlotResponse, TxResponse};
use sov_modules_api::Event;
use sov_rollup_interface::rpc::{
BatchResponse, EventIdentifier, QueryMode, SlotResponse, TxIdAndOffset, TxIdentifier,
TxResponse,
};
use tempfile::tempdir;

async fn rpc_server() -> (jsonrpsee::server::ServerHandle, SocketAddr) {
Expand Down Expand Up @@ -58,6 +62,7 @@ async fn getters_succeed() {
.get_transactions(vec![], QueryMode::Compact)
.await
.unwrap();
rpc_client.get_events(vec![]).await.unwrap();

let hash = HexHash([0; 32]);
rpc_client
Expand Down Expand Up @@ -130,3 +135,41 @@ async fn get_head_with_optional_query_mode() {
.unwrap();
}
}

/// `ledger_getEvents` supports several parameter types, because of a
/// `jsonrpsee` limitation. See:
/// - https://github.com/Sovereign-Labs/sovereign-sdk/pull/1058
/// - https://github.com/Sovereign-Labs/sovereign-sdk/issues/1037
///
/// While `jsonrpsee` macro-generated clients can only generate nested array
/// types as parameters (e.g. `"params": [[1, 2, 3]]`), we want to test that
/// non-nested array types are also supported (e.g. `"params": [1, 2, 3]` and
/// `"params": [{"txId": 1, "offset": 2}]`).
#[tokio::test]
async fn get_events_patterns() {
let (_server_handle, addr) = rpc_server().await;
let rpc_client = rpc_client(addr).await;

rpc_client
.get_events(vec![EventIdentifier::Number(2)])
.await
.unwrap();
rpc_client
.request::<Vec<Option<Event>>, _>("ledger_getEvents", vec![vec![2]])
.await
.unwrap();
rpc_client
.request::<Vec<Option<Event>>, _>("ledger_getEvents", vec![2])
.await
.unwrap();
rpc_client
.request::<Vec<Option<Event>>, _>(
"ledger_getEvents",
vec![EventIdentifier::TxIdAndOffset(TxIdAndOffset {
tx_id: TxIdentifier::Number(1),
offset: 2,
})],
)
.await
.unwrap();
}

0 comments on commit 3f2925b

Please sign in to comment.