Skip to content

feat: debug_dbGet for missing nodes #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
760 changes: 733 additions & 27 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ metrics-util = "0.17"
metrics-tracing-context = "0.16.0"
p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "88d7f05" }
rayon = "1.10"
reqwest = "0.12"
rkyv = "0.8"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1.0" }
Expand All @@ -72,6 +73,7 @@ snark-verifier-sdk = { version = "0.2.0", default-features = false, features = [
"halo2-axiom",
"display",
] }
thiserror = "2"
tiny-keccak = "2.0"
tracing = "0.1"
vm-zstd = { git = "https://github.com/scroll-tech/rust-zstd-decompressor.git", tag = "v0.1.1" }
Expand Down
2 changes: 1 addition & 1 deletion crates/circuits/batch-circuit/batch_exe_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!

pub const COMMIT: [u32; 8] = [340896804, 602821680, 1157331017, 1209391802, 387959812, 1598560358, 1043289328, 1323232354];
pub const COMMIT: [u32; 8] = [909244349, 475713507, 899720144, 1467976792, 1667981332, 808935173, 1050473949, 630660202];
2 changes: 1 addition & 1 deletion crates/circuits/bundle-circuit/bundle_exe_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!

pub const COMMIT: [u32; 8] = [680857057, 82855189, 1900791356, 1462562861, 836632426, 1110053314, 451565233, 894149163];
pub const COMMIT: [u32; 8] = [37108349, 1124555631, 181670790, 1527082333, 1847388449, 1508995882, 1619286704, 1044606245];
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT OVERWRITE!
//! Generated by crates/build-guest. DO NOT EDIT!

pub const COMMIT: [u32; 8] = [1871075008, 751632513, 1263732664, 1412645020, 1947618829, 1669253022, 1064699122, 289306822];
pub const COMMIT: [u32; 8] = [1986274210, 1519349588, 593103632, 974431136, 625999706, 356987166, 106881763, 822833272];
2 changes: 1 addition & 1 deletion crates/circuits/chunk-circuit/chunk_exe_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!

pub const COMMIT: [u32; 8] = [995010908, 1687884465, 1171846309, 508706329, 1119599313, 212876953, 537460444, 771518360];
pub const COMMIT: [u32; 8] = [1879484103, 857881141, 1340253521, 66094948, 524905769, 838507285, 1883368740, 871580145];
2 changes: 1 addition & 1 deletion crates/circuits/chunk-circuit/chunk_exe_rv32_commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(rustfmt, rustfmt_skip)]
//! Generated by crates/build-guest. DO NOT EDIT!

pub const COMMIT: [u32; 8] = [1420855695, 422270118, 821631601, 1579580737, 1029330106, 1390215820, 103274200, 290471537];
pub const COMMIT: [u32; 8] = [896258503, 1264789376, 1894783211, 1331236681, 1464277710, 1435311184, 1585165466, 1219971142];
1 change: 1 addition & 0 deletions crates/circuits/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sbv-kv = { workspace = true }
vm-zstd = { workspace = true }
serde.workspace = true
itertools.workspace = true
thiserror.workspace = true
tiny-keccak = { workspace = true }

openvm = { workspace = true, features = ["std"] }
Expand Down
90 changes: 64 additions & 26 deletions crates/circuits/types/src/chunk/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use sbv_core::{EvmDatabase, EvmExecutor};
use crate::{
chunk::{
ArchivedChunkWitness, ChunkInfo, ForkName, make_providers, public_inputs::BlockContextV2,
},
manually_drop_on_zkvm,
};
use alloy_primitives::SignatureError;
use sbv_core::{DatabaseError, EvmDatabase, EvmExecutor, VerificationError};
use sbv_primitives::{
BlockWitness,
B256, BlockWitness,
chainspec::{
BaseFeeParams, BaseFeeParamsKind, Chain, MAINNET,
reth_chainspec::ChainSpec,
Expand All @@ -14,24 +21,40 @@ use sbv_primitives::{
},
};

use crate::{
chunk::{
ArchivedChunkWitness, ChunkInfo, ForkName, make_providers, public_inputs::BlockContextV2,
},
manually_drop_on_zkvm,
};
#[derive(thiserror::Error, Debug)]
pub enum ChunkExecutionError {
#[error("At least one witness must be provided in chunk mode.")]
EmptyChunk,
#[error("All witnesses must have the same chain id in chunk mode")]
DifferentChainId,
#[error("All witnesses must have sequential block numbers in chunk mode")]
DiscontinuousBlockNumber,
#[error("Failed to build reth block: {0}")]
BuildRethBlock(SignatureError),
#[error("failed to create db: {0}")]
DatabaseCreation(DatabaseError),
#[error("failed to execute block: {0}")]
BlockExecution(VerificationError),
#[error("failed to update db: {0}")]
DatabaseUpdate(DatabaseError),
#[error("failed to get withdraw root: {0}")]
GetWithdrawRoot(DatabaseError),
#[error("state root mismatch: expected={expected}, found={found}")]
PostRootMismatch { expected: B256, found: B256 },
}

type Witness = ArchivedChunkWitness;
pub fn execute(witness: &Witness) -> Result<ChunkInfo, ChunkExecutionError> {
use ChunkExecutionError::*;

pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {
if witness.blocks.is_empty() {
return Err("At least one witness must be provided in chunk mode".into());
return Err(EmptyChunk);
}
if !witness.blocks.has_same_chain_id() {
return Err("All witnesses must have the same chain id in chunk mode".into());
return Err(DifferentChainId);
}
if !witness.blocks.has_seq_block_number() {
return Err("All witnesses must have sequential block numbers in chunk mode".into());
return Err(DiscontinuousBlockNumber);
}
// Get the blocks to build the basic chunk-info.
let blocks = manually_drop_on_zkvm!(
Expand All @@ -40,9 +63,9 @@ pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {
.iter()
.map(|w| w.build_reth_block())
.collect::<Result<Vec<RecoveredBlock<Block>>, _>>()
.map_err(|e| e.to_string())?
.map_err(BuildRethBlock)?
);
let pre_state_root = witness.blocks[0].pre_state_root;
let pre_state_root = witness.blocks[0].pre_state_root();

let fork_name = ForkName::from(&witness.fork_name);
let chain = Chain::from_id(witness.blocks[0].chain_id());
Expand Down Expand Up @@ -76,23 +99,21 @@ pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {
let prev_state_root = witness.blocks[0].pre_state_root();
let mut db = manually_drop_on_zkvm!(
EvmDatabase::new_from_root(code_db, prev_state_root, &nodes_provider, block_hashes)
.map_err(|e| format!("failed to create EvmDatabase: {}", e))?
.map_err(DatabaseCreation)?
);
for block in blocks.iter() {
let output = manually_drop_on_zkvm!(
EvmExecutor::new(std::sync::Arc::new(chain_spec.clone()), &db, block)
.execute()
.map_err(|e| format!("failed to execute block: {}", e))?
.map_err(BlockExecution)?
);
db.update(&nodes_provider, output.state.state.iter())
.map_err(|e| format!("failed to update db: {}", e))?;
.map_err(DatabaseUpdate)?;
}

let post_state_root = db.commit_changes();

let withdraw_root = db
.withdraw_root()
.map_err(|e| format!("failed to get withdraw root: {}", e))?;
let withdraw_root = db.withdraw_root().map_err(GetWithdrawRoot)?;

let mut rlp_buffer = manually_drop_on_zkvm!(Vec::with_capacity(2048));
let (tx_data_length, tx_data_digest) = blocks
Expand All @@ -103,18 +124,17 @@ pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {

let sbv_chunk_info = {
#[allow(unused_mut)]
let mut builder = ChunkInfoBuilder::new(&chain_spec, pre_state_root.into(), &blocks);
let mut builder = ChunkInfoBuilder::new(&chain_spec, pre_state_root, &blocks);
if fork_name == ForkName::EuclidV2 {
builder.set_prev_msg_queue_hash(witness.prev_msg_queue_hash.into());
}
builder.build(withdraw_root)
};
if post_state_root != sbv_chunk_info.post_state_root() {
return Err(format!(
"state root mismatch: expected={}, found={}",
sbv_chunk_info.post_state_root(),
post_state_root
));
return Err(PostRootMismatch {
expected: sbv_chunk_info.post_state_root(),
found: post_state_root,
});
}

let chunk_info = ChunkInfo {
Expand Down Expand Up @@ -146,3 +166,21 @@ pub fn execute(witness: &Witness) -> Result<ChunkInfo, String> {

Ok(chunk_info)
}

impl ChunkExecutionError {
/// Return the hash of the blinded node if the error is a blinded node error.
pub fn as_blinded_node_err(&self) -> Option<B256> {
use ChunkExecutionError::*;
use DatabaseError::PartialStateTrie;
use VerificationError::Database;
use sbv_trie::PartialStateTrieError::BlindedNode;

match self {
DatabaseCreation(PartialStateTrie(BlindedNode { hash, .. }))
| DatabaseUpdate(PartialStateTrie(BlindedNode { hash, .. }))
| GetWithdrawRoot(PartialStateTrie(BlindedNode { hash, .. }))
| BlockExecution(Database(PartialStateTrie(BlindedNode { hash, .. }))) => Some(*hash),
_ => None,
}
}
}
13 changes: 7 additions & 6 deletions crates/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ where
std::fs::create_dir_all(&cache_dir)?;

// Generate proving task for the circuit.
let task = if let Some(t) = task {
let mut task = if let Some(t) = task {
t
} else {
T::gen_proving_task()?
Expand All @@ -243,7 +243,7 @@ where
let prover = scroll_zkvm_prover::Prover::<T::Prover>::setup(config)?;

// Construct root proof for the circuit.
let proof = prover.gen_proof(&task)?;
let proof = prover.gen_proof(&mut task)?;

// Verify proof.
prover.verify_proof(&proof)?;
Expand Down Expand Up @@ -280,11 +280,12 @@ where
let prover = scroll_zkvm_prover::Prover::<T::Prover>::setup(config)?;

// Generate proving task for the circuit.
let tasks = tasks.map_or_else(|| T::gen_multi_proving_tasks(), |tasks| Ok(tasks.to_vec()))?;
let mut tasks =
tasks.map_or_else(|| T::gen_multi_proving_tasks(), |tasks| Ok(tasks.to_vec()))?;

// For each of the tasks, generate and verify proof.
let proofs = tasks
.iter()
.iter_mut()
.map(|task| {
let proof = prover.gen_proof(task)?;
prover.verify_proof(&proof)?;
Expand Down Expand Up @@ -335,10 +336,10 @@ where
)?;

// Generate proving task for the circuit.
let task = task.map_or_else(|| T::gen_proving_task(), Ok)?;
let mut task = task.map_or_else(|| T::gen_proving_task(), Ok)?;

// Construct root proof for the circuit.
let proof = prover.gen_proof_evm(&task)?;
let proof = prover.gen_proof_evm(&mut task)?;

// Verify proof.
prover.verify_proof_evm(&proof)?;
Expand Down
14 changes: 7 additions & 7 deletions crates/integration/tests/chunk_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use scroll_zkvm_prover::{
utils::{self, vm::ExecutionResult},
};

fn exec_chunk(task: &ChunkProvingTask) -> eyre::Result<(ExecutionResult, u64)> {
fn exec_chunk(task: &mut ChunkProvingTask) -> eyre::Result<(ExecutionResult, u64)> {
let (_path_app_config, app_config, path_exe) =
ChunkProverTester::load_with_exe_fd("app.vmexe")?;
let config = app_config.app_vm_config;
Expand Down Expand Up @@ -44,7 +44,7 @@ fn test_cycle() -> eyre::Result<()> {

let blocks = 1..=8;
blocks.into_iter().try_for_each(|blk| -> eyre::Result<()> {
let task = ChunkProvingTask {
let mut task = ChunkProvingTask {
block_witnesses: vec![read_block_witness_from_testdata(blk)?],
prev_msg_queue_hash: Default::default(),
fork_name: if cfg!(feature = "euclidv2") {
Expand All @@ -53,7 +53,7 @@ fn test_cycle() -> eyre::Result<()> {
String::from("euclidv1")
},
};
let (exec_result, gas) = exec_chunk(&task)?;
let (exec_result, gas) = exec_chunk(&mut task)?;
let cycle_per_gas = exec_result.total_cycle / gas;
assert!(cycle_per_gas < 30);
Ok(())
Expand All @@ -65,8 +65,8 @@ fn test_cycle() -> eyre::Result<()> {
fn test_execute() -> eyre::Result<()> {
ChunkProverTester::setup()?;

let task = ChunkProverTester::gen_proving_task()?;
exec_chunk(&task)?;
let mut task = ChunkProverTester::gen_proving_task()?;
exec_chunk(&mut task)?;

Ok(())
}
Expand All @@ -88,8 +88,8 @@ fn test_execute_multi() -> eyre::Result<()> {
let tasks = MultiChunkProverTester::gen_multi_proving_tasks().unwrap();
tasks
.into_par_iter()
.map(|task| -> (u64, u64, u64) {
let (exec_result, gas) = exec_chunk(&task).unwrap();
.map(|mut task| -> (u64, u64, u64) {
let (exec_result, gas) = exec_chunk(&mut task).unwrap();
(gas, exec_result.total_cycle, exec_result.total_tick)
})
.reduce(
Expand Down
2 changes: 2 additions & 0 deletions crates/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ openvm-native-recursion = { workspace = true }
openvm-sdk = { workspace = true }
openvm-stark-sdk = { workspace = true, default-features = false }

backon = { version = "1.5", features = ["std-blocking-sleep"] }
base64 = "0.22"
git-version = "0.3.5"
hex = "0.4"
munge = "=0.4.1"
once_cell = "1.20"
reqwest = { workspace = true, features = ["json", "blocking"] }
serde = "1.0"
serde_json = "1.0"
serde_stacker = "0.1"
Expand Down
4 changes: 2 additions & 2 deletions crates/prover/src/prover/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl ProverType for BatchProverType {

type ProofMetadata = BatchProofMetadata;

fn metadata_with_prechecks(task: &Self::ProvingTask) -> Result<Self::ProofMetadata, Error> {
let batch_info = task.into();
fn metadata_with_prechecks(task: &mut Self::ProvingTask) -> Result<Self::ProofMetadata, Error> {
let batch_info = (&*task).into();
let batch_hash = task.batch_header.batch_hash();

Ok(BatchProofMetadata {
Expand Down
2 changes: 1 addition & 1 deletion crates/prover/src/prover/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<C: Commitments> ProverType for GenericBundleProverType<C> {

type ProofMetadata = BundleProofMetadata;

fn metadata_with_prechecks(task: &Self::ProvingTask) -> Result<Self::ProofMetadata, Error> {
fn metadata_with_prechecks(task: &mut Self::ProvingTask) -> Result<Self::ProofMetadata, Error> {
let err_prefix = format!("metadata_with_prechecks for task_id={}", task.identifier());

for w in task.batch_proofs.windows(2) {
Expand Down
Loading
Loading