Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #136 from johntaiko/feat/evm_circuit_1559
Browse files Browse the repository at this point in the history
Support eip-1559 and Evm-circuit in aggregation
  • Loading branch information
smtmfft committed Sep 21, 2023
2 parents ca0c5b8 + 536165e commit 1025748
Show file tree
Hide file tree
Showing 73 changed files with 1,686 additions and 692 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
.vscode
.idea
/generated
*.srs
*.log
*.json
*.yul
*.sol
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
"eth-types",
"external-tracer",
"mock",
"testool"
"testool",
]

[patch.crates-io]
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ fmt: ## Check whether the code is formated correctly

test: ## Run tests for all the workspace members
# Run light tests
@cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks
@cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks --exclude testool
# Run heavy tests serially to avoid OOM
@cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --test-threads 1
@cargo test --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks --exclude testool serial_ -- --ignored # --test-threads 1


test_doc: ## Test the docs
Expand Down
98 changes: 37 additions & 61 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod block;
mod call;
mod execution;
mod input_state_ref;
mod protocol_instance;
#[cfg(test)]
mod tracer_tests;
mod transaction;
Expand Down Expand Up @@ -34,6 +35,7 @@ pub use execution::{
pub use input_state_ref::CircuitInputStateRef;
use itertools::Itertools;
use log::warn;
pub use protocol_instance::{left_shift, MetaHash, ProtocolInstance, ANCHOR_TX_METHOD_SIGNATURE};
use std::collections::HashMap;
pub use transaction::{Transaction, TransactionContext};

Expand Down Expand Up @@ -75,7 +77,7 @@ impl Default for CircuitsParams {
fn default() -> Self {
CircuitsParams {
max_rws: 1000,
max_txs: 1,
max_txs: 2, // more one for anchor
max_calldata: 256,
// TODO: Check whether this value is correct or we should increase/decrease based on
// this lib tests
Expand Down Expand Up @@ -151,6 +153,7 @@ impl<'a> CircuitInputBuilder {
/// Create a new Transaction from a [`eth_types::Transaction`].
pub fn new_tx(
&mut self,
eth_block: &EthBlock,
eth_tx: &eth_types::Transaction,
is_success: bool,
) -> Result<Transaction, Error> {
Expand All @@ -167,7 +170,15 @@ impl<'a> CircuitInputBuilder {
),
);

Transaction::new(call_id, &self.sdb, &mut self.code_db, eth_tx, is_success)
Transaction::new(
call_id,
&self.sdb,
&mut self.code_db,
eth_block,
eth_tx,
is_success,
self.block.is_taiko(),
)
}

/// Iterate over all generated CallContext RwCounterEndOfReversion
Expand Down Expand Up @@ -197,25 +208,14 @@ impl<'a> CircuitInputBuilder {
&mut self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
) -> Result<(), Error> {
self.handle_block_with_anchor(eth_block, geth_traces, false)
}

/// Handle a block by handling each transaction to generate all the
/// associated operations.
pub fn handle_block_with_anchor(
&mut self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
has_anchor_tx: bool,
) -> Result<(), Error> {
// accumulates gas across all txs in the block
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
let geth_trace = &geth_traces[tx_index];
self.handle_tx(
eth_block,
tx,
geth_trace,
has_anchor_tx && tx_index == 0,
tx_index + 1 == eth_block.transactions.len(),
)?;
}
Expand Down Expand Up @@ -279,13 +279,13 @@ impl<'a> CircuitInputBuilder {
/// generated operations.
fn handle_tx(
&mut self,
eth_block: &EthBlock,
eth_tx: &eth_types::Transaction,
geth_trace: &GethExecTrace,
is_anchor_tx: bool,
is_last_tx: bool,
) -> Result<(), Error> {
let mut tx = self.new_tx(eth_tx, !geth_trace.failed)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_anchor_tx, is_last_tx)?;
let mut tx = self.new_tx(eth_block, eth_tx, !geth_trace.failed)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_last_tx)?;

// Generate BeginTx step
let begin_tx_step = gen_associated_steps(
Expand Down Expand Up @@ -409,12 +409,14 @@ pub struct BuilderClient<P: JsonRpcClient> {
cli: GethClient<P>,
chain_id: Word,
circuits_params: CircuitsParams,
protocol_instance: Option<ProtocolInstance>,
}

/// Get State Accesses from TxExecTraces
pub fn get_state_accesses(
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
protocol_instance: &Option<ProtocolInstance>,
) -> Result<AccessSet, Error> {
let mut block_access_trace = vec![Access::new(
None,
Expand All @@ -430,6 +432,15 @@ pub fn get_state_accesses(
let tx_access_trace = gen_state_access_trace(eth_block, tx, geth_trace)?;
block_access_trace.extend(tx_access_trace);
}
if let Some(pi) = protocol_instance {
block_access_trace.push(Access::new(
None,
RW::WRITE,
AccessValue::Account {
address: pi.meta_hash.treasury,
},
));
}

Ok(AccessSet::from(block_access_trace))
}
Expand Down Expand Up @@ -468,13 +479,15 @@ impl<P: JsonRpcClient> BuilderClient<P> {
pub async fn new(
client: GethClient<P>,
circuits_params: CircuitsParams,
protocol_instance: Option<ProtocolInstance>,
) -> Result<Self, Error> {
let chain_id = client.get_chain_id().await?;

Ok(Self {
cli: client,
chain_id: chain_id.into(),
circuits_params,
protocol_instance,
})
}

Expand Down Expand Up @@ -526,8 +539,9 @@ impl<P: JsonRpcClient> BuilderClient<P> {
pub fn get_state_accesses(
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
protocol_instance: &Option<ProtocolInstance>,
) -> Result<AccessSet, Error> {
get_state_accesses(eth_block, geth_traces)
get_state_accesses(eth_block, geth_traces, protocol_instance)
}

/// Step 3. Query geth for all accounts, storage keys, and codes from
Expand Down Expand Up @@ -584,40 +598,17 @@ impl<P: JsonRpcClient> BuilderClient<P> {
geth_traces: &[eth_types::GethExecTrace],
history_hashes: Vec<Word>,
prev_state_root: Word,
) -> Result<CircuitInputBuilder, Error> {
self.gen_inputs_from_state_with_anchor(
sdb,
code_db,
eth_block,
geth_traces,
history_hashes,
prev_state_root,
false,
)
}

/// Step 5. For each step in TxExecTraces, gen the associated ops and state
/// circuit inputs
#[allow(clippy::too_many_arguments)]
pub fn gen_inputs_from_state_with_anchor(
&self,
sdb: StateDB,
code_db: CodeDB,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
history_hashes: Vec<Word>,
prev_state_root: Word,
has_anchor_tx: bool,
) -> Result<CircuitInputBuilder, Error> {
let block = Block::new(
self.chain_id,
history_hashes,
prev_state_root,
eth_block,
self.circuits_params,
self.protocol_instance.clone(),
)?;
let mut builder = CircuitInputBuilder::new(sdb, code_db, block);
builder.handle_block_with_anchor(eth_block, geth_traces, has_anchor_tx)?;
builder.handle_block(eth_block, geth_traces)?;
Ok(builder)
}

Expand All @@ -631,35 +622,20 @@ impl<P: JsonRpcClient> BuilderClient<P> {
eth_types::Block<eth_types::Transaction>,
),
Error,
> {
self.gen_inputs_with_anchor(block_num, false).await
}

/// Perform all the steps to generate the circuit inputs
pub async fn gen_inputs_with_anchor(
&self,
block_num: u64,
has_anchor_tx: bool,
) -> Result<
(
CircuitInputBuilder,
eth_types::Block<eth_types::Transaction>,
),
Error,
> {
let (eth_block, geth_traces, history_hashes, prev_state_root) =
self.get_block(block_num).await?;
let access_set = Self::get_state_accesses(&eth_block, &geth_traces)?;
let access_set =
Self::get_state_accesses(&eth_block, &geth_traces, &self.protocol_instance)?;
let (proofs, codes) = self.get_state(block_num, access_set).await?;
let (state_db, code_db) = Self::build_state_code_db(proofs, codes);
let builder = self.gen_inputs_from_state_with_anchor(
let builder = self.gen_inputs_from_state(
state_db,
code_db,
&eth_block,
&geth_traces,
history_hashes,
prev_state_root,
has_anchor_tx,
)?;
Ok((builder, eth_block))
}
Expand Down
21 changes: 17 additions & 4 deletions bus-mapping/src/circuit_input_builder/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

use super::{
execution::ExecState, transaction::Transaction, CircuitsParams, CopyEvent, ExecStep, ExpEvent,
ProtocolInstance,
};
use crate::{
operation::{OperationContainer, RWCounter},
Error,
};
use eth_types::{evm_unimplemented, Address, Word};
use eth_types::{evm_unimplemented, Address, Hash, Word};
use std::collections::HashMap;

/// Context of a [`Block`] which can mutate in a [`Transaction`].
Expand Down Expand Up @@ -68,8 +69,8 @@ pub struct Block {
pub number: Word,
/// difficulty
pub timestamp: Word,
/// gas limit
pub difficulty: Word,
/// mix hash
pub mix_hash: Hash,
/// base fee
pub base_fee: Word,
/// State root of the previous block
Expand All @@ -90,6 +91,9 @@ pub struct Block {
pub circuits_params: CircuitsParams,
/// Original block from geth
pub eth_block: eth_types::Block<eth_types::Transaction>,
/// Protocol instance from protocol
/// If this is set, means we are in the taiko context
pub protocol_instance: Option<ProtocolInstance>,
}

impl Block {
Expand All @@ -100,6 +104,7 @@ impl Block {
prev_state_root: Word,
eth_block: &eth_types::Block<eth_types::Transaction>,
circuits_params: CircuitsParams,
protocol_instance: Option<ProtocolInstance>,
) -> Result<Self, Error> {
if eth_block.base_fee_per_gas.is_none() {
// FIXME: resolve this once we have proper EIP-1559 support
Expand All @@ -121,7 +126,9 @@ impl Block {
.low_u64()
.into(),
timestamp: eth_block.timestamp,
difficulty: eth_block.difficulty,
mix_hash: eth_block
.mix_hash
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
base_fee: eth_block.base_fee_per_gas.unwrap_or_default(),
prev_state_root,
container: OperationContainer::new(),
Expand All @@ -141,6 +148,7 @@ impl Block {
sha3_inputs: Vec::new(),
circuits_params,
eth_block: eth_block.clone(),
protocol_instance,
})
}

Expand All @@ -149,6 +157,11 @@ impl Block {
&self.txs
}

/// Check if in the taiko context.
pub fn is_taiko(&self) -> bool {
self.protocol_instance.is_some()
}

#[cfg(test)]
pub fn txs_mut(&mut self) -> &mut Vec<Transaction> {
&mut self.txs
Expand Down
Loading

0 comments on commit 1025748

Please sign in to comment.