Skip to content

Commit

Permalink
Also add derive rpc macro pr
Browse files Browse the repository at this point in the history
  • Loading branch information
ercecan committed Dec 20, 2024
1 parent c72e0fc commit 24b18d9
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 171 deletions.
3 changes: 1 addition & 2 deletions bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ fn test_helper(
.await
.unwrap();
let addr = server.local_addr().unwrap();
let server_rpc_module =
sov_ledger_rpc::server::rpc_module::<LedgerDB, u32, u32>(ledger_db).unwrap();
let server_rpc_module = sov_ledger_rpc::server::create_rpc_module(ledger_db);
let _server_handle = server.start(server_rpc_module);

let rpc_config = RpcConfig {
Expand Down
4 changes: 2 additions & 2 deletions bin/citrea/tests/test_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use reth_primitives::{Address, BlockId, BlockNumberOrTag, Bytes, TxHash, TxKind,
// use reth_rpc_types::TransactionReceipt;
use reth_rpc_types::trace::geth::{GethDebugTracingOptions, GethTrace};
use reth_rpc_types::RichBlock;
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::HexHash;
use sov_ledger_rpc::LedgerRpcClient;
use sov_rollup_interface::rpc::{
LastVerifiedProofResponse, ProofResponse, SequencerCommitmentResponse,
SoftConfirmationResponse, SoftConfirmationStatus, VerifiedProofResponse,
Expand Down Expand Up @@ -519,7 +519,7 @@ impl TestClient {

pub(crate) async fn ledger_get_last_scanned_l1_height(&self) -> u64 {
self.http_client
.request("ledger_getLastScannedL1Hieght", rpc_params![])
.request("ledger_getLastScannedL1Height", rpc_params![])
.await
.unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reth_rpc_types::trace::geth::{GethDebugTracingOptions, GethTrace};
use reth_rpc_types::{FeeHistory, Index};
use serde_json::json;
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::LedgerRpcClient;
use sov_modules_api::da::BlockHeaderTrait;
use sov_modules_api::utils::to_jsonrpsee_error_object;
use sov_modules_api::WorkingSet;
Expand Down
6 changes: 3 additions & 3 deletions crates/fullnode/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use jsonrpsee::RpcModule;
use reth_primitives::U64;
use sov_db::ledger_db::NodeLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::LedgerRpcClient;
use sov_modules_api::Context;
use sov_modules_stf_blueprint::StfBlueprintTrait;
use sov_rollup_interface::da::{BlockHeaderTrait, DaSpec};
Expand Down Expand Up @@ -451,10 +451,10 @@ async fn sync_l2(
let inner_client = &sequencer_client;
let soft_confirmations = match retry_backoff(exponential_backoff.clone(), || async move {
match inner_client
.get_soft_confirmation_range((
.get_soft_confirmation_range(
U64::from(l2_height),
U64::from(l2_height + sync_blocks_count - 1),
))
)
.await
{
Ok(soft_confirmations) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/prover/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use jsonrpsee::RpcModule;
use reth_primitives::U64;
use sov_db::ledger_db::ProverLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_ledger_rpc::client::RpcClient;
use sov_ledger_rpc::LedgerRpcClient;
use sov_modules_api::storage::HierarchicalStorageManager;
use sov_modules_api::{Context, SlotData};
use sov_modules_stf_blueprint::StfBlueprintTrait;
Expand Down Expand Up @@ -475,10 +475,10 @@ async fn sync_l2(
let inner_client = &sequencer_client;
let soft_confirmations = match retry_backoff(exponential_backoff.clone(), || async move {
let soft_confirmations = inner_client
.get_soft_confirmation_range((
.get_soft_confirmation_range(
U64::from(l2_height),
U64::from(l2_height + sync_blocks_count - 1),
))
)
.await;

match soft_confirmations {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use serde::de::DeserializeOwned;
use sov_rollup_interface::rpc::{
sequencer_commitment_to_response, LastVerifiedProofResponse, LedgerRpcProvider, ProofResponse,
SequencerCommitmentResponse, SoftConfirmationIdentifier, SoftConfirmationResponse,
Expand Down Expand Up @@ -115,6 +114,7 @@ impl LedgerRpcProvider for LedgerDB {
None => Ok(sov_rollup_interface::rpc::SoftConfirmationStatus::Trusted),
}
}

fn get_slot_number_by_hash(&self, hash: [u8; 32]) -> Result<Option<u64>, anyhow::Error> {
self.db.get::<SlotByHash>(&hash).map(|v| v.map(|a| a.0))
}
Expand Down Expand Up @@ -201,6 +201,10 @@ impl LedgerRpcProvider for LedgerDB {
let next_ids = self.get_next_items_numbers();
Ok(next_ids.soft_confirmation_number.saturating_sub(1))
}

fn get_l2_genesis_state_root(&self) -> Result<Option<Vec<u8>>, anyhow::Error> {
self.get_l2_state_root(0)
}
}

impl LedgerDB {
Expand Down
124 changes: 121 additions & 3 deletions crates/sovereign-sdk/full-node/sov-ledger-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#![forbid(unsafe_code)]

use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use reth_primitives::U64;
use sov_rollup_interface::rpc::{
LastVerifiedProofResponse, ProofResponse, SequencerCommitmentResponse,
SoftConfirmationResponse, SoftConfirmationStatus, VerifiedProofResponse,
};

#[cfg(feature = "server")]
pub mod server;

#[cfg(feature = "client")]
pub mod client;

/// A 32-byte hash [`serde`]-encoded as a hex string optionally prefixed with
/// `0x`. See [`sov_rollup_interface::rpc::utils::rpc_hex`].
#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -16,3 +21,116 @@ impl From<[u8; 32]> for HexHash {
Self(v)
}
}

/// A [`jsonrpsee`] trait for interacting with the ledger JSON-RPC API.
///
/// Client and server implementations are automatically generated by
/// [`jsonrpsee`], see [`LedgerRpcClient`] and [`LedgerRpcServer`].
///
/// For more information about the specific methods, see the
/// [`sov_rollup_interface::rpc`] module.
#[cfg_attr(
all(feature = "server", feature = "client"),
rpc(server, client, namespace = "ledger")
)]
#[cfg_attr(
all(feature = "server", not(feature = "client")),
rpc(server, namespace = "ledger")
)]
#[cfg_attr(
all(not(feature = "server"), feature = "client"),
rpc(client, namespace = "ledger")
)]
pub trait LedgerRpc {
/// Gets a single soft confirmation by number.
#[method(name = "getSoftConfirmationByNumber")]
#[blocking]
fn get_soft_confirmation_by_number(
&self,
number: U64,
) -> RpcResult<Option<SoftConfirmationResponse>>;

/// Gets a single soft confirmation by hash.
#[method(name = "getSoftConfirmationByHash")]
#[blocking]
fn get_soft_confirmation_by_hash(
&self,
hash: HexHash,
) -> RpcResult<Option<SoftConfirmationResponse>>;

/// Gets all soft confirmations with numbers `range.start` to `range.end`.
#[method(name = "getSoftConfirmationRange")]
#[blocking]
fn get_soft_confirmation_range(
&self,
start: U64,
end: U64,
) -> RpcResult<Vec<Option<SoftConfirmationResponse>>>;

/// Gets a single event by number.
#[method(name = "getSoftConfirmationStatus")]
#[blocking]
fn get_soft_confirmation_status(
&self,
soft_confirmation_receipt: U64,
) -> RpcResult<SoftConfirmationStatus>;

/// Gets the L2 genesis state root.
#[method(name = "getL2GenesisStateRoot")]
#[blocking]
fn get_l2_genesis_state_root(&self) -> RpcResult<Option<Vec<u8>>>;

/// Gets the commitments in the DA slot with the given height.
#[method(name = "getSequencerCommitmentsOnSlotByNumber")]
#[blocking]
fn get_sequencer_commitments_on_slot_by_number(
&self,
height: U64,
) -> RpcResult<Option<Vec<SequencerCommitmentResponse>>>;

/// Gets the commitments in the DA slot with the given hash.
#[method(name = "getSequencerCommitmentsOnSlotByHash")]
#[blocking]
fn get_sequencer_commitments_on_slot_by_hash(
&self,
hash: HexHash,
) -> RpcResult<Option<Vec<SequencerCommitmentResponse>>>;

/// Gets proof by slot height.
#[method(name = "getProofsBySlotHeight")]
#[blocking]
fn get_proofs_by_slot_height(&self, height: U64) -> RpcResult<Option<Vec<ProofResponse>>>;

/// Gets proof by slot hash.
#[method(name = "getProofsBySlotHash")]
#[blocking]
fn get_proofs_by_slot_hash(&self, hash: HexHash) -> RpcResult<Option<Vec<ProofResponse>>>;

/// Gets the height pf most recent committed soft confirmation.
#[method(name = "getHeadSoftConfirmation")]
#[blocking]
fn get_head_soft_confirmation(&self) -> RpcResult<Option<SoftConfirmationResponse>>;

/// Gets the height pf most recent committed soft confirmation.
#[method(name = "getHeadSoftConfirmationHeight")]
#[blocking]
fn get_head_soft_confirmation_height(&self) -> RpcResult<u64>;

/// Gets verified proofs by slot height
#[method(name = "getVerifiedProofsBySlotHeight")]
#[blocking]
fn get_verified_proofs_by_slot_height(
&self,
height: U64,
) -> RpcResult<Option<Vec<VerifiedProofResponse>>>;

/// Gets last verified proog
#[method(name = "getLastVerifiedProof")]
#[blocking]
fn get_last_verified_proof(&self) -> RpcResult<Option<LastVerifiedProofResponse>>;

/// Get last scanned l1 height
#[method(name = "getLastScannedL1Height")]
#[blocking]
fn get_last_scanned_l1_height(&self) -> RpcResult<u64>;
}
Loading

0 comments on commit 24b18d9

Please sign in to comment.