Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): implement ledger_getEvents #1058

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions full-node/sov-ledger-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jsonrpsee = { workspace = true }
serde = "1"
sov-rollup-interface = { path = "../../rollup-interface", features = ["native"] }
# Client dependencies
# (None)
async-trait = { workspace = true, optional = true }
# Server dependencies
anyhow = { version = "1", optional = true }
futures = { version = "0.3", optional = true }
Expand All @@ -33,4 +33,4 @@ sov-ledger-rpc = { path = ".", features = ["client", "server"] }
[features]
default = ["client", "server"]
server = ["anyhow", "futures", "jsonrpsee/server", "sov-modules-api"]
client = ["jsonrpsee/client", "jsonrpsee/macros"]
client = ["jsonrpsee/client", "jsonrpsee/macros", "async-trait"]
40 changes: 36 additions & 4 deletions full-node/sov-ledger-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
//!
//! See [`RpcClient`].

use jsonrpsee::core::params::ArrayParams;
use jsonrpsee::core::Error as RpcError;
use jsonrpsee::proc_macros::rpc;
use sov_rollup_interface::rpc::{BatchIdentifier, QueryMode, SlotIdentifier, TxIdentifier};
use serde::de::DeserializeOwned;
use serde::Serialize;
use sov_rollup_interface::rpc::{
BatchIdentifier, EventIdentifier, QueryMode, SlotIdentifier, TxIdentifier,
};
use sov_rollup_interface::stf::Event;

use crate::HexHash;
Expand All @@ -16,9 +22,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 @@ -140,3 +143,32 @@ where
#[subscription(name = "subscribeSlots", item = u64)]
async fn subscribe_slots(&self) -> SubscriptionResult;
}

/// `jsonrpsee`'s rpc macro does not support dynamic array as parameters.
/// Implement `ledger_getEvents` by extending the core ledger rpc with the hand rolled method.
#[async_trait::async_trait]
pub trait RpcExt<Slot, Batch, Tx>: RpcClient<Slot, Batch, Tx>
where
Slot: DeserializeOwned + Serialize + Send + Sync + 'static,
Batch: DeserializeOwned + Serialize + Send + Sync + 'static,
Tx: DeserializeOwned + Serialize + Send + Sync + 'static,
{
async fn get_events(&self, ids: Vec<EventIdentifier>) -> Result<Vec<Option<Event>>, RpcError>;
}

#[async_trait::async_trait]
impl<Slot, Batch, Tx, T> RpcExt<Slot, Batch, Tx> for T
where
Slot: DeserializeOwned + Serialize + Send + Sync + 'static,
Batch: DeserializeOwned + Serialize + Send + Sync + 'static,
Tx: DeserializeOwned + Serialize + Send + Sync + 'static,
T: RpcClient<Slot, Batch, Tx> + Send + Sync,
{
async fn get_events(&self, ids: Vec<EventIdentifier>) -> Result<Vec<Option<Event>>, RpcError> {
let mut params = ArrayParams::new();
for id in ids {
params.insert(id)?;
}
self.request("ledger_getEvents", params).await
}
}
6 changes: 5 additions & 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,11 @@ 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> = if params.as_str().is_some() {
params.parse()?
} else {
vec![]
};
db.get_events(&ids)
.map_err(|e| to_jsonrpsee_error_object(e, LEDGER_RPC_ERROR))
})?;
Expand Down
19 changes: 17 additions & 2 deletions full-node/sov-ledger-rpc/tests/empty_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use std::sync::Arc;
use jsonrpsee::core::client::{ClientT, SubscriptionClientT};
use jsonrpsee::core::params::ArrayParams;
use sov_db::ledger_db::LedgerDB;
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::client::{RpcClient, RpcExt};
use sov_ledger_rpc::server::rpc_module;
use sov_ledger_rpc::HexHash;
use sov_rollup_interface::rpc::{BatchResponse, QueryMode, SlotResponse, TxResponse};
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 @@ -98,6 +101,18 @@ async fn getters_succeed() {
.get_txs_range(0, 1, QueryMode::Compact)
.await
.unwrap();

rpc_client.get_events(vec![]).await.unwrap();
rpc_client
.get_events(vec![
EventIdentifier::Number(1),
EventIdentifier::TxIdAndOffset(TxIdAndOffset {
tx_id: TxIdentifier::Number(10),
offset: 10,
}),
])
.await
.unwrap();
}

#[tokio::test]
Expand Down
Loading