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

Commit

Permalink
Merge branch 'main' of gh_work:taikoxyz/zkevm-circuits into feat/evm_…
Browse files Browse the repository at this point in the history
…circuit_1559
  • Loading branch information
johntaiko committed Sep 5, 2023
2 parents c1af675 + 7aef0f5 commit 99ff3ef
Show file tree
Hide file tree
Showing 37 changed files with 1,379 additions and 218 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl<'a> CircuitInputBuilder {
eth_block: &EthBlock,
eth_tx: &eth_types::Transaction,
is_success: bool,
is_invalid: bool,
) -> Result<Transaction, Error> {
let call_id = self.block_ctx.rwc.0;

Expand All @@ -178,6 +179,7 @@ impl<'a> CircuitInputBuilder {
eth_tx,
is_success,
self.block.is_taiko(),
is_invalid,
)
}

Expand Down Expand Up @@ -284,7 +286,7 @@ impl<'a> CircuitInputBuilder {
geth_trace: &GethExecTrace,
is_last_tx: bool,
) -> Result<(), Error> {
let mut tx = self.new_tx(eth_block, eth_tx, !geth_trace.failed)?;
let mut tx = self.new_tx(eth_block, eth_tx, !geth_trace.failed, geth_trace.invalid)?;
let mut tx_ctx = TransactionContext::new(eth_tx, geth_trace, is_last_tx)?;

// Generate BeginTx step
Expand Down
4 changes: 3 additions & 1 deletion bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
circuit_input_builder::CallContext, error::ExecError, exec_trace::OperationRef,
operation::RWCounter,
operation::RWCounter, precompile::PrecompileCalls,
};
use eth_types::{
evm_types::{Gas, GasCost, OpcodeId, ProgramCounter},
Expand Down Expand Up @@ -132,6 +132,8 @@ impl ExecStep {
pub enum ExecState {
/// EVM Opcode ID
Op(OpcodeId),
/// Precompile call
Precompile(PrecompileCalls),
/// Virtual step Begin Tx
BeginTx,
/// Virtual step End Tx
Expand Down
28 changes: 17 additions & 11 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ impl<'a> CircuitInputStateRef<'a> {
receiver: Address,
receiver_exists: bool,
must_create: bool,
must_read_caller_balance: bool,
value: Word,
fee: Option<Word>,
) -> Result<(), Error> {
Expand Down Expand Up @@ -535,21 +536,25 @@ impl<'a> CircuitInputStateRef<'a> {
},
)?;
}

// Read the caller balance when required, skip if value == 0 otherwise
if must_read_caller_balance || !value.is_zero() {
self.push_op_reversible(
step,
AccountOp {
address: sender,
field: AccountField::Balance,
value: sender_balance,
value_prev: sender_balance_prev,
},
)?;
}

if value.is_zero() {
// Skip transfer if value == 0
return Ok(());
}

self.push_op_reversible(
step,
AccountOp {
address: sender,
field: AccountField::Balance,
value: sender_balance,
value_prev: sender_balance_prev,
},
)?;

let (_found, receiver_account) = self.sdb.get_account(&receiver);
let receiver_balance_prev = receiver_account.balance;
let receiver_balance = receiver_account.balance + value;
Expand Down Expand Up @@ -582,6 +587,7 @@ impl<'a> CircuitInputStateRef<'a> {
receiver,
receiver_exists,
must_create,
false,
value,
None,
)
Expand Down Expand Up @@ -1147,7 +1153,7 @@ impl<'a> CircuitInputStateRef<'a> {

/// Push a copy event to the state.
pub fn push_copy(&mut self, step: &mut ExecStep, event: CopyEvent) {
step.copy_rw_counter_delta = event.rw_counter_delta();
step.copy_rw_counter_delta += event.rw_counter_delta();
self.block.add_copy_event(event);
}

Expand Down
8 changes: 7 additions & 1 deletion bus-mapping/src/circuit_input_builder/tracer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ impl CircuitInputBuilderTx {
let block = crate::mock::BlockData::new_from_geth_data(geth_data.clone());
let mut builder = block.new_circuit_input_builder();
let tx = builder
.new_tx(&block.eth_block, &block.eth_block.transactions[0], true)
.new_tx(
&block.eth_block,
&block.eth_block.transactions[0],
true,
false,
)
.unwrap();
let tx_ctx = TransactionContext::new(
&block.eth_block.transactions[0],
&GethExecTrace {
gas: Gas(0),
failed: false,
invalid: false,
return_value: "".to_owned(),
struct_logs: vec![geth_step.clone()],
},
Expand Down
8 changes: 8 additions & 0 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ impl TransactionContext {
pub struct Transaction {
/// The raw transaction fields
pub tx: geth_types::Transaction,
/// Invalid tx
pub invalid_tx: bool,
/// AccessListGasCost
pub access_list_gas_cost: u64,
/// Calls made in the transaction
pub(crate) calls: Vec<Call>,
/// Execution steps
Expand All @@ -194,6 +198,7 @@ pub struct Transaction {

impl Transaction {
/// Create a new Self.
#[allow(clippy::too_many_arguments)]
pub fn new(
call_id: usize,
sdb: &StateDB,
Expand All @@ -202,6 +207,7 @@ impl Transaction {
eth_tx: &eth_types::Transaction,
is_success: bool,
is_taiko: bool,
is_invalid: bool,
) -> Result<Self, Error> {
let (found, _) = sdb.get_account(&eth_tx.from);
if !found {
Expand Down Expand Up @@ -264,6 +270,8 @@ impl Transaction {

Ok(Self {
tx,
invalid_tx: is_invalid,
access_list_gas_cost: 0,
calls: vec![call],
steps: Vec::new(),
})
Expand Down
3 changes: 3 additions & 0 deletions bus-mapping/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub use eth_types::evm_types::opcode_ids::OpcodeId;
pub use opcodes::Opcode;

pub use opcodes::Sha3CodeGen;

#[cfg(feature = "test")]
pub use opcodes::PrecompileCallArgs;
5 changes: 5 additions & 0 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ mod error_return_data_outofbound;
mod error_simple;
mod error_write_protection;

mod precompiles;

#[cfg(test)]
mod memory_expansion_test;

Expand Down Expand Up @@ -103,6 +105,9 @@ use stackonlyop::StackOnlyOpcode;
use stop::Stop;
use swap::Swap;

#[cfg(feature = "test")]
pub use callop::tests::PrecompileCallArgs;

/// Generic opcode trait which defines the logic of the
/// [`Operation`](crate::operation::Operation) that should be generated for one
/// or multiple [`ExecStep`](crate::circuit_input_builder::ExecStep) depending
Expand Down
39 changes: 29 additions & 10 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
state.call_context_write(&mut exec_step, call.call_id, field, value);
}

// Increase caller's nonce
// Increase caller's nonce when the tx is not invalid
let caller_address = call.caller_address;
let nonce_prev = state.sdb.get_account(&caller_address).1.nonce;
let nonce = if !state.tx.invalid_tx {
nonce_prev + 1
} else {
nonce_prev
};
state.account_write(
&mut exec_step,
caller_address,
AccountField::Nonce,
(nonce_prev + 1).into(),
nonce.into(),
nonce_prev.into(),
)?;

Expand Down Expand Up @@ -84,7 +89,20 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
GasCost::TX.as_u64()
} + state.tx.tx.call_data_gas_cost()
+ init_code_gas_cost;
exec_step.gas_cost = GasCost(intrinsic_gas_cost);

// Don't pay any fee or transfer any ETH for invalid transactions
let (gas_cost, value, fee) = if state.tx.invalid_tx {
(0, Word::zero(), Some(Word::zero()))
} else {
(
intrinsic_gas_cost,
call.value,
Some(state.tx.tx.gas_price * state.tx.gas()),
)
};

// Set the gas cost
exec_step.gas_cost = GasCost(gas_cost);

// Get code_hash of callee
let (_, callee_account) = state.sdb.get_account(&call.address);
Expand Down Expand Up @@ -112,9 +130,10 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
call.caller_address,
call.address,
callee_exists,
call.is_create(),
call.value,
Some(state.tx.tx.gas_price * state.tx.gas()),
!state.tx.invalid_tx && call.is_create(),
true,
value,
fee,
)?;

// In case of contract creation we wish to verify the correctness of the
Expand All @@ -139,7 +158,7 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
match (
call.is_create(),
state.is_precompiled(&call.address),
is_empty_code_hash,
is_empty_code_hash || state.tx.invalid_tx,
) {
// 1. Creation transaction.
(true, _, _) => {
Expand Down Expand Up @@ -188,13 +207,13 @@ fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<ExecStep, Erro
evm_unimplemented!("Call to precompiled is left unimplemented");
Ok(exec_step)
}
(_, _, is_empty_code_hash) => {
(_, _, do_not_run_code) => {
// 3. Call to account with empty code.
if is_empty_code_hash {
if do_not_run_code {
return Ok(exec_step);
}

// 4. Call to account with non-empty code.
// 4. Call to account with non-empty code/invalid tx.
for (field, value) in [
(CallContextField::Depth, call.depth.into()),
(
Expand Down
Loading

0 comments on commit 99ff3ef

Please sign in to comment.