Skip to content

Commit

Permalink
Fixing broken prover benchmark (#1134)
Browse files Browse the repository at this point in the history
* fixing broken bench

* added new file datagen.rs that was missing

* revert back to using risc0 zkvm bytes

* fix risc0 zkvm bytes and zkvm/prove feature
  • Loading branch information
dubbelosix authored Nov 3, 2023
1 parent e6807ed commit 7cf2e64
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 438 deletions.
719 changes: 427 additions & 292 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion adapters/risc0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sov-rollup-interface = { path = "../../rollup-interface", version = "0.3" }
[features]
default = []
native = ["risc0-zkvm/prove", "dep:risc0-zkp", "dep:risc0-circuit-rv32im"]
bench = ["once_cell", "parking_lot", "native", "sov-zk-cycle-utils/native"]
bench = ["once_cell", "parking_lot","native","sov-zk-cycle-utils/native"]

[[test]]
name = "native"
Expand Down
2 changes: 1 addition & 1 deletion adapters/risc0/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn deserialize_custom(serialized: Bytes) -> Result<(String, u64), anyhow::Error>
/// When the "bench" feature is enabled, this callback is registered as a syscall
/// in the Risc0 VM and invoked whenever a function annotated with the [`sov-zk-cycle-utils::cycle_tracker`]
/// macro is invoked.
pub fn metrics_callback(input: risc0_zkvm::Bytes) -> Result<Bytes, anyhow::Error> {
pub fn metrics_callback(input: Bytes) -> Result<Bytes, anyhow::Error> {
let met_tuple = deserialize_custom(input)?;
add_value(met_tuple.0, met_tuple.1);
Ok(Bytes::new())
Expand Down
3 changes: 2 additions & 1 deletion examples/demo-rollup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ secp256k1 = { workspace = true, optional = true }
[dev-dependencies]
sov-rng-da-service = { path = "../../utils/rng-da-service" }
sov-rollup-interface = { path = "../../rollup-interface", features = ["fuzzing"] }
sov-mock-da = { path = "../../adapters/mock-da" }
sov-evm = { path = "../../module-system/module-implementations/sov-evm", features = ["smart_contracts"] }
sov-bank = { path = "../../module-system/module-implementations/sov-bank", features = ["native"] }
sov-nft-module = { path = "../../module-system/module-implementations/sov-nft-module", features = ["native"] }
sov-zk-cycle-macros = { path = "../../utils/zk-cycle-macros" }

borsh = { workspace = true }
Expand Down Expand Up @@ -108,7 +110,6 @@ path = "benches/prover/prover_bench.rs"
harness = false
required-features = ["bench"]


[[bin]]
name = "sov-cli"
path = "src/sov-cli/main.rs"
Expand Down
5 changes: 0 additions & 5 deletions examples/demo-rollup/benches/prover/blocks.hex

This file was deleted.

25 changes: 25 additions & 0 deletions examples/demo-rollup/benches/prover/datagen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use sov_mock_da::{MockAddress, MockBlock, MockDaService};
use sov_rng_da_service::{generate_create, generate_transfers};
use sov_rollup_interface::services::da::DaService;

pub async fn get_bench_blocks() -> Vec<MockBlock> {
let da_service = MockDaService::new(MockAddress::default());

let mut blocks = vec![];
let blob = generate_create(0);
da_service.send_transaction(&blob).await.unwrap();
let block1 = da_service.get_finalized_at(0).await.unwrap();
blocks.push(block1);

let blob = generate_transfers(3, 1);
da_service.send_transaction(&blob).await.unwrap();
let block2 = da_service.get_finalized_at(0).await.unwrap();
blocks.push(block2);

let blob = generate_transfers(10, 4);
da_service.send_transaction(&blob).await.unwrap();
let block2 = da_service.get_finalized_at(0).await.unwrap();
blocks.push(block2);

blocks
}
68 changes: 25 additions & 43 deletions examples/demo-rollup/benches/prover/prover_bench.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
mod datagen;

use std::collections::HashMap;
use std::env;
use std::fs::{read_to_string, remove_file, File, OpenOptions};
use std::fs::{remove_file, File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use sov_mock_da::{MockAddress, MockDaConfig, MockDaService, MockDaSpec};

#[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};
use sov_celestia_adapter::verifier::{CelestiaSpec, RollupParams};
use sov_celestia_adapter::CelestiaService;
use risc0::MOCK_DA_ELF;
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::SlotData;
use sov_modules_stf_template::kernels::basic::BasicKernel;
Expand All @@ -33,8 +33,7 @@ use sov_rollup_interface::zk::ZkvmHost;
use sov_stf_runner::{from_toml_path, RollupConfig};
use tempfile::TempDir;

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

#[derive(Debug)]
struct RegexAppender {
Expand Down Expand Up @@ -142,28 +141,18 @@ async fn main() -> Result<(), anyhow::Error> {
}

let rollup_config_path = "benches/prover/rollup_config.toml".to_string();
let mut rollup_config: RollupConfig<sov_celestia_adapter::CelestiaConfig> =
from_toml_path(&rollup_config_path)
.context("Failed to read rollup configuration")
.unwrap();
let mut rollup_config: RollupConfig<MockDaConfig> = from_toml_path(&rollup_config_path)
.context("Failed to read rollup configuration")
.unwrap();

let mut num_blocks = 0;
let mut num_blobs = 0;
let mut num_blocks_with_txns = 0;
let mut num_total_transactions = 0;

let temp_dir = TempDir::new().expect("Unable to create temporary directory");

rollup_config.storage.path = PathBuf::from(temp_dir.path());

let da_service = CelestiaService::new(
rollup_config.da.clone(),
RollupParams {
namespace: ROLLUP_NAMESPACE,
},
)
.await;

let da_service = MockDaService::new(MockAddress::default());
let storage_config = sov_state::config::Config {
path: rollup_config.storage.path,
};
Expand All @@ -172,46 +161,39 @@ async fn main() -> Result<(), anyhow::Error> {
.expect("ProverStorageManager initialization has failed");
let stf = AppTemplate::<
DefaultContext,
CelestiaSpec,
MockDaSpec,
Risc0Host,
Runtime<DefaultContext, CelestiaSpec>,
Runtime<DefaultContext, MockDaSpec>,
BasicKernel<DefaultContext>,
>::new();

let genesis_config =
get_genesis_config(&GenesisPaths::from_dir("../test-data/genesis/demo-tests")).unwrap();
let genesis_config = get_genesis_config(&GenesisPaths::from_dir(
"../test-data/genesis/integration-tests",
))
.unwrap();
println!("Starting from empty storage, initialization chain");
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
.lines()
.map(|line| {
let bytes = hex::decode(line).expect("Failed to decode hex data");
bincode::deserialize(&bytes).expect("Failed to deserialize data")
})
.collect();
let blocks = get_bench_blocks().await;

for height in 2..(bincoded_blocks.len() as u64) {
for height in 0..(blocks.len() as u64) {
num_blocks += 1;
let mut host = Risc0Host::new(ROLLUP_ELF);
let mut host = Risc0Host::new(MOCK_DA_ELF);
host.add_hint(prev_state_root);
println!(
"Requesting data for height {} and prev_state_root 0x{}",
height,
hex::encode(prev_state_root.0)
);
let filtered_block = &bincoded_blocks[height as usize];
let _header_hash = hex::encode(filtered_block.header.header.hash());
host.add_hint(&filtered_block.header);
let filtered_block = &blocks[height as usize];
host.add_hint(filtered_block.header);
let (mut blob_txs, inclusion_proof, completeness_proof) = da_service
.extract_relevant_blobs_with_proof(filtered_block)
.await;

host.add_hint(&inclusion_proof);
host.add_hint(&completeness_proof);
host.add_hint(&blob_txs);
host.add_hint(inclusion_proof);
host.add_hint(completeness_proof);

if !blob_txs.is_empty() {
num_blobs += blob_txs.len();
Expand All @@ -225,14 +207,14 @@ async fn main() -> Result<(), anyhow::Error> {
&filtered_block.validity_condition(),
&mut blob_txs,
);
host.add_hint(&blob_txs);
for r in result.batch_receipts {
let num_tx = r.tx_receipts.len();
num_total_transactions += num_tx;
if num_tx > 0 {
num_blocks_with_txns += 1;
}
}
// println!("{:?}",result.batch_receipts);

host.add_hint(&result.witness);

Expand Down
1 change: 1 addition & 0 deletions examples/demo-rollup/benches/prover/rollup_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ celestia_rpc_auth_token = "MY.SECRET.TOKEN"
celestia_rpc_address = "http://localhost:11111/"
# The largest response the rollup will accept from the Celestia node. Defaults to 100 MB
max_celestia_response_body_size = 104_857_600
sender_address = "0000000000000000000000000000000000000000000000000000000000000000"

[storage]
# The path to the rollup's data directory. Paths that do not begin with `/` are interpreted as relative paths.
Expand Down
2 changes: 0 additions & 2 deletions examples/demo-rollup/provers/risc0/.cargo/config.toml

This file was deleted.

2 changes: 1 addition & 1 deletion examples/demo-rollup/provers/risc0/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn get_guest_options() -> HashMap<&'static str, risc0_build::GuestOptions> {
fn get_guest_options() -> HashMap<&'static str, risc0_build::GuestOptions> {
let mut guest_pkg_to_options = HashMap::new();
guest_pkg_to_options.insert(
"sov-demo-prover-guest-celestia",
"sov-demo-prover-guest-mock",
risc0_build::GuestOptions {
features: vec!["bench".to_string()],
},
Expand Down
6 changes: 1 addition & 5 deletions examples/demo-rollup/provers/risc0/guest-celestia/Cargo.lock

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

Loading

0 comments on commit 7cf2e64

Please sign in to comment.