From 062292e3cf95c5c6b1e1462eb130a6e450bc8f6b Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Fri, 20 Sep 2024 12:21:32 +0800 Subject: [PATCH 01/14] feat: more metrics (#55) * add more metrics * range adjustment * fix * update grafana --- crates/bin/src/utils.rs | 8 +- crates/core/src/chunk.rs | 4 +- crates/core/src/database.rs | 94 +++-- crates/core/src/executor/mod.rs | 67 +-- crates/sbv/src/lib.rs | 2 +- crates/utils/src/macros.rs | 38 +- crates/utils/src/metrics/registry.rs | 99 +++-- grafana.json | 607 +++++++++++++++++++++------ 8 files changed, 689 insertions(+), 230 deletions(-) diff --git a/crates/bin/src/utils.rs b/crates/bin/src/utils.rs index fec9f58..1786a62 100644 --- a/crates/bin/src/utils.rs +++ b/crates/bin/src/utils.rs @@ -9,8 +9,8 @@ pub fn verify( l2_trace: T, fork_config: &HardforkConfig, ) -> Result<(), VerificationError> { - measure_duration_histogram!( - total_block_verification_duration_microseconds, + measure_duration_millis!( + total_block_verification_duration_milliseconds, verify_inner(l2_trace, fork_config) ) } @@ -40,8 +40,8 @@ fn verify_inner( let zktrie_db = cycle_track!( { let mut zktrie_db = ZkMemoryDb::new(); - measure_duration_histogram!( - build_zktrie_db_duration_microseconds, + measure_duration_millis!( + build_zktrie_db_duration_milliseconds, l2_trace.build_zktrie_db(&mut zktrie_db) ); Rc::new(zktrie_db) diff --git a/crates/core/src/chunk.rs b/crates/core/src/chunk.rs index 9bca531..7438032 100644 --- a/crates/core/src/chunk.rs +++ b/crates/core/src/chunk.rs @@ -43,8 +43,8 @@ impl ChunkInfo { let mut zktrie_db = ZkMemoryDb::new(); for trace in traces.iter() { - measure_duration_histogram!( - build_zktrie_db_duration_microseconds, + measure_duration_millis!( + build_zktrie_db_duration_milliseconds, trace.build_zktrie_db(&mut zktrie_db) ); } diff --git a/crates/core/src/database.rs b/crates/core/src/database.rs index 2e6f720..f535f60 100644 --- a/crates/core/src/database.rs +++ b/crates/core/src/database.rs @@ -121,7 +121,7 @@ impl ReadOnlyDB { /// Update the database with a new block trace. pub fn update(&mut self, l2_trace: T) -> Result<()> { - measure_duration_histogram!(update_db_duration_microseconds, self.update_inner(l2_trace)) + measure_duration_millis!(update_db_duration_milliseconds, self.update_inner(l2_trace)) } fn update_inner(&mut self, l2_trace: T) -> Result<()> { @@ -162,41 +162,42 @@ impl DatabaseRef for ReadOnlyDB { /// Get basic account information. fn basic_ref(&self, address: Address) -> Result, Self::Error> { - Ok(self - .zktrie_db_ref - .get_account(address.as_slice()) - .map(|account_data| { - let code_size = - u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize; - let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap()); - let balance = U256::from_be_bytes(account_data[1]); - let code_hash = B256::from(account_data[3]); - let poseidon_code_hash = B256::from(account_data[4]); - - let storage_root = B256::from(account_data[2]); - self.prev_storage_roots - .borrow_mut() - .entry(address) - .or_insert(storage_root.0.into()); - - let zktrie_db = self.zktrie_db.clone(); - self.storage_trie_refs.borrow_mut().insert( - address, - Lazy::new(Box::new(move || { - zktrie_db - .new_ref_trie(&storage_root.0) - .expect("storage trie associated with account not found") - })), - ); - AccountInfo { - balance, - nonce, - code_size, - code_hash, - poseidon_code_hash, - code: self.code_db.get(&code_hash).cloned(), - } - })) + Ok(measure_duration_micros!( + zktrie_get_duration_microseconds, + self.zktrie_db_ref.get_account(address.as_slice()) + ) + .map(|account_data| { + let code_size = + u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize; + let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap()); + let balance = U256::from_be_bytes(account_data[1]); + let code_hash = B256::from(account_data[3]); + let poseidon_code_hash = B256::from(account_data[4]); + + let storage_root = B256::from(account_data[2]); + self.prev_storage_roots + .borrow_mut() + .entry(address) + .or_insert(storage_root.0.into()); + + let zktrie_db = self.zktrie_db.clone(); + self.storage_trie_refs.borrow_mut().insert( + address, + Lazy::new(Box::new(move || { + zktrie_db + .new_ref_trie(&storage_root.0) + .expect("storage trie associated with account not found") + })), + ); + AccountInfo { + balance, + nonce, + code_size, + code_hash, + poseidon_code_hash, + code: self.code_db.get(&code_hash).cloned(), + } + })) } /// Get account code by its code hash. @@ -220,11 +221,12 @@ impl DatabaseRef for ReadOnlyDB { let trie = storage_trie_refs .entry(address) .or_insert_with_key(|address| { - let storage_root = self - .zktrie_db_ref - .get_account(address.as_slice()) - .map(|account_data| B256::from(account_data[2])) - .unwrap_or_default(); + let storage_root = measure_duration_micros!( + zktrie_get_duration_microseconds, + self.zktrie_db_ref.get_account(address.as_slice()) + ) + .map(|account_data| B256::from(account_data[2])) + .unwrap_or_default(); let zktrie_db = self.zktrie_db.clone(); Lazy::new(Box::new(move || { zktrie_db @@ -234,10 +236,12 @@ impl DatabaseRef for ReadOnlyDB { })) }); - Ok(trie - .get_store(&index.to_be_bytes::<32>()) - .map(|store_data| U256::from_be_bytes(store_data)) - .unwrap_or_default()) + Ok(measure_duration_micros!( + zktrie_get_duration_microseconds, + trie.get_store(&index.to_be_bytes::<32>()) + ) + .map(|store_data| U256::from_be_bytes(store_data)) + .unwrap_or_default()) } /// Get block hash by block number. diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index 8bb7b03..308887d 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -43,8 +43,8 @@ impl EvmExecutor<'_> { /// Handle a block. pub fn handle_block(&mut self, l2_trace: &T) -> Result<(), VerificationError> { - measure_duration_histogram!( - handle_block_duration_microseconds, + measure_duration_millis!( + handle_block_duration_milliseconds, self.handle_block_inner(l2_trace) )?; @@ -139,13 +139,15 @@ impl EvmExecutor<'_> { dev_trace!("handler cfg: {:?}", revm.handler.cfg); - let _result = + let _result = measure_duration_millis!( + transact_commit_duration_milliseconds, cycle_track!(revm.transact_commit(), "transact_commit").map_err(|e| { VerificationError::EvmExecution { tx_hash: *tx.tx_hash(), source: e, } - })?; + })? + ); dev_trace!("{_result:#?}"); } @@ -159,8 +161,8 @@ impl EvmExecutor<'_> { /// Commit pending changes in cache db to zktrie pub fn commit_changes(&mut self, zktrie_db: &Rc) -> B256 { - measure_duration_histogram!( - commit_changes_duration_microseconds, + measure_duration_millis!( + commit_changes_duration_milliseconds, cycle_track!(self.commit_changes_inner(zktrie_db), "commit_changes") ) } @@ -203,16 +205,22 @@ impl EvmExecutor<'_> { .expect("unable to get storage trie"); for (key, value) in db_acc.storage.iter() { if !value.is_zero() { - cycle_track!( - storage_trie - .update_store(&key.to_be_bytes::<32>(), &value.to_be_bytes()) - .expect("failed to update storage"), - "Zktrie::update_store" + measure_duration_micros!( + zktrie_update_duration_microseconds, + cycle_track!( + storage_trie + .update_store(&key.to_be_bytes::<32>(), &value.to_be_bytes()) + .expect("failed to update storage"), + "Zktrie::update_store" + ) ); } else { - cycle_track!( - storage_trie.delete(&key.to_be_bytes::<32>()), - "Zktrie::delete" + measure_duration_micros!( + zktrie_delete_duration_microseconds, + cycle_track!( + storage_trie.delete(&key.to_be_bytes::<32>()), + "Zktrie::delete" + ) ); } @@ -220,9 +228,12 @@ impl EvmExecutor<'_> { debug_recorder.record_storage(*addr, *key, *value); } - if storage_trie.is_trie_dirty() { - storage_trie.prepare_root(); - } + measure_duration_micros!( + zktrie_commit_duration_microseconds, + if storage_trie.is_trie_dirty() { + storage_trie.prepare_root(); + } + ); cycle_tracker_end!("update storage_tire"); storage_root = storage_trie.root().into(); @@ -268,19 +279,25 @@ impl EvmExecutor<'_> { code_hash.0, poseidon_code_hash.0, ]; - cycle_track!( - zktrie - .update_account(addr.as_slice(), &acc_data) - .expect("failed to update account"), - "Zktrie::update_account" + measure_duration_micros!( + zktrie_update_duration_microseconds, + cycle_track!( + zktrie + .update_account(addr.as_slice(), &acc_data) + .expect("failed to update account"), + "Zktrie::update_account" + ) ); cycle_tracker_end!("commit account {}", addr); } - if zktrie.is_trie_dirty() { - zktrie.prepare_root(); - } + measure_duration_micros!( + zktrie_commit_duration_microseconds, + if zktrie.is_trie_dirty() { + zktrie.prepare_root(); + } + ); let root_after = zktrie.root(); diff --git a/crates/sbv/src/lib.rs b/crates/sbv/src/lib.rs index fb3c466..359b4d2 100644 --- a/crates/sbv/src/lib.rs +++ b/crates/sbv/src/lib.rs @@ -6,5 +6,5 @@ pub use sbv_utils as utils; pub use sbv_utils::{ cycle_track, cycle_tracker_end, cycle_tracker_start, dev_debug, dev_error, dev_info, dev_trace, - dev_warn, measure_duration_histogram, update_metrics_counter, update_metrics_gauge, + dev_warn, measure_duration_millis, update_metrics_counter, update_metrics_gauge, }; diff --git a/crates/utils/src/macros.rs b/crates/utils/src/macros.rs index 77ad3cf..f8f1af1 100644 --- a/crates/utils/src/macros.rs +++ b/crates/utils/src/macros.rs @@ -92,7 +92,37 @@ macro_rules! dev_warn { /// This macro is for measuring duration to metrics #[macro_export] -macro_rules! measure_duration_histogram { +macro_rules! measure_duration_millis { + ($label:ident, $e:expr) => {{ + #[cfg(feature = "metrics")] + let __measure_duration_histogram_start = std::time::Instant::now(); + + #[allow(clippy::let_and_return)] + let __measure_duration_histogram_result = $e; + + #[cfg(feature = "metrics")] + let __measure_duration_histogram_elasped = __measure_duration_histogram_start.elapsed(); + #[cfg(feature = "metrics")] + let __duration_millis = __measure_duration_histogram_elasped.as_secs() as f64 * 1_000.0 + + __measure_duration_histogram_elasped.subsec_nanos() as f64 / 1_000_000.0; + + #[cfg(feature = "metrics")] + $crate::metrics::REGISTRY.$label.observe(__duration_millis); + + #[cfg(feature = "metrics")] + dev_debug!( + "measured duration {} = {:?}ms", + stringify!($label), + __duration_millis, + ); + + __measure_duration_histogram_result + }}; +} + +/// This macro is for measuring duration to metrics +#[macro_export] +macro_rules! measure_duration_micros { ($label:ident, $e:expr) => {{ #[cfg(feature = "metrics")] let __measure_duration_histogram_start = std::time::Instant::now(); @@ -103,13 +133,13 @@ macro_rules! measure_duration_histogram { #[cfg(feature = "metrics")] $crate::metrics::REGISTRY .$label - .observe(__measure_duration_histogram_start.elapsed().as_millis() as f64); + .observe(__measure_duration_histogram_start.elapsed().as_micros() as f64); #[cfg(feature = "metrics")] dev_debug!( - "measured duration {} = {:?}", + "measured duration {} = {:?}us", stringify!($label), - __measure_duration_histogram_start.elapsed(), + __measure_duration_histogram_start.elapsed().as_micros() as f64, ); __measure_duration_histogram_result diff --git a/crates/utils/src/metrics/registry.rs b/crates/utils/src/metrics/registry.rs index 949f37b..64d8d4b 100644 --- a/crates/utils/src/metrics/registry.rs +++ b/crates/utils/src/metrics/registry.rs @@ -17,11 +17,19 @@ pub struct Registry { pub verification_error: Counter, - pub build_zktrie_db_duration_microseconds: Histogram, - pub update_db_duration_microseconds: Histogram, - pub handle_block_duration_microseconds: Histogram, - pub commit_changes_duration_microseconds: Histogram, - pub total_block_verification_duration_microseconds: Histogram, + // database metrics + pub build_zktrie_db_duration_milliseconds: Histogram, + pub update_db_duration_milliseconds: Histogram, + pub zktrie_get_duration_microseconds: Histogram, + pub zktrie_update_duration_microseconds: Histogram, + pub zktrie_delete_duration_microseconds: Histogram, + pub zktrie_commit_duration_microseconds: Histogram, + + // executor metrics + pub transact_commit_duration_milliseconds: Histogram, + pub handle_block_duration_milliseconds: Histogram, + pub commit_changes_duration_milliseconds: Histogram, + pub total_block_verification_duration_milliseconds: Histogram, } pub(super) fn init() -> Registry { @@ -55,40 +63,75 @@ pub(super) fn init() -> Registry { verification_error.clone(), ); - let build_zktrie_db_duration_microseconds = Histogram::new(linear_buckets(50.0, 50.0, 10)); + let build_zktrie_db_duration_milliseconds = Histogram::new(linear_buckets(50.0, 50.0, 10)); registry.register( "build_zktrie_db_duration", - "Duration of build_zktrie_db_duration in microseconds", - build_zktrie_db_duration_microseconds.clone(), + "Duration of build_zktrie_db_duration in milliseconds", + build_zktrie_db_duration_milliseconds.clone(), ); - let update_db_duration_microseconds = Histogram::new(linear_buckets(2.0, 4.0, 10)); + let update_db_duration_milliseconds = Histogram::new(linear_buckets(2.0, 4.0, 10)); registry.register( "update_db_duration", - "Duration of update_db in microseconds", - update_db_duration_microseconds.clone(), + "Duration of update_db in milliseconds", + update_db_duration_milliseconds.clone(), ); - let handle_block_duration_microseconds = Histogram::new(linear_buckets(1.0, 5.0, 10)); + let zktrie_get_duration_microseconds = Histogram::new(linear_buckets(50.0, 500.0, 10)); + registry.register( + "zktrie_get_duration", + "Duration of zktrie_get in microseconds", + zktrie_get_duration_microseconds.clone(), + ); + + let zktrie_update_duration_microseconds = Histogram::new(linear_buckets(50.0, 500.0, 10)); + registry.register( + "zktrie_update_duration", + "Duration of zktrie_update in microseconds", + zktrie_update_duration_microseconds.clone(), + ); + + let zktrie_delete_duration_microseconds = Histogram::new(linear_buckets(50.0, 500.0, 10)); + registry.register( + "zktrie_delete_duration", + "Duration of zktrie_delete in microseconds", + zktrie_delete_duration_microseconds.clone(), + ); + + let zktrie_commit_duration_microseconds = Histogram::new(linear_buckets(100.0, 2000.0, 10)); + registry.register( + "zktrie_commit_duration", + "Duration of zktrie_commit in microseconds", + zktrie_commit_duration_microseconds.clone(), + ); + + let transact_commit_duration_milliseconds = Histogram::new(linear_buckets(0.1, 15.0, 10)); + registry.register( + "transact_commit_duration", + "Duration of transact_commit in milliseconds", + transact_commit_duration_milliseconds.clone(), + ); + + let handle_block_duration_milliseconds = Histogram::new(linear_buckets(1.0, 5.0, 10)); registry.register( "handle_block_duration", - "Duration of handle_block in microseconds", - handle_block_duration_microseconds.clone(), + "Duration of handle_block in milliseconds", + handle_block_duration_milliseconds.clone(), ); - let commit_changes_duration_microseconds = Histogram::new(linear_buckets(25.0, 50.0, 10)); + let commit_changes_duration_milliseconds = Histogram::new(linear_buckets(25.0, 50.0, 10)); registry.register( "commit_changes_duration", - "Duration of commit_changes in microseconds", - commit_changes_duration_microseconds.clone(), + "Duration of commit_changes in milliseconds", + commit_changes_duration_milliseconds.clone(), ); - let total_block_verification_duration_microseconds = + let total_block_verification_duration_milliseconds = Histogram::new(linear_buckets(50.0, 50.0, 15)); registry.register( "total_block_verification_duration", - "Total block verification duration in microseconds", - total_block_verification_duration_microseconds.clone(), + "Total block verification duration in milliseconds", + total_block_verification_duration_milliseconds.clone(), ); Registry { @@ -100,10 +143,16 @@ pub(super) fn init() -> Registry { verification_error, - build_zktrie_db_duration_microseconds, - update_db_duration_microseconds, - handle_block_duration_microseconds, - commit_changes_duration_microseconds, - total_block_verification_duration_microseconds, + build_zktrie_db_duration_milliseconds, + update_db_duration_milliseconds, + zktrie_get_duration_microseconds, + zktrie_update_duration_microseconds, + zktrie_delete_duration_microseconds, + zktrie_commit_duration_microseconds, + + handle_block_duration_milliseconds, + transact_commit_duration_milliseconds, + commit_changes_duration_milliseconds, + total_block_verification_duration_milliseconds, } } diff --git a/grafana.json b/grafana.json index 5f6fd23..19b7e6f 100644 --- a/grafana.json +++ b/grafana.json @@ -1,47 +1,4 @@ { - "__inputs": [ - { - "name": "DS_PROMETHEUS", - "label": "prometheus", - "description": "", - "type": "datasource", - "pluginId": "prometheus", - "pluginName": "Prometheus" - } - ], - "__elements": {}, - "__requires": [ - { - "type": "panel", - "id": "bargauge", - "name": "Bar gauge", - "version": "" - }, - { - "type": "grafana", - "id": "grafana", - "name": "Grafana", - "version": "11.1.4" - }, - { - "type": "panel", - "id": "heatmap", - "name": "Heatmap", - "version": "" - }, - { - "type": "datasource", - "id": "prometheus", - "name": "Prometheus", - "version": "1.0.0" - }, - { - "type": "panel", - "id": "stat", - "name": "Stat", - "version": "" - } - ], "annotations": { "list": [ { @@ -61,27 +18,35 @@ "editable": true, "fiscalYearStartMonth": 0, "graphTooltip": 0, - "id": null, + "id": 1, "links": [], "panels": [ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Counter for all processed blocks.", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, + "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green", + "color": "red", "value": null + }, + { + "color": "yellow", + "value": 0.33 + }, + { + "color": "green", + "value": 0.66 } ] } @@ -94,7 +59,7 @@ "x": 0, "y": 0 }, - "id": 8, + "id": 9, "options": { "colorMode": "value", "graphMode": "area", @@ -117,11 +82,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "builder", - "expr": "block_counter_total", + "expr": "rate(block_counter_total{instance=\"$netowrk\"}[$__rate_interval])", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, @@ -131,35 +96,27 @@ "useBackend": false } ], - "title": "Processed Blocks", + "title": "Block Per Seconds", "type": "stat" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, + "description": "Current height of fetched block for verifying.", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, - "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ - { - "color": "red", - "value": null - }, - { - "color": "yellow", - "value": 0.33 - }, { "color": "green", - "value": 0.66 + "value": null } ] } @@ -172,7 +129,7 @@ "x": 4, "y": 0 }, - "id": 9, + "id": 7, "options": { "colorMode": "value", "graphMode": "area", @@ -195,29 +152,30 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, - "editorMode": "code", - "expr": "rate(block_counter_total[$__rate_interval])", + "editorMode": "builder", + "expr": "fetched_rpc_block_height{instance=\"$netowrk\"}", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, - "legendFormat": "__auto", + "interval": "", + "legendFormat": "verified block height", "range": true, "refId": "A", "useBackend": false } ], - "title": "Block Per Seconds", + "title": "Fetched Block Height", "type": "stat" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Current height of fetched block for verifying.", + "description": "Latest block height of current rpc node.", "fieldConfig": { "defaults": { "color": { @@ -242,7 +200,7 @@ "x": 8, "y": 0 }, - "id": 7, + "id": 10, "options": { "colorMode": "value", "graphMode": "area", @@ -265,11 +223,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "builder", - "expr": "fetched_rpc_block_height", + "expr": "latest_rpc_block_height{instance=\"$netowrk\"}", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, @@ -280,15 +238,15 @@ "useBackend": false } ], - "title": "Fetched Block Height", + "title": "Latest Block Height", "type": "stat" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Latest block height of current rpc node.", + "description": "The distance from fetched block to latest. If this increases, we have trouble following the network.", "fieldConfig": { "defaults": { "color": { @@ -313,7 +271,7 @@ "x": 12, "y": 0 }, - "id": 10, + "id": 11, "options": { "colorMode": "value", "graphMode": "area", @@ -336,11 +294,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "builder", - "expr": "latest_rpc_block_height", + "expr": "latest_rpc_block_height{instance=\"$netowrk\"} - fetched_rpc_block_height{instance=\"$netowrk\"}", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, @@ -351,15 +309,15 @@ "useBackend": false } ], - "title": "Latest Block Height", + "title": "Distance To Head", "type": "stat" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "The distance from fetched block to latest. If this increases, we have trouble following the network.", + "description": "", "fieldConfig": { "defaults": { "color": { @@ -374,7 +332,8 @@ "value": null } ] - } + }, + "unit": "s" }, "overrides": [] }, @@ -384,7 +343,7 @@ "x": 16, "y": 0 }, - "id": 11, + "id": 14, "options": { "colorMode": "value", "graphMode": "area", @@ -407,28 +366,28 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "latest_rpc_block_height - fetched_rpc_block_height", + "expr": "(latest_rpc_block_height{instance=\"$netowrk\"} - fetched_rpc_block_height{instance=\"$netowrk\"}) / rate(block_counter_total[$__rate_interval])", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, "interval": "", - "legendFormat": "verified block height", + "legendFormat": "__auto", "range": true, "refId": "A", "useBackend": false } ], - "title": "Distance To Head", + "title": "Estimate Time To Catch Up", "type": "stat" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "description": "Verification error occurs in the last minute.", "fieldConfig": { @@ -482,11 +441,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, - "editorMode": "code", - "expr": "increase(verification_error_total[1m])", + "editorMode": "builder", + "expr": "increase(verification_error_total{instance=\"$netowrk\"}[1m])", "fullMetaSearch": false, "includeNullMetadata": true, "instant": false, @@ -503,7 +462,7 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "description": "Total time consumed to verify a block.", "fieldConfig": { @@ -574,11 +533,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "sum by(le) (increase(total_block_verification_duration_bucket[$__interval]) / ignoring(le) group_left increase(total_block_verification_duration_count[$__interval]))", + "expr": "sum by(le) (increase(total_block_verification_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(total_block_verification_duration_count{instance=\"$netowrk\"}[$__interval]))", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -596,7 +555,7 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "description": "Distribution of the total time consumed to verify a block.", "fieldConfig": { @@ -669,12 +628,12 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", "exemplar": false, - "expr": "increase(total_block_verification_duration_bucket[1h]) / ignoring(le) group_left increase(total_block_verification_duration_count[1h])", + "expr": "increase(total_block_verification_duration_bucket{instance=\"$netowrk\"}[$__range]) / ignoring(le) group_left increase(total_block_verification_duration_count{instance=\"$netowrk\"}[$__range])", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -687,15 +646,15 @@ "useBackend": false } ], - "title": "Last 1h Total Block Verification Duration Distribution", + "title": "Total Block Verification Duration Distribution", "type": "bargauge" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Time consumed to insert records from trace to REVM database. ", + "description": "Time consumed to update zktrie state from one block trace.", "fieldConfig": { "defaults": { "custom": { @@ -717,7 +676,7 @@ "x": 0, "y": 16 }, - "id": 2, + "id": 3, "maxDataPoints": 15, "options": { "calculate": false, @@ -764,11 +723,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "sum by(le) (increase(update_db_duration_bucket[$__interval]) / ignoring(le) group_left increase(update_db_duration_count[$__interval]))", + "expr": "sum by(le) (increase(build_zktrie_db_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(build_zktrie_db_duration_count{instance=\"$netowrk\"}[$__interval]))", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -780,15 +739,15 @@ "useBackend": false } ], - "title": "Update DB Duration", + "title": "Build Zktrie DB Duration", "type": "heatmap" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Time consumed to update zktrie state from one block trace.", + "description": "Time cost in revm handle_block, most related with EVM execution.", "fieldConfig": { "defaults": { "custom": { @@ -810,7 +769,7 @@ "x": 6, "y": 16 }, - "id": 3, + "id": 4, "maxDataPoints": 15, "options": { "calculate": false, @@ -857,11 +816,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "sum by(le) (increase(build_zktrie_db_duration_bucket[$__interval]) / ignoring(le) group_left increase(build_zktrie_db_duration_count[$__interval]))", + "expr": "sum by(le) (increase(handle_block_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(handle_block_duration_count{instance=\"$netowrk\"}[$__interval]))", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -873,15 +832,15 @@ "useBackend": false } ], - "title": "Build Zktrie State Duration", + "title": "Handle Block Duration", "type": "heatmap" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, - "description": "Time cost in revm handle_block, most related with EVM execution.", + "description": "Time consumed to transact_commit one tx.", "fieldConfig": { "defaults": { "custom": { @@ -903,7 +862,7 @@ "x": 12, "y": 16 }, - "id": 4, + "id": 19, "maxDataPoints": 15, "options": { "calculate": false, @@ -950,11 +909,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "sum by(le) (increase(handle_block_duration_bucket[$__interval]) / ignoring(le) group_left increase(handle_block_duration_count[$__interval]))", + "expr": "sum by(le) (increase(transact_commit_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(transact_commit_duration_count{instance=\"$netowrk\"}[$__interval]))", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -966,13 +925,13 @@ "useBackend": false } ], - "title": "Handle Block Duration", + "title": "Transact Commit Duration", "type": "heatmap" }, { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "description": "Time consumed to populate database changes to zktrie once.", "fieldConfig": { @@ -1043,11 +1002,11 @@ { "datasource": { "type": "prometheus", - "uid": "${DS_PROMETHEUS}" + "uid": "ddvyy4pcgx3i8d" }, "disableTextWrap": false, "editorMode": "code", - "expr": "sum by(le) (increase(commit_changes_duration_bucket[$__interval]) / ignoring(le) group_left increase(commit_changes_duration_count[$__interval]))", + "expr": "sum by(le) (increase(commit_changes_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(commit_changes_duration_count{instance=\"$netowrk\"}[$__interval]))", "format": "heatmap", "fullMetaSearch": false, "hide": false, @@ -1061,22 +1020,422 @@ ], "title": "Commit Changes Duration", "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "description": "Time consumed to get a zktrie node.", + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 6, + "x": 0, + "y": 24 + }, + "id": 15, + "maxDataPoints": 15, + "options": { + "calculate": false, + "cellGap": 1, + "cellValues": { + "decimals": 1, + "unit": "percentunit" + }, + "color": { + "exponent": 0.5, + "fill": "red", + "min": 0, + "mode": "scheme", + "reverse": false, + "scale": "linear", + "scheme": "RdBu", + "steps": 20 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": true, + "yHistogram": true + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false, + "unit": "µs" + } + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "disableTextWrap": false, + "editorMode": "code", + "expr": "sum by(le) (increase(zktrie_get_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(zktrie_get_duration_count{instance=\"$netowrk\"}[$__interval]))", + "format": "heatmap", + "fullMetaSearch": false, + "hide": false, + "includeNullMetadata": true, + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "ZkTrie Get Duration", + "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "description": "Time consumed to update a zktrie node.", + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 6, + "x": 6, + "y": 24 + }, + "id": 16, + "maxDataPoints": 15, + "options": { + "calculate": false, + "cellGap": 1, + "cellValues": { + "decimals": 1, + "unit": "percentunit" + }, + "color": { + "exponent": 0.5, + "fill": "red", + "min": 0, + "mode": "scheme", + "reverse": false, + "scale": "linear", + "scheme": "RdBu", + "steps": 20 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": true, + "yHistogram": true + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false, + "unit": "µs" + } + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "disableTextWrap": false, + "editorMode": "code", + "expr": "sum by(le) (increase(zktrie_update_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(zktrie_update_duration_count{instance=\"$netowrk\"}[$__interval]))", + "format": "heatmap", + "fullMetaSearch": false, + "hide": false, + "includeNullMetadata": true, + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "ZkTrie Update Duration", + "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "description": "Time consumed to delete a zktrie node.", + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 6, + "x": 12, + "y": 24 + }, + "id": 17, + "maxDataPoints": 15, + "options": { + "calculate": false, + "cellGap": 1, + "cellValues": { + "decimals": 1, + "unit": "percentunit" + }, + "color": { + "exponent": 0.5, + "fill": "red", + "min": 0, + "mode": "scheme", + "reverse": false, + "scale": "linear", + "scheme": "RdBu", + "steps": 20 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": true, + "yHistogram": true + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false, + "unit": "µs" + } + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "disableTextWrap": false, + "editorMode": "code", + "expr": "sum by(le) (increase(zktrie_delete_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(zktrie_delete_duration_count{instance=\"$netowrk\"}[$__interval]))", + "format": "heatmap", + "fullMetaSearch": false, + "hide": false, + "includeNullMetadata": true, + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "ZkTrie Delete Duration", + "type": "heatmap" + }, + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "description": "Time consumed to commit zktrie changes.", + "fieldConfig": { + "defaults": { + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "scaleDistribution": { + "type": "linear" + } + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 6, + "x": 18, + "y": 24 + }, + "id": 18, + "maxDataPoints": 15, + "options": { + "calculate": false, + "cellGap": 1, + "cellValues": { + "decimals": 1, + "unit": "percentunit" + }, + "color": { + "exponent": 0.5, + "fill": "red", + "min": 0, + "mode": "scheme", + "reverse": false, + "scale": "linear", + "scheme": "RdBu", + "steps": 20 + }, + "exemplars": { + "color": "rgba(255,0,255,0.7)" + }, + "filterValues": { + "le": 1e-9 + }, + "legend": { + "show": true + }, + "rowsFrame": { + "layout": "auto" + }, + "tooltip": { + "mode": "single", + "showColorScale": true, + "yHistogram": true + }, + "yAxis": { + "axisPlacement": "left", + "reverse": false, + "unit": "µs" + } + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "ddvyy4pcgx3i8d" + }, + "disableTextWrap": false, + "editorMode": "code", + "expr": "sum by(le) (increase(zktrie_commit_duration_bucket{instance=\"$netowrk\"}[$__interval]) / ignoring(le) group_left increase(zktrie_commit_duration_count{instance=\"$netowrk\"}[$__interval]))", + "format": "heatmap", + "fullMetaSearch": false, + "hide": false, + "includeNullMetadata": true, + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "ZkTrie Commit Duration", + "type": "heatmap" } ], - "refresh": "auto", + "refresh": "5s", "schemaVersion": 39, "tags": [], "templating": { - "list": [] + "list": [ + { + "current": { + "selected": false, + "text": "host.docker.internal:9091", + "value": "host.docker.internal:9091" + }, + "hide": 0, + "includeAll": false, + "multi": false, + "name": "netowrk", + "options": [ + { + "selected": true, + "text": "host.docker.internal:9091", + "value": "host.docker.internal:9091" + }, + { + "selected": false, + "text": "host.docker.internal:9092", + "value": "host.docker.internal:9092" + } + ], + "query": "host.docker.internal:9091, host.docker.internal:9092", + "queryValue": "", + "skipUrlSync": false, + "type": "custom" + } + ] }, "time": { - "from": "now-3h", + "from": "now-5m", "to": "now" }, "timepicker": {}, "timezone": "browser", "title": "Stateless Block Verfier", "uid": "edvynqud8kttsd", - "version": 40, + "version": 5, "weekStart": "" } \ No newline at end of file From a9d0916a3d185d357fa31448762d863a206793c6 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Fri, 20 Sep 2024 12:27:39 +0800 Subject: [PATCH 02/14] soundness: recalculate zktrie node hash (#51) --- crates/primitives/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 044dbaa..4bf1ddb 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -90,8 +90,8 @@ pub trait Block: Debug { /// Update zktrie state from trace #[inline] fn build_zktrie_db(&self, zktrie_db: &mut ZkMemoryDb) { - for (k, bytes) in self.flatten_proofs() { - zktrie_db.add_node_bytes(bytes, Some(k.as_slice())).unwrap(); + for (_, bytes) in self.flatten_proofs() { + zktrie_db.add_node_bytes(bytes, None).unwrap(); } } From 8fe7b2ba1ab1aa42d9ecafabe121c7c1c5a10bed Mon Sep 17 00:00:00 2001 From: lightsing Date: Fri, 20 Sep 2024 12:29:39 +0800 Subject: [PATCH 03/14] fix: init_hash_scheme --- crates/bin/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/bin/src/main.rs b/crates/bin/src/main.rs index 22eb275..ab1b2fb 100644 --- a/crates/bin/src/main.rs +++ b/crates/bin/src/main.rs @@ -5,6 +5,7 @@ extern crate sbv; use clap::Parser; use sbv::core::HardforkConfig; +use sbv::primitives::init_hash_scheme; #[cfg(feature = "dev")] use tracing_subscriber::EnvFilter; @@ -42,6 +43,8 @@ async fn main() -> anyhow::Result<()> { ) .init(); + init_hash_scheme(); + let cmd = Cli::parse(); #[cfg(feature = "metrics")] From f8263f5347eff1cff57b1b526174b224cf1600c8 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Mon, 23 Sep 2024 14:40:06 +0800 Subject: [PATCH 04/14] feat: use zktrie-ng (#54) * use zktrie-ng * fix * clean legacy code * add more metrics * range adjustment * update grafana * fix * update * fix --- Cargo.lock | 340 +++++++++--------- Cargo.toml | 2 +- crates/bin/src/commands/run_file.rs | 13 +- crates/bin/src/main.rs | 3 - crates/bin/src/utils.rs | 14 +- crates/core/Cargo.toml | 2 +- crates/core/src/chunk.rs | 19 +- crates/core/src/database.rs | 215 +++++------ crates/core/src/error.rs | 43 ++- crates/core/src/executor/builder.rs | 78 ++-- crates/core/src/executor/hooks.rs | 32 +- crates/core/src/executor/mod.rs | 123 ++++--- crates/core/src/lib.rs | 2 +- crates/primitives/Cargo.toml | 9 +- .../src/imp/archived_block_trace_v2.rs | 215 ----------- crates/primitives/src/imp/blanket.rs | 187 ---------- crates/primitives/src/imp/block_trace.rs | 129 ------- crates/primitives/src/imp/block_trace_v2.rs | 130 ------- crates/primitives/src/imp/mod.rs | 92 ----- crates/primitives/src/lib.rs | 39 +- crates/sbv/Cargo.toml | 2 +- crates/utils/src/utils/debug.rs | 23 +- 22 files changed, 494 insertions(+), 1218 deletions(-) delete mode 100644 crates/primitives/src/imp/archived_block_trace_v2.rs delete mode 100644 crates/primitives/src/imp/blanket.rs delete mode 100644 crates/primitives/src/imp/block_trace.rs delete mode 100644 crates/primitives/src/imp/block_trace_v2.rs delete mode 100644 crates/primitives/src/imp/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 0038369..b92f737 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,18 +4,18 @@ version = 3 [[package]] name = "addr2line" -version = "0.22.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" dependencies = [ "gimli", ] [[package]] -name = "adler" -version = "1.0.2" +name = "adler2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "ahash" @@ -76,12 +76,12 @@ dependencies = [ [[package]] name = "alloy-chains" -version = "0.1.29" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb07629a5d0645d29f68d2fb6f4d0cf15c89ec0965be915f303967180929743f" +checksum = "abf770dad29577cd3580f3dd09005799224a912b8cdfdd6dc04d030d42b3df4e" dependencies = [ "num_enum", - "strum 0.26.3", + "strum", ] [[package]] @@ -378,11 +378,10 @@ dependencies = [ [[package]] name = "alloy-sol-macro" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "183bcfc0f3291d9c41a3774172ee582fb2ce6eb6569085471d8f225de7bb86fc" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" dependencies = [ - "alloy-sol-macro-expander 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "alloy-sol-macro-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-macro-expander 0.8.0", + "alloy-sol-macro-input 0.8.0", "proc-macro-error", "proc-macro2", "quote", @@ -391,12 +390,13 @@ dependencies = [ [[package]] name = "alloy-sol-macro" -version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0458ccb02a564228fcd76efb8eb5a520521a8347becde37b402afec9a1b83859" dependencies = [ - "alloy-sol-macro-expander 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", - "alloy-sol-macro-input 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", - "proc-macro-error", + "alloy-sol-macro-expander 0.8.3", + "alloy-sol-macro-input 0.8.3", + "proc-macro-error2", "proc-macro2", "quote", "syn 2.0.77", @@ -405,65 +405,65 @@ dependencies = [ [[package]] name = "alloy-sol-macro-expander" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71c4d842beb7a6686d04125603bc57614d5ed78bf95e4753274db3db4ba95214" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" dependencies = [ - "alloy-sol-macro-input 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-macro-input 0.8.0", "const-hex", - "heck 0.5.0", + "heck", "indexmap 2.5.0", "proc-macro-error", "proc-macro2", "quote", "syn 2.0.77", - "syn-solidity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn-solidity 0.8.0", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-expander" -version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bc65475025fc1e84bf86fc840f04f63fcccdcf3cf12053c99918e4054dfbc69" dependencies = [ - "alloy-sol-macro-input 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "alloy-sol-macro-input 0.8.3", "const-hex", - "heck 0.5.0", + "heck", "indexmap 2.5.0", - "proc-macro-error", + "proc-macro-error2", "proc-macro2", "quote", "syn 2.0.77", - "syn-solidity 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "syn-solidity 0.8.3", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-input" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1306e8d3c9e6e6ecf7a39ffaf7291e73a5f655a2defd366ee92c2efebcdf7fee" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" dependencies = [ "const-hex", "dunce", - "heck 0.5.0", + "heck", "proc-macro2", "quote", "syn 2.0.77", - "syn-solidity 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn-solidity 0.8.0", ] [[package]] name = "alloy-sol-macro-input" -version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed10f0715a0b69fde3236ff3b9ae5f6f7c97db5a387747100070d3016b9266b" dependencies = [ "const-hex", "dunce", - "heck 0.5.0", + "heck", "proc-macro2", "quote", "syn 2.0.77", - "syn-solidity 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "syn-solidity 0.8.3", ] [[package]] @@ -482,7 +482,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "577e262966e92112edbd15b1b2c0947cc434d6e8311df96d3329793fe8047da9" dependencies = [ "alloy-primitives", - "alloy-sol-macro 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-macro 0.8.3", "const-hex", ] @@ -493,7 +493,7 @@ source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f dependencies = [ "alloy-json-abi", "alloy-primitives", - "alloy-sol-macro 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "alloy-sol-macro 0.8.0", "const-hex", "serde", ] @@ -596,9 +596,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.86" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" [[package]] name = "ark-ff" @@ -810,17 +810,17 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.73" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" dependencies = [ "addr2line", - "cc", "cfg-if", "libc", "miniz_oxide", "object", "rustc-demangle", + "windows-targets", ] [[package]] @@ -908,7 +908,7 @@ dependencies = [ [[package]] name = "bn254" version = "0.1.0" -source = "git+https://github.com/scroll-tech/bn254#1773ed6c51f14652272056370cf135ff5d7a01c5" +source = "git+https://github.com/scroll-tech/bn254#c9f170c6e39cfbc1ff14b9648f125823978041b3" dependencies = [ "ff", "getrandom", @@ -953,9 +953,9 @@ dependencies = [ [[package]] name = "bytemuck" -version = "1.17.1" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773d90827bc3feecfb67fab12e24de0749aad83c74b9504ecde46237b5cd24e2" +checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" [[package]] name = "byteorder" @@ -965,9 +965,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" dependencies = [ "serde", ] @@ -989,9 +989,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.15" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "shlex", ] @@ -1017,9 +1017,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.16" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6719fffa43d0d87e5fd8caeab59be1554fb028cd30edc88fc4369b17971019" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" dependencies = [ "clap_builder", "clap_derive", @@ -1027,9 +1027,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.15" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" dependencies = [ "anstream", "anstyle", @@ -1043,7 +1043,7 @@ version = "4.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" dependencies = [ - "heck 0.5.0", + "heck", "proc-macro2", "quote", "syn 2.0.77", @@ -1122,9 +1122,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad" +checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" dependencies = [ "libc", ] @@ -1231,9 +1231,9 @@ dependencies = [ [[package]] name = "dashmap" -version = "6.0.1" +version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "804c8821570c3f8b70230c2ba75ffa5c0f9a4189b9a432b6656c536712acae28" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" dependencies = [ "cfg-if", "crossbeam-utils", @@ -1637,9 +1637,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.29.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" [[package]] name = "glob" @@ -1647,14 +1647,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" -[[package]] -name = "gobuild" -version = "0.1.0-alpha.2" -source = "git+https://github.com/scroll-tech/gobuild.git#24935c2b8f677841f22acd6710957621bb294e0e" -dependencies = [ - "cc", -] - [[package]] name = "group" version = "0.13.0" @@ -1705,12 +1697,6 @@ dependencies = [ "serde", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "heck" version = "0.5.0" @@ -1822,9 +1808,9 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.27.2" +version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http", @@ -1855,9 +1841,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9" +checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" dependencies = [ "bytes", "futures-channel", @@ -1875,9 +1861,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.60" +version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -1974,9 +1960,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" [[package]] name = "is-terminal" @@ -2043,9 +2029,9 @@ dependencies = [ [[package]] name = "keccak-asm" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "422fbc7ff2f2f5bdffeb07718e5a5324dca72b0c9293d50df4026652385e3314" +checksum = "505d1856a39b200489082f90d897c3f07c455563880bc5952e38eabf731c83b6" dependencies = [ "digest 0.10.7", "sha3-asm", @@ -2120,9 +2106,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "memmap2" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322" +checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" dependencies = [ "libc", ] @@ -2135,11 +2121,11 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "miniz_oxide" -version = "0.7.4" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" dependencies = [ - "adler", + "adler2", ] [[package]] @@ -2233,13 +2219,13 @@ checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" [[package]] name = "num-derive" -version = "0.3.3" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.77", ] [[package]] @@ -2416,9 +2402,9 @@ dependencies = [ [[package]] name = "parking" -version = "2.2.0" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" [[package]] name = "parking_lot" @@ -2457,9 +2443,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.11" +version = "2.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd53dff83f26735fdc1ca837098ccf133605d794cdae66acfc2bfac3ec809d95" +checksum = "fdbef9d1d47087a895abd220ed25eb4ad973a5e26f6a4367b038c25e28dfc2d9" dependencies = [ "memchr", "thiserror", @@ -2517,7 +2503,7 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "poseidon-bn254" version = "0.1.0" -source = "git+https://github.com/scroll-tech/poseidon-bn254?branch=master#a1174dac3d854bd91556b54274551326a94b11e6" +source = "git+https://github.com/scroll-tech/poseidon-bn254?branch=master#526a64a81419bcab6bd8280a8a3f9808189e0373" dependencies = [ "bn254", "itertools 0.13.0", @@ -2603,6 +2589,28 @@ dependencies = [ "version_check", ] +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -2746,9 +2754,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ "bitflags 2.6.0", ] @@ -3053,9 +3061,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.35" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", @@ -3066,9 +3074,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.12" +version = "0.23.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" +checksum = "f2dabaac7466917e566adb06783a81ca48944c6898a1b08b9374106dd671f4c8" dependencies = [ "once_cell", "rustls-pki-types", @@ -3095,9 +3103,9 @@ checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" [[package]] name = "rustls-webpki" -version = "0.102.7" +version = "0.102.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84678086bd54edf2b415183ed7a94d0efb049f1b646a33e22a36f3794be6ae56" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" dependencies = [ "ring", "rustls-pki-types", @@ -3163,11 +3171,12 @@ dependencies = [ "alloy", "poseidon-bn254", "rkyv", + "sbv-utils", "serde", "serde_json", "serde_with", "tiny-keccak", - "zktrie", + "zktrie-ng", ] [[package]] @@ -3190,11 +3199,11 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +checksum = "e9aaafd5a2b6e3d657ff009d82fbd630b6bd54dd4eb06f21693925cdf80f9b8b" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3225,9 +3234,9 @@ dependencies = [ [[package]] name = "secp256k1" -version = "0.29.0" +version = "0.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e0cc0f1cf93f4969faf3ea1c7d8a9faed25918d96affa959720823dfe86d4f3" +checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" dependencies = [ "rand", "secp256k1-sys", @@ -3235,9 +3244,9 @@ dependencies = [ [[package]] name = "secp256k1-sys" -version = "0.10.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1433bd67156263443f14d603720b082dd3121779323fce20cba2aa07b874bc1b" +checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" dependencies = [ "cc", ] @@ -3291,18 +3300,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -3311,9 +3320,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.127" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "indexmap 2.5.0", "itoa", @@ -3377,9 +3386,9 @@ dependencies = [ [[package]] name = "sha3-asm" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57d79b758b7cb2085612b11a235055e485605a5103faccdd633f35bd7aee69dd" +checksum = "c28efc5e327c837aa837c59eae585fc250715ef939ac32881bcc11677cd02d46" dependencies = [ "cc", "cfg-if", @@ -3510,32 +3519,13 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "strum" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" - [[package]] name = "strum" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" dependencies = [ - "strum_macros 0.26.4", -] - -[[package]] -name = "strum_macros" -version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" -dependencies = [ - "heck 0.4.1", - "proc-macro2", - "quote", - "rustversion", - "syn 1.0.109", + "strum_macros", ] [[package]] @@ -3544,7 +3534,7 @@ version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ - "heck 0.5.0", + "heck", "proc-macro2", "quote", "rustversion", @@ -3572,9 +3562,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "symbolic-common" -version = "12.10.1" +version = "12.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1944ea8afd197111bca0c0edea1e1f56abb3edd030e240c1035cc0e3ff51fec" +checksum = "9fdf97c441f18a4f92425b896a4ec7a27e03631a0b1047ec4e34e9916a9a167e" dependencies = [ "debugid", "memmap2", @@ -3584,9 +3574,9 @@ dependencies = [ [[package]] name = "symbolic-demangle" -version = "12.10.1" +version = "12.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddaccaf1bf8e73c4f64f78dbb30aadd6965c71faa4ff3fba33f8d7296cf94a87" +checksum = "bc8ece6b129e97e53d1fbb3f61d33a6a9e5369b11d01228c068094d6d134eaea" dependencies = [ "cpp_demangle", "rustc-demangle", @@ -3618,8 +3608,7 @@ dependencies = [ [[package]] name = "syn-solidity" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "284c41c2919303438fcf8dede4036fd1e82d4fc0fbb2b279bd2a1442c909ca92" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" dependencies = [ "paste", "proc-macro2", @@ -3629,8 +3618,9 @@ dependencies = [ [[package]] name = "syn-solidity" -version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b95156f8b577cb59dc0b1df15c6f29a10afc5f8a7ac9786b0b5c68c19149278" dependencies = [ "paste", "proc-macro2", @@ -3832,9 +3822,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" dependencies = [ "futures-core", "pin-project-lite", @@ -3844,9 +3834,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" dependencies = [ "bytes", "futures-core", @@ -3863,9 +3853,9 @@ checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap 2.5.0", "toml_datetime", @@ -4006,15 +3996,15 @@ checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] @@ -4370,25 +4360,19 @@ dependencies = [ ] [[package]] -name = "zktrie" -version = "0.3.0" -source = "git+https://github.com/scroll-tech/zktrie.git?branch=main#1a1da01f0e3a4c3f3361b25f829a38173538953a" -dependencies = [ - "gobuild", - "zktrie_rust", -] - -[[package]] -name = "zktrie_rust" -version = "0.3.0" -source = "git+https://github.com/scroll-tech/zktrie.git?branch=main#1a1da01f0e3a4c3f3361b25f829a38173538953a" +name = "zktrie-ng" +version = "0.1.0" +source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#d749dca019145bc7898564647945cd0898a842d1" dependencies = [ + "alloy-primitives", + "hashbrown 0.14.5", "hex", - "lazy_static", - "log", - "num", "num-derive", "num-traits", - "strum 0.24.1", - "strum_macros 0.24.3", + "once_cell", + "poseidon-bn254", + "revm-primitives", + "strum", + "thiserror", + "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 886d85a..f91e7d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ tiny-keccak = "2.0" # dependencies from scroll-tech poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master", features = ["bn254"] } -zktrie = { git = "https://github.com/scroll-tech/zktrie.git", branch = "main", features= ["rs_zktrie"] } +zktrie-ng = { git = "https://github.com/scroll-tech/zktrie-ng", branch = "master", features = ["scroll"] } # binary dependencies anyhow = "1.0" diff --git a/crates/bin/src/commands/run_file.rs b/crates/bin/src/commands/run_file.rs index 684dd58..c8badcf 100644 --- a/crates/bin/src/commands/run_file.rs +++ b/crates/bin/src/commands/run_file.rs @@ -3,8 +3,9 @@ use anyhow::bail; use clap::Args; use sbv::{ core::{ChunkInfo, EvmExecutorBuilder, HardforkConfig}, - primitives::{types::BlockTrace, Block, B256}, + primitives::{types::BlockTrace, zk_trie::db::HashMapDb, Block, B256}, }; +use std::rc::Rc; use std::{cell::RefCell, path::PathBuf}; use tiny_keccak::{Hasher, Keccak}; use tokio::task::JoinSet; @@ -75,17 +76,17 @@ impl RunFileCommand { let fork_config = fork_config(traces[0].chain_id()); let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces); + let zktrie_db = Rc::new(RefCell::new(zktrie_db)); let tx_bytes_hasher = RefCell::new(Keccak::v256()); - let mut executor = EvmExecutorBuilder::new(zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) .hardfork_config(fork_config) - .with_execute_hooks(|hooks| { + .with_hooks(&traces[0], |hooks| { hooks.add_tx_rlp_handler(|_, rlp| { tx_bytes_hasher.borrow_mut().update(rlp); }); - }) - .build(&traces[0])?; + })?; executor.handle_block(&traces[0])?; for trace in traces[1..].iter() { @@ -93,7 +94,7 @@ impl RunFileCommand { executor.handle_block(trace)?; } - let post_state_root = executor.commit_changes(&zktrie_db); + let post_state_root = executor.commit_changes(zktrie_db.clone())?; if post_state_root != chunk_info.post_state_root() { bail!("post state root mismatch"); } diff --git a/crates/bin/src/main.rs b/crates/bin/src/main.rs index ab1b2fb..22eb275 100644 --- a/crates/bin/src/main.rs +++ b/crates/bin/src/main.rs @@ -5,7 +5,6 @@ extern crate sbv; use clap::Parser; use sbv::core::HardforkConfig; -use sbv::primitives::init_hash_scheme; #[cfg(feature = "dev")] use tracing_subscriber::EnvFilter; @@ -43,8 +42,6 @@ async fn main() -> anyhow::Result<()> { ) .init(); - init_hash_scheme(); - let cmd = Cli::parse(); #[cfg(feature = "metrics")] diff --git a/crates/bin/src/utils.rs b/crates/bin/src/utils.rs index 1786a62..36ab091 100644 --- a/crates/bin/src/utils.rs +++ b/crates/bin/src/utils.rs @@ -1,8 +1,8 @@ -use sbv::primitives::zk_trie::ZkMemoryDb; use sbv::{ core::{EvmExecutorBuilder, HardforkConfig, VerificationError}, - primitives::Block, + primitives::{zk_trie::db::HashMapDb, Block}, }; +use std::cell::RefCell; use std::rc::Rc; pub fn verify( @@ -39,17 +39,17 @@ fn verify_inner( let zktrie_db = cycle_track!( { - let mut zktrie_db = ZkMemoryDb::new(); + let mut zktrie_db = HashMapDb::default(); measure_duration_millis!( build_zktrie_db_duration_milliseconds, - l2_trace.build_zktrie_db(&mut zktrie_db) + l2_trace.build_zktrie_db(&mut zktrie_db).unwrap() ); - Rc::new(zktrie_db) + Rc::new(RefCell::new(zktrie_db)) }, "build ZktrieState" ); - let mut executor = EvmExecutorBuilder::new(zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) .hardfork_config(*fork_config) .build(&l2_trace)?; @@ -66,7 +66,7 @@ fn verify_inner( update_metrics_counter!(verification_error); e })?; - let revm_root_after = executor.commit_changes(&zktrie_db); + let revm_root_after = executor.commit_changes(zktrie_db.clone())?; #[cfg(feature = "profiling")] if let Ok(report) = guard.report().build() { diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 0401b9d..d0554f0 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -33,7 +33,7 @@ tracing-subscriber.workspace = true [features] debug-account = ["sbv-utils/debug-account"] debug-storage = ["sbv-utils/debug-storage"] -dev = ["sbv-utils/dev"] +dev = ["sbv-primitives/dev", "sbv-utils/dev"] metrics = ["sbv-utils/metrics"] # sp1 related diff --git a/crates/core/src/chunk.rs b/crates/core/src/chunk.rs index 7438032..e04aeea 100644 --- a/crates/core/src/chunk.rs +++ b/crates/core/src/chunk.rs @@ -1,6 +1,5 @@ use revm::primitives::B256; -use sbv_primitives::{zk_trie::ZkMemoryDb, Block}; -use std::rc::Rc; +use sbv_primitives::{zk_trie::db::HashMapDb, Block}; use tiny_keccak::{Hasher, Keccak}; /// A chunk is a set of continuous blocks. @@ -22,7 +21,7 @@ pub struct ChunkInfo { impl ChunkInfo { /// Construct by block traces - pub fn from_block_traces(traces: &[T]) -> (Self, Rc) { + pub fn from_block_traces(traces: &[T]) -> (Self, HashMapDb) { let chain_id = traces.first().unwrap().chain_id(); let prev_state_root = traces .first() @@ -41,14 +40,13 @@ impl ChunkInfo { let mut data_hash = B256::ZERO; data_hasher.finalize(&mut data_hash.0); - let mut zktrie_db = ZkMemoryDb::new(); + let mut zktrie_db = HashMapDb::default(); for trace in traces.iter() { measure_duration_millis!( build_zktrie_db_duration_milliseconds, - trace.build_zktrie_db(&mut zktrie_db) + trace.build_zktrie_db(&mut zktrie_db).unwrap() ); } - let zktrie_db = Rc::new(zktrie_db); let info = ChunkInfo { chain_id, @@ -118,6 +116,7 @@ mod tests { use revm::primitives::b256; use sbv_primitives::types::BlockTrace; use std::cell::RefCell; + use std::rc::Rc; const TRACES_STR: [&str; 4] = [ include_str!("../../../testdata/mainnet_blocks/8370400.json"), @@ -140,17 +139,17 @@ mod tests { let fork_config = HardforkConfig::default_from_chain_id(traces[0].chain_id()); let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces); + let zktrie_db = Rc::new(RefCell::new(zktrie_db)); let tx_bytes_hasher = RefCell::new(Keccak::v256()); - let mut executor = EvmExecutorBuilder::new(zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) .hardfork_config(fork_config) - .with_execute_hooks(|hooks| { + .with_hooks(&traces[0], |hooks| { hooks.add_tx_rlp_handler(|_, rlp| { tx_bytes_hasher.borrow_mut().update(rlp); }); }) - .build(&traces[0]) .unwrap(); executor.handle_block(&traces[0]).unwrap(); @@ -159,7 +158,7 @@ mod tests { executor.handle_block(trace).unwrap(); } - let post_state_root = executor.commit_changes(&zktrie_db); + let post_state_root = executor.commit_changes(zktrie_db.clone()).unwrap(); assert_eq!(post_state_root, chunk_info.post_state_root); drop(executor); // drop executor to release Rc diff --git a/crates/core/src/database.rs b/crates/core/src/database.rs index f535f60..96ac65b 100644 --- a/crates/core/src/database.rs +++ b/crates/core/src/database.rs @@ -1,85 +1,75 @@ -use crate::error::ZkTrieError; +use crate::error::DatabaseError; use once_cell::sync::Lazy; use revm::{ db::{AccountState, DatabaseRef}, primitives::{AccountInfo, Address, Bytecode, B256, U256}, }; use sbv_primitives::{ - init_hash_scheme, - zk_trie::{SharedMemoryDb, ZkMemoryDb, ZkTrie}, + zk_trie::{ + db::{KVDatabase, KVDatabaseItem}, + hash::{key_hasher::NoCacheHasher, poseidon::Poseidon}, + scroll_types::Account, + trie::ZkTrie, + }, Block, }; -use std::rc::Rc; -use std::{cell::RefCell, collections::HashMap, convert::Infallible, fmt}; +use std::{cell::RefCell, collections::HashMap, fmt}; -type Result = std::result::Result; +type Result = std::result::Result; -type StorageTrieLazyFn = Box ZkTrie>; +type StorageTrieLazyFn = Box ZkTrie>; +type LazyStorageTrie = Lazy, StorageTrieLazyFn>; -/// A read-only in-memory database that consists of account and storage information. -pub struct ReadOnlyDB { - /// In-memory map of code hash to bytecode. - code_db: HashMap, +/// A database that consists of account and storage information. +pub struct EvmDatabase { + /// Map of code hash to bytecode. + code_db: CodeDb, /// The initial storage roots of accounts, used for after commit. /// Need to be updated after zkTrie commit. prev_storage_roots: RefCell>, /// 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_refs: RefCell, StorageTrieLazyFn>>>, + storage_trie_refs: RefCell>>, /// Current uncommitted zkTrie root based on the block trace. committed_zktrie_root: B256, /// The underlying zkTrie database. - zktrie_db: Rc, + zktrie_db: ZkDb, /// Current view of zkTrie database. - zktrie_db_ref: ZkTrie, + zktrie: ZkTrie, } -impl fmt::Debug for ReadOnlyDB { +impl fmt::Debug for EvmDatabase { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("ReadOnlyDB") - .field("code_db", &self.code_db.len()) + f.debug_struct("EvmDatabase") .field("committed_zktrie_root", &self.committed_zktrie_root) .finish() } } -impl ReadOnlyDB { +impl EvmDatabase { /// Initialize an EVM database from a block trace. - pub fn new(l2_trace: T, zktrie_db: &Rc) -> Result { - let size_hint = l2_trace.codes().len(); - Self::new_with_size_hint(l2_trace, zktrie_db, size_hint) - } - - /// Initialize an EVM database from a block trace with size hint of code database. - pub fn new_with_size_hint( - l2_trace: T, - zktrie_db: &Rc, - size_hint: usize, - ) -> Result { - init_hash_scheme(); - + pub fn new(l2_trace: T, mut code_db: CodeDb, zktrie_db: ZkDb) -> Result { cycle_tracker_start!("insert CodeDB"); - let mut code_db = HashMap::with_capacity(size_hint); for code in l2_trace.codes() { let hash = revm::primitives::keccak256(code); - code_db.entry(hash).or_insert_with(|| { - dev_trace!("insert code {:?}", hash); - Bytecode::new_raw(revm::primitives::Bytes::from(code.to_vec())) - }); + code_db + .or_put(hash.as_slice(), code) + .map_err(DatabaseError::code_db)?; } cycle_tracker_end!("insert CodeDB"); - let uncommitted_zktrie_root = l2_trace.root_before(); + let committed_zktrie_root = l2_trace.root_before(); + + let zktrie = ZkTrie::new_with_root(zktrie_db.clone(), NoCacheHasher, committed_zktrie_root) + .map_err(DatabaseError::zk_trie)?; - Ok(ReadOnlyDB { + Ok(EvmDatabase { code_db, prev_storage_roots: Default::default(), storage_trie_refs: Default::default(), - committed_zktrie_root: uncommitted_zktrie_root, - zktrie_db: zktrie_db.clone(), - zktrie_db_ref: zktrie_db - .new_ref_trie(&uncommitted_zktrie_root.0) - .ok_or(ZkTrieError::ZkTrieRootNotFound)?, + committed_zktrie_root, + zktrie_db, + zktrie, }) } @@ -107,13 +97,13 @@ impl ReadOnlyDB { .unwrap_or_default() } - /// Get the zkTrie root. + /// Get the committed zkTrie root. #[inline] pub(crate) fn committed_zktrie_root(&self) -> B256 { self.committed_zktrie_root } - /// Get the zkTrie root. + /// Set the committed zkTrie root. #[inline] pub(crate) fn updated_committed_zktrie_root(&mut self, new_root: B256) { self.committed_zktrie_root = new_root; @@ -128,17 +118,18 @@ impl ReadOnlyDB { cycle_tracker_start!("insert CodeDB"); for code in l2_trace.codes() { let hash = revm::primitives::keccak256(code); - self.code_db.entry(hash).or_insert_with(|| { - dev_trace!("insert code {:?}", hash); - Bytecode::new_raw(revm::primitives::Bytes::from(code.to_vec())) - }); + self.code_db + .or_put(hash.as_slice(), code) + .map_err(DatabaseError::code_db)?; } cycle_tracker_end!("insert CodeDB"); - self.zktrie_db_ref = self - .zktrie_db - .new_ref_trie(&l2_trace.root_before().0) - .ok_or(ZkTrieError::ZkTrieRootNotFound)?; + self.zktrie = ZkTrie::new_with_root( + self.zktrie_db.clone(), + NoCacheHasher, + l2_trace.root_before(), + ) + .map_err(DatabaseError::zk_trie)?; Ok(()) } @@ -157,95 +148,111 @@ impl ReadOnlyDB { } } -impl DatabaseRef for ReadOnlyDB { - type Error = Infallible; +impl DatabaseRef + for EvmDatabase +{ + type Error = DatabaseError; /// Get basic account information. - fn basic_ref(&self, address: Address) -> Result, Self::Error> { - Ok(measure_duration_micros!( + fn basic_ref(&self, address: Address) -> Result> { + let Some(account) = measure_duration_micros!( zktrie_get_duration_microseconds, - self.zktrie_db_ref.get_account(address.as_slice()) + self.zktrie.get::(address) ) - .map(|account_data| { - let code_size = - u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize; - let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap()); - let balance = U256::from_be_bytes(account_data[1]); - let code_hash = B256::from(account_data[3]); - let poseidon_code_hash = B256::from(account_data[4]); - - let storage_root = B256::from(account_data[2]); - self.prev_storage_roots - .borrow_mut() - .entry(address) - .or_insert(storage_root.0.into()); - - let zktrie_db = self.zktrie_db.clone(); - self.storage_trie_refs.borrow_mut().insert( - address, + .map_err(DatabaseError::zk_trie)? + else { + return Ok(None); + }; + + self.prev_storage_roots + .borrow_mut() + .entry(address) + .or_insert(account.storage_root); + let zktrie_db = self.zktrie_db.clone(); + self.storage_trie_refs + .borrow_mut() + .entry(address) + .or_insert_with(|| { Lazy::new(Box::new(move || { - zktrie_db - .new_ref_trie(&storage_root.0) + ZkTrie::new_with_root(zktrie_db.clone(), NoCacheHasher, account.storage_root) .expect("storage trie associated with account not found") - })), - ); - AccountInfo { - balance, - nonce, - code_size, - code_hash, - poseidon_code_hash, - code: self.code_db.get(&code_hash).cloned(), - } - })) + })) + }); + + let mut info = AccountInfo::from(account); + info.code = self + .code_db + .get(&account.code_hash) + .map_err(DatabaseError::code_db)? + .map(|v| Bytecode::new_legacy(v.into_bytes().into())); + + Ok(Some(info)) } /// Get account code by its code hash. - fn code_by_hash_ref(&self, hash: B256) -> Result { + fn code_by_hash_ref(&self, hash: B256) -> Result { // Sometimes the code in previous account info is not contained, // and the CacheDB has already loaded the previous account info, // then the upcoming trace contains code (meaning the code is used in this new block), // we can't directly update the CacheDB, so we offer the code by hash here. // However, if the code still cannot be found, this is an error. - self.code_db.get(&hash).cloned().ok_or_else(|| { - unreachable!( - "Code is either loaded or not needed (like EXTCODESIZE), code hash: {:?}", - hash - ); - }) + self.code_db + .get(&hash) + .map_err(DatabaseError::code_db)? + .map(|v| Bytecode::new_legacy(v.into_bytes().into())) + .ok_or_else(|| { + unreachable!( + "Code is either loaded or not needed (like EXTCODESIZE), code hash: {:?}", + hash + ); + }) } /// Get storage value of address at index. - fn storage_ref(&self, address: Address, index: U256) -> Result { + fn storage_ref(&self, address: Address, index: U256) -> Result { + dev_trace!("get storage of {:?} at index {:?}", address, index); let mut storage_trie_refs = self.storage_trie_refs.borrow_mut(); let trie = storage_trie_refs .entry(address) .or_insert_with_key(|address| { let storage_root = measure_duration_micros!( zktrie_get_duration_microseconds, - self.zktrie_db_ref.get_account(address.as_slice()) + self.zktrie.get::(address) ) - .map(|account_data| B256::from(account_data[2])) + .expect("unexpected zktrie error") + .map(|acc| acc.storage_root) .unwrap_or_default(); + dev_debug!("storage root of {:?} is {:?}", address, storage_root); + let zktrie_db = self.zktrie_db.clone(); Lazy::new(Box::new(move || { - zktrie_db - .clone() - .new_ref_trie(&storage_root.0) + ZkTrie::new_with_root(zktrie_db.clone(), NoCacheHasher, storage_root) .expect("storage trie associated with account not found") })) }); + #[cfg(debug_assertions)] + { + let current_root = trie.root().unwrap_ref(); + let expected_root = self + .zktrie + .get::(address) + .expect("unexpected zktrie error") + .map(|acc| acc.storage_root) + .unwrap_or_default(); + assert_eq!(*current_root, expected_root); + } + Ok(measure_duration_micros!( zktrie_get_duration_microseconds, - trie.get_store(&index.to_be_bytes::<32>()) + trie.get::(index.to_be_bytes::<32>()) ) - .map(|store_data| U256::from_be_bytes(store_data)) + .map_err(DatabaseError::zk_trie)? .unwrap_or_default()) } /// Get block hash by block number. - fn block_hash_ref(&self, _: u64) -> Result { + fn block_hash_ref(&self, _: u64) -> Result { unreachable!("BLOCKHASH is disabled") } } diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index d3a6080..322f6c5 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -1,24 +1,21 @@ -use revm::primitives::alloy_primitives::SignatureError; -use revm::primitives::{EVMError, B256}; -use std::convert::Infallible; +use revm::primitives::{alloy_primitives::SignatureError, EVMError, B256}; +use std::error::Error; /// Error variants encountered during manipulation of a zkTrie. #[derive(Debug, thiserror::Error)] -pub enum ZkTrieError { - #[error("zktrie root not found")] - ZkTrieRootNotFound, +pub enum DatabaseError { + #[error("error encountered from code db: {0}")] + CodeDb(Box), + #[error("error encountered from zkTrie: {0}")] + ZkTrie(Box), } /// Error variants encountered during verification of transactions in a L2 block. #[derive(Debug, thiserror::Error)] pub enum VerificationError { - /// Malformed trace. - #[error("malformed trace, unexpected zktrie error: {source}")] - MalformedTrace { - /// The source error that occurred while parsing the trace. - #[from] - source: ZkTrieError, - }, + /// Error while operating on the database. + #[error("database error: {0}")] + Database(#[from] DatabaseError), /// Error while recovering signer from an ECDSA signature. #[error("invalid signature for tx_hash={tx_hash}: {source}")] InvalidSignature { @@ -39,7 +36,7 @@ pub enum VerificationError { /// The tx hash. tx_hash: B256, /// The source error originating in [`revm`]. - source: EVMError, + source: EVMError, }, /// Root mismatch error #[error("root_after in trace doesn't match with root_after in revm: root_trace={root_trace}, root_revm={root_revm}")] @@ -50,3 +47,21 @@ pub enum VerificationError { root_revm: B256, }, } + +impl DatabaseError { + pub(crate) fn code_db(err: E) -> Self { + dev_error!( + "code_db error {err} occurred in:\n{}", + std::backtrace::Backtrace::force_capture() + ); + DatabaseError::CodeDb(Box::new(err)) + } + + pub(crate) fn zk_trie(err: E) -> Self { + dev_error!( + "zk_trie error {err} occurred in:\n{}", + std::backtrace::Backtrace::force_capture() + ); + DatabaseError::ZkTrie(Box::new(err)) + } +} diff --git a/crates/core/src/executor/builder.rs b/crates/core/src/executor/builder.rs index 57e5512..b17fdd0 100644 --- a/crates/core/src/executor/builder.rs +++ b/crates/core/src/executor/builder.rs @@ -1,70 +1,86 @@ -use crate::error::ZkTrieError; -use crate::{executor::hooks::ExecuteHooks, EvmExecutor, HardforkConfig, ReadOnlyDB}; -use core::fmt; +use crate::error::DatabaseError; +use crate::{executor::hooks::ExecuteHooks, EvmDatabase, EvmExecutor, HardforkConfig}; use revm::db::CacheDB; -use sbv_primitives::{zk_trie::ZkMemoryDb, Block}; -use std::rc::Rc; +use sbv_primitives::zk_trie::db::KVDatabase; +use sbv_primitives::Block; +use std::fmt::{self, Debug}; /// Builder for EVM executor. -pub struct EvmExecutorBuilder<'e, H> { +pub struct EvmExecutorBuilder { hardfork_config: H, - execute_hooks: ExecuteHooks<'e>, - zktrie_db: Rc, + code_db: CodeDb, + zktrie_db: ZkDb, } -impl fmt::Debug for EvmExecutorBuilder<'_, ()> { +impl Debug for EvmExecutorBuilder { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("EvmExecutorBuilder") .field("hardfork_config", &self.hardfork_config) - .field("execute_hooks", &self.execute_hooks) + .field("code_db", &"...") .field("zktrie_db", &"...") .finish() } } -impl<'e> EvmExecutorBuilder<'e, ()> { +impl EvmExecutorBuilder<(), CodeDb, ZkDb> { /// Create a new builder. - pub fn new(zktrie_db: Rc) -> Self { + pub fn new(code_db: CodeDb, zktrie_db: ZkDb) -> Self { Self { hardfork_config: (), - execute_hooks: ExecuteHooks::default(), + code_db, zktrie_db, } } } -impl<'e, H> EvmExecutorBuilder<'e, H> { +impl EvmExecutorBuilder { /// Set hardfork config. - pub fn hardfork_config

(self, hardfork_config: H1) -> EvmExecutorBuilder<'e, H1> { + pub fn hardfork_config

(self, hardfork_config: H1) -> EvmExecutorBuilder { EvmExecutorBuilder { hardfork_config, - execute_hooks: self.execute_hooks, + code_db: self.code_db, zktrie_db: self.zktrie_db, } } - /// Modify execute hooks. - pub fn with_execute_hooks(mut self, modify: impl FnOnce(&mut ExecuteHooks<'e>)) -> Self { - modify(&mut self.execute_hooks); - self + /// Set code db. + pub fn code_db(self, code_db: CodeDb1) -> EvmExecutorBuilder { + EvmExecutorBuilder { + hardfork_config: self.hardfork_config, + code_db, + zktrie_db: self.zktrie_db, + } } - /// Set zktrie state. - pub fn zktrie_db(self, zktrie_db: Rc) -> EvmExecutorBuilder<'e, H> { - EvmExecutorBuilder { zktrie_db, ..self } + /// Set zktrie db. + pub fn zktrie_db(self, zktrie_db: ZkDb1) -> EvmExecutorBuilder { + EvmExecutorBuilder { + hardfork_config: self.hardfork_config, + code_db: self.code_db, + zktrie_db, + } } } -impl<'e> EvmExecutorBuilder<'e, HardforkConfig> { +impl + EvmExecutorBuilder +{ /// Initialize an EVM executor from a block trace as the initial state. - pub fn build(self, l2_trace: &T) -> Result, ZkTrieError> { + pub fn with_hooks<'e, T: Block, F: FnOnce(&mut ExecuteHooks<'e, CodeDb, ZkDb>)>( + self, + l2_trace: &T, + with_execute_hooks: F, + ) -> Result, DatabaseError> { + let mut execute_hooks = ExecuteHooks::new(); + with_execute_hooks(&mut execute_hooks); + let block_number = l2_trace.number(); let spec_id = self.hardfork_config.get_spec_id(block_number); dev_trace!("use spec id {:?}", spec_id); let db = cycle_track!( - CacheDB::new(ReadOnlyDB::new(l2_trace, &self.zktrie_db)?), + CacheDB::new(EvmDatabase::new(l2_trace, self.code_db, self.zktrie_db)?), "build ReadOnlyDB" ); @@ -72,7 +88,15 @@ impl<'e> EvmExecutorBuilder<'e, HardforkConfig> { hardfork_config: self.hardfork_config, db, spec_id, - hooks: self.execute_hooks, + hooks: execute_hooks, }) } + + /// Initialize an EVM executor from a block trace as the initial state. + pub fn build<'e, T: Block>( + self, + l2_trace: &T, + ) -> Result, DatabaseError> { + self.with_hooks(l2_trace, |_| {}) + } } diff --git a/crates/core/src/executor/hooks.rs b/crates/core/src/executor/hooks.rs index 8e45cb8..1e17d7c 100644 --- a/crates/core/src/executor/hooks.rs +++ b/crates/core/src/executor/hooks.rs @@ -2,18 +2,26 @@ use crate::EvmExecutor; use std::fmt::{Debug, Formatter}; /// Transaction RLP handler. -pub type TxRLPHandler<'a> = dyn Fn(&EvmExecutor, &[u8]) + 'a; +pub type TxRLPHandler<'a, CodeDb, ZkDb> = dyn Fn(&EvmExecutor, &[u8]) + 'a; /// Post transaction execution handler. -pub type PostTxExecutionHandler<'a> = dyn Fn(&EvmExecutor, usize) + 'a; +pub type PostTxExecutionHandler<'a, CodeDb, ZkDb> = dyn Fn(&EvmExecutor, usize) + 'a; /// Hooks for the EVM executor. -#[derive(Default)] -pub struct ExecuteHooks<'a> { - tx_rlp_handlers: Vec>>, - post_tx_execution_handlers: Vec>>, +pub struct ExecuteHooks<'a, CodeDb, ZkDb> { + tx_rlp_handlers: Vec>>, + post_tx_execution_handlers: Vec>>, } -impl<'a> ExecuteHooks<'a> { +impl<'a, CodeDb, ZkDb> Default for ExecuteHooks<'a, CodeDb, ZkDb> { + fn default() -> Self { + Self { + tx_rlp_handlers: Vec::new(), + post_tx_execution_handlers: Vec::new(), + } + } +} + +impl<'a, CodeDb, ZkDb> ExecuteHooks<'a, CodeDb, ZkDb> { /// Create a new hooks. pub fn new() -> Self { Self::default() @@ -22,7 +30,7 @@ impl<'a> ExecuteHooks<'a> { /// Add a transaction RLP handler. pub fn add_tx_rlp_handler(&mut self, handler: F) where - F: Fn(&EvmExecutor, &[u8]) + 'a, + F: Fn(&EvmExecutor, &[u8]) + 'a, { self.tx_rlp_handlers.push(Box::new(handler)); } @@ -30,26 +38,26 @@ impl<'a> ExecuteHooks<'a> { /// Add a post transaction execution handler. pub fn add_post_tx_execution_handler(&mut self, handler: F) where - F: Fn(&EvmExecutor, usize) + 'a, + F: Fn(&EvmExecutor, usize) + 'a, { self.post_tx_execution_handlers.push(Box::new(handler)); } /// Execute transaction RLP handlers. - pub(crate) fn tx_rlp(&self, executor: &EvmExecutor, rlp: &[u8]) { + pub(crate) fn tx_rlp(&self, executor: &EvmExecutor, rlp: &[u8]) { for handler in &self.tx_rlp_handlers { handler(executor, rlp); } } - pub(crate) fn post_tx_execution(&self, executor: &EvmExecutor, tx_index: usize) { + pub(crate) fn post_tx_execution(&self, executor: &EvmExecutor, tx_index: usize) { for handler in &self.post_tx_execution_handlers { handler(executor, tx_index); } } } -impl Debug for ExecuteHooks<'_> { +impl Debug for ExecuteHooks<'_, CodeDb, ZkDb> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("ExecuteHooks") .field("tx_rlp_handlers", &self.tx_rlp_handlers.len()) diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index 308887d..bcfa9a1 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -1,36 +1,48 @@ -use crate::{database::ReadOnlyDB, error::VerificationError, error::ZkTrieError, HardforkConfig}; +use crate::{ + database::EvmDatabase, error::DatabaseError, error::VerificationError, HardforkConfig, +}; use revm::db::AccountState; use revm::primitives::{BlockEnv, TxEnv, U256}; use revm::{ db::CacheDB, primitives::{AccountInfo, Env, SpecId, B256, KECCAK_EMPTY, POSEIDON_EMPTY}, }; -use sbv_primitives::{zk_trie::ZkMemoryDb, Block, Transaction, TxTrace}; +use sbv_primitives::{ + zk_trie::{ + db::KVDatabase, + hash::{ + key_hasher::NoCacheHasher, + poseidon::{Poseidon, PoseidonError}, + }, + trie::{ZkTrie, ZkTrieError}, + }, + Block, Transaction, TxTrace, +}; use std::fmt::Debug; -use std::rc::Rc; mod builder; pub use builder::EvmExecutorBuilder; +use sbv_primitives::zk_trie::scroll_types::Account; /// Execute hooks pub mod hooks; /// EVM executor that handles the block. -pub struct EvmExecutor<'a> { +pub struct EvmExecutor<'a, CodeDb, ZkDb> { hardfork_config: HardforkConfig, - db: CacheDB, + db: CacheDB>, spec_id: SpecId, - hooks: hooks::ExecuteHooks<'a>, + hooks: hooks::ExecuteHooks<'a, CodeDb, ZkDb>, } -impl EvmExecutor<'_> { +impl EvmExecutor<'_, CodeDb, ZkDb> { /// Get reference to the DB - pub fn db(&self) -> &CacheDB { + pub fn db(&self) -> &CacheDB> { &self.db } /// Update the DB - pub fn update_db(&mut self, l2_trace: &T) -> Result<(), ZkTrieError> { + pub fn update_db(&mut self, l2_trace: &T) -> Result<(), DatabaseError> { self.db.db.invalidate_storage_root_caches( self.db .accounts @@ -160,17 +172,27 @@ impl EvmExecutor<'_> { } /// Commit pending changes in cache db to zktrie - pub fn commit_changes(&mut self, zktrie_db: &Rc) -> B256 { + pub fn commit_changes(&mut self, zktrie_db: ZkDb) -> Result { measure_duration_millis!( commit_changes_duration_milliseconds, - cycle_track!(self.commit_changes_inner(zktrie_db), "commit_changes") + cycle_track!( + self.commit_changes_inner(zktrie_db) + .map_err(DatabaseError::zk_trie), + "commit_changes" + ) ) } - fn commit_changes_inner(&mut self, zktrie_db: &Rc) -> B256 { - let mut zktrie = zktrie_db - .new_trie(&self.db.db.committed_zktrie_root()) - .expect("infallible"); + fn commit_changes_inner( + &mut self, + zktrie_db: ZkDb, + ) -> Result> { + let mut zktrie = ZkTrie::::new_with_root( + zktrie_db.clone(), + NoCacheHasher, + self.db.db.committed_zktrie_root(), + ) + .expect("infallible"); #[cfg(any(feature = "debug-account", feature = "debug-storage"))] let mut debug_recorder = sbv_utils::DebugRecorder::new(); @@ -180,7 +202,7 @@ impl EvmExecutor<'_> { if db_acc.account_state == AccountState::None { continue; } - let Some(info): Option = db_acc.info() else { + let Some(mut info): Option = db_acc.info() else { continue; }; if info.is_empty() { @@ -190,27 +212,25 @@ impl EvmExecutor<'_> { dev_trace!("committing {addr}, {:?} {db_acc:?}", db_acc.account_state); cycle_tracker_start!("commit account {}", addr); - let mut code_size = 0; let mut storage_root = self.db.db.prev_storage_root(addr); - let mut code_hash = B256::ZERO; - let mut poseidon_code_hash = B256::ZERO; if !db_acc.storage.is_empty() { // get current storage root let storage_root_before = storage_root; // get storage tire cycle_tracker_start!("update storage_tire"); - let mut storage_trie = zktrie_db - .new_trie(storage_root_before.as_ref()) - .expect("unable to get storage trie"); + let mut storage_trie = ZkTrie::::new_with_root( + zktrie_db.clone(), + NoCacheHasher, + storage_root_before, + ) + .expect("unable to get storage trie"); for (key, value) in db_acc.storage.iter() { if !value.is_zero() { measure_duration_micros!( zktrie_update_duration_microseconds, cycle_track!( - storage_trie - .update_store(&key.to_be_bytes::<32>(), &value.to_be_bytes()) - .expect("failed to update storage"), + storage_trie.update(key.to_be_bytes::<32>(), value)?, "Zktrie::update_store" ) ); @@ -218,7 +238,7 @@ impl EvmExecutor<'_> { measure_duration_micros!( zktrie_delete_duration_microseconds, cycle_track!( - storage_trie.delete(&key.to_be_bytes::<32>()), + storage_trie.delete(key.to_be_bytes::<32>())?, "Zktrie::delete" ) ); @@ -230,13 +250,11 @@ impl EvmExecutor<'_> { measure_duration_micros!( zktrie_commit_duration_microseconds, - if storage_trie.is_trie_dirty() { - storage_trie.prepare_root(); - } + storage_trie.commit()? ); cycle_tracker_end!("update storage_tire"); - storage_root = storage_trie.root().into(); + storage_root = *storage_trie.root().unwrap_ref(); #[cfg(feature = "debug-storage")] debug_recorder.record_storage_root(*addr, storage_root); @@ -247,43 +265,29 @@ impl EvmExecutor<'_> { // if account not exist, all fields will be zero. // but if account exist, code_hash will be empty hash if code is empty if info.is_empty_code_hash() { - code_hash = KECCAK_EMPTY.0.into(); - poseidon_code_hash = POSEIDON_EMPTY.0.into(); + info.code_hash = KECCAK_EMPTY.0.into(); + info.poseidon_code_hash = POSEIDON_EMPTY.0.into(); } else { assert_ne!( info.poseidon_code_hash, B256::ZERO, "revm didn't update poseidon_code_hash, revm: {info:?}", ); - code_size = info.code_size as u64; - code_hash = info.code_hash.0.into(); - poseidon_code_hash = info.poseidon_code_hash.0.into(); } + } else { + info.code_hash = B256::ZERO; + info.poseidon_code_hash = B256::ZERO; } #[cfg(feature = "debug-account")] - debug_recorder.record_account( - *addr, - info.nonce, - info.balance, - code_hash, - poseidon_code_hash, - code_size, - storage_root, - ); + debug_recorder.record_account(*addr, info.clone(), storage_root); - let acc_data = [ - U256::from_limbs([info.nonce, code_size, 0, 0]).to_be_bytes(), - info.balance.to_be_bytes(), - storage_root.0, - code_hash.0, - poseidon_code_hash.0, - ]; + let acc_data = Account::from_revm_account_with_storage_root(info, storage_root); measure_duration_micros!( zktrie_update_duration_microseconds, cycle_track!( zktrie - .update_account(addr.as_slice(), &acc_data) + .update(addr, acc_data) .expect("failed to update account"), "Zktrie::update_account" ) @@ -292,22 +296,17 @@ impl EvmExecutor<'_> { cycle_tracker_end!("commit account {}", addr); } - measure_duration_micros!( - zktrie_commit_duration_microseconds, - if zktrie.is_trie_dirty() { - zktrie.prepare_root(); - } - ); + measure_duration_micros!(zktrie_commit_duration_microseconds, zktrie.commit()?); - let root_after = zktrie.root(); + let root_after = *zktrie.root().unwrap_ref(); - self.db.db.updated_committed_zktrie_root(root_after.into()); + self.db.db.updated_committed_zktrie_root(root_after); - B256::from(root_after) + Ok(B256::from(root_after)) } } -impl Debug for EvmExecutor<'_> { +impl Debug for EvmExecutor<'_, CodeDb, ZkDb> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("EvmExecutor") .field("db", &self.db) diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 5f60dfb..7faee4a 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -7,7 +7,7 @@ mod chunk; pub use chunk::ChunkInfo; mod database; -pub use database::ReadOnlyDB; +pub use database::EvmDatabase; mod error; pub use error::VerificationError; diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index a3b276b..15ffc8f 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -19,8 +19,13 @@ serde = { workspace = true, features = ["derive"] } serde_with.workspace = true tiny-keccak.workspace = true +sbv-utils.workspace = true + poseidon-bn254.workspace = true -zktrie.workspace = true +zktrie-ng.workspace = true [dev-dependencies] -serde_json.workspace = true \ No newline at end of file +serde_json.workspace = true + +[features] +dev = ["sbv-utils/dev"] \ No newline at end of file diff --git a/crates/primitives/src/imp/archived_block_trace_v2.rs b/crates/primitives/src/imp/archived_block_trace_v2.rs deleted file mode 100644 index 6125ecd..0000000 --- a/crates/primitives/src/imp/archived_block_trace_v2.rs +++ /dev/null @@ -1,215 +0,0 @@ -use crate::*; -use eth_types::l2_types::{ArchivedBlockTraceV2, ArchivedTransactionTrace, TransactionTrace}; -use eth_types::{Address, H256}; -use revm_primitives::{AccessListItem, TransactTo, B256, U256}; -use rkyv::Deserialize; - -impl BlockTraceExt for ArchivedBlockTraceV2 { - #[inline(always)] - fn root_before(&self) -> H256 { - H256::from(self.storage_trace.root_before.0) - } - #[inline(always)] - fn root_after(&self) -> H256 { - H256::from(self.storage_trace.root_after.0) - } - #[inline(always)] - fn withdraw_root(&self) -> H256 { - H256::from(self.withdraw_trie_root.0) - } - #[inline(always)] - fn flatten_proofs(&self) -> Option> { - if self.storage_trace.flatten_proofs.is_empty() { - None - } else { - Some( - self.storage_trace - .flatten_proofs - .iter() - .map(|(k, v)| (k.as_h256(), v.as_ref())), - ) - } - } - #[inline(always)] - fn address_hashes(&self) -> impl Iterator { - self.storage_trace - .address_hashes - .iter() - .map(|(k, v)| (k.as_h160(), v.as_h256())) - } - #[inline(always)] - fn store_key_hashes(&self) -> impl Iterator { - self.storage_trace - .store_key_hashes - .iter() - .map(|(k, v)| (k.as_h256(), v.as_h256())) - } - #[inline(always)] - fn account_proofs(&self) -> impl Iterator)> { - self.storage_trace - .proofs - .iter() - .map(|(addr, b)| (addr.as_h160(), b.iter().map(|b| b.as_ref()))) - } - #[inline(always)] - fn storage_proofs( - &self, - ) -> impl Iterator)> { - self.storage_trace - .storage_proofs - .iter() - .flat_map(|(addr, map)| { - map.iter().map(move |(sk, bts)| { - (addr.as_h160(), sk.as_h256(), bts.iter().map(|b| b.as_ref())) - }) - }) - } - #[inline(always)] - fn additional_proofs(&self) -> impl Iterator { - self.storage_trace - .deletion_proofs - .iter() - .map(|s| s.as_ref()) - } - #[inline] - fn codes(&self) -> impl ExactSizeIterator { - self.codes.iter().map(|code| code.code.as_ref()) - } - #[inline] - fn start_l1_queue_index(&self) -> u64 { - self.start_l1_queue_index - } -} - -impl BlockTraceRevmExt for ArchivedBlockTraceV2 { - type Tx = ArchivedTransactionTrace; - #[inline] - fn number(&self) -> u64 { - self.header.number.0[0] - } - #[inline] - fn block_hash(&self) -> B256 { - self.header.hash.0.into() - } - #[inline] - fn chain_id(&self) -> u64 { - self.chain_id - } - #[inline] - fn coinbase(&self) -> revm_primitives::Address { - self.coinbase.address.0.into() - } - #[inline] - fn timestamp(&self) -> U256 { - U256::from_limbs(self.header.timestamp.0) - } - #[inline] - fn gas_limit(&self) -> U256 { - U256::from_limbs(self.header.gas_limit.0) - } - #[inline] - fn base_fee_per_gas(&self) -> Option { - self.header - .base_fee_per_gas - .as_ref() - .map(|b| U256::from_limbs(b.0)) - } - #[inline] - fn difficulty(&self) -> U256 { - U256::from_limbs(self.header.difficulty.0) - } - #[inline] - fn prevrandao(&self) -> Option { - self.header.mix_hash.as_ref().map(|h| B256::from(h.0)) - } - #[inline] - fn transactions(&self) -> impl Iterator { - self.transactions.iter() - } -} - -impl BlockZktrieExt for ArchivedBlockTraceV2 {} - -impl Transaction for ArchivedTransactionTrace { - #[inline] - fn raw_type(&self) -> u8 { - self.type_ - } - #[inline] - fn tx_hash(&self) -> B256 { - B256::new(self.tx_hash.0) - } - #[inline] - fn caller(&self) -> revm_primitives::Address { - self.from.0.into() - } - #[inline] - fn gas_limit(&self) -> u64 { - self.gas - } - #[inline] - fn gas_price(&self) -> U256 { - U256::from_limbs(self.gas_price.0) - } - #[inline] - fn transact_to(&self) -> TransactTo { - match self.to.as_ref() { - Some(to) => TransactTo::Call(to.0.into()), - None => TransactTo::Create, - } - } - #[inline] - fn value(&self) -> U256 { - U256::from_limbs(self.value.0) - } - #[inline] - fn data(&self) -> revm_primitives::Bytes { - revm_primitives::Bytes::copy_from_slice(self.data.as_ref()) - } - #[inline] - fn nonce(&self) -> u64 { - self.nonce - } - #[inline] - fn chain_id(&self) -> u64 { - self.chain_id.0[0] - } - #[inline] - fn access_list(&self) -> Vec { - self.access_list - .as_ref() - .map(|v| { - v.iter() - .map(|e| AccessListItem { - address: e.address.0.into(), - storage_keys: e.storage_keys.iter().map(|s| s.0.into()).collect(), - }) - .collect() - }) - .unwrap_or_default() - } - #[inline] - fn gas_priority_fee(&self) -> Option { - self.gas_tip_cap.as_ref().map(|g| U256::from_limbs(g.0)) - } - #[inline] - fn to_eth_tx( - &self, - block_hash: B256, - block_number: u64, - transaction_index: usize, - base_fee_per_gas: Option, - ) -> eth_types::Transaction { - // FIXME: zero copy here pls - let tx_trace: TransactionTrace = - Deserialize::::deserialize(self, &mut rkyv::Infallible).unwrap(); - tx_trace.to_eth_tx( - Some(H256::from(block_hash.0)), - Some(block_number.into()), - Some((transaction_index as u64).into()), - base_fee_per_gas.map(|b| eth_types::U256(*b.as_limbs())), - ) - } -} - -impl BlockChunkExt for ArchivedBlockTraceV2 {} diff --git a/crates/primitives/src/imp/blanket.rs b/crates/primitives/src/imp/blanket.rs deleted file mode 100644 index 7f47211..0000000 --- a/crates/primitives/src/imp/blanket.rs +++ /dev/null @@ -1,187 +0,0 @@ -use crate::*; -use eth_types::{Address, H256}; -use revm_primitives::{AccessListItem, TransactTo, B256, U256}; - -impl BlockTraceExt for &T { - #[inline(always)] - fn root_before(&self) -> H256 { - (*self).root_before() - } - #[inline(always)] - fn root_after(&self) -> H256 { - (*self).root_after() - } - #[inline(always)] - fn withdraw_root(&self) -> H256 { - (*self).withdraw_root() - } - #[inline(always)] - fn account_proofs(&self) -> impl Iterator)> { - (*self).account_proofs() - } - #[inline(always)] - fn storage_proofs( - &self, - ) -> impl Iterator)> { - (*self).storage_proofs() - } - #[inline(always)] - fn additional_proofs(&self) -> impl Iterator { - (*self).additional_proofs() - } - #[inline(always)] - fn flatten_proofs(&self) -> Option> { - (*self).flatten_proofs() - } - #[inline(always)] - fn address_hashes(&self) -> impl Iterator { - (*self).address_hashes() - } - #[inline(always)] - fn store_key_hashes(&self) -> impl Iterator { - (*self).store_key_hashes() - } - #[inline(always)] - fn codes(&self) -> impl ExactSizeIterator { - (*self).codes() - } - #[inline(always)] - fn start_l1_queue_index(&self) -> u64 { - (*self).start_l1_queue_index() - } -} - -impl BlockTraceRevmExt for &T { - type Tx = T::Tx; - - #[inline(always)] - fn number(&self) -> u64 { - (*self).number() - } - - #[inline(always)] - fn block_hash(&self) -> B256 { - (*self).block_hash() - } - - #[inline(always)] - fn chain_id(&self) -> u64 { - (*self).chain_id() - } - - #[inline(always)] - fn coinbase(&self) -> revm_primitives::Address { - (*self).coinbase() - } - - #[inline(always)] - fn timestamp(&self) -> U256 { - (*self).timestamp() - } - - #[inline(always)] - fn gas_limit(&self) -> U256 { - (*self).gas_limit() - } - - #[inline(always)] - fn base_fee_per_gas(&self) -> Option { - (*self).base_fee_per_gas() - } - - #[inline(always)] - fn difficulty(&self) -> U256 { - (*self).difficulty() - } - - #[inline(always)] - fn prevrandao(&self) -> Option { - (*self).prevrandao() - } - - #[inline(always)] - fn transactions(&self) -> impl Iterator { - (*self).transactions() - } -} - -impl BlockZktrieExt for &T {} - -impl Transaction for &T { - #[inline(always)] - fn raw_type(&self) -> u8 { - (*self).raw_type() - } - #[inline(always)] - fn tx_hash(&self) -> B256 { - (*self).tx_hash() - } - - #[inline(always)] - fn caller(&self) -> revm_primitives::Address { - (*self).caller() - } - - #[inline(always)] - fn gas_limit(&self) -> u64 { - (*self).gas_limit() - } - - #[inline(always)] - fn gas_price(&self) -> U256 { - (*self).gas_price() - } - - #[inline(always)] - fn transact_to(&self) -> TransactTo { - (*self).transact_to() - } - - #[inline(always)] - fn value(&self) -> U256 { - (*self).value() - } - - #[inline(always)] - fn data(&self) -> revm_primitives::Bytes { - (*self).data() - } - - #[inline(always)] - fn nonce(&self) -> u64 { - (*self).nonce() - } - - #[inline(always)] - fn chain_id(&self) -> u64 { - (*self).chain_id() - } - - #[inline(always)] - fn access_list(&self) -> Vec { - (*self).access_list() - } - - #[inline(always)] - fn gas_priority_fee(&self) -> Option { - (*self).gas_priority_fee() - } - - #[inline(always)] - fn to_eth_tx( - &self, - block_hash: B256, - block_number: u64, - transaction_index: usize, - base_fee_per_gas: Option, - ) -> eth_types::Transaction { - (*self).to_eth_tx( - block_hash, - block_number, - transaction_index, - base_fee_per_gas, - ) - } -} - -impl BlockChunkExt for &T {} diff --git a/crates/primitives/src/imp/block_trace.rs b/crates/primitives/src/imp/block_trace.rs deleted file mode 100644 index c00b6e1..0000000 --- a/crates/primitives/src/imp/block_trace.rs +++ /dev/null @@ -1,129 +0,0 @@ -use crate::*; -use eth_types::l2_types::{BlockTrace, TransactionTrace}; -use eth_types::{Address, H256}; -use revm_primitives::{B256, U256}; - -impl BlockTraceExt for BlockTrace { - #[inline(always)] - fn root_before(&self) -> H256 { - self.storage_trace.root_before - } - #[inline(always)] - fn root_after(&self) -> H256 { - self.storage_trace.root_after - } - #[inline(always)] - fn withdraw_root(&self) -> H256 { - self.withdraw_trie_root - } - #[inline(always)] - fn flatten_proofs(&self) -> Option> { - if self.storage_trace.flatten_proofs.is_empty() { - None - } else { - Some( - self.storage_trace - .flatten_proofs - .iter() - .map(|(k, v)| (k, v.as_ref())), - ) - } - } - #[inline(always)] - fn address_hashes(&self) -> impl Iterator { - self.storage_trace - .address_hashes - .iter() - .map(|(k, v)| (k, v)) - } - #[inline(always)] - fn store_key_hashes(&self) -> impl Iterator { - self.storage_trace - .store_key_hashes - .iter() - .map(|(k, v)| (k, v)) - } - #[inline(always)] - fn account_proofs(&self) -> impl Iterator)> { - self.storage_trace - .proofs - .iter() - .map(|(addr, b)| (addr, b.iter().map(|b| b.as_ref()))) - } - #[inline(always)] - fn storage_proofs( - &self, - ) -> impl Iterator)> { - self.storage_trace - .storage_proofs - .iter() - .flat_map(|(addr, map)| { - map.iter() - .map(move |(sk, bts)| (addr, sk, bts.iter().map(|b| b.as_ref()))) - }) - } - #[inline(always)] - fn additional_proofs(&self) -> impl Iterator { - self.storage_trace - .deletion_proofs - .iter() - .map(|s| s.as_ref()) - } - #[inline] - fn codes(&self) -> impl ExactSizeIterator { - self.codes.iter().map(|code| code.code.as_ref()) - } - #[inline] - fn start_l1_queue_index(&self) -> u64 { - self.start_l1_queue_index - } -} - -impl BlockTraceRevmExt for BlockTrace { - type Tx = TransactionTrace; - - #[inline] - fn number(&self) -> u64 { - self.header.number.expect("incomplete block").as_u64() - } - #[inline] - fn block_hash(&self) -> B256 { - self.header.hash.expect("incomplete block").0.into() - } - #[inline] - fn chain_id(&self) -> u64 { - self.chain_id - } - #[inline] - fn coinbase(&self) -> revm_primitives::Address { - self.coinbase.address.0.into() - } - #[inline] - fn timestamp(&self) -> U256 { - U256::from_limbs(self.header.timestamp.0) - } - #[inline] - fn gas_limit(&self) -> U256 { - U256::from_limbs(self.header.gas_limit.0) - } - #[inline] - fn base_fee_per_gas(&self) -> Option { - self.header.base_fee_per_gas.map(|b| U256::from_limbs(b.0)) - } - #[inline] - fn difficulty(&self) -> U256 { - U256::from_limbs(self.header.difficulty.0) - } - #[inline] - fn prevrandao(&self) -> Option { - self.header.mix_hash.map(|h| B256::from(h.0)) - } - #[inline] - fn transactions(&self) -> impl Iterator { - self.transactions.iter() - } -} - -impl BlockZktrieExt for BlockTrace {} - -impl BlockChunkExt for BlockTrace {} diff --git a/crates/primitives/src/imp/block_trace_v2.rs b/crates/primitives/src/imp/block_trace_v2.rs deleted file mode 100644 index c12cfcf..0000000 --- a/crates/primitives/src/imp/block_trace_v2.rs +++ /dev/null @@ -1,130 +0,0 @@ -use crate::*; -use eth_types::l2_types::{BlockTraceV2, TransactionTrace}; -use eth_types::{Address, H256}; -use revm_primitives::{B256, U256}; - -impl BlockTraceExt for BlockTraceV2 { - #[inline(always)] - fn root_before(&self) -> H256 { - self.storage_trace.root_before - } - #[inline(always)] - fn root_after(&self) -> H256 { - self.storage_trace.root_after - } - #[inline(always)] - fn withdraw_root(&self) -> H256 { - self.withdraw_trie_root - } - #[inline(always)] - fn flatten_proofs(&self) -> Option> { - if self.storage_trace.flatten_proofs.is_empty() { - None - } else { - Some( - self.storage_trace - .flatten_proofs - .iter() - .map(|(k, v)| (k, v.as_ref())), - ) - } - } - #[inline(always)] - fn address_hashes(&self) -> impl Iterator { - self.storage_trace - .address_hashes - .iter() - .map(|(k, v)| (k, v)) - } - #[inline(always)] - fn store_key_hashes(&self) -> impl Iterator { - self.storage_trace - .store_key_hashes - .iter() - .map(|(k, v)| (k, v)) - } - #[inline(always)] - fn account_proofs(&self) -> impl Iterator)> { - self.storage_trace - .proofs - .iter() - .map(|(addr, b)| (addr, b.iter().map(|b| b.as_ref()))) - } - #[inline(always)] - fn storage_proofs( - &self, - ) -> impl Iterator)> { - self.storage_trace - .storage_proofs - .iter() - .flat_map(|(addr, map)| { - map.iter() - .map(move |(sk, bts)| (addr, sk, bts.iter().map(|b| b.as_ref()))) - }) - } - #[inline(always)] - fn additional_proofs(&self) -> impl Iterator { - self.storage_trace - .deletion_proofs - .iter() - .map(|s| s.as_ref()) - } - #[inline] - fn codes(&self) -> impl ExactSizeIterator { - self.codes.iter().map(|code| code.code.as_ref()) - } - #[inline] - fn start_l1_queue_index(&self) -> u64 { - self.start_l1_queue_index - } -} - -impl BlockTraceRevmExt for BlockTraceV2 { - type Tx = TransactionTrace; - #[inline] - fn number(&self) -> u64 { - self.header.number.as_u64() - } - #[inline] - fn block_hash(&self) -> B256 { - self.header.hash.0.into() - } - #[inline] - fn chain_id(&self) -> u64 { - self.chain_id - } - #[inline] - fn coinbase(&self) -> revm_primitives::Address { - self.coinbase.address.0.into() - } - #[inline] - fn timestamp(&self) -> U256 { - U256::from_limbs(self.header.timestamp.0) - } - #[inline] - fn gas_limit(&self) -> U256 { - U256::from_limbs(self.header.gas_limit.0) - } - #[inline] - fn base_fee_per_gas(&self) -> Option { - self.header.base_fee_per_gas.map(|b| U256::from_limbs(b.0)) - } - #[inline] - fn difficulty(&self) -> U256 { - U256::from_limbs(self.header.difficulty.0) - } - #[inline] - fn prevrandao(&self) -> Option { - self.header - .mix_hash - .map(|h| revm_primitives::B256::from(h.0)) - } - #[inline] - fn transactions(&self) -> impl Iterator { - self.transactions.iter() - } -} - -impl BlockZktrieExt for BlockTraceV2 {} - -impl BlockChunkExt for BlockTraceV2 {} diff --git a/crates/primitives/src/imp/mod.rs b/crates/primitives/src/imp/mod.rs deleted file mode 100644 index 4b21688..0000000 --- a/crates/primitives/src/imp/mod.rs +++ /dev/null @@ -1,92 +0,0 @@ -use crate::Transaction; -use eth_types::l2_types::TransactionTrace; -use eth_types::H256; -use revm_primitives::{AccessListItem, TransactTo, B256, U256}; - -mod archived_block_trace_v2; -mod blanket; -mod block_trace; -mod block_trace_v2; - -impl Transaction for TransactionTrace { - #[inline] - fn raw_type(&self) -> u8 { - self.type_ - } - #[inline] - fn tx_hash(&self) -> B256 { - B256::new(self.tx_hash.0) - } - #[inline] - fn caller(&self) -> revm_primitives::Address { - self.from.0.into() - } - #[inline] - fn gas_limit(&self) -> u64 { - self.gas - } - #[inline] - fn gas_price(&self) -> U256 { - U256::from_limbs(self.gas_price.0) - } - #[inline] - fn transact_to(&self) -> TransactTo { - match self.to { - Some(to) => TransactTo::Call(to.0.into()), - None => TransactTo::Create, - } - } - #[inline] - fn value(&self) -> U256 { - U256::from_limbs(self.value.0) - } - #[inline] - fn data(&self) -> revm_primitives::Bytes { - revm_primitives::Bytes::copy_from_slice(self.data.as_ref()) - } - #[inline] - fn nonce(&self) -> u64 { - self.nonce - } - #[inline] - fn chain_id(&self) -> u64 { - self.chain_id.as_u64() - } - #[inline] - fn access_list(&self) -> Vec { - self.access_list - .as_ref() - .map(|v| { - v.iter() - .map(|e| AccessListItem { - address: e.address.0.into(), - storage_keys: e - .storage_keys - .iter() - .map(|s| s.to_fixed_bytes().into()) - .collect(), - }) - .collect() - }) - .unwrap_or_default() - } - #[inline] - fn gas_priority_fee(&self) -> Option { - self.gas_tip_cap.map(|g| U256::from_limbs(g.0)) - } - #[inline] - fn to_eth_tx( - &self, - block_hash: B256, - block_number: u64, - transaction_index: usize, - base_fee_per_gas: Option, - ) -> eth_types::Transaction { - self.to_eth_tx( - Some(H256::from(block_hash.0)), - Some(block_number.into()), - Some((transaction_index as u64).into()), - base_fee_per_gas.map(|b| eth_types::U256(*b.as_limbs())), - ) - } -} diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 4bf1ddb..00ba421 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -1,4 +1,6 @@ //! Stateless Block Verifier primitives library. +#[macro_use] +extern crate sbv_utils; use crate::types::{TxL1Msg, TypedTransaction}; use alloy::{ @@ -7,8 +9,11 @@ use alloy::{ primitives::{Bytes, ChainId, Signature, SignatureError, TxKind}, }; use std::fmt::Debug; -use std::sync::Once; -use zktrie::ZkMemoryDb; +use zktrie_ng::{ + db::KVDatabase, + hash::poseidon::Poseidon, + trie::{Node, MAGIC_NODE_BYTES}, +}; /// Predeployed contracts pub mod predeployed; @@ -19,21 +24,7 @@ pub use alloy::consensus as alloy_consensus; pub use alloy::consensus::Transaction; pub use alloy::primitives as alloy_primitives; pub use alloy::primitives::{Address, B256, U256}; -pub use zktrie as zk_trie; - -/// Initialize the hash scheme for zkTrie. -pub fn init_hash_scheme() { - static INIT: Once = Once::new(); - INIT.call_once(|| { - zktrie::init_hash_scheme_simple(|a: &[u8; 32], b: &[u8; 32], domain: &[u8; 32]| { - use poseidon_bn254::{hash_with_domain, Fr, PrimeField}; - let a = Fr::from_bytes(a).into_option()?; - let b = Fr::from_bytes(b).into_option()?; - let domain = Fr::from_bytes(domain).into_option()?; - Some(hash_with_domain(&[a, b], domain).to_repr()) - }); - }); -} +pub use zktrie_ng as zk_trie; /// Blanket trait for block trace extensions. pub trait Block: Debug { @@ -89,10 +80,18 @@ pub trait Block: Debug { /// Update zktrie state from trace #[inline] - fn build_zktrie_db(&self, zktrie_db: &mut ZkMemoryDb) { - for (_, bytes) in self.flatten_proofs() { - zktrie_db.add_node_bytes(bytes, None).unwrap(); + fn build_zktrie_db(&self, db: &mut Db) -> Result<(), Db::Error> { + for (k, bytes) in self.flatten_proofs() { + if bytes == MAGIC_NODE_BYTES { + continue; + } + let node = Node::::try_from(bytes).expect("invalid node"); + let node_hash = node.get_or_calculate_node_hash().expect("infallible"); + debug_assert_eq!(k.as_slice(), node_hash.as_slice()); + dev_trace!("put zktrie node: {:?}", node); + db.put_owned(node_hash.as_slice(), node.canonical_value(false))?; } + Ok(()) } /// Number of l1 transactions diff --git a/crates/sbv/Cargo.toml b/crates/sbv/Cargo.toml index 44817ac..4a07c1c 100644 --- a/crates/sbv/Cargo.toml +++ b/crates/sbv/Cargo.toml @@ -17,7 +17,7 @@ sbv-utils.workspace = true [features] debug-account = ["sbv-core/debug-account", "sbv-utils/debug-account"] debug-storage = ["sbv-core/debug-storage", "sbv-utils/debug-storage"] -dev = ["sbv-core/dev", "sbv-utils/dev"] +dev = ["sbv-core/dev", "sbv-utils/dev", "sbv-primitives/dev"] metrics = ["sbv-core/metrics", "sbv-utils/metrics"] # sp1 related diff --git a/crates/utils/src/utils/debug.rs b/crates/utils/src/utils/debug.rs index 6c7ea33..8996462 100644 --- a/crates/utils/src/utils/debug.rs +++ b/crates/utils/src/utils/debug.rs @@ -1,4 +1,4 @@ -use revm::primitives::{Address, B256, U256}; +use revm::primitives::{AccountInfo, Address, B256, U256}; use std::collections::BTreeMap; #[derive(Debug, serde::Serialize)] @@ -39,25 +39,16 @@ impl DebugRecorder { /// Record the account data. #[cfg(feature = "debug-account")] #[allow(clippy::too_many_arguments)] - pub fn record_account( - &mut self, - addr: Address, - nonce: u64, - balance: U256, - code_hash: B256, - poseidon_code_hash: B256, - code_size: u64, - storage_root: B256, - ) { + pub fn record_account(&mut self, addr: Address, info: AccountInfo, storage_root: B256) { self.accounts.insert( addr, AccountData { addr, - nonce, - balance, - code_hash, - poseidon_code_hash, - code_size, + nonce: info.nonce, + balance: info.balance, + code_hash: info.code_hash, + poseidon_code_hash: info.poseidon_code_hash, + code_size: info.code_size as u64, storage_root, }, ); From 85edc4562ae26cf87f75292d38ded5a5517a05a7 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Tue, 24 Sep 2024 11:26:46 +0800 Subject: [PATCH 05/14] feat: make trace fields pub (#57) * make trace fields pub * update zktrie-ng --- Cargo.lock | 42 ++++++++++++------------- crates/primitives/src/types/mod.rs | 50 +++++++++++++++--------------- crates/primitives/src/types/tx.rs | 34 ++++++++++---------- 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b92f737..2bb314a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,9 +76,9 @@ dependencies = [ [[package]] name = "alloy-chains" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abf770dad29577cd3580f3dd09005799224a912b8cdfdd6dc04d030d42b3df4e" +checksum = "805f7a974de5804f5c053edc6ca43b20883bdd3a733b3691200ae3a4b454a2db" dependencies = [ "num_enum", "strum", @@ -1017,9 +1017,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" dependencies = [ "clap_builder", "clap_derive", @@ -1027,9 +1027,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" dependencies = [ "anstream", "anstyle", @@ -1039,9 +1039,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck", "proc-macro2", @@ -2016,9 +2016,9 @@ dependencies = [ [[package]] name = "k256" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "956ff9b67e26e1a6a866cb758f12c6f8746208489e3e4a4b5580802f2f0a587b" +checksum = "f6e3919bbaa2945715f0bb6d3934a173d1e9a59ac23767fbaaef277265a7411b" dependencies = [ "cfg-if", "ecdsa", @@ -2496,9 +2496,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "poseidon-bn254" @@ -3266,9 +3266,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "ea4a292869320c0272d7bc55a5a6aafaff59b4f63404a003887b679a2e05b4b6" dependencies = [ "core-foundation-sys", "libc", @@ -3430,9 +3430,9 @@ dependencies = [ [[package]] name = "simdutf8" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "slab" @@ -3679,18 +3679,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", @@ -4362,7 +4362,7 @@ dependencies = [ [[package]] name = "zktrie-ng" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#d749dca019145bc7898564647945cd0898a842d1" +source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#4d884b22c52d69ed4959d9b8353870e2eae4e405" dependencies = [ "alloy-primitives", "hashbrown 0.14.5", diff --git a/crates/primitives/src/types/mod.rs b/crates/primitives/src/types/mod.rs index 1e91587..0ab6a3b 100644 --- a/crates/primitives/src/types/mod.rs +++ b/crates/primitives/src/types/mod.rs @@ -12,27 +12,27 @@ pub use tx::{ArchivedTransactionTrace, TransactionTrace, TxL1Msg, TypedTransacti )] #[archive(check_bytes)] #[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -struct BlockHeader { +pub struct BlockHeader { /// block number - number: U256, + pub number: U256, /// block hash - hash: B256, + pub hash: B256, /// timestamp - timestamp: U256, + pub timestamp: U256, /// gas limit #[serde(rename = "gasLimit")] - gas_limit: U256, + pub gas_limit: U256, /// gas used #[serde(rename = "gasUsed")] - gas_used: U256, + pub gas_used: U256, /// base fee per gas #[serde(rename = "baseFeePerGas")] - base_fee_per_gas: Option, + pub base_fee_per_gas: Option, /// difficulty - difficulty: U256, + pub difficulty: U256, /// mix hash #[serde(rename = "mixHash")] - mix_hash: Option, + pub mix_hash: Option, } /// Coinbase @@ -41,9 +41,9 @@ struct BlockHeader { )] #[archive(check_bytes)] #[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -struct Coinbase { +pub struct Coinbase { /// address of coinbase - address: Address, + pub address: Address, } /// Bytecode trace @@ -52,9 +52,9 @@ struct Coinbase { )] #[archive(check_bytes)] #[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -struct BytecodeTrace { +pub struct BytecodeTrace { /// bytecode - code: Bytes, + pub code: Bytes, } /// storage trace @@ -73,17 +73,17 @@ struct BytecodeTrace { )] #[archive(check_bytes)] #[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -struct StorageTrace { +pub struct StorageTrace { /// root before #[serde(rename = "rootBefore")] - root_before: B256, + pub root_before: B256, /// root after #[serde(rename = "rootAfter")] - root_after: B256, + pub root_after: B256, /// proofs #[serde(rename = "flattenProofs")] #[serde_as(as = "Map<_, _>")] - flatten_proofs: Vec<(B256, Bytes)>, + pub flatten_proofs: Vec<(B256, Bytes)>, } /// Block trace format @@ -97,23 +97,23 @@ struct StorageTrace { pub struct BlockTrace { /// chain id #[serde(rename = "chainID", default)] - chain_id: u64, + pub chain_id: u64, /// coinbase - coinbase: Coinbase, + pub coinbase: Coinbase, /// block - header: BlockHeader, + pub header: BlockHeader, /// txs - transactions: Vec, + pub transactions: Vec, /// bytecodes - codes: Vec, + pub codes: Vec, /// storage trace BEFORE execution #[serde(rename = "storageTrace")] - storage_trace: StorageTrace, + pub storage_trace: StorageTrace, /// l1 tx queue #[serde(rename = "startL1QueueIndex", default)] - start_l1_queue_index: u64, + pub start_l1_queue_index: u64, /// Withdraw root - withdraw_trie_root: B256, + pub withdraw_trie_root: B256, } impl Block for BlockTrace { diff --git a/crates/primitives/src/types/tx.rs b/crates/primitives/src/types/tx.rs index d8e99ae..443d59e 100644 --- a/crates/primitives/src/types/tx.rs +++ b/crates/primitives/src/types/tx.rs @@ -65,47 +65,47 @@ pub struct TxL1Msg { pub struct TransactionTrace { /// tx hash #[serde(default, rename = "txHash")] - pub(crate) tx_hash: B256, + pub tx_hash: B256, /// tx type (in raw from) #[serde(rename = "type")] - pub(crate) ty: u8, + pub ty: u8, /// nonce - pub(crate) nonce: u64, + pub nonce: u64, /// gas limit - pub(crate) gas: u64, + pub gas: u64, #[serde(rename = "gasPrice")] /// gas price - pub(crate) gas_price: U256, + pub gas_price: U256, #[serde(rename = "gasTipCap")] /// gas tip cap - pub(crate) gas_tip_cap: Option, + pub gas_tip_cap: Option, #[serde(rename = "gasFeeCap")] /// gas fee cap - pub(crate) gas_fee_cap: Option, + pub gas_fee_cap: Option, /// from - pub(crate) from: Address, + pub from: Address, /// to, NONE for creation (0 addr) - pub(crate) to: Option
, + pub to: Option
, /// chain id #[serde(rename = "chainId")] - pub(crate) chain_id: U64, + pub chain_id: U64, /// value amount - pub(crate) value: U256, + pub value: U256, /// call data - pub(crate) data: Bytes, + pub data: Bytes, /// is creation #[serde(rename = "isCreate")] - pub(crate) is_create: bool, + pub is_create: bool, /// access list #[serde(rename = "accessList")] #[serde_as(as = "DefaultOnNull")] - pub(crate) access_list: AccessList, + pub access_list: AccessList, /// signature v - pub(crate) v: U64, + pub v: U64, /// signature r - pub(crate) r: U256, + pub r: U256, /// signature s - pub(crate) s: U256, + pub s: U256, } impl TxTrace for TransactionTrace { From 66f5313e86407421a6af7eddfde13761c2c8fa87 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 24 Sep 2024 13:13:44 +0800 Subject: [PATCH 06/14] fix: pub DatabaseError --- crates/core/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 7faee4a..5d5779e 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -10,7 +10,7 @@ mod database; pub use database::EvmDatabase; mod error; -pub use error::VerificationError; +pub use error::{DatabaseError, VerificationError}; mod executor; pub use executor::{hooks, EvmExecutor, EvmExecutorBuilder}; From ac6476b53d99ea350cfa4f0fb3bb1a6b6abecf92 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 24 Sep 2024 13:49:05 +0800 Subject: [PATCH 07/14] feat: return gas used --- crates/core/src/executor/mod.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index bcfa9a1..159c460 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -54,8 +54,9 @@ impl EvmExecutor<'_, Cod } /// Handle a block. - pub fn handle_block(&mut self, l2_trace: &T) -> Result<(), VerificationError> { - measure_duration_millis!( + pub fn handle_block(&mut self, l2_trace: &T) -> Result { + #[allow(clippy::let_and_return)] + let gas_used = measure_duration_millis!( handle_block_duration_milliseconds, self.handle_block_inner(l2_trace) )?; @@ -63,11 +64,11 @@ impl EvmExecutor<'_, Cod #[cfg(feature = "metrics")] sbv_utils::metrics::REGISTRY.block_counter.inc(); - Ok(()) + Ok(gas_used) } #[inline(always)] - fn handle_block_inner(&mut self, l2_trace: &T) -> Result<(), VerificationError> { + fn handle_block_inner(&mut self, l2_trace: &T) -> Result { self.hardfork_config .migrate(l2_trace.number(), &mut self.db) .unwrap(); @@ -86,6 +87,8 @@ impl EvmExecutor<'_, Cod blob_excess_gas_and_price: None, }; + let mut gas_used = 0; + for (idx, tx) in l2_trace.transactions().enumerate() { cycle_tracker_start!("handle tx {}", idx); @@ -151,7 +154,7 @@ impl EvmExecutor<'_, Cod dev_trace!("handler cfg: {:?}", revm.handler.cfg); - let _result = measure_duration_millis!( + let result = measure_duration_millis!( transact_commit_duration_milliseconds, cycle_track!(revm.transact_commit(), "transact_commit").map_err(|e| { VerificationError::EvmExecution { @@ -161,14 +164,16 @@ impl EvmExecutor<'_, Cod })? ); - dev_trace!("{_result:#?}"); + gas_used += result.gas_used(); + + dev_trace!("{result:#?}"); } self.hooks.post_tx_execution(self, idx); dev_debug!("handle {idx}th tx done"); cycle_tracker_end!("handle tx {}", idx); } - Ok(()) + Ok(gas_used) } /// Commit pending changes in cache db to zktrie From 851f5141ded76ddba7594814b9761df1dc469a12 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Wed, 25 Sep 2024 13:32:56 +0800 Subject: [PATCH 08/14] feat: add legacy support (#58) * add legacy support * clippy * update changelog * defaults to legacy --- CHANGELOG.md | 16 ++++ crates/bin/src/commands/run_rpc.rs | 51 ++++++---- crates/core/src/error.rs | 2 + crates/primitives/src/lib.rs | 7 +- crates/primitives/src/types/mod.rs | 145 +++++++++++++++++++++++++---- 5 files changed, 183 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b29b4fc..507e817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add a new struct `LegacyStorageTrace` to support legacy storage trace support + ([#58](https://github.com/scroll-tech/stateless-block-verifier/pull/58)) + +### Changed + +- `flatten_proofs` in `StorageTrace` is changed from `Vec<(B256, Bytes)` to `Vec` + since the node hash will be recalculated when adding to zktrie + ([#58](https://github.com/scroll-tech/stateless-block-verifier/pull/58)) +- `BlockTrace` now has a generic parameter `S` for the storage trace type, default to `StorageTrace` + ([#58](https://github.com/scroll-tech/stateless-block-verifier/pull/58)) +- rpc mode defaults to use legacy storage trace, using the flag `--flatten-proofs` to enable support of flatten proofs + ([#58](https://github.com/scroll-tech/stateless-block-verifier/pull/58)) + + ## [2.0.0] - 2024-09-04 ### Added diff --git a/crates/bin/src/commands/run_rpc.rs b/crates/bin/src/commands/run_rpc.rs index 27553ef..ec4ff3e 100644 --- a/crates/bin/src/commands/run_rpc.rs +++ b/crates/bin/src/commands/run_rpc.rs @@ -2,6 +2,7 @@ use crate::utils; use alloy::providers::{Provider, ProviderBuilder}; use clap::Args; use futures::future::OptionFuture; +use sbv::primitives::types::LegacyStorageTrace; use sbv::{ core::HardforkConfig, primitives::{types::BlockTrace, Block}, @@ -19,6 +20,9 @@ pub struct RunRpcCommand { /// RPC URL #[arg(short, long, default_value = "http://localhost:8545")] url: Url, + /// Enable flatten proofs + #[arg(short, long)] + flatten_proofs: bool, /// Start Block number #[arg(short, long, default_value = "latest")] start_block: StartBlockSpec, @@ -49,7 +53,11 @@ pub enum StartBlockSpec { impl RunRpcCommand { pub async fn run(self, fork_config: impl Fn(u64) -> HardforkConfig) -> anyhow::Result<()> { - dev_info!("Running RPC command with url: {}", self.url); + dev_info!( + "Running RPC command with url: {}, flatten proofs support: {}", + self.url, + self.flatten_proofs + ); let provider = ProviderBuilder::new().on_http(self.url); let chain_id = provider.get_chain_id().await?; @@ -75,21 +83,32 @@ impl RunRpcCommand { let rx = rx.clone(); handles.spawn(async move { while let Ok(block_number) = rx.recv().await { - let l2_trace = _provider - .raw_request::<_, BlockTrace>( - "scroll_getBlockTraceByNumberOrHash".into(), - ( - format!("0x{:x}", block_number), - serde_json::json!({ - "ExcludeExecutionResults": true, - "ExcludeTxStorageTraces": true, - "StorageProofFormat": "flatten", - "FlattenProofsOnly": true - }), - ), - ) - .await - .map_err(|e| (block_number, e.into()))?; + let l2_trace: BlockTrace = if !self.flatten_proofs { + let trace = _provider + .raw_request::<_, BlockTrace>( + "scroll_getBlockTraceByNumberOrHash".into(), + (format!("0x{:x}", block_number),), + ) + .await + .map_err(|e| (block_number, e.into()))?; + trace.into() + } else { + _provider + .raw_request::<_, BlockTrace>( + "scroll_getBlockTraceByNumberOrHash".into(), + ( + format!("0x{:x}", block_number), + serde_json::json!({ + "ExcludeExecutionResults": true, + "ExcludeTxStorageTraces": true, + "StorageProofFormat": "flatten", + "FlattenProofsOnly": true + }), + ), + ) + .await + .map_err(|e| (block_number, e.into()))? + }; dev_info!( "worker#{_idx}: load trace for block #{block_number}({})", diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 322f6c5..174f260 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -4,8 +4,10 @@ use std::error::Error; /// Error variants encountered during manipulation of a zkTrie. #[derive(Debug, thiserror::Error)] pub enum DatabaseError { + /// Error encountered from code db. #[error("error encountered from code db: {0}")] CodeDb(Box), + /// Error encountered from zkTrie. #[error("error encountered from zkTrie: {0}")] ZkTrie(Box), } diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 00ba421..f0f7f7a 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -76,18 +76,17 @@ pub trait Block: Debug { fn start_l1_queue_index(&self) -> u64; /// flatten proofs - fn flatten_proofs(&self) -> impl Iterator; + fn flatten_proofs(&self) -> impl Iterator; /// Update zktrie state from trace #[inline] fn build_zktrie_db(&self, db: &mut Db) -> Result<(), Db::Error> { - for (k, bytes) in self.flatten_proofs() { + for bytes in self.flatten_proofs() { if bytes == MAGIC_NODE_BYTES { continue; } let node = Node::::try_from(bytes).expect("invalid node"); let node_hash = node.get_or_calculate_node_hash().expect("infallible"); - debug_assert_eq!(k.as_slice(), node_hash.as_slice()); dev_trace!("put zktrie node: {:?}", node); db.put_owned(node_hash.as_slice(), node.canonical_value(false))?; } @@ -334,7 +333,7 @@ impl Block for &T { (*self).start_l1_queue_index() } - fn flatten_proofs(&self) -> impl Iterator { + fn flatten_proofs(&self) -> impl Iterator { (*self).flatten_proofs() } } diff --git a/crates/primitives/src/types/mod.rs b/crates/primitives/src/types/mod.rs index 0ab6a3b..74bb09d 100644 --- a/crates/primitives/src/types/mod.rs +++ b/crates/primitives/src/types/mod.rs @@ -1,7 +1,11 @@ use crate::Block; use alloy::primitives::{Address, Bytes, B256, U256}; -use serde::{Deserialize, Serialize}; +use rkyv::Archive; +use serde::{Deserialize, Deserializer, Serialize}; use serde_with::{serde_as, Map}; +use std::collections::{BTreeSet, HashMap}; +use std::fmt::Debug; +use std::hash::Hash; mod tx; pub use tx::{ArchivedTransactionTrace, TransactionTrace, TxL1Msg, TypedTransaction}; @@ -58,16 +62,15 @@ pub struct BytecodeTrace { } /// storage trace -#[serde_as] #[derive( rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Serialize, - Deserialize, Default, Debug, Clone, + Hash, Eq, PartialEq, )] @@ -82,8 +85,45 @@ pub struct StorageTrace { pub root_after: B256, /// proofs #[serde(rename = "flattenProofs")] + pub flatten_proofs: Vec, +} + +/// legacy storage trace +#[serde_as] +#[derive( + rkyv::Archive, + rkyv::Serialize, + rkyv::Deserialize, + Serialize, + Deserialize, + Default, + Debug, + Clone, + Hash, + Eq, + PartialEq, +)] +#[archive(check_bytes)] +#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[allow(clippy::type_complexity)] +pub struct LegacyStorageTrace { + /// root before + #[serde(rename = "rootBefore")] + pub root_before: B256, + /// root after + #[serde(rename = "rootAfter")] + pub root_after: B256, + /// account proofs + #[serde(default)] #[serde_as(as = "Map<_, _>")] - pub flatten_proofs: Vec<(B256, Bytes)>, + pub proofs: Vec<(Address, Vec)>, + #[serde(rename = "storageProofs", default)] + #[serde_as(as = "Map<_, Map<_, _>>")] + /// storage proofs for each account + pub storage_proofs: Vec<(Address, Vec<(B256, Vec)>)>, + #[serde(rename = "deletionProofs", default)] + /// additional deletion proofs + pub deletion_proofs: Vec, } /// Block trace format @@ -94,7 +134,11 @@ pub struct StorageTrace { )] #[archive(check_bytes)] #[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -pub struct BlockTrace { +pub struct BlockTrace +where + S: Archive, + ::Archived: Debug + Hash + PartialEq + Eq, +{ /// chain id #[serde(rename = "chainID", default)] pub chain_id: u64, @@ -108,7 +152,7 @@ pub struct BlockTrace { pub codes: Vec, /// storage trace BEFORE execution #[serde(rename = "storageTrace")] - pub storage_trace: StorageTrace, + pub storage_trace: S, /// l1 tx queue #[serde(rename = "startL1QueueIndex", default)] pub start_l1_queue_index: u64, @@ -116,6 +160,78 @@ pub struct BlockTrace { pub withdraw_trie_root: B256, } +impl<'de> Deserialize<'de> for StorageTrace { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + #[derive(Deserialize)] + #[serde(untagged)] + enum FlattenProofs { + Map(HashMap), + Vec(Vec), + } + #[derive(Deserialize)] + struct StorageTraceDe { + #[serde(rename = "rootBefore")] + pub root_before: B256, + #[serde(rename = "rootAfter")] + pub root_after: B256, + #[serde(rename = "flattenProofs")] + pub flatten_proofs: FlattenProofs, + } + + let de = StorageTraceDe::deserialize(deserializer)?; + let mut flatten_proofs = match de.flatten_proofs { + FlattenProofs::Map(map) => map.into_values().collect(), + FlattenProofs::Vec(vec) => vec, + }; + flatten_proofs.sort(); + + Ok(StorageTrace { + root_before: de.root_before, + root_after: de.root_after, + flatten_proofs, + }) + } +} + +impl From for StorageTrace { + fn from(trace: LegacyStorageTrace) -> Self { + let mut flatten_proofs = BTreeSet::new(); + for (_, proofs) in trace.proofs { + flatten_proofs.extend(proofs); + } + for (_, proofs) in trace.storage_proofs { + for (_, proofs) in proofs { + flatten_proofs.extend(proofs); + } + } + flatten_proofs.extend(trace.deletion_proofs); + + StorageTrace { + root_before: trace.root_before, + root_after: trace.root_after, + flatten_proofs: flatten_proofs.into_iter().collect(), + } + } +} + +impl From> for BlockTrace { + fn from(trace: BlockTrace) -> Self { + BlockTrace { + chain_id: trace.chain_id, + coinbase: trace.coinbase, + header: trace.header, + transactions: trace.transactions, + codes: trace.codes, + storage_trace: trace.storage_trace.into(), + start_l1_queue_index: trace.start_l1_queue_index, + withdraw_trie_root: trace.withdraw_trie_root, + } + } +} + impl Block for BlockTrace { type Tx = TransactionTrace; @@ -179,11 +295,8 @@ impl Block for BlockTrace { self.start_l1_queue_index } - fn flatten_proofs(&self) -> impl Iterator { - self.storage_trace - .flatten_proofs - .iter() - .map(|(k, v)| (k, v.as_ref())) + fn flatten_proofs(&self) -> impl Iterator { + self.storage_trace.flatten_proofs.iter().map(|v| v.as_ref()) } } @@ -254,11 +367,8 @@ impl Block for ArchivedBlockTrace { self.start_l1_queue_index } - fn flatten_proofs(&self) -> impl Iterator { - self.storage_trace - .flatten_proofs - .iter() - .map(|(k, v)| (k, v.as_ref())) + fn flatten_proofs(&self) -> impl Iterator { + self.storage_trace.flatten_proofs.iter().map(|v| v.as_ref()) } } @@ -404,8 +514,7 @@ mod tests { .iter() .zip(archived_block.storage_trace.flatten_proofs.iter()) { - assert_eq!(proof.0, archived_proof.0); - assert_eq!(proof.1.as_ref(), archived_proof.1.as_ref()); + assert_eq!(proof.as_ref(), archived_proof.as_ref()); } assert_eq!( From b7357c91c0328c16642accebd045797d6a4ae1d9 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Sun, 29 Sep 2024 15:10:55 +0800 Subject: [PATCH 09/14] feat: more tracker points (#59) --- crates/core/src/database.rs | 26 ++++++++---------------- crates/core/src/executor/mod.rs | 35 ++++++++++++++++----------------- crates/primitives/Cargo.toml | 4 +++- crates/primitives/src/lib.rs | 14 ++++++++++--- crates/sbv/Cargo.toml | 4 ++-- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/crates/core/src/database.rs b/crates/core/src/database.rs index 96ac65b..63c3811 100644 --- a/crates/core/src/database.rs +++ b/crates/core/src/database.rs @@ -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::{ @@ -124,28 +124,18 @@ impl EvmDatabase, - ) { - 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 DatabaseRef diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index 159c460..49f52aa 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -43,13 +43,6 @@ impl EvmExecutor<'_, Cod /// Update the DB pub fn update_db(&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) } @@ -58,7 +51,7 @@ impl 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")] @@ -90,7 +83,7 @@ impl 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"); @@ -171,7 +164,7 @@ impl 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) } @@ -215,7 +208,7 @@ impl 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); @@ -224,10 +217,13 @@ impl EvmExecutor<'_, Cod let storage_root_before = storage_root; // get storage tire cycle_tracker_start!("update storage_tire"); - let mut storage_trie = ZkTrie::::new_with_root( - zktrie_db.clone(), - NoCacheHasher, - storage_root_before, + let mut storage_trie = cycle_track!( + ZkTrie::::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() { @@ -255,7 +251,7 @@ impl 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"); @@ -298,10 +294,13 @@ impl 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(); diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 15ffc8f..493b911 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -28,4 +28,6 @@ zktrie-ng.workspace = true serde_json.workspace = true [features] -dev = ["sbv-utils/dev"] \ No newline at end of file +dev = ["sbv-utils/dev"] +sp1 = [] +cycle-tracker = [] \ No newline at end of file diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index f0f7f7a..982df16 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -85,10 +85,18 @@ pub trait Block: Debug { if bytes == MAGIC_NODE_BYTES { continue; } - let node = Node::::try_from(bytes).expect("invalid node"); - let node_hash = node.get_or_calculate_node_hash().expect("infallible"); + let node = cycle_track!(Node::::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(()) } diff --git a/crates/sbv/Cargo.toml b/crates/sbv/Cargo.toml index 4a07c1c..0195497 100644 --- a/crates/sbv/Cargo.toml +++ b/crates/sbv/Cargo.toml @@ -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"] \ No newline at end of file From a615e961ed6085fdc5b94cf004a6d3767084bb3d Mon Sep 17 00:00:00 2001 From: lightsing Date: Sun, 29 Sep 2024 15:22:07 +0800 Subject: [PATCH 10/14] feat: track Node::canonical_value --- crates/primitives/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 982df16..333298a 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -93,8 +93,9 @@ pub trait Block: Debug { ) .expect("infallible"); dev_trace!("put zktrie node: {:?}", node); + let node_bytes = cycle_track!(node.canonical_value(false), "Node::canonical_value"); cycle_track!( - db.put_owned(node_hash.as_slice(), node.canonical_value(false))?, + db.put_owned(node_hash.as_slice(), node_bytes)?, "KVDatabase::put_owned" ); } From 06f85ee89bf900de91b121e3e717eaf4f115ce6b Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Tue, 8 Oct 2024 15:47:20 +0800 Subject: [PATCH 11/14] feat: zktrie-ng rkyv node (#60) * update with zktrie-ng * update zktrie-ng * fix dep version * remove sp1 related changes on master * fix clippy * fix tests --- Cargo.lock | 349 +++++++++++++++------------- crates/bin/src/commands/run_file.rs | 10 +- crates/bin/src/utils.rs | 15 +- crates/core/src/chunk.rs | 15 +- crates/core/src/database.rs | 133 +++++------ crates/core/src/executor/builder.rs | 37 +-- crates/core/src/executor/mod.rs | 51 ++-- crates/primitives/src/lib.rs | 13 +- 8 files changed, 314 insertions(+), 309 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bb314a..887a3e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.24.1" +version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" dependencies = [ "gimli", ] @@ -76,9 +76,9 @@ dependencies = [ [[package]] name = "alloy-chains" -version = "0.1.33" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "805f7a974de5804f5c053edc6ca43b20883bdd3a733b3691200ae3a4b454a2db" +checksum = "94c225801d42099570d0674701dddd4142f0ef715282aeb5985042e2ec962df7" dependencies = [ "num_enum", "strum", @@ -301,7 +301,7 @@ checksum = "4d0f2d905ebd295e7effec65e5f6868d153936130ae718352771de3e7d03c75c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -385,21 +385,21 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "alloy-sol-macro" -version = "0.8.3" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0458ccb02a564228fcd76efb8eb5a520521a8347becde37b402afec9a1b83859" +checksum = "0409e3ba5d1de409997a7db8b8e9d679d52088c1dee042a85033affd3cadeab4" dependencies = [ - "alloy-sol-macro-expander 0.8.3", - "alloy-sol-macro-input 0.8.3", + "alloy-sol-macro-expander 0.8.6", + "alloy-sol-macro-input 0.8.6", "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -410,30 +410,30 @@ dependencies = [ "alloy-sol-macro-input 0.8.0", "const-hex", "heck", - "indexmap 2.5.0", + "indexmap 2.6.0", "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", "syn-solidity 0.8.0", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-expander" -version = "0.8.3" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bc65475025fc1e84bf86fc840f04f63fcccdcf3cf12053c99918e4054dfbc69" +checksum = "a18372ef450d59f74c7a64a738f546ba82c92f816597fed1802ef559304c81f1" dependencies = [ - "alloy-sol-macro-input 0.8.3", + "alloy-sol-macro-input 0.8.6", "const-hex", "heck", - "indexmap 2.5.0", + "indexmap 2.6.0", "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.77", - "syn-solidity 0.8.3", + "syn 2.0.79", + "syn-solidity 0.8.6", "tiny-keccak", ] @@ -447,23 +447,23 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", "syn-solidity 0.8.0", ] [[package]] name = "alloy-sol-macro-input" -version = "0.8.3" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ed10f0715a0b69fde3236ff3b9ae5f6f7c97db5a387747100070d3016b9266b" +checksum = "f7bad89dd0d5f109e8feeaf787a9ed7a05a91a9a0efc6687d147a70ebca8eff7" dependencies = [ "const-hex", "dunce", "heck", "proc-macro2", "quote", - "syn 2.0.77", - "syn-solidity 0.8.3", + "syn 2.0.79", + "syn-solidity 0.8.6", ] [[package]] @@ -482,7 +482,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "577e262966e92112edbd15b1b2c0947cc434d6e8311df96d3329793fe8047da9" dependencies = [ "alloy-primitives", - "alloy-sol-macro 0.8.3", + "alloy-sol-macro 0.8.6", "const-hex", ] @@ -744,9 +744,9 @@ dependencies = [ [[package]] name = "async-stream" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" dependencies = [ "async-stream-impl", "futures-core", @@ -755,24 +755,24 @@ dependencies = [ [[package]] name = "async-stream-impl" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "async-trait" -version = "0.1.82" +version = "0.1.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -799,14 +799,14 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "backtrace" @@ -908,7 +908,7 @@ dependencies = [ [[package]] name = "bn254" version = "0.1.0" -source = "git+https://github.com/scroll-tech/bn254#c9f170c6e39cfbc1ff14b9648f125823978041b3" +source = "git+https://github.com/scroll-tech/bn254#3653980f712ad44d4e079fa8f0199c5678104ef4" dependencies = [ "ff", "getrandom", @@ -989,9 +989,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.21" +version = "1.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" +checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1" dependencies = [ "shlex", ] @@ -1017,9 +1017,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.18" +version = "4.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" +checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615" dependencies = [ "clap_builder", "clap_derive", @@ -1027,9 +1027,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.18" +version = "4.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" +checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b" dependencies = [ "anstream", "anstyle", @@ -1046,7 +1046,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1072,9 +1072,9 @@ dependencies = [ [[package]] name = "const-hex" -version = "1.12.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94fb8a24a26d37e1ffd45343323dc9fe6654ceea44c12f2fcb3d7ac29e610bc6" +checksum = "0121754e84117e65f9d90648ee6aa4882a6e63110307ab73967a4c5e7e69e586" dependencies = [ "cfg-if", "cpufeatures", @@ -1191,7 +1191,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" dependencies = [ "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1215,7 +1215,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1226,7 +1226,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1293,7 +1293,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version 0.4.1", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1391,7 +1391,7 @@ checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -1488,6 +1488,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + [[package]] name = "foreign-types" version = "0.3.2" @@ -1520,9 +1526,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" dependencies = [ "futures-channel", "futures-core", @@ -1535,9 +1541,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", "futures-sink", @@ -1545,15 +1551,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" [[package]] name = "futures-executor" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" dependencies = [ "futures-core", "futures-task", @@ -1562,38 +1568,38 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" [[package]] name = "futures-macro" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "futures-sink" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" [[package]] name = "futures-task" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" [[package]] name = "futures-util" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-channel", "futures-core", @@ -1637,9 +1643,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.31.0" +version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "glob" @@ -1670,7 +1676,7 @@ dependencies = [ "futures-core", "futures-sink", "http", - "indexmap 2.5.0", + "indexmap 2.6.0", "slab", "tokio", "tokio-util", @@ -1697,6 +1703,17 @@ dependencies = [ "serde", ] +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "heck" version = "0.5.0" @@ -1775,9 +1792,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" [[package]] name = "httpdate" @@ -1841,9 +1858,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", @@ -1854,7 +1871,6 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", "tower-service", "tracing", ] @@ -1931,12 +1947,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.14.5", + "hashbrown 0.15.0", "serde", ] @@ -1947,7 +1963,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "232929e1d75fe899576a3d5c7416ad0d88dbfbb3c3d6aa00873a7408a50ddb88" dependencies = [ "ahash 0.8.11", - "indexmap 2.5.0", + "indexmap 2.6.0", "is-terminal", "itoa", "log", @@ -1960,9 +1976,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.10.0" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" [[package]] name = "is-terminal" @@ -2048,9 +2064,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.158" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "libm" @@ -2082,11 +2098,11 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lru" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37ee39891760e7d94734f6f63fedc29a2e4a152f836120753a72503f09fcf904" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" dependencies = [ - "hashbrown 0.14.5", + "hashbrown 0.15.0", ] [[package]] @@ -2225,7 +2241,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -2306,23 +2322,23 @@ checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "object" -version = "0.36.4" +version = "0.36.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" dependencies = [ "memchr", ] [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "openssl" @@ -2347,7 +2363,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -2454,22 +2470,22 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +checksum = "baf123a161dde1e524adf36f90bc5d8d3462824a9c43553ad07a8183161189ec" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +checksum = "a4502d8515ca9f32f1fb543d987f63d95a14934883db45bdb48060b6b69257f8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -2608,14 +2624,14 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a" dependencies = [ "unicode-ident", ] @@ -2640,7 +2656,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -2657,7 +2673,7 @@ dependencies = [ "rand", "rand_chacha", "rand_xorshift", - "regex-syntax 0.8.4", + "regex-syntax 0.8.5", "rusty-fork", "tempfile", "unarray", @@ -2754,23 +2770,23 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.4" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ "bitflags 2.6.0", ] [[package]] name = "regex" -version = "1.10.6" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.7", - "regex-syntax 0.8.4", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", ] [[package]] @@ -2784,13 +2800,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.4", + "regex-syntax 0.8.5", ] [[package]] @@ -2801,9 +2817,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rend" @@ -2816,9 +2832,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.12.7" +version = "0.12.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8f4955649ef5c38cc7f9e8aa41761d48fb9677197daea9984dc54f56aad5e63" +checksum = "f713147fbe92361e52392c73b8c9e48c04c6625bce969ef54dc901e58e042a7b" dependencies = [ "base64", "bytes", @@ -3074,9 +3090,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.13" +version = "0.23.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2dabaac7466917e566adb06783a81ca48944c6898a1b08b9374106dd671f4c8" +checksum = "415d9944693cb90382053259f89fbb077ea730ad7273047ec63b19bc9b160ba8" dependencies = [ "once_cell", "rustls-pki-types", @@ -3087,19 +3103,18 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64", "rustls-pki-types", ] [[package]] name = "rustls-pki-types" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" +checksum = "0e696e35370c65c9c541198af4543ccd580cf17fc25d8e05c5a242b202488c55" [[package]] name = "rustls-webpki" @@ -3199,9 +3214,9 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.24" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9aaafd5a2b6e3d657ff009d82fbd630b6bd54dd4eb06f21693925cdf80f9b8b" +checksum = "01227be5826fa0690321a2ba6c5cd57a19cf3f6a09e76973b58e61de6ab9d1c1" dependencies = [ "windows-sys 0.59.0", ] @@ -3315,7 +3330,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3324,7 +3339,7 @@ version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ - "indexmap 2.5.0", + "indexmap 2.6.0", "itoa", "memchr", "ryu", @@ -3345,15 +3360,15 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.9.0" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cecfa94848272156ea67b2b1a53f20fc7bc638c4a46d2f8abde08f05f4b857" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" dependencies = [ "base64", "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.5.0", + "indexmap 2.6.0", "serde", "serde_derive", "serde_json", @@ -3363,14 +3378,14 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.9.0" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8fee4991ef4f274617a51ad4af30519438dacb2f56ac773b08a1922ff743350" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3538,7 +3553,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3562,9 +3577,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "symbolic-common" -version = "12.11.1" +version = "12.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fdf97c441f18a4f92425b896a4ec7a27e03631a0b1047ec4e34e9916a9a167e" +checksum = "366f1b4c6baf6cfefc234bbd4899535fca0b06c74443039a73f6dfb2fad88d77" dependencies = [ "debugid", "memmap2", @@ -3574,9 +3589,9 @@ dependencies = [ [[package]] name = "symbolic-demangle" -version = "12.11.1" +version = "12.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc8ece6b129e97e53d1fbb3f61d33a6a9e5369b11d01228c068094d6d134eaea" +checksum = "aba05ba5b9962ea5617baf556293720a8b2d0a282aa14ee4bf10e22efc7da8c8" dependencies = [ "cpp_demangle", "rustc-demangle", @@ -3596,9 +3611,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.77" +version = "2.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" dependencies = [ "proc-macro2", "quote", @@ -3613,19 +3628,19 @@ dependencies = [ "paste", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "syn-solidity" -version = "0.8.3" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b95156f8b577cb59dc0b1df15c6f29a10afc5f8a7ac9786b0b5c68c19149278" +checksum = "f3a850d65181df41b83c6be01a7d91f5e9377c43d48faa5af7d95816f437f5a3" dependencies = [ "paste", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3666,9 +3681,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tempfile" -version = "3.12.0" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand", @@ -3694,7 +3709,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3796,7 +3811,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3853,11 +3868,11 @@ checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" -version = "0.22.21" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ - "indexmap 2.5.0", + "indexmap 2.6.0", "toml_datetime", "winnow", ] @@ -3872,7 +3887,6 @@ dependencies = [ "futures-util", "pin-project", "pin-project-lite", - "tokio", "tower-layer", "tower-service", "tracing", @@ -3910,7 +3924,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -3966,9 +3980,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "ucd-trie" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" [[package]] name = "uint" @@ -3990,9 +4004,9 @@ checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" [[package]] name = "unicode-bidi" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" +checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" [[package]] name = "unicode-ident" @@ -4102,7 +4116,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", "wasm-bindgen-shared", ] @@ -4136,7 +4150,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4302,9 +4316,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.18" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] @@ -4336,7 +4350,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] @@ -4356,13 +4370,13 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn 2.0.79", ] [[package]] name = "zktrie-ng" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#4d884b22c52d69ed4959d9b8353870e2eae4e405" +source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#0804d10ae6f721bed397fc3524602bba15d2589e" dependencies = [ "alloy-primitives", "hashbrown 0.14.5", @@ -4372,6 +4386,7 @@ dependencies = [ "once_cell", "poseidon-bn254", "revm-primitives", + "rkyv", "strum", "thiserror", "tracing", diff --git a/crates/bin/src/commands/run_file.rs b/crates/bin/src/commands/run_file.rs index c8badcf..7ea23a1 100644 --- a/crates/bin/src/commands/run_file.rs +++ b/crates/bin/src/commands/run_file.rs @@ -3,9 +3,8 @@ use anyhow::bail; use clap::Args; use sbv::{ core::{ChunkInfo, EvmExecutorBuilder, HardforkConfig}, - primitives::{types::BlockTrace, zk_trie::db::HashMapDb, Block, B256}, + primitives::{types::BlockTrace, zk_trie::db::kv::HashMapDb, Block, B256}, }; -use std::rc::Rc; use std::{cell::RefCell, path::PathBuf}; use tiny_keccak::{Hasher, Keccak}; use tokio::task::JoinSet; @@ -75,12 +74,11 @@ impl RunFileCommand { } let fork_config = fork_config(traces[0].chain_id()); - let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces); - let zktrie_db = Rc::new(RefCell::new(zktrie_db)); + let (chunk_info, mut zktrie_db) = ChunkInfo::from_block_traces(&traces); let tx_bytes_hasher = RefCell::new(Keccak::v256()); - let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), &mut zktrie_db) .hardfork_config(fork_config) .with_hooks(&traces[0], |hooks| { hooks.add_tx_rlp_handler(|_, rlp| { @@ -94,7 +92,7 @@ impl RunFileCommand { executor.handle_block(trace)?; } - let post_state_root = executor.commit_changes(zktrie_db.clone())?; + let post_state_root = executor.commit_changes()?; if post_state_root != chunk_info.post_state_root() { bail!("post state root mismatch"); } diff --git a/crates/bin/src/utils.rs b/crates/bin/src/utils.rs index 36ab091..7782a80 100644 --- a/crates/bin/src/utils.rs +++ b/crates/bin/src/utils.rs @@ -1,9 +1,8 @@ +use sbv::primitives::zk_trie::db::NodeDb; use sbv::{ core::{EvmExecutorBuilder, HardforkConfig, VerificationError}, - primitives::{zk_trie::db::HashMapDb, Block}, + primitives::{zk_trie::db::kv::HashMapDb, Block}, }; -use std::cell::RefCell; -use std::rc::Rc; pub fn verify( l2_trace: T, @@ -37,19 +36,19 @@ fn verify_inner( .build() .unwrap(); - let zktrie_db = cycle_track!( + let mut zktrie_db = cycle_track!( { - let mut zktrie_db = HashMapDb::default(); + let mut zktrie_db = NodeDb::new(HashMapDb::default()); measure_duration_millis!( build_zktrie_db_duration_milliseconds, l2_trace.build_zktrie_db(&mut zktrie_db).unwrap() ); - Rc::new(RefCell::new(zktrie_db)) + zktrie_db }, "build ZktrieState" ); - let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), &mut zktrie_db) .hardfork_config(*fork_config) .build(&l2_trace)?; @@ -66,7 +65,7 @@ fn verify_inner( update_metrics_counter!(verification_error); e })?; - let revm_root_after = executor.commit_changes(zktrie_db.clone())?; + let revm_root_after = executor.commit_changes()?; #[cfg(feature = "profiling")] if let Ok(report) = guard.report().build() { diff --git a/crates/core/src/chunk.rs b/crates/core/src/chunk.rs index e04aeea..ef875bc 100644 --- a/crates/core/src/chunk.rs +++ b/crates/core/src/chunk.rs @@ -1,5 +1,6 @@ use revm::primitives::B256; -use sbv_primitives::{zk_trie::db::HashMapDb, Block}; +use sbv_primitives::zk_trie::db::NodeDb; +use sbv_primitives::{zk_trie::db::kv::HashMapDb, Block}; use tiny_keccak::{Hasher, Keccak}; /// A chunk is a set of continuous blocks. @@ -21,7 +22,7 @@ pub struct ChunkInfo { impl ChunkInfo { /// Construct by block traces - pub fn from_block_traces(traces: &[T]) -> (Self, HashMapDb) { + pub fn from_block_traces(traces: &[T]) -> (Self, NodeDb) { let chain_id = traces.first().unwrap().chain_id(); let prev_state_root = traces .first() @@ -40,7 +41,7 @@ impl ChunkInfo { let mut data_hash = B256::ZERO; data_hasher.finalize(&mut data_hash.0); - let mut zktrie_db = HashMapDb::default(); + let mut zktrie_db = NodeDb::new(HashMapDb::default()); for trace in traces.iter() { measure_duration_millis!( build_zktrie_db_duration_milliseconds, @@ -116,7 +117,6 @@ mod tests { use revm::primitives::b256; use sbv_primitives::types::BlockTrace; use std::cell::RefCell; - use std::rc::Rc; const TRACES_STR: [&str; 4] = [ include_str!("../../../testdata/mainnet_blocks/8370400.json"), @@ -138,12 +138,11 @@ mod tests { }); let fork_config = HardforkConfig::default_from_chain_id(traces[0].chain_id()); - let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces); - let zktrie_db = Rc::new(RefCell::new(zktrie_db)); + let (chunk_info, mut zktrie_db) = ChunkInfo::from_block_traces(&traces); let tx_bytes_hasher = RefCell::new(Keccak::v256()); - let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone()) + let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), &mut zktrie_db) .hardfork_config(fork_config) .with_hooks(&traces[0], |hooks| { hooks.add_tx_rlp_handler(|_, rlp| { @@ -158,7 +157,7 @@ mod tests { executor.handle_block(trace).unwrap(); } - let post_state_root = executor.commit_changes(zktrie_db.clone()).unwrap(); + let post_state_root = executor.commit_changes().unwrap(); assert_eq!(post_state_root, chunk_info.post_state_root); drop(executor); // drop executor to release Rc diff --git a/crates/core/src/database.rs b/crates/core/src/database.rs index 63c3811..7256fa7 100644 --- a/crates/core/src/database.rs +++ b/crates/core/src/database.rs @@ -1,12 +1,13 @@ use crate::error::DatabaseError; -use once_cell::sync::Lazy; use revm::{ db::DatabaseRef, primitives::{AccountInfo, Address, Bytecode, B256, U256}, }; +use sbv_primitives::zk_trie::db::NodeDb; +use sbv_primitives::zk_trie::hash::ZkHash; use sbv_primitives::{ zk_trie::{ - db::{KVDatabase, KVDatabaseItem}, + db::kv::{KVDatabase, KVDatabaseItem}, hash::{key_hasher::NoCacheHasher, poseidon::Poseidon}, scroll_types::Account, trie::ZkTrie, @@ -17,28 +18,24 @@ use std::{cell::RefCell, collections::HashMap, fmt}; type Result = std::result::Result; -type StorageTrieLazyFn = Box ZkTrie>; -type LazyStorageTrie = Lazy, StorageTrieLazyFn>; - /// A database that consists of account and storage information. -pub struct EvmDatabase { +pub struct EvmDatabase<'a, CodeDb, ZkDb> { /// Map of code hash to bytecode. code_db: CodeDb, - /// The initial storage roots of accounts, used for after commit. - /// Need to be updated after zkTrie commit. - prev_storage_roots: RefCell>, + /// Storage root cache, avoid re-query account when storage root is needed + storage_root_caches: RefCell>, /// 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_refs: RefCell>>, + /// Need to invalidate before `update`, otherwise the trie root may be outdated + storage_trie_caches: RefCell>>, /// Current uncommitted zkTrie root based on the block trace. committed_zktrie_root: B256, /// The underlying zkTrie database. - zktrie_db: ZkDb, + pub(crate) zktrie_db: &'a mut NodeDb, /// Current view of zkTrie database. - zktrie: ZkTrie, + zktrie: ZkTrie, } -impl fmt::Debug for EvmDatabase { +impl fmt::Debug for EvmDatabase<'_, CodeDb, Db> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("EvmDatabase") .field("committed_zktrie_root", &self.committed_zktrie_root) @@ -46,9 +43,13 @@ impl fmt::Debug for EvmDatabase { } } -impl EvmDatabase { +impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb, ZkDb> { /// Initialize an EVM database from a block trace. - pub fn new(l2_trace: T, mut code_db: CodeDb, zktrie_db: ZkDb) -> Result { + pub fn new( + l2_trace: T, + mut code_db: CodeDb, + zktrie_db: &'a mut NodeDb, + ) -> Result { cycle_tracker_start!("insert CodeDB"); for code in l2_trace.codes() { let hash = revm::primitives::keccak256(code); @@ -60,43 +61,45 @@ impl EvmDatabase Option { - self.prev_storage_roots - .borrow_mut() - .insert(address, storage_root) - } - /// Get the previous storage root of an account. #[inline] pub(crate) fn prev_storage_root(&self, address: &Address) -> B256 { - self.prev_storage_roots + self.storage_root_caches .borrow() .get(address) .copied() .unwrap_or_default() } + #[inline] + pub(crate) fn update_storage_root_cache(&self, address: Address, storage_root: ZkTrie) { + let new_root = *storage_root.root().unwrap_ref(); + let old = self + .storage_root_caches + .borrow_mut() + .insert(address, new_root); + + let mut storage_trie_caches = self.storage_trie_caches.borrow_mut(); + if let Some(old) = old { + storage_trie_caches.remove(&old); + } + + storage_trie_caches.insert(new_root, storage_root); + } + /// Get the committed zkTrie root. #[inline] pub(crate) fn committed_zktrie_root(&self) -> B256 { @@ -125,11 +128,7 @@ impl EvmDatabase EvmDatabase DatabaseRef - for EvmDatabase -{ +impl DatabaseRef for EvmDatabase<'_, CodeDb, ZkDb> { type Error = DatabaseError; /// Get basic account information. fn basic_ref(&self, address: Address) -> Result> { let Some(account) = measure_duration_micros!( zktrie_get_duration_microseconds, - self.zktrie.get::(address) + self.zktrie.get::<_, Account, _>(self.zktrie_db, address) ) .map_err(DatabaseError::zk_trie)? else { return Ok(None); }; - self.prev_storage_roots + self.storage_root_caches .borrow_mut() - .entry(address) - .or_insert(account.storage_root); - let zktrie_db = self.zktrie_db.clone(); - self.storage_trie_refs - .borrow_mut() - .entry(address) - .or_insert_with(|| { - Lazy::new(Box::new(move || { - ZkTrie::new_with_root(zktrie_db.clone(), NoCacheHasher, account.storage_root) - .expect("storage trie associated with account not found") - })) - }); + .insert(address, account.storage_root); let mut info = AccountInfo::from(account); info.code = self @@ -201,24 +187,27 @@ impl DatabaseRef /// Get storage value of address at index. fn storage_ref(&self, address: Address, index: U256) -> Result { dev_trace!("get storage of {:?} at index {:?}", address, index); - let mut storage_trie_refs = self.storage_trie_refs.borrow_mut(); - let trie = storage_trie_refs + let storage_root = *self + .storage_root_caches + .borrow_mut() .entry(address) .or_insert_with_key(|address| { - let storage_root = measure_duration_micros!( - zktrie_get_duration_microseconds, - self.zktrie.get::(address) - ) - .expect("unexpected zktrie error") - .map(|acc| acc.storage_root) - .unwrap_or_default(); + self.zktrie + .get::<_, Account, _>(self.zktrie_db, address) + .expect("unexpected zktrie error") + .map(|acc| acc.storage_root) + .unwrap_or_default() + }); + + let mut storage_trie_caches = self.storage_trie_caches.borrow_mut(); + + let trie = storage_trie_caches + .entry(storage_root) + .or_insert_with_key(|storage_root| { dev_debug!("storage root of {:?} is {:?}", address, storage_root); - let zktrie_db = self.zktrie_db.clone(); - Lazy::new(Box::new(move || { - ZkTrie::new_with_root(zktrie_db.clone(), NoCacheHasher, storage_root) - .expect("storage trie associated with account not found") - })) + ZkTrie::new_with_root(self.zktrie_db, NoCacheHasher, *storage_root) + .expect("storage trie associated with account not found") }); #[cfg(debug_assertions)] @@ -226,7 +215,7 @@ impl DatabaseRef let current_root = trie.root().unwrap_ref(); let expected_root = self .zktrie - .get::(address) + .get::<_, Account, _>(self.zktrie_db, address) .expect("unexpected zktrie error") .map(|acc| acc.storage_root) .unwrap_or_default(); @@ -235,7 +224,7 @@ impl DatabaseRef Ok(measure_duration_micros!( zktrie_get_duration_microseconds, - trie.get::(index.to_be_bytes::<32>()) + trie.get::<_, U256, _>(self.zktrie_db, index.to_be_bytes::<32>()) ) .map_err(DatabaseError::zk_trie)? .unwrap_or_default()) diff --git a/crates/core/src/executor/builder.rs b/crates/core/src/executor/builder.rs index b17fdd0..de796f7 100644 --- a/crates/core/src/executor/builder.rs +++ b/crates/core/src/executor/builder.rs @@ -1,18 +1,19 @@ use crate::error::DatabaseError; use crate::{executor::hooks::ExecuteHooks, EvmDatabase, EvmExecutor, HardforkConfig}; use revm::db::CacheDB; -use sbv_primitives::zk_trie::db::KVDatabase; +use sbv_primitives::zk_trie::db::kv::KVDatabase; +use sbv_primitives::zk_trie::db::NodeDb; use sbv_primitives::Block; use std::fmt::{self, Debug}; /// Builder for EVM executor. -pub struct EvmExecutorBuilder { +pub struct EvmExecutorBuilder<'a, H, CodeDb, ZkDb> { hardfork_config: H, code_db: CodeDb, - zktrie_db: ZkDb, + zktrie_db: &'a mut NodeDb, } -impl Debug for EvmExecutorBuilder { +impl Debug for EvmExecutorBuilder<'_, H, CodeDb, ZkDb> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("EvmExecutorBuilder") .field("hardfork_config", &self.hardfork_config) @@ -22,9 +23,9 @@ impl Debug for EvmExecutorBuilder { } } -impl EvmExecutorBuilder<(), CodeDb, ZkDb> { +impl<'a, CodeDb, ZkDb> EvmExecutorBuilder<'a, (), CodeDb, ZkDb> { /// Create a new builder. - pub fn new(code_db: CodeDb, zktrie_db: ZkDb) -> Self { + pub fn new(code_db: CodeDb, zktrie_db: &'a mut NodeDb) -> Self { Self { hardfork_config: (), code_db, @@ -33,9 +34,12 @@ impl EvmExecutorBuilder<(), CodeDb, ZkDb> { } } -impl EvmExecutorBuilder { +impl<'a, H, CodeDb, ZkDb> EvmExecutorBuilder<'a, H, CodeDb, ZkDb> { /// Set hardfork config. - pub fn hardfork_config

(self, hardfork_config: H1) -> EvmExecutorBuilder { + pub fn hardfork_config

( + self, + hardfork_config: H1, + ) -> EvmExecutorBuilder<'a, H1, CodeDb, ZkDb> { EvmExecutorBuilder { hardfork_config, code_db: self.code_db, @@ -44,7 +48,7 @@ impl EvmExecutorBuilder { } /// Set code db. - pub fn code_db(self, code_db: CodeDb1) -> EvmExecutorBuilder { + pub fn code_db(self, code_db: CodeDb1) -> EvmExecutorBuilder<'a, H, CodeDb1, ZkDb> { EvmExecutorBuilder { hardfork_config: self.hardfork_config, code_db, @@ -53,7 +57,10 @@ impl EvmExecutorBuilder { } /// Set zktrie db. - pub fn zktrie_db(self, zktrie_db: ZkDb1) -> EvmExecutorBuilder { + pub fn zktrie_db( + self, + zktrie_db: &mut NodeDb, + ) -> EvmExecutorBuilder { EvmExecutorBuilder { hardfork_config: self.hardfork_config, code_db: self.code_db, @@ -62,15 +69,15 @@ impl EvmExecutorBuilder { } } -impl - EvmExecutorBuilder +impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> + EvmExecutorBuilder<'a, HardforkConfig, CodeDb, ZkDb> { /// Initialize an EVM executor from a block trace as the initial state. - pub fn with_hooks<'e, T: Block, F: FnOnce(&mut ExecuteHooks<'e, CodeDb, ZkDb>)>( + pub fn with_hooks<'h, T: Block, F: FnOnce(&mut ExecuteHooks<'h, CodeDb, ZkDb>)>( self, l2_trace: &T, with_execute_hooks: F, - ) -> Result, DatabaseError> { + ) -> Result, DatabaseError> { let mut execute_hooks = ExecuteHooks::new(); with_execute_hooks(&mut execute_hooks); @@ -96,7 +103,7 @@ impl pub fn build<'e, T: Block>( self, l2_trace: &T, - ) -> Result, DatabaseError> { + ) -> Result, DatabaseError> { self.with_hooks(l2_trace, |_| {}) } } diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index 49f52aa..562fb37 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -9,11 +9,12 @@ use revm::{ }; use sbv_primitives::{ zk_trie::{ - db::KVDatabase, + db::kv::KVDatabase, hash::{ key_hasher::NoCacheHasher, poseidon::{Poseidon, PoseidonError}, }, + scroll_types::Account, trie::{ZkTrie, ZkTrieError}, }, Block, Transaction, TxTrace, @@ -22,20 +23,19 @@ use std::fmt::Debug; mod builder; pub use builder::EvmExecutorBuilder; -use sbv_primitives::zk_trie::scroll_types::Account; /// Execute hooks pub mod hooks; /// EVM executor that handles the block. -pub struct EvmExecutor<'a, CodeDb, ZkDb> { +pub struct EvmExecutor<'db, 'h, CodeDb, ZkDb> { hardfork_config: HardforkConfig, - db: CacheDB>, + db: CacheDB>, spec_id: SpecId, - hooks: hooks::ExecuteHooks<'a, CodeDb, ZkDb>, + hooks: hooks::ExecuteHooks<'h, CodeDb, ZkDb>, } -impl EvmExecutor<'_, CodeDb, ZkDb> { +impl EvmExecutor<'_, '_, CodeDb, ZkDb> { /// Get reference to the DB pub fn db(&self) -> &CacheDB> { &self.db @@ -170,23 +170,19 @@ impl EvmExecutor<'_, Cod } /// Commit pending changes in cache db to zktrie - pub fn commit_changes(&mut self, zktrie_db: ZkDb) -> Result { + pub fn commit_changes(&mut self) -> Result { measure_duration_millis!( commit_changes_duration_milliseconds, cycle_track!( - self.commit_changes_inner(zktrie_db) - .map_err(DatabaseError::zk_trie), + self.commit_changes_inner().map_err(DatabaseError::zk_trie), "commit_changes" ) ) } - fn commit_changes_inner( - &mut self, - zktrie_db: ZkDb, - ) -> Result> { - let mut zktrie = ZkTrie::::new_with_root( - zktrie_db.clone(), + fn commit_changes_inner(&mut self) -> Result> { + let mut zktrie = ZkTrie::::new_with_root( + self.db.db.zktrie_db, NoCacheHasher, self.db.db.committed_zktrie_root(), ) @@ -218,8 +214,8 @@ impl EvmExecutor<'_, Cod // get storage tire cycle_tracker_start!("update storage_tire"); let mut storage_trie = cycle_track!( - ZkTrie::::new_with_root( - zktrie_db.clone(), + ZkTrie::::new_with_root( + self.db.db.zktrie_db, NoCacheHasher, storage_root_before, ), @@ -231,7 +227,11 @@ impl EvmExecutor<'_, Cod measure_duration_micros!( zktrie_update_duration_microseconds, cycle_track!( - storage_trie.update(key.to_be_bytes::<32>(), value)?, + storage_trie.update( + self.db.db.zktrie_db, + key.to_be_bytes::<32>(), + value + )?, "Zktrie::update_store" ) ); @@ -239,7 +239,8 @@ impl EvmExecutor<'_, Cod measure_duration_micros!( zktrie_delete_duration_microseconds, cycle_track!( - storage_trie.delete(key.to_be_bytes::<32>())?, + storage_trie + .delete(self.db.db.zktrie_db, key.to_be_bytes::<32>())?, "Zktrie::delete" ) ); @@ -251,16 +252,16 @@ impl EvmExecutor<'_, Cod measure_duration_micros!( zktrie_commit_duration_microseconds, - cycle_track!(storage_trie.commit()?, "Zktrie::commit") + cycle_track!(storage_trie.commit(self.db.db.zktrie_db)?, "Zktrie::commit") ); cycle_tracker_end!("update storage_tire"); storage_root = *storage_trie.root().unwrap_ref(); + self.db.db.update_storage_root_cache(*addr, storage_trie); + #[cfg(feature = "debug-storage")] debug_recorder.record_storage_root(*addr, storage_root); - - self.db.db.set_prev_storage_root(*addr, storage_root); } if !info.is_empty() { // if account not exist, all fields will be zero. @@ -288,7 +289,7 @@ impl EvmExecutor<'_, Cod zktrie_update_duration_microseconds, cycle_track!( zktrie - .update(addr, acc_data) + .update(self.db.db.zktrie_db, addr, acc_data) .expect("failed to update account"), "Zktrie::update_account" ) @@ -299,7 +300,7 @@ impl EvmExecutor<'_, Cod measure_duration_micros!( zktrie_commit_duration_microseconds, - cycle_track!(zktrie.commit()?, "Zktrie::commit") + cycle_track!(zktrie.commit(self.db.db.zktrie_db)?, "Zktrie::commit") ); let root_after = *zktrie.root().unwrap_ref(); @@ -310,7 +311,7 @@ impl EvmExecutor<'_, Cod } } -impl Debug for EvmExecutor<'_, CodeDb, ZkDb> { +impl Debug for EvmExecutor<'_, '_, CodeDb, ZkDb> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("EvmExecutor") .field("db", &self.db) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 333298a..b81b592 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -10,7 +10,7 @@ use alloy::{ }; use std::fmt::Debug; use zktrie_ng::{ - db::KVDatabase, + db::kv::KVDatabase, hash::poseidon::Poseidon, trie::{Node, MAGIC_NODE_BYTES}, }; @@ -25,6 +25,7 @@ pub use alloy::consensus::Transaction; pub use alloy::primitives as alloy_primitives; pub use alloy::primitives::{Address, B256, U256}; pub use zktrie_ng as zk_trie; +use zktrie_ng::db::NodeDb; /// Blanket trait for block trace extensions. pub trait Block: Debug { @@ -80,24 +81,20 @@ pub trait Block: Debug { /// Update zktrie state from trace #[inline] - fn build_zktrie_db(&self, db: &mut Db) -> Result<(), Db::Error> { + fn build_zktrie_db(&self, db: &mut NodeDb) -> Result<(), Db::Error> { for bytes in self.flatten_proofs() { if bytes == MAGIC_NODE_BYTES { continue; } let node = cycle_track!(Node::::try_from(bytes), "Node::try_from") .expect("invalid node"); - let node_hash = cycle_track!( + cycle_track!( node.get_or_calculate_node_hash(), "Node::get_or_calculate_node_hash" ) .expect("infallible"); dev_trace!("put zktrie node: {:?}", node); - let node_bytes = cycle_track!(node.canonical_value(false), "Node::canonical_value"); - cycle_track!( - db.put_owned(node_hash.as_slice(), node_bytes)?, - "KVDatabase::put_owned" - ); + cycle_track!(db.put_node(&node)?, "NodeDb::put_node"); } Ok(()) } From 2bafb0c7d5a2593988ab79fc5595800c83c32d76 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Thu, 10 Oct 2024 18:02:12 +0800 Subject: [PATCH 12/14] feat: upgrade rkyv (#61) * upgrade rkyv * fix tests * add docs --- .github/workflows/ci.yml | 2 +- Cargo.lock | 287 ++++++++-------------- Cargo.toml | 9 +- crates/core/src/chunk.rs | 4 + crates/primitives/src/lib.rs | 38 ++- crates/primitives/src/types/mod.rs | 378 ++++++++++++++++++++++++----- crates/primitives/src/types/tx.rs | 65 +++-- 7 files changed, 498 insertions(+), 285 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0606958..660b52f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,7 +83,7 @@ jobs: matrix: network: [ "mainnet" ] hardfork: [ "pre-bernoulli", "bernoulli", "curie", "darwin" ] - rust: [ "1.75", "nightly-2024-07-07" ] + rust: [ "1.81", "nightly-2024-07-07" ] steps: - uses: actions/checkout@v4 diff --git a/Cargo.lock b/Cargo.lock index 887a3e8..b56661a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,17 +17,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" -[[package]] -name = "ahash" -version = "0.7.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "ahash" version = "0.8.11" @@ -100,24 +89,24 @@ dependencies = [ [[package]] name = "alloy-core" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "alloy-dyn-abi", "alloy-json-abi", "alloy-primitives", "alloy-rlp", - "alloy-sol-types 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "alloy-sol-types", ] [[package]] name = "alloy-dyn-abi" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "alloy-json-abi", "alloy-primitives", "alloy-sol-type-parser", - "alloy-sol-types 0.8.0 (git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0)", + "alloy-sol-types", "const-hex", "itoa", "serde", @@ -128,7 +117,7 @@ dependencies = [ [[package]] name = "alloy-eip2930" version = "0.1.0" -source = "git+https://github.com/scroll-tech/alloy-eips?branch=v0.1.0#f8d7bec6cb9d38320742b89e54c3d8be59937918" +source = "git+https://github.com/scroll-tech/alloy-eips?branch=v0.1.0#06cb417f8b3df8c29a8bf69a8a10663ceb5a2c37" dependencies = [ "alloy-primitives", "alloy-rlp", @@ -139,7 +128,7 @@ dependencies = [ [[package]] name = "alloy-eip7702" version = "0.1.0" -source = "git+https://github.com/scroll-tech/alloy-eips?branch=v0.1.0#f8d7bec6cb9d38320742b89e54c3d8be59937918" +source = "git+https://github.com/scroll-tech/alloy-eips?branch=v0.1.0#06cb417f8b3df8c29a8bf69a8a10663ceb5a2c37" dependencies = [ "alloy-primitives", "alloy-rlp", @@ -176,7 +165,7 @@ dependencies = [ [[package]] name = "alloy-json-abi" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "alloy-primitives", "alloy-sol-type-parser", @@ -190,7 +179,7 @@ version = "0.3.0" source = "git+https://github.com/scroll-tech/alloy.git?branch=v0.3.0#41bbe34a38a2b1536c58b7c938313a2db4e9369a" dependencies = [ "alloy-primitives", - "alloy-sol-types 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-types", "serde", "serde_json", "thiserror", @@ -210,7 +199,7 @@ dependencies = [ "alloy-rpc-types-eth", "alloy-serde", "alloy-signer", - "alloy-sol-types 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-types", "async-trait", "auto_impl", "futures-utils-wasm", @@ -230,7 +219,7 @@ dependencies = [ [[package]] name = "alloy-primitives" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "alloy-rlp", "bytes", @@ -345,7 +334,7 @@ dependencies = [ "alloy-primitives", "alloy-rlp", "alloy-serde", - "alloy-sol-types 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "alloy-sol-types", "itertools 0.13.0", "serde", "serde_json", @@ -378,36 +367,22 @@ dependencies = [ [[package]] name = "alloy-sol-macro" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ - "alloy-sol-macro-expander 0.8.0", - "alloy-sol-macro-input 0.8.0", + "alloy-sol-macro-expander", + "alloy-sol-macro-input", "proc-macro-error", "proc-macro2", "quote", "syn 2.0.79", ] -[[package]] -name = "alloy-sol-macro" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0409e3ba5d1de409997a7db8b8e9d679d52088c1dee042a85033affd3cadeab4" -dependencies = [ - "alloy-sol-macro-expander 0.8.6", - "alloy-sol-macro-input 0.8.6", - "proc-macro-error2", - "proc-macro2", - "quote", - "syn 2.0.79", -] - [[package]] name = "alloy-sol-macro-expander" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ - "alloy-sol-macro-input 0.8.0", + "alloy-sol-macro-input", "const-hex", "heck", "indexmap 2.6.0", @@ -415,32 +390,14 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.79", - "syn-solidity 0.8.0", - "tiny-keccak", -] - -[[package]] -name = "alloy-sol-macro-expander" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a18372ef450d59f74c7a64a738f546ba82c92f816597fed1802ef559304c81f1" -dependencies = [ - "alloy-sol-macro-input 0.8.6", - "const-hex", - "heck", - "indexmap 2.6.0", - "proc-macro-error2", - "proc-macro2", - "quote", - "syn 2.0.79", - "syn-solidity 0.8.6", + "syn-solidity", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-input" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "const-hex", "dunce", @@ -448,28 +405,13 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.79", - "syn-solidity 0.8.0", -] - -[[package]] -name = "alloy-sol-macro-input" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7bad89dd0d5f109e8feeaf787a9ed7a05a91a9a0efc6687d147a70ebca8eff7" -dependencies = [ - "const-hex", - "dunce", - "heck", - "proc-macro2", - "quote", - "syn 2.0.79", - "syn-solidity 0.8.6", + "syn-solidity", ] [[package]] name = "alloy-sol-type-parser" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "serde", "winnow", @@ -478,22 +420,11 @@ dependencies = [ [[package]] name = "alloy-sol-types" version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "577e262966e92112edbd15b1b2c0947cc434d6e8311df96d3329793fe8047da9" -dependencies = [ - "alloy-primitives", - "alloy-sol-macro 0.8.6", - "const-hex", -] - -[[package]] -name = "alloy-sol-types" -version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "alloy-json-abi", "alloy-primitives", - "alloy-sol-macro 0.8.0", + "alloy-sol-macro", "const-hex", "serde", ] @@ -931,24 +862,25 @@ checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c" [[package]] name = "bytecheck" -version = "0.6.12" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" +checksum = "50c8f430744b23b54ad15161fcbc22d82a29b73eacbe425fea23ec822600bc6f" dependencies = [ "bytecheck_derive", "ptr_meta", + "rancor", "simdutf8", ] [[package]] name = "bytecheck_derive" -version = "0.6.12" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" +checksum = "523363cbe1df49b68215efdf500b103ac3b0fb4836aed6d15689a076eadb8fff" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.79", ] [[package]] @@ -1017,9 +949,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.19" +version = "4.5.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" dependencies = [ "clap_builder", "clap_derive", @@ -1027,9 +959,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.19" +version = "4.5.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" dependencies = [ "anstream", "anstyle", @@ -1688,9 +1620,6 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash 0.7.8", -] [[package]] name = "hashbrown" @@ -1698,7 +1627,7 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ - "ahash 0.8.11", + "ahash", "allocator-api2", "serde", ] @@ -1962,7 +1891,7 @@ version = "0.11.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "232929e1d75fe899576a3d5c7416ad0d88dbfbb3c3d6aa00873a7408a50ddb88" dependencies = [ - "ahash 0.8.11", + "ahash", "indexmap 2.6.0", "is-terminal", "itoa", @@ -2023,9 +1952,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" -version = "0.3.70" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +checksum = "0cb94a0ffd3f3ee755c20f7d8752f45cac88605a4dcf808abcff72873296ec7b" dependencies = [ "wasm-bindgen", ] @@ -2156,6 +2085,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "munge" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64142d38c84badf60abf06ff9bd80ad2174306a5b11bd4706535090a30a419df" +dependencies = [ + "munge_macro", +] + +[[package]] +name = "munge_macro" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bb5c1d8184f13f7d0ccbeeca0def2f9a181bce2624302793005f5ca8aa62e5e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "native-tls" version = "0.2.12" @@ -2605,28 +2554,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "proc-macro-error-attr2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" -dependencies = [ - "proc-macro2", - "quote", -] - -[[package]] -name = "proc-macro-error2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" -dependencies = [ - "proc-macro-error-attr2", - "proc-macro2", - "quote", - "syn 2.0.79", -] - [[package]] name = "proc-macro2" version = "1.0.87" @@ -2681,22 +2608,22 @@ dependencies = [ [[package]] name = "ptr_meta" -version = "0.1.4" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +checksum = "fe9e76f66d3f9606f44e45598d155cb13ecf09f4a28199e48daf8c8fc937ea90" dependencies = [ "ptr_meta_derive", ] [[package]] name = "ptr_meta_derive" -version = "0.1.4" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +checksum = "ca414edb151b4c8d125c12566ab0d74dc9cdba36fb80eb7b848c15f495fd32d1" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.79", ] [[package]] @@ -2729,6 +2656,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "rancor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf5f7161924b9d1cea0e4cabc97c372cea92b5f927fc13c6bca67157a0ad947" +dependencies = [ + "ptr_meta", +] + [[package]] name = "rand" version = "0.8.5" @@ -2823,9 +2759,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rend" -version = "0.4.2" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" +checksum = "a31c1f1959e4db12c985c0283656be0925f1539549db1e47c4bd0b8b599e1ef7" dependencies = [ "bytecheck", ] @@ -2876,7 +2812,7 @@ dependencies = [ [[package]] name = "revm" version = "14.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#921525ab7b132414fe1ba1df2b6b2941fceb8f4d" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" dependencies = [ "auto_impl", "cfg-if", @@ -2890,7 +2826,7 @@ dependencies = [ [[package]] name = "revm-interpreter" version = "10.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#921525ab7b132414fe1ba1df2b6b2941fceb8f4d" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" dependencies = [ "cfg-if", "revm-primitives", @@ -2900,7 +2836,7 @@ dependencies = [ [[package]] name = "revm-precompile" version = "11.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#921525ab7b132414fe1ba1df2b6b2941fceb8f4d" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" dependencies = [ "aurora-engine-modexp", "c-kzg", @@ -2917,7 +2853,7 @@ dependencies = [ [[package]] name = "revm-primitives" version = "9.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#921525ab7b132414fe1ba1df2b6b2941fceb8f4d" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" dependencies = [ "alloy-eips", "alloy-primitives", @@ -2979,31 +2915,32 @@ dependencies = [ [[package]] name = "rkyv" -version = "0.7.45" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" +checksum = "395027076c569819ea6035ee62e664f5e03d74e281744f55261dd1afd939212b" dependencies = [ - "bitvec", "bytecheck", "bytes", - "hashbrown 0.12.3", + "hashbrown 0.14.5", + "indexmap 2.6.0", + "munge", "ptr_meta", + "rancor", "rend", "rkyv_derive", - "seahash", "tinyvec", "uuid", ] [[package]] name = "rkyv_derive" -version = "0.7.45" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "503d1d27590a2b0a3a4ca4c94755aa2875657196ecbf401a42eff41d7de532c0" +checksum = "09cb82b74b4810f07e460852c32f522e979787691b0b7b7439fe473e49d49b2f" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.79", ] [[package]] @@ -3019,7 +2956,7 @@ dependencies = [ [[package]] name = "ruint" version = "1.12.3" -source = "git+https://github.com/scroll-tech/uint.git?branch=v1.12.3#6d6f74be1169bdc41052f50ddfb66ae96bf78475" +source = "git+https://github.com/scroll-tech/uint.git?branch=v1.12.3#d26b1f2a4412bba2b9b366f43f7684fd32ac1c8a" dependencies = [ "alloy-rlp", "ark-ff 0.3.0", @@ -3043,7 +2980,7 @@ dependencies = [ [[package]] name = "ruint-macro" version = "1.2.1" -source = "git+https://github.com/scroll-tech/uint.git?branch=v1.12.3#6d6f74be1169bdc41052f50ddfb66ae96bf78475" +source = "git+https://github.com/scroll-tech/uint.git?branch=v1.12.3#d26b1f2a4412bba2b9b366f43f7684fd32ac1c8a" [[package]] name = "rustc-demangle" @@ -3227,12 +3164,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - [[package]] name = "sec1" version = "0.7.3" @@ -3623,19 +3554,7 @@ dependencies = [ [[package]] name = "syn-solidity" version = "0.8.0" -source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#3c8299a45f4722c024a0c34a1ef9b6453b7b3f45" -dependencies = [ - "paste", - "proc-macro2", - "quote", - "syn 2.0.79", -] - -[[package]] -name = "syn-solidity" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a850d65181df41b83c6be01a7d91f5e9377c43d48faa5af7d95816f437f5a3" +source = "git+https://github.com/scroll-tech/alloy-core?branch=v0.8.0#4642d710a73aec9513222db586498951f84534cd" dependencies = [ "paste", "proc-macro2", @@ -4096,9 +4015,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.93" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +checksum = "ef073ced962d62984fb38a36e5fdc1a2b23c9e0e1fa0689bb97afa4202ef6887" dependencies = [ "cfg-if", "once_cell", @@ -4107,9 +4026,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.93" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +checksum = "c4bfab14ef75323f4eb75fa52ee0a3fb59611977fd3240da19b2cf36ff85030e" dependencies = [ "bumpalo", "log", @@ -4122,9 +4041,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.43" +version = "0.4.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" +checksum = "65471f79c1022ffa5291d33520cbbb53b7687b01c2f8e83b57d102eed7ed479d" dependencies = [ "cfg-if", "js-sys", @@ -4134,9 +4053,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.93" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +checksum = "a7bec9830f60924d9ceb3ef99d55c155be8afa76954edffbb5936ff4509474e7" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -4144,9 +4063,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.93" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +checksum = "4c74f6e152a76a2ad448e223b0fc0b6b5747649c3d769cc6bf45737bf97d0ed6" dependencies = [ "proc-macro2", "quote", @@ -4157,15 +4076,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.93" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +checksum = "a42f6c679374623f295a8623adfe63d9284091245c3504bde47c17a3ce2777d9" [[package]] name = "web-sys" -version = "0.3.70" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" +checksum = "44188d185b5bdcae1052d08bcbcf9091a5524038d4572cc4f4f2bb9d5554ddd9" dependencies = [ "js-sys", "wasm-bindgen", @@ -4376,7 +4295,7 @@ dependencies = [ [[package]] name = "zktrie-ng" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#0804d10ae6f721bed397fc3524602bba15d2589e" +source = "git+https://github.com/scroll-tech/zktrie-ng?branch=master#8ad8038e39f9b88abfe8680e32bba19ac4497a1d" dependencies = [ "alloy-primitives", "hashbrown 0.14.5", diff --git a/Cargo.toml b/Cargo.toml index f91e7d5..a5839db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ repository = "https://github.com/scroll-tech/stateless-block-verifier" alloy = "0.3" hex = "0.4" once_cell = "1.19" -rkyv = { version = "0.7", features = ["validation"] } +rkyv = "0.8" thiserror = "1.0" tiny-keccak = "2.0" @@ -85,7 +85,7 @@ missing-debug-implementations = "deny" [patch.crates-io] ff = { git = "https://github.com/scroll-tech/ff", branch = "feat/sp1" } -# patched add rkyv support & MSRV 1.75 +# patched add rkyv support & MSRV 1.77 ruint = { git = "https://github.com/scroll-tech/uint.git", branch = "v1.12.3" } alloy = { git = "https://github.com/scroll-tech/alloy.git", branch = "v0.3.0" } @@ -94,8 +94,9 @@ alloy-eips = { git = "https://github.com/scroll-tech/alloy.git", branch = "v0.3. alloy-eip2930 = { git = "https://github.com/scroll-tech/alloy-eips", branch = "v0.1.0" } alloy-eip7702 = { git = "https://github.com/scroll-tech/alloy-eips", branch = "v0.1.0" } -alloy-core = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.0"} -alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.0"} +alloy-core = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.0" } +alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.0" } +alloy-sol-types = {git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.0" } # for local development # [patch."https://github.com/scroll-tech/revm"] diff --git a/crates/core/src/chunk.rs b/crates/core/src/chunk.rs index ef875bc..0fb3578 100644 --- a/crates/core/src/chunk.rs +++ b/crates/core/src/chunk.rs @@ -31,6 +31,7 @@ impl ChunkInfo { let post_state_root = traces.last().expect("at least 1 block needed").root_after(); let withdraw_root = traces.last().unwrap().withdraw_root(); + cycle_tracker_start!("Keccak::v256"); let mut data_hasher = Keccak::v256(); for trace in traces.iter() { trace.hash_da_header(&mut data_hasher); @@ -40,14 +41,17 @@ impl ChunkInfo { } let mut data_hash = B256::ZERO; data_hasher.finalize(&mut data_hash.0); + cycle_tracker_end!("Keccak::v256"); let mut zktrie_db = NodeDb::new(HashMapDb::default()); + cycle_tracker_start!("Block::build_zktrie_db"); for trace in traces.iter() { measure_duration_millis!( build_zktrie_db_duration_milliseconds, trace.build_zktrie_db(&mut zktrie_db).unwrap() ); } + cycle_tracker_end!("Block::build_zktrie_db"); let info = ChunkInfo { chain_id, diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index b81b592..939f994 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -9,11 +9,7 @@ use alloy::{ primitives::{Bytes, ChainId, Signature, SignatureError, TxKind}, }; use std::fmt::Debug; -use zktrie_ng::{ - db::kv::KVDatabase, - hash::poseidon::Poseidon, - trie::{Node, MAGIC_NODE_BYTES}, -}; +use zktrie_ng::db::kv::KVDatabase; /// Predeployed contracts pub mod predeployed; @@ -27,8 +23,16 @@ pub use alloy::primitives::{Address, B256, U256}; pub use zktrie_ng as zk_trie; use zktrie_ng::db::NodeDb; +/// Node proof trait +pub trait NodeProof { + /// Import itself into zktrie db + fn import_node(&self, db: &mut NodeDb) -> Result<(), Db::Error>; +} + /// Blanket trait for block trace extensions. pub trait Block: Debug { + /// Node proof type + type Node: NodeProof; /// transaction type type Tx: TxTrace; @@ -76,25 +80,14 @@ pub trait Block: Debug { /// start l1 queue index fn start_l1_queue_index(&self) -> u64; - /// flatten proofs - fn flatten_proofs(&self) -> impl Iterator; + /// node proofs + fn node_proofs(&self) -> impl Iterator; /// Update zktrie state from trace #[inline] fn build_zktrie_db(&self, db: &mut NodeDb) -> Result<(), Db::Error> { - for bytes in self.flatten_proofs() { - if bytes == MAGIC_NODE_BYTES { - continue; - } - let node = cycle_track!(Node::::try_from(bytes), "Node::try_from") - .expect("invalid node"); - cycle_track!( - node.get_or_calculate_node_hash(), - "Node::get_or_calculate_node_hash" - ) - .expect("infallible"); - dev_trace!("put zktrie node: {:?}", node); - cycle_track!(db.put_node(&node)?, "NodeDb::put_node"); + for node in self.node_proofs() { + node.import_node(db)?; } Ok(()) } @@ -273,6 +266,7 @@ pub trait TxTrace { } impl Block for &T { + type Node = ::Node; type Tx = ::Tx; fn number(&self) -> u64 { @@ -339,8 +333,8 @@ impl Block for &T { (*self).start_l1_queue_index() } - fn flatten_proofs(&self) -> impl Iterator { - (*self).flatten_proofs() + fn node_proofs(&self) -> impl Iterator { + (*self).node_proofs() } } diff --git a/crates/primitives/src/types/mod.rs b/crates/primitives/src/types/mod.rs index 74bb09d..4f6fd73 100644 --- a/crates/primitives/src/types/mod.rs +++ b/crates/primitives/src/types/mod.rs @@ -1,11 +1,16 @@ -use crate::Block; +use crate::{Block, NodeProof}; use alloy::primitives::{Address, Bytes, B256, U256}; -use rkyv::Archive; -use serde::{Deserialize, Deserializer, Serialize}; +use rkyv::vec::ArchivedVec; +use rkyv::{rancor, Archive}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_with::{serde_as, Map}; use std::collections::{BTreeSet, HashMap}; use std::fmt::Debug; -use std::hash::Hash; +use std::hash::{Hash, Hasher}; +use zktrie_ng::db::kv::KVDatabase; +use zktrie_ng::db::NodeDb; +use zktrie_ng::hash::poseidon::Poseidon; +use zktrie_ng::trie::{ArchivedNode, Node, MAGIC_NODE_BYTES}; mod tx; pub use tx::{ArchivedTransactionTrace, TransactionTrace, TxL1Msg, TypedTransaction}; @@ -14,27 +19,34 @@ pub use tx::{ArchivedTransactionTrace, TransactionTrace, TxL1Msg, TypedTransacti #[derive( rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Serialize, Deserialize, Default, Debug, Clone, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] pub struct BlockHeader { /// block number + #[rkyv(attr(doc = "block number"))] pub number: U256, /// block hash + #[rkyv(attr(doc = "block hash"))] pub hash: B256, /// timestamp + #[rkyv(attr(doc = "timestamp"))] pub timestamp: U256, /// gas limit #[serde(rename = "gasLimit")] + #[rkyv(attr(doc = "gas limit"))] pub gas_limit: U256, /// gas used #[serde(rename = "gasUsed")] + #[rkyv(attr(doc = "gas used"))] pub gas_used: U256, /// base fee per gas #[serde(rename = "baseFeePerGas")] + #[rkyv(attr(doc = "base fee per gas"))] pub base_fee_per_gas: Option, /// difficulty + #[rkyv(attr(doc = "difficulty"))] pub difficulty: U256, /// mix hash + #[rkyv(attr(doc = "mix hash"))] #[serde(rename = "mixHash")] pub mix_hash: Option, } @@ -43,10 +55,10 @@ pub struct BlockHeader { #[derive( rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Serialize, Deserialize, Default, Debug, Clone, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] pub struct Coinbase { /// address of coinbase + #[rkyv(attr(doc = "address of coinbase"))] pub address: Address, } @@ -54,10 +66,10 @@ pub struct Coinbase { #[derive( rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Serialize, Deserialize, Default, Debug, Clone, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] pub struct BytecodeTrace { /// bytecode + #[rkyv(attr(doc = "bytecode"))] pub code: Bytes, } @@ -74,20 +86,33 @@ pub struct BytecodeTrace { Eq, PartialEq, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] -pub struct StorageTrace { +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] +pub struct StorageTrace +where + N: Archive, + Vec: Archive::Archived>>, + as Archive>::Archived: Debug + Hash + PartialEq + Eq, +{ /// root before + #[rkyv(attr(doc = "root before"))] #[serde(rename = "rootBefore")] pub root_before: B256, /// root after + #[rkyv(attr(doc = "root after"))] #[serde(rename = "rootAfter")] pub root_after: B256, /// proofs #[serde(rename = "flattenProofs")] - pub flatten_proofs: Vec, + #[rkyv(attr(doc = "proofs"))] + pub flatten_proofs: Vec, } +/// rkyv serialized node bytes +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default, Debug, Clone)] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] +#[repr(C, align(4))] +pub struct ArchivedNodeBytes(Vec); + /// legacy storage trace #[serde_as] #[derive( @@ -103,26 +128,30 @@ pub struct StorageTrace { Eq, PartialEq, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] #[allow(clippy::type_complexity)] pub struct LegacyStorageTrace { /// root before + #[rkyv(attr(doc = "root before"))] #[serde(rename = "rootBefore")] pub root_before: B256, /// root after + #[rkyv(attr(doc = "root after"))] #[serde(rename = "rootAfter")] pub root_after: B256, /// account proofs + #[rkyv(attr(doc = "account proofs"))] #[serde(default)] #[serde_as(as = "Map<_, _>")] pub proofs: Vec<(Address, Vec)>, + /// storage proofs for each account + #[rkyv(attr(doc = "storage proofs for each account"))] #[serde(rename = "storageProofs", default)] #[serde_as(as = "Map<_, Map<_, _>>")] - /// storage proofs for each account pub storage_proofs: Vec<(Address, Vec<(B256, Vec)>)>, - #[serde(rename = "deletionProofs", default)] /// additional deletion proofs + #[rkyv(attr(doc = "additional deletion proofs"))] + #[serde(rename = "deletionProofs", default)] pub deletion_proofs: Vec, } @@ -132,34 +161,65 @@ pub struct LegacyStorageTrace { #[derive( rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Serialize, Deserialize, Default, Debug, Clone, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] pub struct BlockTrace where S: Archive, ::Archived: Debug + Hash + PartialEq + Eq, { /// chain id + #[rkyv(attr(doc = "chain id"))] #[serde(rename = "chainID", default)] pub chain_id: u64, /// coinbase + #[rkyv(attr(doc = "coinbase"))] pub coinbase: Coinbase, /// block + #[rkyv(attr(doc = "block"))] pub header: BlockHeader, /// txs + #[rkyv(attr(doc = "txs"))] pub transactions: Vec, /// bytecodes + #[rkyv(attr(doc = "bytecodes"))] pub codes: Vec, /// storage trace BEFORE execution + #[rkyv(attr(doc = "storage trace BEFORE execution"))] #[serde(rename = "storageTrace")] pub storage_trace: S, /// l1 tx queue + #[rkyv(attr(doc = "l1 tx queue"))] #[serde(rename = "startL1QueueIndex", default)] pub start_l1_queue_index: u64, - /// Withdraw root + /// withdraw root + #[rkyv(attr(doc = "withdraw root"))] pub withdraw_trie_root: B256, } +impl Hash for ArchivedNodeBytes { + fn hash(&self, state: &mut H) { + state.write_usize(self.0.len()); + Hash::hash_slice(self.0.as_ref(), state) + } +} + +impl PartialEq for ArchivedNodeBytes { + fn eq(&self, other: &Self) -> bool { + self.0.as_slice().eq(other.0.as_slice()) + } +} + +impl Eq for ArchivedNodeBytes {} + +impl Serialize for ArchivedNodeBytes { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.as_slice().serialize(serializer) + } +} + impl<'de> Deserialize<'de> for StorageTrace { fn deserialize(deserializer: D) -> Result where @@ -196,6 +256,28 @@ impl<'de> Deserialize<'de> for StorageTrace { } } +impl From for StorageTrace { + fn from(trace: StorageTrace) -> Self { + StorageTrace { + root_before: trace.root_before, + root_after: trace.root_after, + flatten_proofs: trace + .flatten_proofs + .into_iter() + .filter(|proof| proof.as_ref() != MAGIC_NODE_BYTES) + .map(|proof| { + ArchivedNodeBytes( + Node::::try_from(proof.as_ref()) + .expect("invalid node") + .archived() + .to_vec(), + ) + }) + .collect(), + } + } +} + impl From for StorageTrace { fn from(trace: LegacyStorageTrace) -> Self { let mut flatten_proofs = BTreeSet::new(); @@ -217,6 +299,21 @@ impl From for StorageTrace { } } +impl From for BlockTrace> { + fn from(trace: BlockTrace) -> Self { + BlockTrace { + chain_id: trace.chain_id, + coinbase: trace.coinbase, + header: trace.header, + transactions: trace.transactions, + codes: trace.codes, + storage_trace: trace.storage_trace.into(), + start_l1_queue_index: trace.start_l1_queue_index, + withdraw_trie_root: trace.withdraw_trie_root, + } + } +} + impl From> for BlockTrace { fn from(trace: BlockTrace) -> Self { BlockTrace { @@ -232,7 +329,13 @@ impl From> for BlockTrace { } } -impl Block for BlockTrace { +impl Block for BlockTrace +where + S: StorageTraceExt + Archive + Debug, + ::Archived: Debug + Hash + PartialEq + Eq, + ::Node: NodeProof, +{ + type Node = S::Node; type Tx = TransactionTrace; fn number(&self) -> u64 { @@ -276,11 +379,11 @@ impl Block for BlockTrace { } fn root_before(&self) -> B256 { - self.storage_trace.root_before + self.storage_trace.root_before() } fn root_after(&self) -> B256 { - self.storage_trace.root_after + self.storage_trace.root_after() } fn withdraw_root(&self) -> B256 { @@ -295,52 +398,59 @@ impl Block for BlockTrace { self.start_l1_queue_index } - fn flatten_proofs(&self) -> impl Iterator { - self.storage_trace.flatten_proofs.iter().map(|v| v.as_ref()) + fn node_proofs(&self) -> impl Iterator { + self.storage_trace.node_proofs() } } -impl Block for ArchivedBlockTrace { +impl Block for ArchivedBlockTrace +where + S: Archive + Debug, + ::Archived: StorageTraceExt + Debug + Hash + PartialEq + Eq, + <::Archived as StorageTraceExt>::Node: NodeProof, +{ + type Node = <::Archived as StorageTraceExt>::Node; type Tx = ArchivedTransactionTrace; fn number(&self) -> u64 { - self.header.number.to() + let number: U256 = self.header.number.into(); + number.to() } fn block_hash(&self) -> B256 { - self.header.hash + self.header.hash.into() } fn chain_id(&self) -> u64 { - self.chain_id + self.chain_id.into() } fn coinbase(&self) -> Address { - self.coinbase.address + self.coinbase.address.into() } fn timestamp(&self) -> U256 { - self.header.timestamp + self.header.timestamp.into() } fn gas_limit(&self) -> U256 { - self.header.gas_limit + self.header.gas_limit.into() } fn gas_used(&self) -> U256 { - self.header.gas_used + self.header.gas_used.into() } fn base_fee_per_gas(&self) -> Option { - self.header.base_fee_per_gas.as_ref().copied() + self.header.base_fee_per_gas.as_ref().map(|p| p.into()) } fn difficulty(&self) -> U256 { - self.header.difficulty + self.header.difficulty.into() } fn prevrandao(&self) -> Option { - self.header.mix_hash.as_ref().copied() + self.header.mix_hash.as_ref().map(|p| p.into()) } fn transactions(&self) -> impl Iterator { @@ -348,15 +458,15 @@ impl Block for ArchivedBlockTrace { } fn root_before(&self) -> B256 { - self.storage_trace.root_before + self.storage_trace.root_before() } fn root_after(&self) -> B256 { - self.storage_trace.root_after + self.storage_trace.root_after() } fn withdraw_root(&self) -> B256 { - self.withdraw_trie_root + self.withdraw_trie_root.into() } fn codes(&self) -> impl ExactSizeIterator { @@ -364,11 +474,150 @@ impl Block for ArchivedBlockTrace { } fn start_l1_queue_index(&self) -> u64 { - self.start_l1_queue_index + self.start_l1_queue_index.into() } - fn flatten_proofs(&self) -> impl Iterator { - self.storage_trace.flatten_proofs.iter().map(|v| v.as_ref()) + fn node_proofs(&self) -> impl Iterator { + self.storage_trace.node_proofs() + } +} + +/// Extension trait for storage trace +pub trait StorageTraceExt { + /// Node type + type Node: Debug; + + /// Get root before + fn root_before(&self) -> B256; + + /// Get root after + fn root_after(&self) -> B256; + + /// Get node proofs + fn node_proofs(&self) -> impl Iterator; +} + +impl StorageTraceExt for StorageTrace +where + N: Archive + Debug, + Vec: Archive::Archived>>, + as Archive>::Archived: Debug + Hash + PartialEq + Eq, +{ + type Node = N; + + fn root_before(&self) -> B256 { + self.root_before + } + + fn root_after(&self) -> B256 { + self.root_after + } + + fn node_proofs(&self) -> impl Iterator { + self.flatten_proofs.iter() + } +} + +impl StorageTraceExt for ArchivedStorageTrace +where + N: Archive + Debug, + ::Archived: Debug, + Vec: Archive::Archived>>, + as Archive>::Archived: Debug + Hash + PartialEq + Eq, +{ + type Node = ::Archived; + + fn root_before(&self) -> B256 { + self.root_before.into() + } + + fn root_after(&self) -> B256 { + self.root_after.into() + } + + fn node_proofs(&self) -> impl Iterator { + self.flatten_proofs.as_ref().iter() + } +} + +impl StorageTraceExt for LegacyStorageTrace { + type Node = Bytes; + + fn root_before(&self) -> B256 { + self.root_before + } + + fn root_after(&self) -> B256 { + self.root_after + } + + fn node_proofs(&self) -> impl Iterator { + self.proofs.iter().flat_map(|(_, proofs)| proofs.iter()) + } +} + +fn import_serialized_node, Db: KVDatabase>( + node: N, + db: &mut NodeDb, +) -> Result<(), Db::Error> { + let bytes = node.as_ref(); + if bytes == MAGIC_NODE_BYTES { + return Ok(()); + } + let node = + cycle_track!(Node::::try_from(bytes), "Node::try_from").expect("invalid node"); + cycle_track!( + node.get_or_calculate_node_hash(), + "Node::get_or_calculate_node_hash" + ) + .expect("infallible"); + dev_trace!("put zktrie node: {:?}", node); + cycle_track!(db.put_node(node), "NodeDb::put_node") +} + +fn import_archived_node, Db: KVDatabase>( + node: N, + db: &mut NodeDb, +) -> Result<(), Db::Error> { + let bytes = node.as_ref(); + let node = cycle_track!( + rkyv::access::(bytes), + "rkyv::access" + ) + .expect("invalid node"); + let node_hash = cycle_track!( + node.calculate_node_hash::(), + "Node::calculate_node_hash" + ) + .expect("infallible"); + dev_trace!("put zktrie node: {:?}", node); + cycle_track!( + unsafe { db.put_archived_node_unchecked(node_hash, bytes.to_owned()) }, + "NodeDb::put_archived_node_unchecked" + ) +} + +impl NodeProof for Bytes { + fn import_node(&self, db: &mut NodeDb) -> Result<(), Db::Error> { + import_serialized_node(self, db) + } +} + +impl NodeProof for ArchivedVec { + fn import_node(&self, db: &mut NodeDb) -> Result<(), Db::Error> { + import_serialized_node(self, db) + } +} + +impl NodeProof for ArchivedNodeBytes { + fn import_node(&self, db: &mut NodeDb) -> Result<(), Db::Error> { + import_archived_node(&self.0, db) + } +} + +impl NodeProof for ArchivedArchivedNodeBytes { + fn import_node(&self, db: &mut NodeDb) -> Result<(), Db::Error> { + import_archived_node(&self.0, db) } } @@ -466,23 +715,42 @@ mod tests { fn test_rkyv() { let trace = serde_json::from_str::(TRACE).unwrap()["result"].clone(); let block: BlockTrace = serde_json::from_value(trace).unwrap(); - let archived_bytes = rkyv::to_bytes::<_, 4096>(&block).unwrap(); + let archived_bytes = rkyv::to_bytes::(&block).unwrap(); let archived_block = - rkyv::check_archived_root::(archived_bytes.as_ref()).unwrap(); + rkyv::access::(archived_bytes.as_ref()).unwrap(); assert_eq!(block.chain_id, archived_block.chain_id); - assert_eq!(block.coinbase.address, archived_block.coinbase.address); + assert_eq!( + block.coinbase.address, + Address::from(archived_block.coinbase.address) + ); - assert_eq!(block.header.number, archived_block.header.number); - assert_eq!(block.header.hash, archived_block.header.hash); - assert_eq!(block.header.timestamp, archived_block.header.timestamp); - assert_eq!(block.header.gas_limit, archived_block.header.gas_limit); + assert_eq!(block.header.number, archived_block.header.number.into()); + assert_eq!(block.header.hash, B256::from(archived_block.header.hash)); + assert_eq!( + block.header.timestamp, + archived_block.header.timestamp.into() + ); + assert_eq!( + block.header.gas_limit, + archived_block.header.gas_limit.into() + ); assert_eq!( block.header.base_fee_per_gas, - archived_block.header.base_fee_per_gas + archived_block + .header + .base_fee_per_gas + .as_ref() + .map(|p| p.into()) + ); + assert_eq!( + block.header.difficulty, + archived_block.header.difficulty.into() + ); + assert_eq!( + block.header.mix_hash, + archived_block.header.mix_hash.as_ref().map(|p| p.into()) ); - assert_eq!(block.header.difficulty, archived_block.header.difficulty); - assert_eq!(block.header.mix_hash, archived_block.header.mix_hash); let txs = block .transactions @@ -502,11 +770,11 @@ mod tests { assert_eq!( block.storage_trace.root_before, - archived_block.storage_trace.root_before + B256::from(archived_block.storage_trace.root_before) ); assert_eq!( block.storage_trace.root_after, - archived_block.storage_trace.root_after + B256::from(archived_block.storage_trace.root_after) ); for (proof, archived_proof) in block .storage_trace diff --git a/crates/primitives/src/types/tx.rs b/crates/primitives/src/types/tx.rs index 443d59e..3220eb8 100644 --- a/crates/primitives/src/types/tx.rs +++ b/crates/primitives/src/types/tx.rs @@ -6,6 +6,7 @@ use alloy::{ primitives::{Address, Bytes, ChainId, Signature, SignatureError, TxKind, B256, U256, U64}, rlp::{BufMut, BytesMut, Encodable, Header}, }; +use rkyv::rancor; use serde_with::{serde_as, DefaultOnNull}; /// Wrapped Ethereum Transaction @@ -60,51 +61,68 @@ pub struct TxL1Msg { Debug, Clone, )] -#[archive(check_bytes)] -#[archive_attr(derive(Debug, Hash, PartialEq, Eq))] +#[rkyv(attr(doc = "Archived `TransactionTrace`"))] +#[rkyv(derive(Debug, Hash, PartialEq, Eq))] pub struct TransactionTrace { /// tx hash + #[rkyv(attr(doc = "tx hash"))] #[serde(default, rename = "txHash")] pub tx_hash: B256, /// tx type (in raw from) + #[rkyv(attr(doc = "tx type (in raw from)"))] #[serde(rename = "type")] pub ty: u8, /// nonce + #[rkyv(attr(doc = "nonce"))] pub nonce: u64, /// gas limit + #[rkyv(attr(doc = "gas limit"))] pub gas: u64, - #[serde(rename = "gasPrice")] /// gas price + #[rkyv(attr(doc = "gas price"))] + #[serde(rename = "gasPrice")] pub gas_price: U256, - #[serde(rename = "gasTipCap")] /// gas tip cap + #[rkyv(attr(doc = "gas tip cap"))] + #[serde(rename = "gasTipCap")] pub gas_tip_cap: Option, - #[serde(rename = "gasFeeCap")] /// gas fee cap + #[rkyv(attr(doc = "gas fee cap"))] + #[serde(rename = "gasFeeCap")] pub gas_fee_cap: Option, /// from + #[rkyv(attr(doc = "from"))] pub from: Address, /// to, NONE for creation (0 addr) + #[rkyv(attr(doc = "to, NONE for creation (0 addr)"))] pub to: Option
, /// chain id + #[rkyv(attr(doc = "chain id"))] #[serde(rename = "chainId")] pub chain_id: U64, /// value amount + #[rkyv(attr(doc = "value amount"))] pub value: U256, /// call data + #[rkyv(attr(doc = "call data"))] pub data: Bytes, /// is creation + #[rkyv(attr(doc = "is creation"))] #[serde(rename = "isCreate")] pub is_create: bool, /// access list + #[rkyv(attr(doc = "access list"))] #[serde(rename = "accessList")] #[serde_as(as = "DefaultOnNull")] pub access_list: AccessList, /// signature v + #[rkyv(attr(doc = "signature v"))] pub v: U64, /// signature r + #[rkyv(attr(doc = "signature r"))] pub r: U256, /// signature s + #[rkyv(attr(doc = "signature s"))] pub s: U256, } @@ -173,7 +191,7 @@ impl TxTrace for TransactionTrace { impl TxTrace for ArchivedTransactionTrace { fn tx_hash(&self) -> B256 { - self.tx_hash + self.tx_hash.into() } fn ty(&self) -> u8 { @@ -181,50 +199,59 @@ impl TxTrace for ArchivedTransactionTrace { } fn nonce(&self) -> u64 { - self.nonce + self.nonce.into() } fn gas_limit(&self) -> u128 { - self.gas as u128 + u64::from(self.gas) as u128 } fn gas_price(&self) -> u128 { - self.gas_price.to() + let gas_price: U256 = self.gas_price.into(); + gas_price.to() } fn max_fee_per_gas(&self) -> u128 { self.gas_fee_cap .as_ref() - .map(|v| v.to()) + .map(|g| { + let gas_fee_cap: U256 = g.into(); + gas_fee_cap.to() + }) .unwrap_or_default() } fn max_priority_fee_per_gas(&self) -> u128 { self.gas_tip_cap .as_ref() - .map(|v| v.to()) + .map(|g| { + let gas_tip_cap: U256 = g.into(); + gas_tip_cap.to() + }) .unwrap_or_default() } unsafe fn get_from_unchecked(&self) -> Address { - self.from + self.from.into() } fn to(&self) -> TxKind { if self.is_create { TxKind::Create } else { - debug_assert!(self.to.as_ref().map(|a| !a.is_zero()).unwrap_or(false)); - TxKind::Call(*self.to.as_ref().expect("to address must be present")) + let to: Address = self.to.as_ref().expect("to address must be present").into(); + debug_assert!(!to.is_zero()); + TxKind::Call(to) } } fn chain_id(&self) -> ChainId { - self.chain_id.to() + let chain_id: U64 = self.chain_id.into(); + chain_id.to() } fn value(&self) -> U256 { - self.value + self.value.into() } fn data(&self) -> Bytes { @@ -232,12 +259,12 @@ impl TxTrace for ArchivedTransactionTrace { } fn access_list(&self) -> AccessList { - rkyv::Deserialize::::deserialize(&self.access_list, &mut rkyv::Infallible) - .unwrap() + rkyv::deserialize::<_, rancor::Error>(&self.access_list).unwrap() } fn signature(&self) -> Result { - Signature::from_rs_and_parity(self.r, self.s, self.v) + let v: U64 = self.v.into(); + Signature::from_rs_and_parity(self.r.into(), self.s.into(), v) } } From a6f3612a755b8098dda6c49fd554f26f4f8ddf1d Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Mon, 14 Oct 2024 14:59:43 +0800 Subject: [PATCH 13/14] fix: build typed tx (#62) --- crates/primitives/src/lib.rs | 9 ++++++++- crates/primitives/src/types/tx.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 939f994..68a682b 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -190,6 +190,9 @@ pub trait TxTrace { /// Get `access_list`. fn access_list(&self) -> AccessList; + /// Get `v`. + fn v(&self) -> u64; + /// Get `signature`. fn signature(&self) -> Result; @@ -205,7 +208,7 @@ pub trait TxTrace { let tx = match self.ty() { 0x0 => { let tx = TxLegacy { - chain_id: if chain_id >= 35 { Some(chain_id) } else { None }, + chain_id: if self.v() >= 35 { Some(chain_id) } else { None }, nonce: self.nonce(), gas_price: self.gas_price(), gas_limit: self.gas_limit(), @@ -391,6 +394,10 @@ impl TxTrace for &T { (*self).access_list() } + fn v(&self) -> u64 { + (*self).v() + } + fn signature(&self) -> Result { (*self).signature() } diff --git a/crates/primitives/src/types/tx.rs b/crates/primitives/src/types/tx.rs index 3220eb8..5b8c372 100644 --- a/crates/primitives/src/types/tx.rs +++ b/crates/primitives/src/types/tx.rs @@ -184,6 +184,10 @@ impl TxTrace for TransactionTrace { self.access_list.clone() } + fn v(&self) -> u64 { + self.v.to() + } + fn signature(&self) -> Result { Signature::from_rs_and_parity(self.r, self.s, self.v) } @@ -262,6 +266,11 @@ impl TxTrace for ArchivedTransactionTrace { rkyv::deserialize::<_, rancor::Error>(&self.access_list).unwrap() } + fn v(&self) -> u64 { + let v: U64 = self.v.into(); + v.to() + } + fn signature(&self) -> Result { let v: U64 = self.v.into(); Signature::from_rs_and_parity(self.r.into(), self.s.into(), v) From d2220caf27d3fffe88201f23b0434cc2e8c7eda9 Mon Sep 17 00:00:00 2001 From: Akase Haruka Date: Thu, 17 Oct 2024 12:53:49 +0800 Subject: [PATCH 14/14] fix: handle missing proofs (#65) * fix * bump MSRV * remove clear storage fix --- Cargo.lock | 22 ++++++++---- Cargo.toml | 2 +- crates/bin/src/commands/run_file.rs | 54 +++++++++++++++++++++-------- crates/core/src/database.rs | 13 +++++-- crates/core/src/error.rs | 3 ++ crates/core/src/executor/mod.rs | 1 + crates/utils/src/macros.rs | 10 +++--- 7 files changed, 75 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b56661a..d11bcc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -839,12 +839,13 @@ dependencies = [ [[package]] name = "bn254" version = "0.1.0" -source = "git+https://github.com/scroll-tech/bn254#3653980f712ad44d4e079fa8f0199c5678104ef4" +source = "git+https://github.com/scroll-tech/bn254.git?branch=master#db67681f5e9ae1736565a48ec294a6d0dacb4e0d" dependencies = [ "ff", "getrandom", "rand", "rand_core", + "sp1-intrinsics", "subtle", ] @@ -2468,10 +2469,11 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "poseidon-bn254" version = "0.1.0" -source = "git+https://github.com/scroll-tech/poseidon-bn254?branch=master#526a64a81419bcab6bd8280a8a3f9808189e0373" +source = "git+https://github.com/scroll-tech/poseidon-bn254?branch=master#a96ba028dd00987551451d8f4bd65b7ea5469a44" dependencies = [ "bn254", "itertools 0.13.0", + "sp1-intrinsics", ] [[package]] @@ -2812,7 +2814,7 @@ dependencies = [ [[package]] name = "revm" version = "14.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#df2fc95185c1f24c5d3ebabb0067a864d8cfc754" dependencies = [ "auto_impl", "cfg-if", @@ -2826,7 +2828,7 @@ dependencies = [ [[package]] name = "revm-interpreter" version = "10.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#df2fc95185c1f24c5d3ebabb0067a864d8cfc754" dependencies = [ "cfg-if", "revm-primitives", @@ -2836,7 +2838,7 @@ dependencies = [ [[package]] name = "revm-precompile" version = "11.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#df2fc95185c1f24c5d3ebabb0067a864d8cfc754" dependencies = [ "aurora-engine-modexp", "c-kzg", @@ -2853,7 +2855,7 @@ dependencies = [ [[package]] name = "revm-primitives" version = "9.0.0" -source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#f7f0745a46b16097a274c2def9d1334cbdd9da26" +source = "git+https://github.com/scroll-tech/revm?branch=scroll-evm-executor/v42#df2fc95185c1f24c5d3ebabb0067a864d8cfc754" dependencies = [ "alloy-eips", "alloy-primitives", @@ -3405,6 +3407,14 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "sp1-intrinsics" +version = "0.0.0" +source = "git+https://github.com/scroll-tech/sp1-intrinsics.git?branch=master#7e038e60db0b2e847f6d8f49e148ccac8c6fc394" +dependencies = [ + "cfg-if", +] + [[package]] name = "spin" version = "0.9.8" diff --git a/Cargo.toml b/Cargo.toml index a5839db..6998911 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/bin/src/commands/run_file.rs b/crates/bin/src/commands/run_file.rs index 7ea23a1..114ac2f 100644 --- a/crates/bin/src/commands/run_file.rs +++ b/crates/bin/src/commands/run_file.rs @@ -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; @@ -43,7 +45,7 @@ impl RunFileCommand { while let Some(task) = tasks.join_next().await { if let Err(err) = task? { - bail!("{:?}", err); + dev_error!("{:?}", err); } } @@ -114,19 +116,21 @@ async fn read_block_trace(path: &PathBuf) -> anyhow::Result { } fn deserialize_block_trace(trace: &str) -> anyhow::Result { + // 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::(trace).or_else(|_| { - #[derive(serde::Deserialize, Default, Debug, Clone)] - pub struct BlockTraceJsonRpcResult { - pub result: BlockTrace, - } - Ok::<_, serde_json::Error>( - serde_json::from_str::(trace)?.result, - ) - })?, + serde_json::from_str::>(trace) + .or_else(|_| { + #[derive(serde::Deserialize, Default, Debug, Clone)] + pub struct BlockTraceJsonRpcResult { + pub result: BlockTrace, + } + Ok::<_, serde_json::Error>( + serde_json::from_str::(trace)?.result, + ) + })? + .into(), ) } @@ -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::() + .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(()) } diff --git a/crates/core/src/database.rs b/crates/core/src/database.rs index 7256fa7..32cce45 100644 --- a/crates/core/src/database.rs +++ b/crates/core/src/database.rs @@ -26,7 +26,7 @@ pub struct EvmDatabase<'a, CodeDb, ZkDb> { storage_root_caches: RefCell>, /// 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>>, + storage_trie_caches: RefCell>>>, /// Current uncommitted zkTrie root based on the block trace. committed_zktrie_root: B256, /// The underlying zkTrie database. @@ -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. @@ -207,8 +207,15 @@ impl 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)] { diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 174f260..7406a35 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -10,6 +10,9 @@ pub enum DatabaseError { /// Error encountered from zkTrie. #[error("error encountered from zkTrie: {0}")] ZkTrie(Box), + /// 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. diff --git a/crates/core/src/executor/mod.rs b/crates/core/src/executor/mod.rs index 562fb37..838fb4f 100644 --- a/crates/core/src/executor/mod.rs +++ b/crates/core/src/executor/mod.rs @@ -285,6 +285,7 @@ impl 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!( diff --git a/crates/utils/src/macros.rs b/crates/utils/src/macros.rs index f8f1af1..c3b21d2 100644 --- a/crates/utils/src/macros.rs +++ b/crates/utils/src/macros.rs @@ -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)*); } }; @@ -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)*); } }; @@ -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)*); } }; @@ -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)*); } }; @@ -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)*); } };