Skip to content

Commit

Permalink
refactor: remove dead client_code_hash_resolve_f (#514)
Browse files Browse the repository at this point in the history
* mark: 0xaatif/no-code-hash-resolve-fn

* refactor: remove dead code_hash_resolve_fn

* fix: feature-gated code >:(
  • Loading branch information
0xaatif authored Aug 19, 2024
1 parent 0e55293 commit 67dbf7a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
4 changes: 1 addition & 3 deletions trace_decoder/benches/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
});
Expand Down
10 changes: 3 additions & 7 deletions trace_decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,12 @@ pub struct BlockLevelData {
pub fn entrypoint(
trace: BlockTrace,
other: OtherBlockData,
resolve: impl Fn(H256) -> Vec<u8>,
) -> anyhow::Result<Vec<GenerationInputs>> {
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::{
Expand Down Expand Up @@ -406,10 +405,7 @@ pub fn entrypoint(
code_db
};

let mut code_hash_resolver = CodeHashResolving {
client_code_hash_resolve_f: |it: &ethereum_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);

Expand Down Expand Up @@ -437,7 +433,7 @@ pub fn entrypoint(
&mut code_hash_resolver,
)
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>, _>>()?;

while txn_info.len() < 2 {
txn_info.insert(0, ProcessedTxnInfo::default());
Expand Down
51 changes: 26 additions & 25 deletions trace_decoder/src/processed_block_trace.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -34,39 +36,38 @@ pub(crate) struct ProcessedTxnInfo {
pub meta: TxnMetaState,
}

pub(crate) struct CodeHashResolving<F> {
/// 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<H256, Vec<u8>>,
/// 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<H256, Vec<u8>>,
}

impl<F: Fn(&H256) -> Vec<u8>> CodeHashResolving<F> {
fn resolve(&mut self, c_hash: &H256) -> Vec<u8> {
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<H256, Vec<u8>>) -> Self {
Self { inner }
}
fn resolve(&mut self, c_hash: &H256) -> anyhow::Result<Vec<u8>> {
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<u8>) {
self.extra_code_hash_mappings.insert(c_hash, code);
self.inner.insert(c_hash, code);
}
}

impl TxnInfo {
pub(crate) fn into_processed_txn_info<F: Fn(&H256) -> Vec<u8>>(
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<F>,
) -> ProcessedTxnInfo {
hash2code: &mut Hash2Code,
) -> anyhow::Result<ProcessedTxnInfo> {
let mut nodes_used_by_txn = NodesUsedByTxn::default();
let mut contract_code_accessed = create_empty_code_access_map();

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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,
}
})
}
}

Expand Down
1 change: 0 additions & 1 deletion trace_decoder/tests/trace_decoder_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}",
Expand Down
6 changes: 2 additions & 4 deletions zero_bin/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 67dbf7a

Please sign in to comment.