From dd31b7bf36b1802d623ba7bb34e6e7f5a30a6d15 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 19 Nov 2024 11:37:39 +0800 Subject: [PATCH] fix conv --- Cargo.lock | 1 + crates/primitives/src/types/mod.rs | 45 ++++++++++++++++++------------ crates/stateful/Cargo.toml | 1 + crates/stateful/src/bin/tracer.rs | 8 +++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5a4a3af..3f0a58a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3646,6 +3646,7 @@ dependencies = [ "bincode", "clap", "revm", + "rkyv", "sbv", "serde", "serde_json", diff --git a/crates/primitives/src/types/mod.rs b/crates/primitives/src/types/mod.rs index 5249eeeb..a25ce2a5 100644 --- a/crates/primitives/src/types/mod.rs +++ b/crates/primitives/src/types/mod.rs @@ -320,22 +320,27 @@ impl<'de> Deserialize<'de> for StorageTrace { } } -impl From for StorageTrace { - fn from(trace: StorageTrace) -> Self { +impl StorageTrace { + /// turn the proofs to archived + fn to_archived(self, hash_scheme: HashSchemeKind) -> StorageTrace { StorageTrace { - root_before: trace.root_before, - root_after: trace.root_after, - flatten_proofs: trace + root_before: self.root_before, + root_after: self.root_after, + flatten_proofs: self .flatten_proofs .into_iter() .filter(|proof| proof.as_ref() != MAGIC_NODE_BYTES) .map(|proof| { - ArchivedNodeBytes( - Node::::try_from(proof.as_ref()) + ArchivedNodeBytes(match hash_scheme { + HashSchemeKind::Poseidon => Node::::try_from(proof.as_ref()) + .expect("invalid node") + .archived() + .to_vec(), + HashSchemeKind::Keccak => Node::::try_from(proof.as_ref()) .expect("invalid node") .archived() .to_vec(), - ) + }) }) .collect(), } @@ -363,17 +368,21 @@ impl From for StorageTrace { } } -impl From for BlockTrace> { - fn from(trace: BlockTrace) -> Self { +impl BlockTrace { + /// turn the proofs to archived + pub fn to_archived_nodes( + self: BlockTrace, + hash_scheme: HashSchemeKind, + ) -> BlockTrace> { 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, + chain_id: self.chain_id, + coinbase: self.coinbase, + header: self.header, + transactions: self.transactions, + codes: self.codes, + storage_trace: self.storage_trace.to_archived(hash_scheme), + start_l1_queue_index: self.start_l1_queue_index, + withdraw_trie_root: self.withdraw_trie_root, } } } diff --git a/crates/stateful/Cargo.toml b/crates/stateful/Cargo.toml index 45f2950e..18dc0ad0 100644 --- a/crates/stateful/Cargo.toml +++ b/crates/stateful/Cargo.toml @@ -23,6 +23,7 @@ anyhow.workspace = true clap = { workspace = true, features = ["derive"] } bincode.workspace = true revm.workspace = true +rkyv.workspace = true sled.workspace = true serde = { workspace = true, features = ["derive"] } serde_json.workspace = true diff --git a/crates/stateful/src/bin/tracer.rs b/crates/stateful/src/bin/tracer.rs index 91788256..634eb74a 100644 --- a/crates/stateful/src/bin/tracer.rs +++ b/crates/stateful/src/bin/tracer.rs @@ -1,6 +1,8 @@ //! trace dumper use clap::Parser; +use rkyv::rancor; use sbv::core::{EvmExecutorBuilder, HardforkConfig}; +use sbv::primitives::alloy_primitives::Bytes; use sbv::primitives::types::{BlockTrace, BytecodeTrace, StorageTrace}; use stateful_block_verifier::Metadata; use std::path::PathBuf; @@ -9,6 +11,7 @@ use zktrie_ng::db::NodeDb; use zktrie_ng::hash::keccak::Keccak; use zktrie_ng::hash::poseidon::Poseidon; use zktrie_ng::hash::HashSchemeKind; +use zktrie_ng::trie::ArchivedNode; #[derive(Parser)] struct Cli { @@ -103,7 +106,10 @@ fn main() -> anyhow::Result<()> { .into_inner() .take_read_items() .into_iter() - .map(|(_, v)| v.into()) + .map(|(_, v)| { + let node = rkyv::access::(v.as_ref()).unwrap(); + Bytes::from(node.canonical_value(false)) + }) .collect(), }, &block,