Skip to content
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

fix: clear storage root when empty #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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.

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
6 changes: 6 additions & 0 deletions crates/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, '_, CodeDb,

#[cfg(feature = "debug-storage")]
debug_recorder.record_storage_root(*addr, storage_root);
} else if db_acc.account_state.is_storage_cleared() {
// previous storage root might not be zero, and evm cleared the storage
// e.g. create storage collision
// not something could happen in real world
storage_root = B256::ZERO;
}
if !info.is_empty() {
// if account not exist, all fields will be zero.
Expand All @@ -285,6 +290,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
Loading