Skip to content

feat: more tracker points #59

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

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
26 changes: 8 additions & 18 deletions crates/core/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::DatabaseError;
use once_cell::sync::Lazy;
use revm::{
db::{AccountState, DatabaseRef},
db::DatabaseRef,
primitives::{AccountInfo, Address, Bytecode, B256, U256},
};
use sbv_primitives::{
Expand Down Expand Up @@ -124,28 +124,18 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmDatabase<CodeDb,
}
cycle_tracker_end!("insert CodeDB");

self.zktrie = ZkTrie::new_with_root(
self.zktrie_db.clone(),
NoCacheHasher,
l2_trace.root_before(),
self.zktrie = cycle_track!(
ZkTrie::new_with_root(
self.zktrie_db.clone(),
NoCacheHasher,
l2_trace.root_before(),
),
"ZkTrie::new_with_root"
)
.map_err(DatabaseError::zk_trie)?;

Ok(())
}

/// Invalidate internal cache for any account touched by EVM.
pub(crate) fn invalidate_storage_root_caches(
&mut self,
account_states: impl Iterator<Item = (Address, AccountState)>,
) {
let mut storage_trie_refs = self.storage_trie_refs.borrow_mut();
for (address, account_state) in account_states {
if account_state != AccountState::None {
storage_trie_refs.remove(&address);
}
}
}
}

impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> DatabaseRef
Expand Down
35 changes: 17 additions & 18 deletions crates/core/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod

/// Update the DB
pub fn update_db<T: Block>(&mut self, l2_trace: &T) -> Result<(), DatabaseError> {
self.db.db.invalidate_storage_root_caches(
self.db
.accounts
.iter()
.map(|(addr, acc)| (*addr, acc.account_state.clone())),
);

self.db.db.update(l2_trace)
}

Expand All @@ -58,7 +51,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
#[allow(clippy::let_and_return)]
let gas_used = measure_duration_millis!(
handle_block_duration_milliseconds,
self.handle_block_inner(l2_trace)
cycle_track!(self.handle_block_inner(l2_trace), "handle_block")
)?;

#[cfg(feature = "metrics")]
Expand Down Expand Up @@ -90,7 +83,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
let mut gas_used = 0;

for (idx, tx) in l2_trace.transactions().enumerate() {
cycle_tracker_start!("handle tx {}", idx);
cycle_tracker_start!("handle tx");

dev_trace!("handle {idx}th tx");

Expand Down Expand Up @@ -171,7 +164,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
self.hooks.post_tx_execution(self, idx);

dev_debug!("handle {idx}th tx done");
cycle_tracker_end!("handle tx {}", idx);
cycle_tracker_end!("handle tx");
}
Ok(gas_used)
}
Expand Down Expand Up @@ -215,7 +208,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
}

dev_trace!("committing {addr}, {:?} {db_acc:?}", db_acc.account_state);
cycle_tracker_start!("commit account {}", addr);
cycle_tracker_start!("commit account");

let mut storage_root = self.db.db.prev_storage_root(addr);

Expand All @@ -224,10 +217,13 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
let storage_root_before = storage_root;
// get storage tire
cycle_tracker_start!("update storage_tire");
let mut storage_trie = ZkTrie::<Poseidon, ZkDb>::new_with_root(
zktrie_db.clone(),
NoCacheHasher,
storage_root_before,
let mut storage_trie = cycle_track!(
ZkTrie::<Poseidon, ZkDb>::new_with_root(
zktrie_db.clone(),
NoCacheHasher,
storage_root_before,
),
"Zktrie::new_with_root"
)
.expect("unable to get storage trie");
for (key, value) in db_acc.storage.iter() {
Expand Down Expand Up @@ -255,7 +251,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod

measure_duration_micros!(
zktrie_commit_duration_microseconds,
storage_trie.commit()?
cycle_track!(storage_trie.commit()?, "Zktrie::commit")
);

cycle_tracker_end!("update storage_tire");
Expand Down Expand Up @@ -298,10 +294,13 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + Clone + 'static> EvmExecutor<'_, Cod
)
);

cycle_tracker_end!("commit account {}", addr);
cycle_tracker_end!("commit account");
}

measure_duration_micros!(zktrie_commit_duration_microseconds, zktrie.commit()?);
measure_duration_micros!(
zktrie_commit_duration_microseconds,
cycle_track!(zktrie.commit()?, "Zktrie::commit")
);

let root_after = *zktrie.root().unwrap_ref();

Expand Down
4 changes: 3 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ zktrie-ng.workspace = true
serde_json.workspace = true

[features]
dev = ["sbv-utils/dev"]
dev = ["sbv-utils/dev"]
sp1 = []
cycle-tracker = []
14 changes: 11 additions & 3 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,18 @@ pub trait Block: Debug {
if bytes == MAGIC_NODE_BYTES {
continue;
}
let node = Node::<Poseidon>::try_from(bytes).expect("invalid node");
let node_hash = node.get_or_calculate_node_hash().expect("infallible");
let node = cycle_track!(Node::<Poseidon>::try_from(bytes), "Node::try_from")
.expect("invalid node");
let node_hash = cycle_track!(
node.get_or_calculate_node_hash(),
"Node::get_or_calculate_node_hash"
)
.expect("infallible");
dev_trace!("put zktrie node: {:?}", node);
db.put_owned(node_hash.as_slice(), node.canonical_value(false))?;
cycle_track!(
db.put_owned(node_hash.as_slice(), node.canonical_value(false))?,
"KVDatabase::put_owned"
);
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/sbv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ dev = ["sbv-core/dev", "sbv-utils/dev", "sbv-primitives/dev"]
metrics = ["sbv-core/metrics", "sbv-utils/metrics"]

# sp1 related
sp1 = ["sbv-core/sp1"]
cycle-tracker = ["sbv-core/cycle-tracker"]
sp1 = ["sbv-core/sp1", "sbv-primitives/sp1"]
cycle-tracker = ["sbv-core/cycle-tracker", "sbv-primitives/cycle-tracker"]
ordered-db = ["sbv-core/ordered-db"]