Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip missing Cancun bits for non Eth mainnet chains #605

Merged
merged 11 commits into from
Sep 12, 2024
5 changes: 2 additions & 3 deletions evm_arithmetization/src/generation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use anyhow::anyhow;
use ethereum_types::H160;
use ethereum_types::{Address, BigEndianHash, H256, U256};
use keccak_hash::keccak;
use log::log_enabled;
Expand Down Expand Up @@ -72,7 +71,7 @@ pub struct GenerationInputs {
/// `None`, then the base fee is directly burnt.
///
/// Note: this is only used when feature `cdk_erigon` is activated.
pub burn_addr: Option<H160>,
pub burn_addr: Option<Address>,
/// Withdrawal pairs `(addr, amount)`. At the end of the txs, `amount` is
/// added to `addr`'s balance. See EIP-4895.
pub withdrawals: Vec<(Address, U256)>,
Expand Down Expand Up @@ -144,7 +143,7 @@ pub struct TrimmedGenerationInputs {

/// Address where the burnt fees are stored. Only used if the `cfg_erigon`
/// feature is activated.
pub burn_addr: Option<H160>,
pub burn_addr: Option<Address>,

/// The hash of the current block, and a list of the 256 previous block
/// hashes.
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pub fn check_abort_signal(abort_signal: Option<Arc<AtomicBool>>) -> Result<()> {
/// Sanity checks on the consistency between this proof payload and the feature
/// flags being used.
pub(crate) fn features_check(inputs: &TrimmedGenerationInputs) {
if cfg!(feature = "polygon_pos") || cfg!(feature = "cdk_erigon") {
if !cfg!(feature = "eth_mainnet") {
assert!(inputs.block_metadata.parent_beacon_block_root.is_zero());
assert!(inputs.block_metadata.block_blob_gas_used.is_zero());
assert!(inputs.block_metadata.block_excess_blob_gas.is_zero());
Expand Down
3 changes: 3 additions & 0 deletions proof_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ eth_mainnet = [
cdk_erigon = [
"evm_arithmetization/cdk_erigon"
]
polygon_pos = [
"evm_arithmetization/polygon_pos"
]

[lints]
workspace = true
11 changes: 11 additions & 0 deletions proof_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@
//! assert!(verifier_state.verify(block_proof.intern).is_ok());
//! ```

#[cfg_attr(
not(any(feature = "polygon_pos", feature = "cdk_erigon")),
cfg(feature = "eth_mainnet")
)]
Nashtare marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(
all(feature = "cdk_erigon", feature = "polygon_pos"),
all(feature = "cdk_erigon", feature = "eth_mainnet"),
all(feature = "polygon_pos", feature = "eth_mainnet"),
))]
compile_error!("Only a single network feature should be enabled at a time!");

pub(crate) mod constants;
pub mod proof_gen;
pub mod proof_types;
Expand Down
4 changes: 4 additions & 0 deletions trace_decoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ cdk_erigon = [
"evm_arithmetization/cdk_erigon",
"prover/cdk_erigon",
]
polygon_pos = [
"evm_arithmetization/polygon_pos",
"prover/polygon_pos",
]

[[bench]]
name = "block_processing"
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/benches/block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn criterion_benchmark(c: &mut Criterion) {
block_trace,
other_data,
}| {
trace_decoder::entrypoint(block_trace, other_data, batch_size, false).unwrap()
trace_decoder::entrypoint(block_trace, other_data, batch_size).unwrap()
},
BatchSize::LargeInput,
)
Expand Down
64 changes: 40 additions & 24 deletions trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{cmp::min, collections::HashMap, ops::Range};

use anyhow::{anyhow, Context as _};
use ethereum_types::H160;
use ethereum_types::{Address, BigEndianHash, H256, U256, U512};
use ethereum_types::{Address, H256, U256, U512};
#[cfg(feature = "eth_mainnet")]
use evm_arithmetization::testing_utils::{
BEACON_ROOTS_CONTRACT_ADDRESS, BEACON_ROOTS_CONTRACT_ADDRESS_HASHED, HISTORY_BUFFER_LENGTH,
};
use evm_arithmetization::{
generation::{
mpt::{decode_receipt, AccountRlp},
GenerationInputs, TrieInputs,
},
proof::{BlockMetadata, ExtraBlockData, TrieRoots},
testing_utils::{
BEACON_ROOTS_CONTRACT_ADDRESS, BEACON_ROOTS_CONTRACT_ADDRESS_HASHED, HISTORY_BUFFER_LENGTH,
},
proof::{ExtraBlockData, TrieRoots},
};
use mpt_trie::{
nibbles::Nibbles,
Expand Down Expand Up @@ -56,7 +56,6 @@ pub fn into_txn_proof_gen_ir(
withdrawals,
}: ProcessedBlockTrace,
other_data: OtherBlockData,
use_burn_addr: bool,
batch_size: usize,
) -> anyhow::Result<Vec<GenerationInputs>> {
let mut curr_block_tries = PartialTrieState {
Expand Down Expand Up @@ -93,7 +92,6 @@ pub fn into_txn_proof_gen_ir(
&mut curr_block_tries,
&mut extra_data,
&other_data,
use_burn_addr,
)
.context(format!(
"at transaction range {}..{}",
Expand All @@ -113,15 +111,35 @@ pub fn into_txn_proof_gen_ir(
Ok(txn_gen_inputs)
}

/// Includes additional state and storage updates for pre-block execution.
#[allow(unused)]
atanmarko marked this conversation as resolved.
Show resolved Hide resolved
fn pre_block_execution(
trie_state: &mut PartialTrieState<impl StateTrie>,
delta_out: &mut TrieDeltaApplicationOutput,
nodes_used: &mut NodesUsedByTxnBatch,
block_data: &evm_arithmetization::proof::BlockMetadata,
) -> anyhow::Result<()> {
#[cfg(feature = "eth_mainnet")]
return update_beacon_block_root_contract_storage(
trie_state, delta_out, nodes_used, block_data,
);

#[cfg(not(feature = "eth_mainnet"))]
Ok(())
}

#[cfg(feature = "eth_mainnet")]
/// Cancun HF specific: At the start of a block, prior txn execution, we
/// need to update the storage of the beacon block root contract.
// See <https://eips.ethereum.org/EIPS/eip-4788>.
fn update_beacon_block_root_contract_storage(
trie_state: &mut PartialTrieState<impl StateTrie>,
delta_out: &mut TrieDeltaApplicationOutput,
nodes_used: &mut NodesUsedByTxnBatch,
block_data: &BlockMetadata,
block_data: &evm_arithmetization::proof::BlockMetadata,
) -> anyhow::Result<()> {
use ethereum_types::BigEndianHash;

const HISTORY_BUFFER_LENGTH_MOD: U256 = U256([HISTORY_BUFFER_LENGTH.1, 0, 0, 0]);

let timestamp_idx = block_data.block_timestamp % HISTORY_BUFFER_LENGTH_MOD;
Expand Down Expand Up @@ -457,18 +475,23 @@ fn add_withdrawals_to_txns(

if last_inputs.signed_txns.is_empty() {
let mut state_trie = final_trie_state.state.clone();
let is_eth_mainnet = cfg!(feature = "eth_mainnet");
state_trie.trim_to(
// This is a dummy payload, hence it does not contain yet
// state accesses to the withdrawal addresses.
withdrawals
.iter()
.map(|(addr, _)| *addr)
.chain(match last_inputs.txn_number_before == 0.into() {
// We need to include the beacon roots contract as this payload is at the
// start of the block execution.
true => Some(BEACON_ROOTS_CONTRACT_ADDRESS),
false => None,
})
.chain(
match is_eth_mainnet && last_inputs.txn_number_before == 0.into() {
// We need to include the beacon roots contract as this payload is at the
// start of the block execution.
true => {
Some(evm_arithmetization::testing_utils::BEACON_ROOTS_CONTRACT_ADDRESS)
}
false => None,
},
)
.map(TrieKey::from_address),
)?;
last_inputs.tries.state_trie = state_trie.try_into()?;
Expand Down Expand Up @@ -518,7 +541,6 @@ fn process_txn_info(
>,
extra_data: &mut ExtraBlockData,
other_data: &OtherBlockData,
use_burn_target: bool,
) -> anyhow::Result<GenerationInputs> {
log::trace!(
"Generating proof IR for txn {} through {}...",
Expand Down Expand Up @@ -558,7 +580,7 @@ fn process_txn_info(

let nodes_used_by_txn = if is_initial_payload {
let mut nodes_used = txn_info.nodes_used_by_txn;
update_beacon_block_root_contract_storage(
pre_block_execution(
curr_block_tries,
&mut delta_out,
&mut nodes_used,
Expand All @@ -577,15 +599,9 @@ fn process_txn_info(
delta_out,
)?;

let burn_addr = match use_burn_target {
// TODO: https://github.com/0xPolygonZero/zk_evm/issues/565
// Retrieve the actual burn address from `cdk-erigon`.
true => Some(H160::zero()),
false => None,
};
let gen_inputs = GenerationInputs {
txn_number_before: extra_data.txn_number_before,
burn_addr,
burn_addr: other_data.burn_addr,
gas_used_before: extra_data.gas_used_before,
gas_used_after: extra_data.gas_used_after,
signed_txns: txn_info
Expand Down
16 changes: 14 additions & 2 deletions trace_decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]

#[cfg_attr(
not(any(feature = "polygon_pos", feature = "cdk_erigon")),
cfg(feature = "eth_mainnet")
)]
#[cfg(any(
all(feature = "cdk_erigon", feature = "polygon_pos"),
all(feature = "cdk_erigon", feature = "eth_mainnet"),
all(feature = "polygon_pos", feature = "eth_mainnet"),
))]
compile_error!("Only a single network feature should be enabled at a time!");

/// The broad overview is as follows:
///
/// 1. Ethereum nodes emit a bunch of binary [`wire::Instruction`]s, which are
Expand Down Expand Up @@ -268,6 +279,9 @@ pub struct OtherBlockData {
pub b_data: BlockLevelData,
/// State trie root hash at the checkpoint.
pub checkpoint_state_trie_root: H256,
/// Address to store the base fee to be burnt.
/// Will be `None` when `cdk_erigon` feature flag is off.
pub burn_addr: Option<Address>,
}

/// Data that is specific to a block and is constant for all txns in a given
Expand All @@ -288,7 +302,6 @@ pub fn entrypoint(
trace: BlockTrace,
other: OtherBlockData,
mut batch_size: usize,
use_burn_addr: bool,
) -> anyhow::Result<Vec<GenerationInputs>> {
use anyhow::Context as _;
use mpt_trie::partial_trie::PartialTrie as _;
Expand Down Expand Up @@ -444,7 +457,6 @@ pub fn entrypoint(
withdrawals: other.b_data.withdrawals.clone(),
},
other,
use_burn_addr,
batch_size,
)
}
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/tests/consistent-with-header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> anyhow::Result<()> {
} in cases()?
{
trials.push(Trial::test(format!("{name}@{batch_size}"), move || {
let gen_inputs = trace_decoder::entrypoint(trace, other.clone(), batch_size, false)
let gen_inputs = trace_decoder::entrypoint(trace, other.clone(), batch_size)
.map_err(|e| format!("{e:?}"))?; // get the full cause chain
check!(gen_inputs.len() >= 2);
check!(
Expand Down
2 changes: 1 addition & 1 deletion trace_decoder/tests/simulate-execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> anyhow::Result<()> {
other,
} in cases()?
{
let gen_inputs = trace_decoder::entrypoint(trace, other, batch_size, false).context(
let gen_inputs = trace_decoder::entrypoint(trace, other, batch_size).context(
format!("error in `trace_decoder` for {name} at batch size {batch_size}"),
)?;
for (ix, gi) in gen_inputs.into_iter().enumerate() {
Expand Down
5 changes: 5 additions & 0 deletions zero_bin/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ cdk_erigon = [
"proof_gen/cdk_erigon",
"trace_decoder/cdk_erigon",
]
polygon_pos = [
"evm_arithmetization/polygon_pos",
"proof_gen/polygon_pos",
"trace_decoder/polygon_pos",
]

[lints]
workspace = true
11 changes: 11 additions & 0 deletions zero_bin/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#[cfg_attr(
not(any(feature = "polygon_pos", feature = "cdk_erigon")),
cfg(feature = "eth_mainnet")
)]
#[cfg(any(
all(feature = "cdk_erigon", feature = "polygon_pos"),
all(feature = "cdk_erigon", feature = "eth_mainnet"),
all(feature = "polygon_pos", feature = "eth_mainnet"),
))]
compile_error!("Only a single network feature should be enabled at a time!");
pub mod block_interval;

pub mod debug_utils;
pub mod fs;
pub mod parsing;
Expand Down
8 changes: 8 additions & 0 deletions zero_bin/leader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ cdk_erigon = [
"rpc/cdk_erigon",
"zero_bin_common/cdk_erigon"
]
polygon_pos = [
"evm_arithmetization/polygon_pos",
"ops/polygon_pos",
"proof_gen/polygon_pos",
"prover/polygon_pos",
"rpc/polygon_pos",
"zero_bin_common/polygon_pos",
]

[build-dependencies]
cargo_metadata = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions zero_bin/leader/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[cfg_attr(
not(any(feature = "polygon_pos", feature = "cdk_erigon")),
cfg(feature = "eth_mainnet")
)]
#[cfg(any(
all(feature = "cdk_erigon", feature = "polygon_pos"),
all(feature = "cdk_erigon", feature = "eth_mainnet"),
all(feature = "polygon_pos", feature = "eth_mainnet"),
))]
compile_error!("Only a single network feature should be enabled at a time!");

use std::sync::Arc;
use std::{env, io};
use std::{fs::File, path::PathBuf};
Expand Down
6 changes: 6 additions & 0 deletions zero_bin/ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ cdk_erigon = [
"trace_decoder/cdk_erigon",
"zero_bin_common/cdk_erigon",
]
polygon_pos = [
"evm_arithmetization/polygon_pos",
"proof_gen/polygon_pos",
"trace_decoder/polygon_pos",
"zero_bin_common/polygon_pos",
]
11 changes: 11 additions & 0 deletions zero_bin/ops/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[cfg_attr(
not(any(feature = "polygon_pos", feature = "cdk_erigon")),
cfg(feature = "eth_mainnet")
)]
#[cfg(any(
all(feature = "cdk_erigon", feature = "polygon_pos"),
all(feature = "cdk_erigon", feature = "eth_mainnet"),
all(feature = "polygon_pos", feature = "eth_mainnet"),
))]
compile_error!("Only a single network feature should be enabled at a time!");

use std::time::Instant;

use evm_arithmetization::generation::TrimmedGenerationInputs;
Expand Down
6 changes: 6 additions & 0 deletions zero_bin/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ cdk_erigon = [
"trace_decoder/cdk_erigon",
"zero_bin_common/cdk_erigon",
]
polygon_pos = [
"evm_arithmetization/polygon_pos",
"proof_gen/polygon_pos",
"trace_decoder/polygon_pos",
"zero_bin_common/polygon_pos",
]

[lints]
workspace = true
Loading
Loading