Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feat/continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Aug 20, 2024
2 parents b1e0166 + 67dbf7a commit 3fca90e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 52 deletions.
8 changes: 1 addition & 7 deletions trace_decoder/benches/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ fn criterion_benchmark(c: &mut Criterion) {
block_trace,
other_data,
}| {
trace_decoder::entrypoint(
block_trace,
other_data,
batch_size,
|_| unimplemented!(),
)
.unwrap()
trace_decoder::entrypoint(block_trace, other_data, batch_size).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 @@ -294,13 +294,12 @@ pub fn entrypoint(
trace: BlockTrace,
other: OtherBlockData,
batch_size: usize,
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 @@ -407,10 +406,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) / batch_size;

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

while txn_info.len() < 2 {
txn_info.push(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::{BTreeSet, 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 itertools::Itertools;
Expand Down Expand Up @@ -35,39 +37,38 @@ pub(crate) struct ProcessedTxnInfo {
pub meta: Vec<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(
tx_infos: &[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();
let mut meta = Vec::with_capacity(tx_infos.len());
Expand Down Expand Up @@ -189,15 +190,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.clone());
hash2code.insert_code(c_hash, c_bytes.clone());
}
}
}
Expand Down Expand Up @@ -249,11 +250,11 @@ impl TxnInfo {
});
}

ProcessedTxnInfo {
Ok(ProcessedTxnInfo {
nodes_used_by_txn,
contract_code_accessed,
meta,
}
})
}
}

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 @@ -83,7 +83,6 @@ fn decode_generation_inputs(
block_prover_input.block_trace,
block_prover_input.other_data.clone(),
3,
|_| unimplemented!(),
)
.context(format!(
"Failed to execute trace decoder on block {}",
Expand Down
16 changes: 4 additions & 12 deletions zero_bin/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ impl BlockProverInput {

let block_number = self.get_block_number();

let block_generation_inputs = trace_decoder::entrypoint(
self.block_trace,
self.other_data,
batch_size,
|_| unimplemented!(),
)?;
let block_generation_inputs =
trace_decoder::entrypoint(self.block_trace, self.other_data, batch_size)?;

// Create segment proof.
let seg_prove_ops = ops::SegmentProof {
Expand Down Expand Up @@ -148,12 +144,8 @@ impl BlockProverInput {
let block_number = self.get_block_number();
info!("Testing witness generation for block {block_number}.");

let block_generation_inputs = trace_decoder::entrypoint(
self.block_trace,
self.other_data,
batch_size,
|_| unimplemented!(),
)?;
let block_generation_inputs =
trace_decoder::entrypoint(self.block_trace, self.other_data, batch_size)?;

let batch_ops = ops::BatchTestOnly {
save_inputs_on_error,
Expand Down

0 comments on commit 3fca90e

Please sign in to comment.