Skip to content

Commit d2220ca

Browse files
authored
fix: handle missing proofs (#65)
* fix * bump MSRV * remove clear storage fix
1 parent a6f3612 commit d2220ca

File tree

7 files changed

+75
-30
lines changed

7 files changed

+75
-30
lines changed

Cargo.lock

+16-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resolver = "2"
55
[workspace.package]
66
version = "2.0.0"
77
edition = "2021"
8-
rust-version = "1.75"
8+
rust-version = "1.81"
99
authors = ["Scroll developers"]
1010
license = "MIT OR Apache-2.0"
1111
homepage = "https://github.com/scroll-tech/stateless-block-verifier"

crates/bin/src/commands/run_file.rs

+39-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use crate::utils;
2-
use anyhow::bail;
2+
use anyhow::{anyhow, bail};
33
use clap::Args;
4+
use sbv::primitives::types::LegacyStorageTrace;
45
use sbv::{
56
core::{ChunkInfo, EvmExecutorBuilder, HardforkConfig},
67
primitives::{types::BlockTrace, zk_trie::db::kv::HashMapDb, Block, B256},
78
};
9+
use std::panic::catch_unwind;
810
use std::{cell::RefCell, path::PathBuf};
911
use tiny_keccak::{Hasher, Keccak};
1012
use tokio::task::JoinSet;
@@ -43,7 +45,7 @@ impl RunFileCommand {
4345

4446
while let Some(task) = tasks.join_next().await {
4547
if let Err(err) = task? {
46-
bail!("{:?}", err);
48+
dev_error!("{:?}", err);
4749
}
4850
}
4951

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

116118
fn deserialize_block_trace(trace: &str) -> anyhow::Result<BlockTrace> {
119+
// Try to deserialize `BlockTrace` from JSON. In case of failure, try to
120+
// deserialize `BlockTrace` from a JSON-RPC response that has the actual block
121+
// trace nested in the value of the key "result".
117122
Ok(
118-
// Try to deserialize `BlockTrace` from JSON. In case of failure, try to
119-
// deserialize `BlockTrace` from a JSON-RPC response that has the actual block
120-
// trace nested in the value of the key "result".
121-
serde_json::from_str::<BlockTrace>(trace).or_else(|_| {
122-
#[derive(serde::Deserialize, Default, Debug, Clone)]
123-
pub struct BlockTraceJsonRpcResult {
124-
pub result: BlockTrace,
125-
}
126-
Ok::<_, serde_json::Error>(
127-
serde_json::from_str::<BlockTraceJsonRpcResult>(trace)?.result,
128-
)
129-
})?,
123+
serde_json::from_str::<BlockTrace<LegacyStorageTrace>>(trace)
124+
.or_else(|_| {
125+
#[derive(serde::Deserialize, Default, Debug, Clone)]
126+
pub struct BlockTraceJsonRpcResult {
127+
pub result: BlockTrace<LegacyStorageTrace>,
128+
}
129+
Ok::<_, serde_json::Error>(
130+
serde_json::from_str::<BlockTraceJsonRpcResult>(trace)?.result,
131+
)
132+
})?
133+
.into(),
130134
)
131135
}
132136

@@ -136,6 +140,26 @@ async fn run_trace(
136140
) -> anyhow::Result<()> {
137141
let trace = read_block_trace(&path).await?;
138142
let fork_config = fork_config(trace.chain_id());
139-
tokio::task::spawn_blocking(move || utils::verify(&trace, &fork_config)).await??;
143+
if let Err(e) =
144+
tokio::task::spawn_blocking(move || catch_unwind(|| utils::verify(&trace, &fork_config)))
145+
.await?
146+
.map_err(|e| {
147+
e.downcast_ref::<&str>()
148+
.map(|s| anyhow!("task panics with: {s}"))
149+
.or_else(|| {
150+
e.downcast_ref::<String>()
151+
.map(|s| anyhow!("task panics with: {s}"))
152+
})
153+
.unwrap_or_else(|| anyhow!("task panics"))
154+
})
155+
.and_then(|r| r.map_err(anyhow::Error::from))
156+
{
157+
dev_error!(
158+
"Error occurs when verifying block ({}): {:?}",
159+
path.display(),
160+
e
161+
);
162+
return Err(e);
163+
}
140164
Ok(())
141165
}

crates/core/src/database.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct EvmDatabase<'a, CodeDb, ZkDb> {
2626
storage_root_caches: RefCell<HashMap<Address, ZkHash>>,
2727
/// Storage trie cache, avoid re-creating trie for the same account.
2828
/// Need to invalidate before `update`, otherwise the trie root may be outdated
29-
storage_trie_caches: RefCell<HashMap<ZkHash, ZkTrie<Poseidon>>>,
29+
storage_trie_caches: RefCell<HashMap<ZkHash, Option<ZkTrie<Poseidon>>>>,
3030
/// Current uncommitted zkTrie root based on the block trace.
3131
committed_zktrie_root: B256,
3232
/// The underlying zkTrie database.
@@ -97,7 +97,7 @@ impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb,
9797
storage_trie_caches.remove(&old);
9898
}
9999

100-
storage_trie_caches.insert(new_root, storage_root);
100+
storage_trie_caches.insert(new_root, Some(storage_root));
101101
}
102102

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

209209
ZkTrie::new_with_root(self.zktrie_db, NoCacheHasher, *storage_root)
210-
.expect("storage trie associated with account not found")
210+
.inspect_err(|e| {
211+
dev_warn!("storage trie associated with account({address}) not found: {e}")
212+
})
213+
.ok()
211214
});
215+
if trie.is_none() {
216+
return Err(DatabaseError::NotIncluded);
217+
}
218+
let trie = trie.as_mut().unwrap();
212219

213220
#[cfg(debug_assertions)]
214221
{

crates/core/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub enum DatabaseError {
1010
/// Error encountered from zkTrie.
1111
#[error("error encountered from zkTrie: {0}")]
1212
ZkTrie(Box<dyn Error + Send + Sync>),
13+
/// Error when try to load un-included account/storage.
14+
#[error("account/storage not included in zkTrie")]
15+
NotIncluded,
1316
}
1417

1518
/// Error variants encountered during verification of transactions in a L2 block.

crates/core/src/executor/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, '_, CodeDb,
285285
debug_recorder.record_account(*addr, info.clone(), storage_root);
286286

287287
let acc_data = Account::from_revm_account_with_storage_root(info, storage_root);
288+
dev_trace!("committing account {addr}: {acc_data:?}");
288289
measure_duration_micros!(
289290
zktrie_update_duration_microseconds,
290291
cycle_track!(

crates/utils/src/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ macro_rules! cycle_tracker_end {
3939
#[macro_export]
4040
macro_rules! dev_trace {
4141
($($arg:tt)*) => {
42-
#[cfg(any(feature = "dev", test))]
4342
{
43+
#[cfg(any(feature = "dev", test))]
4444
$crate::tracing::trace!($($arg)*);
4545
}
4646
};
@@ -50,8 +50,8 @@ macro_rules! dev_trace {
5050
#[macro_export]
5151
macro_rules! dev_info {
5252
($($arg:tt)*) => {
53-
#[cfg(any(feature = "dev", test))]
5453
{
54+
#[cfg(any(feature = "dev", test))]
5555
$crate::tracing::info!($($arg)*);
5656
}
5757
};
@@ -61,8 +61,8 @@ macro_rules! dev_info {
6161
#[macro_export]
6262
macro_rules! dev_error {
6363
($($arg:tt)*) => {
64-
#[cfg(any(feature = "dev", test))]
6564
{
65+
#[cfg(any(feature = "dev", test))]
6666
$crate::tracing::error!($($arg)*);
6767
}
6868
};
@@ -72,8 +72,8 @@ macro_rules! dev_error {
7272
#[macro_export]
7373
macro_rules! dev_debug {
7474
($($arg:tt)*) => {
75-
#[cfg(any(feature = "dev", test))]
7675
{
76+
#[cfg(any(feature = "dev", test))]
7777
$crate::tracing::debug!($($arg)*);
7878
}
7979
};
@@ -83,8 +83,8 @@ macro_rules! dev_debug {
8383
#[macro_export]
8484
macro_rules! dev_warn {
8585
($($arg:tt)*) => {
86-
#[cfg(any(feature = "dev", test))]
8786
{
87+
#[cfg(any(feature = "dev", test))]
8888
$crate::tracing::warn!($($arg)*);
8989
}
9090
};

0 commit comments

Comments
 (0)