From a33f004a0da5a9367996ad32fce1d84559b8045f Mon Sep 17 00:00:00 2001 From: Blazej Kolad Date: Fri, 18 Aug 2023 17:32:29 +0200 Subject: [PATCH] Rollup strcture in demo-rollup (#680) --- examples/demo-rollup/benches/rng_xfers.rs | 1 + examples/demo-rollup/src/lib.rs | 3 +- examples/demo-rollup/src/main.rs | 68 +++--------------- examples/demo-rollup/src/register_rpc.rs | 11 +-- examples/demo-rollup/src/rollup.rs | 86 +++++++++++++++++++++++ full-node/sov-stf-runner/src/config.rs | 1 + full-node/sov-stf-runner/src/lib.rs | 3 +- 7 files changed, 106 insertions(+), 67 deletions(-) create mode 100644 examples/demo-rollup/src/rollup.rs diff --git a/examples/demo-rollup/benches/rng_xfers.rs b/examples/demo-rollup/benches/rng_xfers.rs index a5b6c9faf..7f903dd54 100644 --- a/examples/demo-rollup/benches/rng_xfers.rs +++ b/examples/demo-rollup/benches/rng_xfers.rs @@ -17,6 +17,7 @@ use sov_rollup_interface::mocks::{ }; use sov_rollup_interface::services::da::DaService; +#[derive(Clone)] /// A simple DaService for a random number generator. pub struct RngDaService; diff --git a/examples/demo-rollup/src/lib.rs b/examples/demo-rollup/src/lib.rs index 27d125810..08402a19c 100644 --- a/examples/demo-rollup/src/lib.rs +++ b/examples/demo-rollup/src/lib.rs @@ -2,7 +2,7 @@ #![doc = include_str!("../README.md")] pub mod register_rpc; - +mod rollup; use std::str::FromStr; use celestia::types::NamespaceId; @@ -11,6 +11,7 @@ use const_rollup_config::{ROLLUP_NAMESPACE_RAW, SEQUENCER_DA_ADDRESS}; use demo_stf::app::DefaultPrivateKey; use demo_stf::genesis_config::create_demo_genesis_config; use demo_stf::runtime::GenesisConfig; +pub use rollup::{new_rollup_with_celestia_da, Rollup}; use sov_db::ledger_db::LedgerDB; use sov_modules_api::default_context::DefaultContext; diff --git a/examples/demo-rollup/src/main.rs b/examples/demo-rollup/src/main.rs index 6e7abc70a..c95278050 100644 --- a/examples/demo-rollup/src/main.rs +++ b/examples/demo-rollup/src/main.rs @@ -1,18 +1,8 @@ use std::env; -use anyhow::Context; -use celestia::verifier::RollupParams; -use celestia::CelestiaService; -use demo_stf::app::{App, DefaultContext}; -use demo_stf::runtime::get_rpc_methods; -#[cfg(feature = "experimental")] -use sov_demo_rollup::register_rpc::register_ethereum; -use sov_demo_rollup::register_rpc::{register_ledger, register_sequencer}; -use sov_demo_rollup::{get_genesis_config, initialize_ledger, ROLLUP_NAMESPACE}; -use sov_rollup_interface::services::da::DaService; -use sov_state::storage::Storage; -use sov_stf_runner::{from_toml_path, RollupConfig, StateTransitionRunner}; -use tracing::{debug, Level}; +use sov_demo_rollup::new_rollup_with_celestia_da; +use tracing::Level; + #[cfg(test)] mod test_rpc; @@ -22,59 +12,19 @@ mod test_rpc; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { - let rollup_config_path = env::args() - .nth(1) - .unwrap_or_else(|| "rollup_config.toml".to_string()); - - debug!("Starting demo rollup with config {}", rollup_config_path); - let rollup_config: RollupConfig = - from_toml_path(&rollup_config_path).context("Failed to read rollup configuration")?; - // Initializing logging let subscriber = tracing_subscriber::fmt() .with_max_level(Level::INFO) .finish(); + tracing::subscriber::set_global_default(subscriber) .map_err(|_err| eprintln!("Unable to set global default subscriber")) .expect("Cannot fail to set subscriber"); - let ledger_db = initialize_ledger(&rollup_config.storage.path); - - let da_service = CelestiaService::new( - rollup_config.da.clone(), - RollupParams { - namespace: ROLLUP_NAMESPACE, - }, - ) - .await; - - let mut app = App::new(rollup_config.storage.clone()); - - let storage = app.get_storage(); - let mut methods = get_rpc_methods::(storage); - - // register rpc methods - { - register_ledger(ledger_db.clone(), &mut methods)?; - register_sequencer(da_service.clone(), &mut app, &mut methods)?; - #[cfg(feature = "experimental")] - register_ethereum(da_service.clone(), &mut methods)?; - } - - let storage = app.get_storage(); - let genesis_config = get_genesis_config(); - - let mut runner = StateTransitionRunner::new( - rollup_config.runner, - da_service, - ledger_db, - app.stf, - storage.is_empty(), - genesis_config, - )?; - - runner.start_rpc_server(methods).await; - runner.run().await?; + let rollup_config_path = env::args() + .nth(1) + .unwrap_or_else(|| "rollup_config.toml".to_string()); - Ok(()) + let rollup = new_rollup_with_celestia_da(&rollup_config_path).await?; + rollup.run().await } diff --git a/examples/demo-rollup/src/register_rpc.rs b/examples/demo-rollup/src/register_rpc.rs index 30d7c9de2..3d64bb0f5 100644 --- a/examples/demo-rollup/src/register_rpc.rs +++ b/examples/demo-rollup/src/register_rpc.rs @@ -3,10 +3,10 @@ use anyhow::Context; use celestia::verifier::address::CelestiaAddress; use demo_stf::app::App; -use risc0_adapter::host::Risc0Verifier; use sov_db::ledger_db::LedgerDB; use sov_modules_stf_template::{SequencerOutcome, TxEffect}; use sov_rollup_interface::services::da::DaService; +use sov_rollup_interface::zk::Zkvm; use sov_sequencer::get_sequencer_rpc; use sov_stf_runner::get_ledger_rpc; @@ -14,15 +14,16 @@ use sov_stf_runner::get_ledger_rpc; const TX_SIGNER_PRIV_KEY_PATH: &str = "../test-data/keys/tx_signer_private_key.json"; /// register sequencer rpc methods. -pub fn register_sequencer( +pub fn register_sequencer( da_service: DA, - demo_runner: &mut App, + app: &mut App, methods: &mut jsonrpsee::RpcModule<()>, ) -> Result<(), anyhow::Error> where - DA: DaService + Send + Sync + 'static, + DA: DaService, + Vm: Zkvm, { - let batch_builder = demo_runner.batch_builder.take().unwrap(); + let batch_builder = app.batch_builder.take().unwrap(); let sequencer_rpc = get_sequencer_rpc(batch_builder, da_service); methods .merge(sequencer_rpc) diff --git a/examples/demo-rollup/src/rollup.rs b/examples/demo-rollup/src/rollup.rs new file mode 100644 index 000000000..340d2de7f --- /dev/null +++ b/examples/demo-rollup/src/rollup.rs @@ -0,0 +1,86 @@ +use anyhow::Context; +use celestia::verifier::RollupParams; +use celestia::CelestiaService; +use demo_stf::app::{App, DefaultContext}; +use demo_stf::runtime::get_rpc_methods; +use risc0_adapter::host::Risc0Verifier; +use sov_db::ledger_db::LedgerDB; +use sov_rollup_interface::services::da::DaService; +use sov_rollup_interface::zk::Zkvm; +use sov_state::storage::Storage; +use sov_stf_runner::{from_toml_path, RollupConfig, RunnerConfig, StateTransitionRunner}; +use tracing::debug; + +#[cfg(feature = "experimental")] +use crate::register_rpc::register_ethereum; +use crate::register_rpc::{register_ledger, register_sequencer}; +use crate::{get_genesis_config, initialize_ledger, ROLLUP_NAMESPACE}; + +/// Dependencies needed to run the rollup. +pub struct Rollup { + app: App, + da_service: DA, + ledger_db: LedgerDB, + runner_config: RunnerConfig, +} + +/// Creates celestia based rollup. +pub async fn new_rollup_with_celestia_da( + rollup_config_path: &str, +) -> Result, anyhow::Error> { + debug!("Starting demo rollup with config {}", rollup_config_path); + let rollup_config: RollupConfig = + from_toml_path(rollup_config_path).context("Failed to read rollup configuration")?; + + let ledger_db = initialize_ledger(&rollup_config.storage.path); + + let da_service = CelestiaService::new( + rollup_config.da.clone(), + RollupParams { + namespace: ROLLUP_NAMESPACE, + }, + ) + .await; + + let app = App::new(rollup_config.storage); + + Ok(Rollup { + app, + da_service, + ledger_db, + runner_config: rollup_config.runner, + }) +} + +impl + Clone> Rollup { + /// Runs the rollup. + pub async fn run(mut self) -> Result<(), anyhow::Error> { + let storage = self.app.get_storage(); + let mut methods = get_rpc_methods::(storage); + + // register rpc methods + { + register_ledger(self.ledger_db.clone(), &mut methods)?; + register_sequencer(self.da_service.clone(), &mut self.app, &mut methods)?; + #[cfg(feature = "experimental")] + register_ethereum(self.da_service.clone(), &mut methods)?; + } + + let storage = self.app.get_storage(); + let genesis_config = get_genesis_config(); + + let mut runner = StateTransitionRunner::new( + self.runner_config, + self.da_service, + self.ledger_db, + self.app.stf, + storage.is_empty(), + genesis_config, + )?; + + runner.start_rpc_server(methods).await; + runner.run().await?; + + Ok(()) + } +} diff --git a/full-node/sov-stf-runner/src/config.rs b/full-node/sov-stf-runner/src/config.rs index a206ec1f4..c1f684f5e 100644 --- a/full-node/sov-stf-runner/src/config.rs +++ b/full-node/sov-stf-runner/src/config.rs @@ -6,6 +6,7 @@ use serde::de::DeserializeOwned; use serde::Deserialize; pub use sov_state::config::Config as StorageConfig; +/// Configuration for StateTransitionRunner. #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct RunnerConfig { /// DA start height. diff --git a/full-node/sov-stf-runner/src/lib.rs b/full-node/sov-stf-runner/src/lib.rs index 69c003315..39538b6e4 100644 --- a/full-node/sov-stf-runner/src/lib.rs +++ b/full-node/sov-stf-runner/src/lib.rs @@ -8,8 +8,7 @@ use std::net::SocketAddr; pub use config::RpcConfig; mod ledger_rpc; pub use batch_builder::FiFoStrictBatchBuilder; -use config::RunnerConfig; -pub use config::{from_toml_path, RollupConfig, StorageConfig}; +pub use config::{from_toml_path, RollupConfig, RunnerConfig, StorageConfig}; use jsonrpsee::RpcModule; pub use ledger_rpc::get_ledger_rpc; use sov_db::ledger_db::{LedgerDB, SlotCommit};