Skip to content

Commit

Permalink
move to iterator-based parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
einar-polygon committed Oct 3, 2024
1 parent 10b6a22 commit 143f05d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
65 changes: 42 additions & 23 deletions zero/src/rpc/jerigon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::iter::Iterator;
use std::collections::BTreeMap;
use std::ops::Deref as _;

use __compat_primitive_types::H160;
use alloy::{
providers::Provider,
rpc::types::{eth::BlockId, trace::geth::StructLog, Block, BlockTransactionsKind, Transaction},
Expand Down Expand Up @@ -51,11 +49,39 @@ where
"debug_traceBlockByNumber".into(),
(target_block_id, json!({"tracer": "zeroTracer"})),
)
.await?
.into_iter()
.map(|ztr| ztr.result)
.await?;

// Convert all [`H160`]` addresses to [`Address`] addresses in owned memory.
let adresses = tx_results
.iter()
.map(
|ZeroTxResult {
tx_hash: _,
result: TxnInfo { traces, meta: _ },
}| {
traces
.iter()
.map(|(h, _t)| Address::from(h.to_fixed_bytes()))
.collect::<Vec<Address>>()
},
)
.collect::<Vec<_>>();

let tx_traces = tx_results.iter().zip(adresses.iter()).map(
|(
ZeroTxResult {
tx_hash: _,
result: TxnInfo { traces, meta: _ },
},
addresses,
)| {
traces
.iter()
.zip(addresses.iter())
.map(|((_h, txntrace), addr)| (addr, txntrace))
},
);

// Grab block witness info (packed as combined trie pre-images)
let block_witness = cached_provider
.get_provider()
Expand All @@ -73,7 +99,7 @@ where
process_transactions(
&block,
cached_provider.get_provider().await?.deref(),
tx_results.iter().map(|TxnInfo { traces, meta: _ }| traces), // &tx_traces,
tx_traces,
)
.await?
}
Expand All @@ -87,11 +113,11 @@ where
.into_iter()
.zip(jdts)
.map(|(mut tx_info, jdt)| {
tx_info.meta.jumpdest_table = jdt.map(|(j, c)| {
tx_info.result.meta.jumpdest_table = jdt.map(|(j, c)| {
code_db.extend(c);
j
});
tx_info
tx_info.result
})
.collect();

Expand All @@ -113,15 +139,16 @@ where
}

/// Processes the transactions in the given block and updates the code db.
pub async fn process_transactions<'i, I, ProviderT, TransportT>(
pub async fn process_transactions<'i, 'j, ProviderT, TransportT, I, J>(
block: &Block,
provider: &ProviderT,
tx_traces: I,
tx_traces: J,
) -> anyhow::Result<Vec<Option<(JumpDestTableWitness, CodeDb)>>>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
I: Iterator<Item = &'i BTreeMap<H160, TxnTrace>>,
I: Iterator<Item = (&'i Address, &'i TxnTrace)>,
J: Iterator<Item = I>,
{
let futures = block
.transactions
Expand All @@ -141,27 +168,23 @@ where

/// Processes the transaction with the given transaction hash and updates the
/// accounts state.
pub async fn process_transaction<ProviderT, TransportT>(
pub async fn process_transaction<'i, ProviderT, TransportT, I>(
provider: &ProviderT,
tx: &Transaction,
tx_trace: &BTreeMap<H160, TxnTrace>,
tx_trace: I,
) -> anyhow::Result<Option<(JumpDestTableWitness, CodeDb)>>
where
ProviderT: Provider<TransportT>,
TransportT: Transport + Clone,
I: Iterator<Item = (&'i Address, &'i TxnTrace)>,
{
let tx_traces = tx_trace
.iter()
.map(|(h, t)| (Address::from(h.to_fixed_bytes()), t.clone()))
.collect();

let structlog_opt: Option<Vec<StructLog>> = get_normalized_structlog(provider, &tx.hash)
.await
.ok()
.flatten();

let jc: Option<(JumpDestTableWitness, CodeDb)> = structlog_opt.and_then(|struct_log| {
jumpdest::generate_jumpdest_table(tx, &struct_log, &tx_traces).map_or_else(
jumpdest::generate_jumpdest_table(tx, &struct_log, tx_trace).map_or_else(
|error| {
info!(
"{:#?}: JumpDestTable generation failed with reason: {}",
Expand All @@ -178,9 +201,5 @@ where
},
)
});

// TODO
// let jumpdest_table = jc.map(|(j, c)| j);

Ok(jc)
}
11 changes: 6 additions & 5 deletions zero/src/rpc/jumpdest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use core::default::Default;
use core::option::Option::None;
use core::str::FromStr as _;
use core::time::Duration;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::ops::Not as _;

Expand Down Expand Up @@ -117,11 +116,14 @@ where

/// Generate at JUMPDEST table by simulating the call stack in EVM,
/// using a Geth structlog as input.
pub(crate) fn generate_jumpdest_table(
pub(crate) fn generate_jumpdest_table<'i, I>(
tx: &Transaction,
struct_log: &[StructLog],
tx_traces: &BTreeMap<Address, TxnTrace>,
) -> anyhow::Result<(JumpDestTableWitness, CodeDb)> {
tx_traces: I,
) -> anyhow::Result<(JumpDestTableWitness, CodeDb)>
where
I: Iterator<Item = (&'i Address, &'i TxnTrace)>,
{
trace!("Generating JUMPDEST table for tx: {}", tx.hash);

let mut jumpdest_table = JumpDestTableWitness::default();
Expand All @@ -130,7 +132,6 @@ pub(crate) fn generate_jumpdest_table(
// This map does neither contain the `init` field of Contract Deployment
// transactions nor CREATE, CREATE2 payloads.
let callee_addr_to_code_hash: HashMap<Address, H256> = tx_traces
.iter()
.filter_map(|(callee_addr, trace)| {
trace
.code_usage
Expand Down
2 changes: 1 addition & 1 deletion zero/src/rpc/native/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
};

let jc: Option<(JumpDestTableWitness, CodeDb)> = structlog_opt.and_then(|struct_logs| {
jumpdest::generate_jumpdest_table(tx, &struct_logs, &tx_traces).map_or_else(
jumpdest::generate_jumpdest_table(tx, &struct_logs, tx_traces.iter()).map_or_else(
|error| {
info!(
"{:#?}: JumpDestTable generation failed with reason: {}",
Expand Down

0 comments on commit 143f05d

Please sign in to comment.