Skip to content

Commit

Permalink
Rollup strcture in demo-rollup (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad authored Aug 18, 2023
1 parent cf06f2a commit a33f004
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 67 deletions.
1 change: 1 addition & 0 deletions examples/demo-rollup/benches/rng_xfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion examples/demo-rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![doc = include_str!("../README.md")]

pub mod register_rpc;

mod rollup;
use std::str::FromStr;

use celestia::types::NamespaceId;
Expand All @@ -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;

Expand Down
68 changes: 9 additions & 59 deletions examples/demo-rollup/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<celestia::DaServiceConfig> =
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::<DefaultContext>(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
}
11 changes: 6 additions & 5 deletions examples/demo-rollup/src/register_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
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;

#[cfg(feature = "experimental")]
const TX_SIGNER_PRIV_KEY_PATH: &str = "../test-data/keys/tx_signer_private_key.json";

/// register sequencer rpc methods.
pub fn register_sequencer<DA>(
pub fn register_sequencer<Vm, DA>(
da_service: DA,
demo_runner: &mut App<Risc0Verifier, DA::Spec>,
app: &mut App<Vm, DA::Spec>,
methods: &mut jsonrpsee::RpcModule<()>,
) -> Result<(), anyhow::Error>
where
DA: DaService<Error = anyhow::Error> + 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)
Expand Down
86 changes: 86 additions & 0 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
@@ -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<Vm: Zkvm, DA: DaService + Clone> {
app: App<Vm, DA::Spec>,
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<Rollup<Risc0Verifier, CelestiaService>, anyhow::Error> {
debug!("Starting demo rollup with config {}", rollup_config_path);
let rollup_config: RollupConfig<celestia::DaServiceConfig> =
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<Vm: Zkvm, DA: DaService<Error = anyhow::Error> + Clone> Rollup<Vm, DA> {
/// Runs the rollup.
pub async fn run(mut self) -> Result<(), anyhow::Error> {
let storage = self.app.get_storage();
let mut methods = get_rpc_methods::<DefaultContext>(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(())
}
}
1 change: 1 addition & 0 deletions full-node/sov-stf-runner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions full-node/sov-stf-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit a33f004

Please sign in to comment.