From b635be475f6040cdd85b9dfbc2172905e0323bc8 Mon Sep 17 00:00:00 2001
From: Terry <644052732@qq.com>
Date: Thu, 16 May 2024 20:02:27 +0800
Subject: [PATCH 1/4] feat: extend eigen rpc, add get_batch_proof,
get_block_by_number
---
src/batch_proposer/mod.rs | 23 ++++++++++++++++++--
src/commands/run.rs | 13 +++++++++--
src/custom_reth/eigen.rs | 45 +++++++++++++++++++++++++++++++++++----
src/custom_reth/mod.rs | 3 +++
src/db/mod.rs | 38 +++++++++++++++++++++++++++++++++
src/operator.rs | 14 +++++-------
src/prover/provider.rs | 12 +----------
src/settlement/worker.rs | 21 ++++++++++++++++--
8 files changed, 139 insertions(+), 30 deletions(-)
diff --git a/src/batch_proposer/mod.rs b/src/batch_proposer/mod.rs
index 3289af4..b82faa3 100644
--- a/src/batch_proposer/mod.rs
+++ b/src/batch_proposer/mod.rs
@@ -1,5 +1,5 @@
-use crate::db::keys;
use crate::db::Database;
+use crate::db::{keys, prefix, Status};
use anyhow::{anyhow, bail, Result};
use ethers_providers::{Http, Middleware, Provider};
use std::sync::Arc;
@@ -75,7 +75,26 @@ impl L2Watcher {
.map_err(|e| anyhow!("{:?}", e))
.map(|number| {
log::info!("L2Watcher fetched block({})", number);
- db.put(keys::KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER.to_vec(), number.as_u64().to_be_bytes().to_vec())
+
+ let last_fetched_block = match db.get(keys::KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER) {
+ None => {
+ db.put(keys::KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER.to_vec(), number.as_u64().to_be_bytes().to_vec());
+ 0
+ }
+ Some(block_number_bytes) => {
+ u64::from_be_bytes(block_number_bytes.try_into().unwrap())
+ }
+ };
+
+ db.put(keys::KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER.to_vec(), number.as_u64().to_be_bytes().to_vec());
+ for number in last_fetched_block+1..number.as_u64()+1 {
+ // update block status to sequenced
+ let status_key = format!("{}{}", std::str::from_utf8(prefix::PREFIX_BLOCK_STATUS).unwrap(), number);
+ let status = Status::Sequenced;
+ let encoded_status = serde_json::to_vec(&status).unwrap();
+ db.put(status_key.as_bytes().to_vec(), encoded_status);
+
+ }
})
},
_ = stop_rx.recv() => {
diff --git a/src/commands/run.rs b/src/commands/run.rs
index c3be2f2..2e088c5 100644
--- a/src/commands/run.rs
+++ b/src/commands/run.rs
@@ -1,6 +1,7 @@
use std::fmt;
+use std::sync::Arc;
-use anyhow::Result;
+use anyhow::{anyhow, Result};
use tokio::select;
use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::mpsc;
@@ -211,15 +212,21 @@ impl RunCmd {
reth_stop_tx.send(()).await.unwrap();
});
+ // initialize the database
+ let rollup_db =
+ lfs::open_db(db_config).map_err(|e| anyhow!("Failed to open db: {:?}", e))?;
+ let arc_rollup_db = Arc::new(rollup_db);
+
let (reth_started_signal_tx, reth_started_signal_rx) = mpsc::channel::<()>(1);
let a = aggregator_addr.clone();
+ let operator_rollup_db = arc_rollup_db.clone();
tokio::spawn(async move {
// Run the operator
Operator::run(
&GLOBAL_ENV.l2addr,
&GLOBAL_ENV.prover_addr,
settlement_spec.clone(),
- db_config.clone(),
+ operator_rollup_db,
a.as_str(),
stop_rx,
reth_started_signal_rx,
@@ -231,11 +238,13 @@ impl RunCmd {
let rpc_args = self.reth_cmd.rpc.clone();
let dev_args = self.reth_cmd.dev;
let data_dir = self.reth_cmd.datadir.clone();
+ let reth_rollup_db = arc_rollup_db.clone();
// Launch the custom reth
custom_reth::launch_custom_node(
reth_stop_rx,
reth_started_signal_tx,
+ reth_rollup_db,
chain_spec,
rpc_args,
data_dir,
diff --git a/src/custom_reth/eigen.rs b/src/custom_reth/eigen.rs
index 25b4b00..3495096 100644
--- a/src/custom_reth/eigen.rs
+++ b/src/custom_reth/eigen.rs
@@ -1,10 +1,13 @@
+use std::sync::Arc;
// Reth block related imports
use reth_primitives::{Block, B256};
use reth_provider::BlockReaderIdExt;
// Rpc related imports
+use crate::db::{prefix, BlockExt, Database as RollupDatabase, ProofResult, Status};
use jsonrpsee::proc_macros::rpc;
-use reth_rpc::eth::error::EthResult;
+use reth_interfaces::RethError;
+use reth_rpc::eth::error::{EthApiError, EthResult};
/// trait interface for a custom rpc namespace: `EigenRpc`
///
@@ -15,14 +18,17 @@ pub trait EigenRpcExtApi {
#[method(name = "customMethod")]
fn custom_methhod(&self) -> EthResult