From 67dbf7a8003b43e09bc416b21ae71371e678416b Mon Sep 17 00:00:00 2001 From: 0xaatif <169152398+0xaatif@users.noreply.github.com> Date: Mon, 19 Aug 2024 23:34:35 +0100 Subject: [PATCH] refactor: remove dead `client_code_hash_resolve_f` (#514) * mark: 0xaatif/no-code-hash-resolve-fn * refactor: remove dead code_hash_resolve_fn * fix: feature-gated code >:( --- trace_decoder/benches/block_processing.rs | 4 +- trace_decoder/src/lib.rs | 10 ++--- trace_decoder/src/processed_block_trace.rs | 51 +++++++++++----------- trace_decoder/tests/trace_decoder_tests.rs | 1 - zero_bin/prover/src/lib.rs | 6 +-- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/trace_decoder/benches/block_processing.rs b/trace_decoder/benches/block_processing.rs index 784b487db..d2862d11f 100644 --- a/trace_decoder/benches/block_processing.rs +++ b/trace_decoder/benches/block_processing.rs @@ -25,9 +25,7 @@ fn criterion_benchmark(c: &mut Criterion) { |ProverInput { block_trace, other_data, - }| { - trace_decoder::entrypoint(block_trace, other_data, |_| unimplemented!()).unwrap() - }, + }| { trace_decoder::entrypoint(block_trace, other_data).unwrap() }, BatchSize::LargeInput, ) }); diff --git a/trace_decoder/src/lib.rs b/trace_decoder/src/lib.rs index 468d4dc15..a59243a24 100644 --- a/trace_decoder/src/lib.rs +++ b/trace_decoder/src/lib.rs @@ -293,13 +293,12 @@ pub struct BlockLevelData { pub fn entrypoint( trace: BlockTrace, other: OtherBlockData, - resolve: impl Fn(H256) -> Vec, ) -> anyhow::Result> { use anyhow::Context as _; use mpt_trie::partial_trie::PartialTrie as _; use crate::processed_block_trace::{ - CodeHashResolving, ProcessedBlockTrace, ProcessedBlockTracePreImages, + Hash2Code, ProcessedBlockTrace, ProcessedBlockTracePreImages, }; use crate::PartialTriePreImages; use crate::{ @@ -406,10 +405,7 @@ pub fn entrypoint( code_db }; - let mut code_hash_resolver = CodeHashResolving { - client_code_hash_resolve_f: |it: ðereum_types::H256| resolve(*it), - extra_code_hash_mappings: code_db, - }; + let mut code_hash_resolver = Hash2Code::new(code_db); let last_tx_idx = txn_info.len().saturating_sub(1); @@ -437,7 +433,7 @@ pub fn entrypoint( &mut code_hash_resolver, ) }) - .collect::>(); + .collect::, _>>()?; while txn_info.len() < 2 { txn_info.insert(0, ProcessedTxnInfo::default()); diff --git a/trace_decoder/src/processed_block_trace.rs b/trace_decoder/src/processed_block_trace.rs index 95dcfcd48..dac816530 100644 --- a/trace_decoder/src/processed_block_trace.rs +++ b/trace_decoder/src/processed_block_trace.rs @@ -1,7 +1,9 @@ +use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::iter::once; +use anyhow::bail; use ethereum_types::{Address, H256, U256}; use evm_arithmetization::generation::mpt::{AccountRlp, LegacyReceiptRlp}; use zk_evm_common::{EMPTY_CODE_HASH, EMPTY_TRIE_HASH}; @@ -34,39 +36,38 @@ pub(crate) struct ProcessedTxnInfo { pub meta: TxnMetaState, } -pub(crate) struct CodeHashResolving { - /// If we have not seen this code hash before, use the resolve function that - /// the client passes down to us. This will likely be an rpc call/cache - /// check. - pub client_code_hash_resolve_f: F, - - /// Code hash mappings that we have constructed from parsing the block - /// trace. If there are any txns that create contracts, then they will also - /// get added here as we process the deltas. - pub extra_code_hash_mappings: HashMap>, +/// Code hash mappings that we have constructed from parsing the block +/// trace. +/// If there are any txns that create contracts, then they will also +/// get added here as we process the deltas. +pub(crate) struct Hash2Code { + inner: HashMap>, } -impl Vec> CodeHashResolving { - fn resolve(&mut self, c_hash: &H256) -> Vec { - match self.extra_code_hash_mappings.get(c_hash) { - Some(code) => code.clone(), - None => (self.client_code_hash_resolve_f)(c_hash), +impl Hash2Code { + pub fn new(inner: HashMap>) -> Self { + Self { inner } + } + fn resolve(&mut self, c_hash: &H256) -> anyhow::Result> { + match self.inner.get(c_hash) { + Some(code) => Ok(code.clone()), + None => bail!("no code for hash {}", c_hash), } } fn insert_code(&mut self, c_hash: H256, code: Vec) { - self.extra_code_hash_mappings.insert(c_hash, code); + self.inner.insert(c_hash, code); } } impl TxnInfo { - pub(crate) fn into_processed_txn_info Vec>( + pub(crate) fn into_processed_txn_info( self, tries: &PartialTriePreImages, all_accounts_in_pre_image: &[(H256, AccountRlp)], extra_state_accesses: &[H256], - code_hash_resolver: &mut CodeHashResolving, - ) -> ProcessedTxnInfo { + hash2code: &mut Hash2Code, + ) -> anyhow::Result { let mut nodes_used_by_txn = NodesUsedByTxn::default(); let mut contract_code_accessed = create_empty_code_access_map(); @@ -138,15 +139,15 @@ impl TxnInfo { if let Some(c_usage) = trace.code_usage { match c_usage { ContractCodeUsage::Read(c_hash) => { - contract_code_accessed - .entry(c_hash) - .or_insert_with(|| code_hash_resolver.resolve(&c_hash)); + if let Entry::Vacant(vacant) = contract_code_accessed.entry(c_hash) { + vacant.insert(hash2code.resolve(&c_hash)?); + } } ContractCodeUsage::Write(c_bytes) => { let c_hash = hash(&c_bytes); contract_code_accessed.insert(c_hash, c_bytes.clone()); - code_hash_resolver.insert_code(c_hash, c_bytes); + hash2code.insert_code(c_hash, c_bytes); } } } @@ -194,11 +195,11 @@ impl TxnInfo { gas_used: self.meta.gas_used, }; - ProcessedTxnInfo { + Ok(ProcessedTxnInfo { nodes_used_by_txn, contract_code_accessed, meta: new_meta_state, - } + }) } } diff --git a/trace_decoder/tests/trace_decoder_tests.rs b/trace_decoder/tests/trace_decoder_tests.rs index e4938b8f3..5875bdf27 100644 --- a/trace_decoder/tests/trace_decoder_tests.rs +++ b/trace_decoder/tests/trace_decoder_tests.rs @@ -82,7 +82,6 @@ fn decode_generation_inputs( let trace_decoder_output = trace_decoder::entrypoint( block_prover_input.block_trace, block_prover_input.other_data.clone(), - |_| unimplemented!(), ) .context(format!( "Failed to execute trace decoder on block {}", diff --git a/zero_bin/prover/src/lib.rs b/zero_bin/prover/src/lib.rs index 78005f99e..a43c74104 100644 --- a/zero_bin/prover/src/lib.rs +++ b/zero_bin/prover/src/lib.rs @@ -40,8 +40,7 @@ impl BlockProverInput { let block_number = self.get_block_number(); - let txs = - trace_decoder::entrypoint(self.block_trace, self.other_data, |_| unimplemented!())?; + let txs = trace_decoder::entrypoint(self.block_trace, self.other_data)?; let agg_proof = IndexedStream::from(txs) .map(&TxProof { @@ -87,8 +86,7 @@ impl BlockProverInput { let block_number = self.get_block_number(); info!("Testing witness generation for block {block_number}."); - let txs = - trace_decoder::entrypoint(self.block_trace, self.other_data, |_| unimplemented!())?; + let txs = trace_decoder::entrypoint(self.block_trace, self.other_data)?; IndexedStream::from(txs) .map(&TxProof {