Skip to content

Commit 68b4b43

Browse files
authored
feat: add getL1MessageByIndex rpc method (#370)
* add getL1MessageByIndex rpc method * integrate with new database API
1 parent 15c4b3b commit 68b4b43

File tree

9 files changed

+73
-4
lines changed

9 files changed

+73
-4
lines changed

crates/chain-orchestrator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ serde = [
117117
"scroll-alloy-hardforks/serde",
118118
"scroll-engine/serde",
119119
"scroll-network/serde",
120+
"rollup-node-primitives/serde",
120121
]

crates/chain-orchestrator/src/handle/command.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{ChainOrchestratorEvent, ChainOrchestratorStatus};
33
use reth_network_api::FullNetwork;
44
use reth_scroll_node::ScrollNetworkPrimitives;
55
use reth_tokio_util::EventStream;
6-
use rollup_node_primitives::BlockInfo;
6+
use rollup_node_primitives::{BlockInfo, L1MessageEnvelope};
77
use scroll_network::ScrollNetworkHandle;
88
use tokio::sync::oneshot;
99

@@ -24,7 +24,16 @@ pub enum ChainOrchestratorCommand<N: FullNetwork<Primitives = ScrollNetworkPrimi
2424
EnableAutomaticSequencing(oneshot::Sender<bool>),
2525
/// Disable automatic sequencing.
2626
DisableAutomaticSequencing(oneshot::Sender<bool>),
27+
/// Send a database query to the rollup manager.
28+
DatabaseQuery(DatabaseQuery),
2729
/// Enable gossiping of blocks to peers.
2830
#[cfg(feature = "test-utils")]
2931
SetGossip((bool, oneshot::Sender<()>)),
3032
}
33+
34+
/// The database queries that can be sent to the rollup manager.
35+
#[derive(Debug)]
36+
pub enum DatabaseQuery {
37+
/// Get L1 message by its index.
38+
GetL1MessageByIndex(u64, oneshot::Sender<Option<L1MessageEnvelope>>),
39+
}

crates/chain-orchestrator/src/handle/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use super::ChainOrchestratorEvent;
55
use reth_network_api::FullNetwork;
66
use reth_scroll_node::ScrollNetworkPrimitives;
77
use reth_tokio_util::EventStream;
8-
use rollup_node_primitives::BlockInfo;
8+
use rollup_node_primitives::{BlockInfo, L1MessageEnvelope};
99
use scroll_network::ScrollNetworkHandle;
1010
use tokio::sync::{mpsc, oneshot};
1111
use tracing::error;
1212

1313
mod command;
14-
pub use command::ChainOrchestratorCommand;
14+
pub use command::{ChainOrchestratorCommand, DatabaseQuery};
1515

1616
mod metrics;
1717
use metrics::ChainOrchestratorHandleMetrics;
@@ -90,6 +90,18 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> ChainOrchestratorHand
9090
rx.await
9191
}
9292

93+
/// Get an L1 message by its index.
94+
pub async fn get_l1_message_by_index(
95+
&self,
96+
index: u64,
97+
) -> Result<Option<L1MessageEnvelope>, oneshot::error::RecvError> {
98+
let (tx, rx) = oneshot::channel();
99+
self.send_command(ChainOrchestratorCommand::DatabaseQuery(
100+
DatabaseQuery::GetL1MessageByIndex(index, tx),
101+
));
102+
rx.await
103+
}
104+
93105
/// Sends a command to the rollup manager to enable or disable gossiping of blocks to peers.
94106
#[cfg(feature = "test-utils")]
95107
pub async fn set_gossip(&self, enabled: bool) -> Result<(), oneshot::error::RecvError> {

crates/chain-orchestrator/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod error;
5959
pub use error::ChainOrchestratorError;
6060

6161
mod handle;
62-
pub use handle::{ChainOrchestratorCommand, ChainOrchestratorHandle};
62+
pub use handle::{ChainOrchestratorCommand, ChainOrchestratorHandle, DatabaseQuery};
6363

6464
mod metrics;
6565
pub use metrics::{ChainOrchestratorItem, ChainOrchestratorMetrics};
@@ -376,6 +376,16 @@ impl<
376376
let _ = tx.send(false);
377377
}
378378
}
379+
ChainOrchestratorCommand::DatabaseQuery(query) => match query {
380+
DatabaseQuery::GetL1MessageByIndex(index, sender) => {
381+
let l1_message = self
382+
.database
383+
.get_n_l1_messages(Some(L1MessageKey::from_queue_index(index)), 1)
384+
.await?
385+
.pop();
386+
let _ = sender.send(l1_message);
387+
}
388+
},
379389
#[cfg(feature = "test-utils")]
380390
ChainOrchestratorCommand::SetGossip((enabled, tx)) => {
381391
self.network.handle().set_gossip(enabled).await;

crates/engine/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ serde = [
7575
"reth-primitives-traits/serde",
7676
"alloy-consensus/serde",
7777
"scroll-alloy-consensus/serde",
78+
"rollup-node-primitives/serde",
7879
]

crates/network/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ serde = [
5050
"reth-primitives-traits/serde",
5151
"reth-storage-api/serde",
5252
"scroll-alloy-hardforks/serde",
53+
"rollup-node-primitives/serde",
5354
]
5455
test-utils = [
5556
"reth-chainspec/test-utils",

crates/node/src/add_ons/rpc.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use jsonrpsee::{
77
use reth_network_api::FullNetwork;
88
use reth_scroll_node::ScrollNetworkPrimitives;
99
use rollup_node_chain_orchestrator::{ChainOrchestratorHandle, ChainOrchestratorStatus};
10+
use rollup_node_primitives::L1MessageEnvelope;
1011
use tokio::sync::{oneshot, Mutex, OnceCell};
1112

1213
/// RPC extension for rollup node management operations.
@@ -79,6 +80,10 @@ pub trait RollupNodeExtApi {
7980
/// Returns the current status of the rollup node.
8081
#[method(name = "status")]
8182
async fn status(&self) -> RpcResult<ChainOrchestratorStatus>;
83+
84+
/// Returns the L1 message by index.
85+
#[method(name = "getL1MessageByIndex")]
86+
async fn get_l1_message_by_index(&self, index: u64) -> RpcResult<Option<L1MessageEnvelope>>;
8287
}
8388

8489
#[async_trait]
@@ -139,4 +144,22 @@ where
139144
)
140145
})
141146
}
147+
148+
async fn get_l1_message_by_index(&self, index: u64) -> RpcResult<Option<L1MessageEnvelope>> {
149+
let handle = self.rollup_manager_handle().await.map_err(|e| {
150+
ErrorObjectOwned::owned(
151+
error::INTERNAL_ERROR_CODE,
152+
format!("Failed to get rollup manager handle: {}", e),
153+
None::<()>,
154+
)
155+
})?;
156+
157+
handle.get_l1_message_by_index(index).await.map_err(|e| {
158+
ErrorObjectOwned::owned(
159+
error::INTERNAL_ERROR_CODE,
160+
format!("Failed to get L1 message by index: {}", e),
161+
None::<()>,
162+
)
163+
})
164+
}
142165
}

crates/primitives/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ arbitrary = [
6767
"alloy-chains/arbitrary",
6868
"reth-chainspec/arbitrary",
6969
]
70+
serde = [
71+
"alloy-chains/serde",
72+
"alloy-consensus/serde",
73+
"alloy-eips/serde",
74+
"alloy-primitives/serde",
75+
"alloy-rpc-types-engine/serde",
76+
"reth-primitives-traits/serde",
77+
"reth-scroll-primitives/serde",
78+
"scroll-alloy-consensus/serde",
79+
"scroll-alloy-rpc-types-engine/serde",
80+
]

crates/primitives/src/transaction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use scroll_alloy_consensus::TxL1Message;
33

44
/// A L1 message envelope, containing extra information about the message.
55
#[derive(Debug, Clone, PartialEq, Eq)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
pub struct L1MessageEnvelope {
78
/// The L1 transaction.
89
pub transaction: TxL1Message,

0 commit comments

Comments
 (0)