From 4db375abcce01c6900ebe062b1489fa460cf65a7 Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Wed, 25 Sep 2024 14:22:40 +0200 Subject: [PATCH] fix: improvements --- evm_arithmetization/src/generation/mod.rs | 6 +- mpt_trie/src/debug_tools/diff.rs | 226 ++++++++++++++++++---- zero/src/bin/trie_diff.rs | 8 + 3 files changed, 194 insertions(+), 46 deletions(-) diff --git a/evm_arithmetization/src/generation/mod.rs b/evm_arithmetization/src/generation/mod.rs index 9279e88d9..14cb4b9ac 100644 --- a/evm_arithmetization/src/generation/mod.rs +++ b/evm_arithmetization/src/generation/mod.rs @@ -520,13 +520,11 @@ pub fn generate_traces, const D: usize>( let registers_after: RegistersData = RegistersData::from(*registers_after); apply_metadata_and_tries_memops(&mut state, inputs, ®isters_before, ®isters_after); - let cpu_res = timed!( + timed!( timing, "simulate CPU", simulate_cpu(&mut state, *max_cpu_len_log) - ); - - cpu_res?; + )?; let trace_lengths = state.traces.get_lengths(); diff --git a/mpt_trie/src/debug_tools/diff.rs b/mpt_trie/src/debug_tools/diff.rs index c08cb80d1..96a7791c8 100644 --- a/mpt_trie/src/debug_tools/diff.rs +++ b/mpt_trie/src/debug_tools/diff.rs @@ -147,7 +147,7 @@ pub struct NodeInfo { impl Display for NodeInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "NodeInfo {{ Key: 0x{:x}, ", self.key)?; + write!(f, "NodeInfo {{ Key: {:x}, ", self.key)?; match &self.value { Some(v) => write!(f, "Value: 0x{}, ", hex::encode(v))?, @@ -458,6 +458,8 @@ mod tests { use rlp_derive::{RlpDecodable, RlpEncodable}; use super::create_full_diff_between_tries; + use crate::trie_ops::ValOrHash; + use crate::utils::TryFromIterator; use crate::{ debug_tools::diff::{DiffPoint, NodeInfo}, nibbles::Nibbles, @@ -466,10 +468,17 @@ mod tests { utils::{TrieNodeType, TriePath}, }; + fn create_trie(data: impl IntoIterator) -> TrieOpResult + where + K: Into, + V: Into, + { + HashedPartialTrie::try_from_iter(data) + } + #[test] - fn depth_single_node_hash_diffs_work() -> TrieOpResult<()> { - let mut a = HashedPartialTrie::default(); - a.insert(0x1234, vec![0])?; + fn single_node_diff_works() -> Result<(), Box> { + let a = create_trie(vec![(0x1234, vec![0])])?; let a_hash = a.hash(); let mut b = a.clone(); @@ -505,7 +514,177 @@ mod tests { } #[test] - fn depth_multi_node_diff_works() -> std::result::Result<(), Box> { + fn singe_node_diff_works_2() -> Result<(), Box> { + let a = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1112")?, 0x02u8), + (Nibbles::from_str("0x3333")?, 0x03u8), + (Nibbles::from_str("0x4444")?, 0x04u8), + ])?; + + let b = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1112")?, 0x03u8), + (Nibbles::from_str("0x3333")?, 0x03u8), + (Nibbles::from_str("0x4444")?, 0x04u8), + ])?; + + let diff = create_full_diff_between_tries(&a, &b); + + assert_eq!(diff.diff_points.len(), 1); + assert_eq!(diff.diff_points[0].a_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[0].a_info.key, Nibbles::from_str("0x1112")?); + assert_eq!(diff.diff_points[0].a_info.value, Some(vec![0x02u8])); + assert_eq!(diff.diff_points[0].b_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[0].b_info.key, Nibbles::from_str("0x1112")?); + assert_eq!(diff.diff_points[0].b_info.value, Some(vec![0x03u8])); + + Ok(()) + } + + #[test] + fn singe_node_diff_works_3() -> Result<(), Box> { + let a = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1122")?, 0x02u8), + (Nibbles::from_str("0x3333")?, 0x03u8), + (Nibbles::from_str("0x4444")?, 0x04u8), + ])?; + + let mut b = a.clone(); + b.insert(Nibbles::from_str("0x3334")?, 0x05u8)?; + + let diff = create_full_diff_between_tries(&a, &b); + + assert_eq!(diff.diff_points.len(), 1); + assert_eq!(diff.diff_points[0].a_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[0].a_info.key, Nibbles::from_str("0x3333")?); + assert_eq!( + diff.diff_points[0].b_info.node_type, + TrieNodeType::Extension + ); + assert_eq!(diff.diff_points[0].b_info.key, Nibbles::from_str("0x333")?); + + Ok(()) + } + + #[test] + fn multi_node_diff_works() -> Result<(), Box> { + let a = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1122")?, 0x02u8), + (Nibbles::from_str("0x3333")?, 0x03u8), + (Nibbles::from_str("0x4444")?, 0x04u8), + ])?; + + let mut b = a.clone(); + b.insert(Nibbles::from_str("0x1113")?, 0x05u8)?; + b.insert(Nibbles::from_str("0x3334")?, 0x06u8)?; + + let diff = create_full_diff_between_tries(&a, &b); + println!("Diff is: {}", diff); + assert_eq!(diff.diff_points.len(), 2); + assert_eq!(diff.diff_points[0].a_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[0].a_info.key, Nibbles::from_str("0x1111")?); + assert_eq!(diff.diff_points[0].b_info.node_type, TrieNodeType::Branch); + assert_eq!(diff.diff_points[0].b_info.key, Nibbles::from_str("0x111")?); + + assert_eq!(diff.diff_points[1].a_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[1].a_info.key, Nibbles::from_str("0x3333")?); + assert_eq!( + diff.diff_points[1].b_info.node_type, + TrieNodeType::Extension + ); + assert_eq!(diff.diff_points[1].b_info.key, Nibbles::from_str("0x333")?); + + Ok(()) + } + + #[test] + fn multi_node_diff_works_2() -> Result<(), Box> { + let a = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1122")?, 0x02u8), + (Nibbles::from_str("0x3333")?, 0x03u8), + (Nibbles::from_str("0x4444")?, 0x04u8), + ])?; + + let b = create_trie(vec![ + (Nibbles::from_str("0x1112")?, 0x01u8), + (Nibbles::from_str("0x1123")?, 0x02u8), + (Nibbles::from_str("0x3334")?, 0x03u8), + (Nibbles::from_str("0x4445")?, 0x04u8), + ])?; + + let diff = create_full_diff_between_tries(&a, &b); + println!("Diff is: {}", diff); + assert_eq!(diff.diff_points.len(), 4); + assert_eq!(diff.diff_points[0].a_info.key, Nibbles::from_str("0x1111")?); + assert_eq!(diff.diff_points[0].b_info.key, Nibbles::from_str("0x1112")?); + assert_eq!(diff.diff_points[1].a_info.key, Nibbles::from_str("0x1122")?); + assert_eq!(diff.diff_points[1].b_info.key, Nibbles::from_str("0x1123")?); + assert_eq!(diff.diff_points[2].a_info.key, Nibbles::from_str("0x3333")?); + assert_eq!(diff.diff_points[2].b_info.key, Nibbles::from_str("0x3334")?); + assert_eq!(diff.diff_points[3].a_info.key, Nibbles::from_str("0x4444")?); + assert_eq!(diff.diff_points[3].b_info.key, Nibbles::from_str("0x4445")?); + + Ok(()) + } + + #[test] + fn multi_node_diff_works_3() -> Result<(), Box> { + let a = create_trie(vec![ + (Nibbles::from_str("0x1111")?, 0x01u8), + (Nibbles::from_str("0x1112")?, 0x02u8), + (Nibbles::from_str("0x1113")?, 0x03u8), + (Nibbles::from_str("0x1114")?, 0x04u8), + (Nibbles::from_str("0x2221")?, 0x06u8), + (Nibbles::from_str("0x2222")?, 0x07u8), + (Nibbles::from_str("0x2223")?, 0x08u8), + (Nibbles::from_str("0x2224")?, 0x09u8), + ])?; + + let b = create_trie(vec![ + (Nibbles::from_str("0x1114")?, 0x04u8), + (Nibbles::from_str("0x1115")?, 0x06u8), + (Nibbles::from_str("0x1116")?, 0x07u8), + (Nibbles::from_str("0x1117")?, 0x08u8), + (Nibbles::from_str("0x2224")?, 0x09u8), + (Nibbles::from_str("0x2225")?, 0x07u8), + (Nibbles::from_str("0x2226")?, 0x08u8), + (Nibbles::from_str("0x2227")?, 0x09u8), + ])?; + + let diff = create_full_diff_between_tries(&a, &b); + assert_eq!(diff.diff_points.len(), 12); + + assert_eq!(diff.diff_points[0].a_info.key, Nibbles::from_str("0x1111")?); + assert_eq!(diff.diff_points[0].a_info.node_type, TrieNodeType::Leaf); + assert_eq!(diff.diff_points[0].b_info.key, Nibbles::from_str("0x1111")?); + assert_eq!(diff.diff_points[0].b_info.node_type, TrieNodeType::Empty); + + assert_eq!(diff.diff_points[4].a_info.key, Nibbles::from_str("0x1116")?); + assert_eq!(diff.diff_points[4].a_info.node_type, TrieNodeType::Empty); + assert_eq!(diff.diff_points[4].b_info.key, Nibbles::from_str("0x1116")?); + assert_eq!(diff.diff_points[4].b_info.node_type, TrieNodeType::Leaf); + + assert_eq!( + diff.diff_points[11].a_info.key, + Nibbles::from_str("0x2227")? + ); + assert_eq!(diff.diff_points[11].a_info.node_type, TrieNodeType::Empty); + assert_eq!( + diff.diff_points[11].b_info.key, + Nibbles::from_str("0x2227")? + ); + assert_eq!(diff.diff_points[11].b_info.node_type, TrieNodeType::Leaf); + + Ok(()) + } + + #[test] + /// Do one real world test where we change the values of the accounts. + fn multi_node_diff_works_accounts() -> Result<(), Box> { use ethereum_types::{H256, U256}; use keccak_hash::keccak; #[derive( @@ -582,41 +761,4 @@ mod tests { Ok(()) } - - // TODO: Will finish these tests later (low-priority). - #[test] - #[ignore] - fn depth_multi_node_single_node_hash_diffs_work() { - todo!() - } - - #[test] - #[ignore] - fn depth_multi_node_single_node_node_diffs_work() { - todo!() - } - - #[test] - #[ignore] - fn depth_massive_single_node_diff_tests() { - todo!() - } - - #[test] - #[ignore] - fn depth_multi_node_multi_node_hash_diffs_work() { - todo!() - } - - #[test] - #[ignore] - fn depth_multi_node_multi_node_node_diffs_work() { - todo!() - } - - #[test] - #[ignore] - fn depth_massive_multi_node_diff_tests() { - todo!() - } } diff --git a/zero/src/bin/trie_diff.rs b/zero/src/bin/trie_diff.rs index 312feceb1..4c00d2ca3 100644 --- a/zero/src/bin/trie_diff.rs +++ b/zero/src/bin/trie_diff.rs @@ -28,9 +28,17 @@ use tracing::{error, info}; use zero::ops::register; use zero::prover::{cli::CliProverConfig, BlockProverInput, ProverConfig}; +/// This binary is a debugging tool used to compare +/// the trace decoder output tries and the post kernel execution tries (state, +/// transaction and receipt). +/// +/// Usage: +/// +/// `trie_diff < ./witness_json_input.json` #[derive(Parser)] #[command(version = zero::version(), propagate_version = true)] pub(crate) struct Cli { + /// Prover configuration #[clap(flatten)] pub(crate) prover_config: CliProverConfig,