Skip to content

Commit

Permalink
fix: handle missing proofs (#65)
Browse files Browse the repository at this point in the history
* fix

* bump MSRV

* remove clear storage fix
  • Loading branch information
lightsing authored Oct 17, 2024
1 parent a6f3612 commit d2220ca
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 30 deletions.
22 changes: 16 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"
[workspace.package]
version = "2.0.0"
edition = "2021"
rust-version = "1.75"
rust-version = "1.81"
authors = ["Scroll developers"]
license = "MIT OR Apache-2.0"
homepage = "https://github.com/scroll-tech/stateless-block-verifier"
Expand Down
54 changes: 39 additions & 15 deletions crates/bin/src/commands/run_file.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::utils;
use anyhow::bail;
use anyhow::{anyhow, bail};
use clap::Args;
use sbv::primitives::types::LegacyStorageTrace;
use sbv::{
core::{ChunkInfo, EvmExecutorBuilder, HardforkConfig},
primitives::{types::BlockTrace, zk_trie::db::kv::HashMapDb, Block, B256},
};
use std::panic::catch_unwind;
use std::{cell::RefCell, path::PathBuf};
use tiny_keccak::{Hasher, Keccak};
use tokio::task::JoinSet;
Expand Down Expand Up @@ -43,7 +45,7 @@ impl RunFileCommand {

while let Some(task) = tasks.join_next().await {
if let Err(err) = task? {
bail!("{:?}", err);
dev_error!("{:?}", err);
}
}

Expand Down Expand Up @@ -114,19 +116,21 @@ async fn read_block_trace(path: &PathBuf) -> anyhow::Result<BlockTrace> {
}

fn deserialize_block_trace(trace: &str) -> anyhow::Result<BlockTrace> {
// Try to deserialize `BlockTrace` from JSON. In case of failure, try to
// deserialize `BlockTrace` from a JSON-RPC response that has the actual block
// trace nested in the value of the key "result".
Ok(
// Try to deserialize `BlockTrace` from JSON. In case of failure, try to
// deserialize `BlockTrace` from a JSON-RPC response that has the actual block
// trace nested in the value of the key "result".
serde_json::from_str::<BlockTrace>(trace).or_else(|_| {
#[derive(serde::Deserialize, Default, Debug, Clone)]
pub struct BlockTraceJsonRpcResult {
pub result: BlockTrace,
}
Ok::<_, serde_json::Error>(
serde_json::from_str::<BlockTraceJsonRpcResult>(trace)?.result,
)
})?,
serde_json::from_str::<BlockTrace<LegacyStorageTrace>>(trace)
.or_else(|_| {
#[derive(serde::Deserialize, Default, Debug, Clone)]
pub struct BlockTraceJsonRpcResult {
pub result: BlockTrace<LegacyStorageTrace>,
}
Ok::<_, serde_json::Error>(
serde_json::from_str::<BlockTraceJsonRpcResult>(trace)?.result,
)
})?
.into(),
)
}

Expand All @@ -136,6 +140,26 @@ async fn run_trace(
) -> anyhow::Result<()> {
let trace = read_block_trace(&path).await?;
let fork_config = fork_config(trace.chain_id());
tokio::task::spawn_blocking(move || utils::verify(&trace, &fork_config)).await??;
if let Err(e) =
tokio::task::spawn_blocking(move || catch_unwind(|| utils::verify(&trace, &fork_config)))
.await?
.map_err(|e| {
e.downcast_ref::<&str>()
.map(|s| anyhow!("task panics with: {s}"))
.or_else(|| {
e.downcast_ref::<String>()
.map(|s| anyhow!("task panics with: {s}"))
})
.unwrap_or_else(|| anyhow!("task panics"))
})
.and_then(|r| r.map_err(anyhow::Error::from))
{
dev_error!(
"Error occurs when verifying block ({}): {:?}",
path.display(),
e
);
return Err(e);
}
Ok(())
}
13 changes: 10 additions & 3 deletions crates/core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct EvmDatabase<'a, CodeDb, ZkDb> {
storage_root_caches: RefCell<HashMap<Address, ZkHash>>,
/// Storage trie cache, avoid re-creating trie for the same account.
/// Need to invalidate before `update`, otherwise the trie root may be outdated
storage_trie_caches: RefCell<HashMap<ZkHash, ZkTrie<Poseidon>>>,
storage_trie_caches: RefCell<HashMap<ZkHash, Option<ZkTrie<Poseidon>>>>,
/// Current uncommitted zkTrie root based on the block trace.
committed_zktrie_root: B256,
/// The underlying zkTrie database.
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb,
storage_trie_caches.remove(&old);
}

storage_trie_caches.insert(new_root, storage_root);
storage_trie_caches.insert(new_root, Some(storage_root));
}

/// Get the committed zkTrie root.
Expand Down Expand Up @@ -207,8 +207,15 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> DatabaseRef for EvmDatabase
dev_debug!("storage root of {:?} is {:?}", address, storage_root);

ZkTrie::new_with_root(self.zktrie_db, NoCacheHasher, *storage_root)
.expect("storage trie associated with account not found")
.inspect_err(|e| {
dev_warn!("storage trie associated with account({address}) not found: {e}")
})
.ok()
});
if trie.is_none() {
return Err(DatabaseError::NotIncluded);
}
let trie = trie.as_mut().unwrap();

#[cfg(debug_assertions)]
{
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub enum DatabaseError {
/// Error encountered from zkTrie.
#[error("error encountered from zkTrie: {0}")]
ZkTrie(Box<dyn Error + Send + Sync>),
/// Error when try to load un-included account/storage.
#[error("account/storage not included in zkTrie")]
NotIncluded,
}

/// Error variants encountered during verification of transactions in a L2 block.
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, '_, CodeDb,
debug_recorder.record_account(*addr, info.clone(), storage_root);

let acc_data = Account::from_revm_account_with_storage_root(info, storage_root);
dev_trace!("committing account {addr}: {acc_data:?}");
measure_duration_micros!(
zktrie_update_duration_microseconds,
cycle_track!(
Expand Down
10 changes: 5 additions & 5 deletions crates/utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ macro_rules! cycle_tracker_end {
#[macro_export]
macro_rules! dev_trace {
($($arg:tt)*) => {
#[cfg(any(feature = "dev", test))]
{
#[cfg(any(feature = "dev", test))]
$crate::tracing::trace!($($arg)*);
}
};
Expand All @@ -50,8 +50,8 @@ macro_rules! dev_trace {
#[macro_export]
macro_rules! dev_info {
($($arg:tt)*) => {
#[cfg(any(feature = "dev", test))]
{
#[cfg(any(feature = "dev", test))]
$crate::tracing::info!($($arg)*);
}
};
Expand All @@ -61,8 +61,8 @@ macro_rules! dev_info {
#[macro_export]
macro_rules! dev_error {
($($arg:tt)*) => {
#[cfg(any(feature = "dev", test))]
{
#[cfg(any(feature = "dev", test))]
$crate::tracing::error!($($arg)*);
}
};
Expand All @@ -72,8 +72,8 @@ macro_rules! dev_error {
#[macro_export]
macro_rules! dev_debug {
($($arg:tt)*) => {
#[cfg(any(feature = "dev", test))]
{
#[cfg(any(feature = "dev", test))]
$crate::tracing::debug!($($arg)*);
}
};
Expand All @@ -83,8 +83,8 @@ macro_rules! dev_debug {
#[macro_export]
macro_rules! dev_warn {
($($arg:tt)*) => {
#[cfg(any(feature = "dev", test))]
{
#[cfg(any(feature = "dev", test))]
$crate::tracing::warn!($($arg)*);
}
};
Expand Down

0 comments on commit d2220ca

Please sign in to comment.