Skip to content

Commit

Permalink
refactor: replace TransactTo with TxKind (#1542)
Browse files Browse the repository at this point in the history
* refactor: replace TransactTo with TxKind

* misc: apply review suggestions
  • Loading branch information
Wodann committed Jun 19, 2024
1 parent e6b52af commit 7db1adc
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 93 deletions.
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use revm::{
db::BenchmarkDB,
interpreter::analysis::to_analysed,
primitives::{address, bytes, Bytecode, Bytes, TransactTo},
primitives::{address, bytes, Bytecode, Bytes, TxKind},
Evm,
};
use std::time::Instant;
Expand All @@ -17,7 +17,7 @@ fn main() {
.modify_tx_env(|tx| {
// execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
//evm.env.tx.data = Bytes::from(hex::decode("30627b7c").unwrap());
tx.data = bytes!("8035F0CE");
})
Expand Down
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/burntpix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use revm::{
db::{CacheDB, EmptyDB},
primitives::{
address, hex, keccak256, AccountInfo, Address, Bytecode, Bytes, ExecutionResult, Output,
TransactTo, B256, U256,
TxKind, B256, U256,
},
Evm,
};
Expand Down Expand Up @@ -38,7 +38,7 @@ fn main() {
let mut evm = Evm::builder()
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TransactTo::Call(BURNTPIX_MAIN_ADDRESS);
tx.transact_to = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
tx.data = run_call_data.clone().into();
})
.with_db(db)
Expand Down
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/snailtracer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use revm::{
db::BenchmarkDB,
interpreter::analysis::to_analysed,
primitives::{address, bytes, Bytecode, Bytes, TransactTo},
primitives::{address, bytes, Bytecode, Bytes, TxKind},
Evm,
};

Expand All @@ -14,7 +14,7 @@ pub fn simple_example() {
.modify_tx_env(|tx| {
// execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.data = bytes!("30627b7c");
})
.build();
Expand Down
4 changes: 2 additions & 2 deletions bins/revm-test/src/bin/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use revm::{
db::BenchmarkDB,
primitives::{Bytecode, TransactTo, U256},
primitives::{Bytecode, TxKind, U256},
Evm,
};

Expand All @@ -16,7 +16,7 @@ fn main() {
.parse()
.unwrap();
tx.value = U256::from(10);
tx.transact_to = TransactTo::Call(
tx.transact_to = TxKind::Call(
"0x0000000000000000000000000000000000000000"
.parse()
.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use revm::{
db::BenchmarkDB,
inspector_handle_register,
inspectors::TracerEip3155,
primitives::{Address, Bytecode, TransactTo},
primitives::{Address, Bytecode, TxKind},
Evm,
};
use std::io::Error as IoError;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Cmd {
tx.caller = "0x0000000000000000000000000000000000000001"
.parse()
.unwrap();
tx.transact_to = TransactTo::Call(Address::ZERO);
tx.transact_to = TxKind::Call(Address::ZERO);
tx.data = input;
})
.build();
Expand Down
6 changes: 3 additions & 3 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use revm::{
inspectors::TracerEip3155,
primitives::{
calc_excess_blob_gas, keccak256, Bytecode, Bytes, EVMResultGeneric, Env, Eof,
ExecutionResult, SpecId, TransactTo, B256, EOF_MAGIC_BYTES, U256,
ExecutionResult, SpecId, TxKind, B256, EOF_MAGIC_BYTES, U256,
},
Evm, State,
};
Expand Down Expand Up @@ -364,8 +364,8 @@ pub fn execute_test_suite(
.collect();

let to = match unit.transaction.to {
Some(add) => TransactTo::Call(add),
None => TransactTo::Create,
Some(add) => TxKind::Call(add),
None => TxKind::Create,
};
env.tx.transact_to = to;

Expand Down
8 changes: 5 additions & 3 deletions crates/interpreter/src/interpreter/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use revm_primitives::TxKind;

use super::analysis::to_analysed;
use crate::{
primitives::{Address, Bytecode, Bytes, Env, TransactTo, B256, U256},
primitives::{Address, Bytecode, Bytes, Env, B256, U256},
CallInputs,
};

Expand Down Expand Up @@ -50,8 +52,8 @@ impl Contract {
#[inline]
pub fn new_env(env: &Env, bytecode: Bytecode, hash: Option<B256>) -> Self {
let contract_address = match env.tx.transact_to {
TransactTo::Call(caller) => caller,
TransactTo::Create => Address::ZERO,
TxKind::Call(caller) => caller,
TxKind::Create => Address::ZERO,
};
Self::new(
env.tx.data.clone(),
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/interpreter_action/call_inputs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::primitives::{Address, Bytes, TransactTo, TxEnv, U256};
use crate::primitives::{Address, Bytes, TxEnv, TxKind, U256};
use core::ops::Range;
use std::boxed::Box;

Expand Down Expand Up @@ -47,7 +47,7 @@ impl CallInputs {
///
/// Returns `None` if the transaction is not a call.
pub fn new(tx_env: &TxEnv, gas_limit: u64) -> Option<Self> {
let TransactTo::Call(target_address) = tx_env.transact_to else {
let TxKind::Call(target_address) = tx_env.transact_to else {
return None;
};
Some(CallInputs {
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/interpreter_action/create_inputs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use crate::primitives::CreateScheme;
use crate::primitives::{Address, Bytes, TransactTo, TxEnv, U256};
use crate::primitives::{Address, Bytes, TxEnv, TxKind, U256};
use std::boxed::Box;

/// Inputs for a create call.
Expand All @@ -21,7 +21,7 @@ pub struct CreateInputs {
impl CreateInputs {
/// Creates new create inputs.
pub fn new(tx_env: &TxEnv, gas_limit: u64) -> Option<Self> {
let TransactTo::Create = tx_env.transact_to else {
let TxKind::Create = tx_env.transact_to else {
return None;
};

Expand Down
41 changes: 5 additions & 36 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod handler_cfg;

use alloy_primitives::TxKind;
pub use handler_cfg::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};

use crate::{
Expand Down Expand Up @@ -503,7 +504,7 @@ pub struct TxEnv {
/// The gas price of the transaction.
pub gas_price: U256,
/// The destination of the transaction.
pub transact_to: TransactTo,
pub transact_to: TxKind,
/// The value sent to `transact_to`.
pub value: U256,
/// The data of the transaction.
Expand Down Expand Up @@ -585,7 +586,7 @@ impl Default for TxEnv {
gas_limit: u64::MAX,
gas_price: U256::ZERO,
gas_priority_fee: None,
transact_to: TransactTo::Call(Address::ZERO), // will do nothing
transact_to: TxKind::Call(Address::ZERO), // will do nothing
value: U256::ZERO,
data: Bytes::new(),
chain_id: None,
Expand Down Expand Up @@ -658,40 +659,8 @@ pub struct OptimismFields {
pub enveloped_tx: Option<Bytes>,
}

/// Transaction destination.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TransactTo {
/// Simple call to an address.
Call(Address),
/// Contract creation.
Create,
}

impl TransactTo {
/// Calls the given address.
#[inline]
pub fn call(address: Address) -> Self {
Self::Call(address)
}

/// Creates a contract.
#[inline]
pub fn create() -> Self {
Self::Create
}
/// Returns `true` if the transaction is `Call`.
#[inline]
pub fn is_call(&self) -> bool {
matches!(self, Self::Call(_))
}

/// Returns `true` if the transaction is `Create` or `Create2`.
#[inline]
pub fn is_create(&self) -> bool {
matches!(self, Self::Create)
}
}
/// Transaction destination
pub type TransactTo = TxKind;

/// Create scheme.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod state;
pub mod utilities;
pub use alloy_primitives::{
self, address, b256, bytes, fixed_bytes, hex, hex_literal, ruint, uint, Address, Bytes,
FixedBytes, Log, LogData, B256, I256, U256,
FixedBytes, Log, LogData, TxKind, B256, I256, U256,
};
pub use bitvec;
pub use bytecode::*;
Expand Down
8 changes: 4 additions & 4 deletions crates/revm/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use criterion::{
use revm::{
db::BenchmarkDB,
interpreter::{analysis::to_analysed, Contract, DummyHost, Interpreter},
primitives::{address, bytes, hex, BerlinSpec, Bytecode, Bytes, TransactTo, U256},
primitives::{address, bytes, hex, BerlinSpec, Bytecode, Bytes, TxKind, U256},
Evm,
};
use revm_interpreter::{opcode::make_instruction_table, SharedMemory, EMPTY_SHARED_MEMORY};
Expand All @@ -14,7 +14,7 @@ fn analysis(c: &mut Criterion) {
let evm = Evm::builder()
.modify_tx_env(|tx| {
tx.caller = address!("0000000000000000000000000000000000000002");
tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
// evm.env.tx.data = bytes!("30627b7c");
tx.data = bytes!("8035F0CE");
})
Expand Down Expand Up @@ -50,7 +50,7 @@ fn snailtracer(c: &mut Criterion) {
.with_db(BenchmarkDB::new_bytecode(bytecode(SNAILTRACER)))
.modify_tx_env(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.data = bytes!("30627b7c");
})
.build();
Expand All @@ -70,7 +70,7 @@ fn transfer(c: &mut Criterion) {
.with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
.modify_tx_env(|tx| {
tx.caller = address!("0000000000000000000000000000000000000001");
tx.transact_to = TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.value = U256::from(10);
})
.build();
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ mod test {
inspector::inspector_handle_register,
inspectors::NoOpInspector,
primitives::{
address, AccountInfo, Address, Bytecode, Bytes, PrecompileResult, TransactTo, U256,
address, AccountInfo, Address, Bytecode, Bytes, PrecompileResult, TxKind, U256,
},
Context, ContextPrecompile, ContextStatefulPrecompile, Evm, InMemoryDB, InnerEvmContext,
};
Expand Down Expand Up @@ -474,7 +474,7 @@ mod test {
.modify_db(|db| {
db.insert_account_info(to_addr, AccountInfo::new(U256::ZERO, 0, code_hash, code))
})
.modify_tx_env(|tx| tx.transact_to = TransactTo::Call(to_addr))
.modify_tx_env(|tx| tx.transact_to = TxKind::Call(to_addr))
// we need to use handle register box to capture the custom context in the handle
// register
.append_handler_register_box(Box::new(move |handler| {
Expand Down Expand Up @@ -523,7 +523,7 @@ mod test {
.modify_db(|db| {
db.insert_account_info(to_addr, AccountInfo::new(U256::ZERO, 0, code_hash, code))
})
.modify_tx_env(|tx| tx.transact_to = TransactTo::Call(to_addr))
.modify_tx_env(|tx| tx.transact_to = TxKind::Call(to_addr))
.append_handler_register(|handler| {
handler.instruction_table.insert(0xEF, custom_instruction)
})
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
primitives::{
specification::SpecId, BlockEnv, Bytes, CfgEnv, EVMError, EVMResult, EnvWithHandlerCfg,
ExecutionResult, HandlerCfg, ResultAndState, TransactTo, TxEnv,
ExecutionResult, HandlerCfg, ResultAndState, TxEnv, TxKind,
},
Context, ContextWithHandlerCfg, Frame, FrameOrResult, FrameResult,
};
Expand Down Expand Up @@ -344,11 +344,11 @@ impl<EXT, DB: Database> Evm<'_, EXT, DB> {
let exec = self.handler.execution();
// call inner handling of call/create
let first_frame_or_result = match ctx.evm.env.tx.transact_to {
TransactTo::Call(_) => exec.call(
TxKind::Call(_) => exec.call(
ctx,
CallInputs::new_boxed(&ctx.evm.env.tx, gas_limit).unwrap(),
)?,
TransactTo::Create => {
TxKind::Create => {
// if first byte of data is magic 0xEF00, then it is EOFCreate.
if spec_id.is_enabled_in(SpecId::PRAGUE)
&& ctx
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/handler/mainnet/pre_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
db::Database,
Account, EVMError, Env, Spec,
SpecId::{CANCUN, PRAGUE, SHANGHAI},
TransactTo, BLOCKHASH_STORAGE_ADDRESS, U256,
TxKind, BLOCKHASH_STORAGE_ADDRESS, U256,
},
Context, ContextPrecompiles,
};
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn deduct_caller_inner<SPEC: Spec>(caller_account: &mut Account, env: &Env)
caller_account.info.balance = caller_account.info.balance.saturating_sub(gas_cost);

// bump the nonce for calls. Nonce for CREATE will be bumped in `handle_create`.
if matches!(env.tx.transact_to, TransactTo::Call(_)) {
if matches!(env.tx.transact_to, TxKind::Call(_)) {
// Nonce is already checked
caller_account.info.nonce = caller_account.info.nonce.saturating_add(1);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/inspector/customprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod test {
})
.modify_tx_env(|tx| {
tx.caller = address!("5fdcca53617f4d2b9134b29090c87d01058e27e0");
tx.transact_to = crate::primitives::TransactTo::Call(callee);
tx.transact_to = crate::primitives::TxKind::Call(callee);
tx.data = crate::primitives::Bytes::new();
tx.value = crate::primitives::U256::ZERO;
})
Expand Down
5 changes: 2 additions & 3 deletions crates/revm/src/inspector/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod tests {
db::BenchmarkDB,
inspector::inspector_handle_register,
interpreter::opcode,
primitives::{address, Bytecode, Bytes, TransactTo},
primitives::{address, Bytecode, Bytes, TxKind},
Evm,
};

Expand All @@ -189,8 +189,7 @@ mod tests {
.modify_tx_env(|tx| {
tx.clear();
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to =
TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.gas_limit = 21100;
})
.append_handler_register(inspector_handle_register)
Expand Down
5 changes: 2 additions & 3 deletions crates/revm/src/inspector/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ mod tests {
db::BenchmarkDB,
inspector::inspector_handle_register,
interpreter::opcode,
primitives::{address, Bytecode, Bytes, TransactTo},
primitives::{address, Bytecode, Bytes, TxKind},
Evm,
};

Expand All @@ -360,8 +360,7 @@ mod tests {
.modify_tx_env(|tx| {
tx.clear();
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to =
TransactTo::Call(address!("0000000000000000000000000000000000000000"));
tx.transact_to = TxKind::Call(address!("0000000000000000000000000000000000000000"));
tx.gas_limit = 21100;
})
.append_handler_register(inspector_handle_register)
Expand Down
Loading

0 comments on commit 7db1adc

Please sign in to comment.