Skip to content

Commit

Permalink
Extract storage creation from State Transition Function (Sovereign-La…
Browse files Browse the repository at this point in the history
…bs#1103)

* [no ci] Extracting storage creation from STF

* Fixes after rebase

* [no ci] Explicit empty type for change set

* Rename and clean up

* Remove `ProverStorage::db`

* Expand on TODO

* Addressing comments

* Finish renaming

* More rename

* Use the same runtime + working set to access modules

* Annotate accessory value setter tests

* Zk Storage is stored inside runner now.
  • Loading branch information
citizen-stig authored Oct 26, 2023
1 parent eb04e6f commit 8b4eb37
Show file tree
Hide file tree
Showing 36 changed files with 598 additions and 398 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 13 additions & 14 deletions examples/demo-rollup/benches/node/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@ use sov_modules_api::default_context::DefaultContext;
use sov_modules_stf_template::AppTemplate;
use sov_risc0_adapter::host::Risc0Verifier;
use sov_rng_da_service::{RngDaService, RngDaSpec};
use sov_rollup_interface::da::DaSpec;
use sov_rollup_interface::mocks::{MockBlock, MockBlockHeader};
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::Zkvm;
use sov_state::{ProverStorage, Storage};
use sov_rollup_interface::storage::StorageManager;
use sov_stf_runner::{from_toml_path, RollupConfig};
use tempfile::TempDir;

fn new_app<Vm: Zkvm, Da: DaSpec>(
storage_config: sov_state::config::Config,
) -> AppTemplate<DefaultContext, Da, Vm, Runtime<DefaultContext, Da>> {
let storage =
ProverStorage::with_config(storage_config).expect("Failed to open prover storage");
AppTemplate::new(storage.clone())
}

fn rollup_bench(_bench: &mut Criterion) {
let start_height: u64 = 0u64;
let mut end_height: u64 = 100u64;
Expand All @@ -55,14 +45,22 @@ fn rollup_bench(_bench: &mut Criterion) {
let storage_config = sov_state::config::Config {
path: rollup_config.storage.path,
};
let mut demo = new_app::<Risc0Verifier, RngDaSpec>(storage_config);
let storage_manager = sov_state::storage_manager::ProverStorageManager::new(storage_config)
.expect("Failed to initialize prover storage manager");
let stf = AppTemplate::<
DefaultContext,
RngDaSpec,
Risc0Verifier,
Runtime<DefaultContext, RngDaSpec>,
>::new();

let demo_genesis_config = get_genesis_config(&GenesisPaths::from_dir(
"../test-data/genesis/integration-tests",
))
.unwrap();

let mut current_root = demo.init_chain(demo_genesis_config);
let (mut current_root, _) =
stf.init_chain(storage_manager.get_native_storage(), demo_genesis_config);

// data generation
let mut blobs = vec![];
Expand Down Expand Up @@ -92,8 +90,9 @@ fn rollup_bench(_bench: &mut Criterion) {
let filtered_block = &blocks[height as usize];

let mut data_to_commit = SlotCommit::new(filtered_block.clone());
let apply_block_result = demo.apply_slot(
let apply_block_result = stf.apply_slot(
&current_root,
storage_manager.get_native_storage(),
Default::default(),
&filtered_block.header,
&filtered_block.validity_cond,
Expand Down
41 changes: 20 additions & 21 deletions examples/demo-rollup/benches/node/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

#[macro_use]
extern crate prettytable;

use anyhow::Context;
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths};
use demo_stf::runtime::Runtime;
use prettytable::Table;
use prometheus::{Histogram, HistogramOpts, Registry};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_stf_template::AppTemplate;
use sov_modules_stf_template::{AppTemplate, TxEffect};
use sov_risc0_adapter::host::Risc0Verifier;
use sov_rng_da_service::{RngDaService, RngDaSpec};
use sov_rollup_interface::da::DaSpec;
use sov_rollup_interface::mocks::{MockBlock, MockBlockHeader};
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::Zkvm;
use sov_state::{ProverStorage, Storage};
use sov_rollup_interface::storage::StorageManager;
use sov_stf_runner::{from_toml_path, RollupConfig};
use tempfile::TempDir;
#[macro_use]
extern crate prettytable;

use prettytable::Table;
use sov_modules_stf_template::TxEffect;

fn new_app<Vm: Zkvm, Da: DaSpec>(
storage_config: sov_state::config::Config,
) -> AppTemplate<DefaultContext, Da, Vm, Runtime<DefaultContext, Da>> {
let storage =
ProverStorage::with_config(storage_config).expect("Failed to open prover storage");
AppTemplate::new(storage.clone())
}

fn print_times(
total: Duration,
Expand Down Expand Up @@ -111,14 +100,22 @@ async fn main() -> Result<(), anyhow::Error> {
let storage_config = sov_state::config::Config {
path: rollup_config.storage.path,
};
let mut demo = new_app::<Risc0Verifier, RngDaSpec>(storage_config);
let storage_manager = sov_state::storage_manager::ProverStorageManager::new(storage_config)
.expect("ProverStorageManager initialization has failed");
let stf = AppTemplate::<
DefaultContext,
RngDaSpec,
Risc0Verifier,
Runtime<DefaultContext, RngDaSpec>,
>::new();

let demo_genesis_config = get_genesis_config(&GenesisPaths::from_dir(
"../test-data/genesis/integration-tests",
))
.unwrap();

let mut current_root = demo.init_chain(demo_genesis_config);
let (mut current_root, _) =
stf.init_chain(storage_manager.get_native_storage(), demo_genesis_config);

// data generation
let mut blobs = vec![];
Expand All @@ -145,8 +142,9 @@ async fn main() -> Result<(), anyhow::Error> {
// Setup. Block 0 has a single txn that creates the token. Exclude from timers
let filtered_block = &blocks[0usize];
let mut data_to_commit = SlotCommit::new(filtered_block.clone());
let apply_block_results = demo.apply_slot(
let apply_block_results = stf.apply_slot(
&current_root,
storage_manager.get_native_storage(),
Default::default(),
&filtered_block.header,
&filtered_block.validity_cond,
Expand All @@ -167,8 +165,9 @@ async fn main() -> Result<(), anyhow::Error> {

let now = Instant::now();

let apply_block_results = demo.apply_slot(
let apply_block_results = stf.apply_slot(
&current_root,
storage_manager.get_native_storage(),
Default::default(),
&filtered_block.header,
&filtered_block.validity_cond,
Expand Down
54 changes: 26 additions & 28 deletions examples/demo-rollup/benches/prover/prover_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

#[macro_use]
extern crate prettytable;

use anyhow::Context;
use const_rollup_config::ROLLUP_NAMESPACE_RAW;
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths};
use demo_stf::runtime::Runtime;
use log4rs::config::{Appender, Config, Root};
use prettytable::Table;
use regex::Regex;
use risc0::ROLLUP_ELF;
use sov_celestia_adapter::types::{FilteredCelestiaBlock, Namespace};
Expand All @@ -19,21 +23,17 @@ use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::SlotData;
use sov_modules_stf_template::AppTemplate;
use sov_risc0_adapter::host::Risc0Host;
use sov_rollup_interface::da::DaSpec;
#[cfg(feature = "bench")]
use sov_risc0_adapter::metrics::GLOBAL_HASHMAP;
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::stf::StateTransitionFunction;
use sov_rollup_interface::zk::{Zkvm, ZkvmHost};
use sov_state::{ProverStorage, Storage};
use sov_rollup_interface::storage::StorageManager;
use sov_rollup_interface::zk::ZkvmHost;
use sov_stf_runner::{from_toml_path, RollupConfig};
use tempfile::TempDir;

fn new_app<Vm: Zkvm, Da: DaSpec>(
storage_config: sov_state::config::Config,
) -> AppTemplate<DefaultContext, Da, Vm, Runtime<DefaultContext, Da>> {
let storage =
ProverStorage::with_config(storage_config).expect("Failed to open prover storage");
AppTemplate::new(storage.clone())
}
// The rollup stores its data in the namespace b"sov-test" on Celestia
const ROLLUP_NAMESPACE: Namespace = Namespace::const_v0(ROLLUP_NAMESPACE_RAW);

#[derive(Debug)]
struct RegexAppender {
Expand All @@ -59,6 +59,10 @@ impl RegexAppender {
}

impl log::Log for RegexAppender {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}

fn log(&self, record: &log::Record) {
if let Some(captures) = self.regex.captures(record.args().to_string().as_str()) {
let mut file_guard = self.file.lock().unwrap();
Expand All @@ -75,10 +79,6 @@ impl log::Log for RegexAppender {
}
}

fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}

fn flush(&self) {}
}

Expand All @@ -98,17 +98,6 @@ fn get_config(rollup_trace: &str) -> Config {
.unwrap()
}

#[cfg(feature = "bench")]
use sov_risc0_adapter::metrics::GLOBAL_HASHMAP;

// The rollup stores its data in the namespace b"sov-test" on Celestia
const ROLLUP_NAMESPACE: Namespace = Namespace::const_v0(ROLLUP_NAMESPACE_RAW);

#[macro_use]
extern crate prettytable;

use prettytable::Table;

fn print_cycle_averages(metric_map: HashMap<String, (u64, u64)>) {
let mut metrics_vec: Vec<(String, (u64, u64))> = metric_map
.iter()
Expand Down Expand Up @@ -178,12 +167,20 @@ async fn main() -> Result<(), anyhow::Error> {
path: rollup_config.storage.path,
};

let mut demo = new_app::<Risc0Host, CelestiaSpec>(storage_config);
let storage_manager = sov_state::storage_manager::ProverStorageManager::new(storage_config)
.expect("ProverStorageManager initialization has failed");
let stf = AppTemplate::<
DefaultContext,
CelestiaSpec,
Risc0Host,
Runtime<DefaultContext, CelestiaSpec>,
>::new();

let genesis_config =
get_genesis_config(&GenesisPaths::from_dir("../test-data/genesis/demo-tests")).unwrap();
println!("Starting from empty storage, initialization chain");
let mut prev_state_root = demo.init_chain(genesis_config);
let (mut prev_state_root, _) =
stf.init_chain(storage_manager.get_native_storage(), genesis_config);

let hex_data = read_to_string("benches/prover/blocks.hex").expect("Failed to read data");
let bincoded_blocks: Vec<FilteredCelestiaBlock> = hex_data
Expand Down Expand Up @@ -218,8 +215,9 @@ async fn main() -> Result<(), anyhow::Error> {
num_blobs += blob_txs.len();
}

let result = demo.apply_slot(
let result = stf.apply_slot(
&prev_state_root,
storage_manager.get_native_storage(),
Default::default(),
&filtered_block.header,
&filtered_block.validity_condition(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ risc0_zkvm::guest::entry!(main);
pub fn main() {
let guest = Risc0Guest::new();
let storage = ZkStorage::new();
let app: AppTemplate<ZkDefaultContext, _, _, Runtime<_, _>> = AppTemplate::new(storage);
let app: AppTemplate<ZkDefaultContext, _, _, Runtime<_, _>> = AppTemplate::new();

let mut stf_verifier = AppVerifier::new(
app,
Expand All @@ -29,6 +29,6 @@ pub fn main() {
},
);
stf_verifier
.run_block(guest)
.run_block(guest, storage)
.expect("Prover must be honest");
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ risc0_zkvm::guest::entry!(main);
pub fn main() {
let guest = Risc0Guest::new();
let storage = ZkStorage::new();
let app: AppTemplate<ZkDefaultContext, _, _, Runtime<_, _>> = AppTemplate::new(storage);
let app: AppTemplate<ZkDefaultContext, _, _, Runtime<_, _>> = AppTemplate::new();

let mut stf_verifier = AppVerifier::new(app, MockDaVerifier {});

stf_verifier
.run_block(guest)
.run_block(guest, storage)
.expect("Prover must be honest");
}
Loading

0 comments on commit 8b4eb37

Please sign in to comment.