From a7dd2bfb32a2266cd26a136f39bf2da56cd8aa41 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 10 Nov 2023 08:48:07 +0100 Subject: [PATCH] Move origin and gas price into transaction context (#210) --- interpreter/src/eval/mod.rs | 67 ++++++++++++++++++---------------- interpreter/src/eval/system.rs | 57 ++++++++++++++++------------- interpreter/src/lib.rs | 3 +- interpreter/src/runtime.rs | 21 ++++++++--- interpreter/tests/usability.rs | 48 ++++++++++++------------ jsontests/src/run.rs | 2 - src/backend/in_memory.rs | 18 +++------ src/standard/invoker.rs | 49 ++++++++++++++++++++----- 8 files changed, 154 insertions(+), 111 deletions(-) diff --git a/interpreter/src/eval/mod.rs b/interpreter/src/eval/mod.rs index b808598e9..7b3651ed5 100644 --- a/interpreter/src/eval/mod.rs +++ b/interpreter/src/eval/mod.rs @@ -6,7 +6,7 @@ mod misc; mod system; use crate::{ - CallCreateTrap, ExitException, ExitResult, ExitSucceed, Machine, Opcode, RuntimeBackend, + CallCreateTrap, ExitException, ExitResult, ExitSucceed, Machine, Opcode, RuntimeHandle, RuntimeState, }; use core::marker::PhantomData; @@ -19,6 +19,9 @@ pub type Efn = fn(&mut Machine, &mut H, Opcode, usize) -> Control>([F; 256], PhantomData<(S, H, Tr)>); +unsafe impl Send for Etable {} +unsafe impl Sync for Etable {} + impl Deref for Etable { type Target = [F; 256]; @@ -179,7 +182,7 @@ impl Etable { } } -impl, H: RuntimeBackend, Tr: CallCreateTrap> Etable { +impl, H: RuntimeHandle, Tr: CallCreateTrap> Etable { /// Runtime Etable. pub const fn runtime() -> Etable { let mut table = Self::core(); @@ -1217,7 +1220,7 @@ fn eval_unknown( Control::Exit(ExitException::InvalidOpcode(opcode).into()) } -fn eval_sha3, H: RuntimeBackend, Tr>( +fn eval_sha3, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1226,7 +1229,7 @@ fn eval_sha3, H: RuntimeBackend, Tr>( self::system::sha3(machine) } -fn eval_address, H: RuntimeBackend, Tr>( +fn eval_address, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1235,7 +1238,7 @@ fn eval_address, H: RuntimeBackend, Tr>( self::system::address(machine) } -fn eval_balance, H: RuntimeBackend, Tr>( +fn eval_balance, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1244,7 +1247,7 @@ fn eval_balance, H: RuntimeBackend, Tr>( self::system::balance(machine, handle) } -fn eval_selfbalance, H: RuntimeBackend, Tr>( +fn eval_selfbalance, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1253,7 +1256,7 @@ fn eval_selfbalance, H: RuntimeBackend, Tr>( self::system::selfbalance(machine, handle) } -fn eval_origin, H: RuntimeBackend, Tr>( +fn eval_origin, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1262,7 +1265,7 @@ fn eval_origin, H: RuntimeBackend, Tr>( self::system::origin(machine, handle) } -fn eval_caller, H: RuntimeBackend, Tr>( +fn eval_caller, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1271,7 +1274,7 @@ fn eval_caller, H: RuntimeBackend, Tr>( self::system::caller(machine) } -fn eval_callvalue, H: RuntimeBackend, Tr>( +fn eval_callvalue, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1280,7 +1283,7 @@ fn eval_callvalue, H: RuntimeBackend, Tr>( self::system::callvalue(machine) } -fn eval_gasprice, H: RuntimeBackend, Tr>( +fn eval_gasprice, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1289,7 +1292,7 @@ fn eval_gasprice, H: RuntimeBackend, Tr>( self::system::gasprice(machine, handle) } -fn eval_extcodesize, H: RuntimeBackend, Tr>( +fn eval_extcodesize, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1298,7 +1301,7 @@ fn eval_extcodesize, H: RuntimeBackend, Tr>( self::system::extcodesize(machine, handle) } -fn eval_extcodehash, H: RuntimeBackend, Tr>( +fn eval_extcodehash, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1307,7 +1310,7 @@ fn eval_extcodehash, H: RuntimeBackend, Tr>( self::system::extcodehash(machine, handle) } -fn eval_extcodecopy, H: RuntimeBackend, Tr>( +fn eval_extcodecopy, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1316,7 +1319,7 @@ fn eval_extcodecopy, H: RuntimeBackend, Tr>( self::system::extcodecopy(machine, handle) } -fn eval_returndatasize, H: RuntimeBackend, Tr>( +fn eval_returndatasize, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1325,7 +1328,7 @@ fn eval_returndatasize, H: RuntimeBackend, Tr>( self::system::returndatasize(machine) } -fn eval_returndatacopy, H: RuntimeBackend, Tr>( +fn eval_returndatacopy, H: RuntimeHandle, Tr>( machine: &mut Machine, _handle: &mut H, _opcode: Opcode, @@ -1334,7 +1337,7 @@ fn eval_returndatacopy, H: RuntimeBackend, Tr>( self::system::returndatacopy(machine) } -fn eval_blockhash, H: RuntimeBackend, Tr>( +fn eval_blockhash, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1343,7 +1346,7 @@ fn eval_blockhash, H: RuntimeBackend, Tr>( self::system::blockhash(machine, handle) } -fn eval_coinbase, H: RuntimeBackend, Tr>( +fn eval_coinbase, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1352,7 +1355,7 @@ fn eval_coinbase, H: RuntimeBackend, Tr>( self::system::coinbase(machine, handle) } -fn eval_timestamp, H: RuntimeBackend, Tr>( +fn eval_timestamp, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1361,7 +1364,7 @@ fn eval_timestamp, H: RuntimeBackend, Tr>( self::system::timestamp(machine, handle) } -fn eval_number, H: RuntimeBackend, Tr>( +fn eval_number, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1370,7 +1373,7 @@ fn eval_number, H: RuntimeBackend, Tr>( self::system::number(machine, handle) } -fn eval_difficulty, H: RuntimeBackend, Tr>( +fn eval_difficulty, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1379,7 +1382,7 @@ fn eval_difficulty, H: RuntimeBackend, Tr>( self::system::prevrandao(machine, handle) } -fn eval_gaslimit, H: RuntimeBackend, Tr>( +fn eval_gaslimit, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1388,7 +1391,7 @@ fn eval_gaslimit, H: RuntimeBackend, Tr>( self::system::gaslimit(machine, handle) } -fn eval_sload, H: RuntimeBackend, Tr>( +fn eval_sload, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1397,7 +1400,7 @@ fn eval_sload, H: RuntimeBackend, Tr>( self::system::sload(machine, handle) } -fn eval_sstore, H: RuntimeBackend, Tr>( +fn eval_sstore, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1406,7 +1409,7 @@ fn eval_sstore, H: RuntimeBackend, Tr>( self::system::sstore(machine, handle) } -fn eval_gas, H: RuntimeBackend, Tr>( +fn eval_gas, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1415,7 +1418,7 @@ fn eval_gas, H: RuntimeBackend, Tr>( self::system::gas(machine, handle) } -fn eval_log0, H: RuntimeBackend, Tr>( +fn eval_log0, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1424,7 +1427,7 @@ fn eval_log0, H: RuntimeBackend, Tr>( self::system::log(machine, 0, handle) } -fn eval_log1, H: RuntimeBackend, Tr>( +fn eval_log1, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1433,7 +1436,7 @@ fn eval_log1, H: RuntimeBackend, Tr>( self::system::log(machine, 1, handle) } -fn eval_log2, H: RuntimeBackend, Tr>( +fn eval_log2, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1442,7 +1445,7 @@ fn eval_log2, H: RuntimeBackend, Tr>( self::system::log(machine, 2, handle) } -fn eval_log3, H: RuntimeBackend, Tr>( +fn eval_log3, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1451,7 +1454,7 @@ fn eval_log3, H: RuntimeBackend, Tr>( self::system::log(machine, 3, handle) } -fn eval_log4, H: RuntimeBackend, Tr>( +fn eval_log4, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1460,7 +1463,7 @@ fn eval_log4, H: RuntimeBackend, Tr>( self::system::log(machine, 4, handle) } -fn eval_suicide, H: RuntimeBackend, Tr>( +fn eval_suicide, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1469,7 +1472,7 @@ fn eval_suicide, H: RuntimeBackend, Tr>( self::system::suicide(machine, handle) } -fn eval_chainid, H: RuntimeBackend, Tr>( +fn eval_chainid, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, @@ -1478,7 +1481,7 @@ fn eval_chainid, H: RuntimeBackend, Tr>( self::system::chainid(machine, handle) } -fn eval_basefee, H: RuntimeBackend, Tr>( +fn eval_basefee, H: RuntimeHandle, Tr>( machine: &mut Machine, handle: &mut H, _opcode: Opcode, diff --git a/interpreter/src/eval/system.rs b/interpreter/src/eval/system.rs index 6e183faa8..8bbde22fc 100644 --- a/interpreter/src/eval/system.rs +++ b/interpreter/src/eval/system.rs @@ -1,5 +1,5 @@ use super::Control; -use crate::{ExitException, ExitFatal, ExitSucceed, Log, Machine, RuntimeBackend, RuntimeState}; +use crate::{ExitException, ExitFatal, ExitSucceed, Log, Machine, RuntimeHandle, RuntimeState}; use alloc::vec::Vec; use primitive_types::{H256, U256}; use sha3::{Digest, Keccak256}; @@ -23,7 +23,7 @@ pub fn sha3, Tr>(machine: &mut Machine) -> Control Control::Continue } -pub fn chainid, H: RuntimeBackend, Tr>( +pub fn chainid, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -39,7 +39,7 @@ pub fn address, Tr>(machine: &mut Machine) -> Control< Control::Continue } -pub fn balance, H: RuntimeBackend, Tr>( +pub fn balance, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -50,7 +50,7 @@ pub fn balance, H: RuntimeBackend, Tr>( Control::Continue } -pub fn selfbalance, H: RuntimeBackend, Tr>( +pub fn selfbalance, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -62,11 +62,11 @@ pub fn selfbalance, H: RuntimeBackend, Tr>( Control::Continue } -pub fn origin, H: RuntimeBackend, Tr>( +pub fn origin, H: RuntimeHandle, Tr>( machine: &mut Machine, - handler: &H, + _handler: &H, ) -> Control { - let ret = H256::from(handler.origin()); + let ret = H256::from(machine.state.as_ref().transaction_context.origin); push!(machine, ret); Control::Continue @@ -92,18 +92,23 @@ pub fn callvalue, Tr>(machine: &mut Machine) -> Contro Control::Continue } -pub fn gasprice, H: RuntimeBackend, Tr>( +pub fn gasprice, H: RuntimeHandle, Tr>( machine: &mut Machine, - handler: &H, + _handler: &H, ) -> Control { let mut ret = H256::default(); - handler.gas_price().to_big_endian(&mut ret[..]); + machine + .state + .as_ref() + .transaction_context + .gas_price + .to_big_endian(&mut ret[..]); push!(machine, ret); Control::Continue } -pub fn basefee, H: RuntimeBackend, Tr>( +pub fn basefee, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -114,7 +119,7 @@ pub fn basefee, H: RuntimeBackend, Tr>( Control::Continue } -pub fn extcodesize, H: RuntimeBackend, Tr>( +pub fn extcodesize, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -126,7 +131,7 @@ pub fn extcodesize, H: RuntimeBackend, Tr>( Control::Continue } -pub fn extcodehash, H: RuntimeBackend, Tr>( +pub fn extcodehash, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -138,7 +143,7 @@ pub fn extcodehash, H: RuntimeBackend, Tr>( Control::Continue } -pub fn extcodecopy, H: RuntimeBackend, Tr>( +pub fn extcodecopy, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -190,7 +195,7 @@ pub fn returndatacopy, Tr>(machine: &mut Machine) -> C } } -pub fn blockhash, H: RuntimeBackend, Tr>( +pub fn blockhash, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -200,7 +205,7 @@ pub fn blockhash, H: RuntimeBackend, Tr>( Control::Continue } -pub fn coinbase, H: RuntimeBackend, Tr>( +pub fn coinbase, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -208,7 +213,7 @@ pub fn coinbase, H: RuntimeBackend, Tr>( Control::Continue } -pub fn timestamp, H: RuntimeBackend, Tr>( +pub fn timestamp, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -216,7 +221,7 @@ pub fn timestamp, H: RuntimeBackend, Tr>( Control::Continue } -pub fn number, H: RuntimeBackend, Tr>( +pub fn number, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -224,7 +229,7 @@ pub fn number, H: RuntimeBackend, Tr>( Control::Continue } -pub fn difficulty, H: RuntimeBackend, Tr>( +pub fn difficulty, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -232,7 +237,7 @@ pub fn difficulty, H: RuntimeBackend, Tr>( Control::Continue } -pub fn prevrandao, H: RuntimeBackend, Tr>( +pub fn prevrandao, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -244,7 +249,7 @@ pub fn prevrandao, H: RuntimeBackend, Tr>( } } -pub fn gaslimit, H: RuntimeBackend, Tr>( +pub fn gaslimit, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &H, ) -> Control { @@ -252,7 +257,7 @@ pub fn gaslimit, H: RuntimeBackend, Tr>( Control::Continue } -pub fn sload, H: RuntimeBackend, Tr>( +pub fn sload, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -264,7 +269,7 @@ pub fn sload, H: RuntimeBackend, Tr>( Control::Continue } -pub fn sstore, H: RuntimeBackend, Tr>( +pub fn sstore, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { @@ -277,7 +282,7 @@ pub fn sstore, H: RuntimeBackend, Tr>( } } -pub fn gas, H: RuntimeBackend, Tr>( +pub fn gas, H: RuntimeHandle, Tr>( machine: &mut Machine, _handler: &H, ) -> Control { @@ -286,7 +291,7 @@ pub fn gas, H: RuntimeBackend, Tr>( Control::Continue } -pub fn log, H: RuntimeBackend, Tr>( +pub fn log, H: RuntimeHandle, Tr>( machine: &mut Machine, n: u8, handler: &mut H, @@ -323,7 +328,7 @@ pub fn log, H: RuntimeBackend, Tr>( } } -pub fn suicide, H: RuntimeBackend, Tr>( +pub fn suicide, H: RuntimeHandle, Tr>( machine: &mut Machine, handler: &mut H, ) -> Control { diff --git a/interpreter/src/lib.rs b/interpreter/src/lib.rs index 29fe09459..16ad618b2 100644 --- a/interpreter/src/lib.rs +++ b/interpreter/src/lib.rs @@ -21,7 +21,8 @@ pub use crate::eval::{Control, Efn, Etable}; pub use crate::memory::Memory; pub use crate::opcode::Opcode; pub use crate::runtime::{ - CallCreateTrap, Context, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeState, Transfer, + CallCreateTrap, Context, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, + RuntimeHandle, RuntimeState, TransactionContext, Transfer, }; pub use crate::stack::Stack; pub use crate::valids::Valids; diff --git a/interpreter/src/runtime.rs b/interpreter/src/runtime.rs index 0f77e3d07..f45b3e91c 100644 --- a/interpreter/src/runtime.rs +++ b/interpreter/src/runtime.rs @@ -1,4 +1,5 @@ use crate::{ExitError, Opcode}; +use alloc::rc::Rc; use primitive_types::{H160, H256, U256}; use sha3::{Digest, Keccak256}; @@ -7,6 +8,7 @@ use sha3::{Digest, Keccak256}; pub struct RuntimeState { /// Runtime context. pub context: Context, + pub transaction_context: Rc, /// Return data buffer. pub retbuf: Vec, /// Current gas. @@ -36,6 +38,14 @@ pub struct Context { pub apparent_value: U256, } +#[derive(Clone, Debug)] +pub struct TransactionContext { + /// Gas price. + pub gas_price: U256, + /// Origin. + pub origin: H160, +} + /// Transfer from source to target, with given value. #[derive(Clone, Debug)] pub struct Transfer { @@ -65,7 +75,7 @@ impl CallCreateTrap for Opcode { } } -pub trait RuntimeBaseBackend { +pub trait RuntimeEnvironment { /// Get environmental block hash. fn block_hash(&self, number: U256) -> H256; /// Get environmental block number. @@ -84,11 +94,9 @@ pub trait RuntimeBaseBackend { fn block_base_fee_per_gas(&self) -> U256; /// Get environmental chain ID. fn chain_id(&self) -> U256; - /// Get the gas price value. - fn gas_price(&self) -> U256; - /// Get execution origin. - fn origin(&self) -> H160; +} +pub trait RuntimeBaseBackend { /// Get balance of address. fn balance(&self, address: H160) -> U256; /// Get code size of address. @@ -139,3 +147,6 @@ pub trait RuntimeBackend: RuntimeBaseBackend { /// Increase the nonce value. fn inc_nonce(&mut self, address: H160) -> Result<(), ExitError>; } + +pub trait RuntimeHandle: RuntimeBackend + RuntimeEnvironment {} +impl RuntimeHandle for T {} diff --git a/interpreter/tests/usability.rs b/interpreter/tests/usability.rs index ffab5fd1c..e1dfbef67 100644 --- a/interpreter/tests/usability.rs +++ b/interpreter/tests/usability.rs @@ -1,6 +1,7 @@ use evm_interpreter::{ Capture, Context, Control, Etable, ExitError, ExitSucceed, Log, Machine, Opcode, - RuntimeBackend, RuntimeBaseBackend, RuntimeState, Transfer, + RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, RuntimeState, TransactionContext, + Transfer, }; use primitive_types::{H160, H256, U256}; use std::rc::Rc; @@ -57,54 +58,50 @@ fn etable_wrap2() { pub struct UnimplementedHandler; -impl<'a> RuntimeBaseBackend for UnimplementedHandler { - fn balance(&self, _address: H160) -> U256 { - unimplemented!() - } - fn code_size(&self, _address: H160) -> U256 { - unimplemented!() - } - fn code_hash(&self, _address: H160) -> H256 { +impl RuntimeEnvironment for UnimplementedHandler { + fn block_hash(&self, _number: U256) -> H256 { unimplemented!() } - fn code(&self, _address: H160) -> Vec { + fn block_number(&self) -> U256 { unimplemented!() } - fn storage(&self, _address: H160, _index: H256) -> H256 { + fn block_coinbase(&self) -> H160 { unimplemented!() } - - fn gas_price(&self) -> U256 { + fn block_timestamp(&self) -> U256 { unimplemented!() } - fn origin(&self) -> H160 { + fn block_difficulty(&self) -> U256 { unimplemented!() } - fn block_hash(&self, _number: U256) -> H256 { + fn block_randomness(&self) -> Option { unimplemented!() } - fn block_number(&self) -> U256 { + fn block_gas_limit(&self) -> U256 { unimplemented!() } - fn block_coinbase(&self) -> H160 { + fn block_base_fee_per_gas(&self) -> U256 { unimplemented!() } - fn block_timestamp(&self) -> U256 { + fn chain_id(&self) -> U256 { unimplemented!() } - fn block_difficulty(&self) -> U256 { +} + +impl<'a> RuntimeBaseBackend for UnimplementedHandler { + fn balance(&self, _address: H160) -> U256 { unimplemented!() } - fn block_randomness(&self) -> Option { + fn code_size(&self, _address: H160) -> U256 { unimplemented!() } - fn block_gas_limit(&self) -> U256 { + fn code_hash(&self, _address: H160) -> H256 { unimplemented!() } - fn block_base_fee_per_gas(&self) -> U256 { + fn code(&self, _address: H160) -> Vec { unimplemented!() } - fn chain_id(&self) -> U256 { + fn storage(&self, _address: H160, _index: H256) -> H256 { unimplemented!() } @@ -183,6 +180,11 @@ fn etable_runtime() { caller: H160::default(), apparent_value: U256::default(), }, + transaction_context: TransactionContext { + gas_price: U256::default(), + origin: H160::default(), + } + .into(), retbuf: Vec::new(), gas: U256::zero(), }, diff --git a/jsontests/src/run.rs b/jsontests/src/run.rs index 95867b8dc..4b2c4304b 100644 --- a/jsontests/src/run.rs +++ b/jsontests/src/run.rs @@ -29,8 +29,6 @@ pub fn run_test(test_name: &str, test: Test) -> Result<(), Error> { block_gas_limit: test.env.current_gas_limit, block_base_fee_per_gas: U256::zero(), // TODO: fill in this field. chain_id: U256::zero(), // TODO: fill in this field. - gas_price: test.transaction.gas_price, - origin: test.transaction.sender, }; let state = test diff --git a/src/backend/in_memory.rs b/src/backend/in_memory.rs index 982422552..c23d4edef 100644 --- a/src/backend/in_memory.rs +++ b/src/backend/in_memory.rs @@ -1,6 +1,6 @@ use crate::{ - ExitError, ExitException, Log, RuntimeBackend, RuntimeBaseBackend, TransactionalBackend, - TransactionalMergeStrategy, Transfer, + ExitError, ExitException, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, + TransactionalBackend, TransactionalMergeStrategy, Transfer, }; use alloc::collections::{BTreeMap, BTreeSet}; use primitive_types::{H160, H256, U256}; @@ -16,8 +16,6 @@ pub struct InMemoryEnvironment { pub block_gas_limit: U256, pub block_base_fee_per_gas: U256, pub chain_id: U256, - pub gas_price: U256, - pub origin: H160, } #[derive(Clone, Debug, Default)] @@ -59,7 +57,7 @@ impl InMemoryBackend { } } -impl RuntimeBaseBackend for InMemoryBackend { +impl RuntimeEnvironment for InMemoryBackend { fn block_hash(&self, number: U256) -> H256 { self.environment .block_hashes @@ -99,15 +97,9 @@ impl RuntimeBaseBackend for InMemoryBackend { fn chain_id(&self) -> U256 { self.environment.chain_id } +} - fn gas_price(&self) -> U256 { - self.environment.gas_price - } - - fn origin(&self) -> H160 { - self.environment.origin - } - +impl RuntimeBaseBackend for InMemoryBackend { fn balance(&self, address: H160) -> U256 { self.current_layer() .state diff --git a/src/standard/invoker.rs b/src/standard/invoker.rs index a919bb845..1b6998bd1 100644 --- a/src/standard/invoker.rs +++ b/src/standard/invoker.rs @@ -2,8 +2,8 @@ use super::{gasometer::TransactionCost, Config, Etable, GasedMachine, Gasometer, use crate::call_create::{CallCreateTrapData, CallTrapData, CreateTrapData}; use crate::{ Capture, Context, ExitError, ExitException, ExitResult, Gasometer as GasometerT, - GasometerMergeStrategy, Invoker as InvokerT, Opcode, RuntimeBackend, RuntimeState, - TransactionalBackend, TransactionalMergeStrategy, Transfer, + GasometerMergeStrategy, Invoker as InvokerT, Opcode, RuntimeHandle, RuntimeState, + TransactionContext, TransactionalBackend, TransactionalMergeStrategy, Transfer, }; use alloc::rc::Rc; use core::cmp::min; @@ -14,11 +14,13 @@ pub enum CallCreateTrapPrepareData { Call { gas_limit: u64, is_static: bool, + transaction_context: Rc, trap: CallTrapData, }, Create { gas_limit: u64, is_static: bool, + transaction_context: Rc, trap: CreateTrapData, }, } @@ -52,7 +54,7 @@ impl<'config> Invoker<'config> { etable: &Etable, ) -> ExitResult where - H: RuntimeBackend + TransactionalBackend, + H: RuntimeHandle + TransactionalBackend, { handler.push_substate(); @@ -86,6 +88,11 @@ impl<'config> Invoker<'config> { self.config.memory_limit, RuntimeState { context, + transaction_context: TransactionContext { + origin: caller, + gas_price, + } + .into(), retbuf: Vec::new(), gas: U256::zero(), }, @@ -147,7 +154,7 @@ impl<'config> Invoker<'config> { impl<'config, H> InvokerT, H, Opcode> for Invoker<'config> where - H: RuntimeBackend + TransactionalBackend, + H: RuntimeHandle + TransactionalBackend, { type Interrupt = Infallible; type CallCreateTrapPrepareData = CallCreateTrapPrepareData; @@ -201,15 +208,19 @@ where } }; + let transaction_context = machine.machine.state.as_ref().transaction_context.clone(); + Capture::Exit(Ok(match trap_data { CallCreateTrapData::Call(call_trap_data) => CallCreateTrapPrepareData::Call { gas_limit, is_static, + transaction_context, trap: call_trap_data, }, CallCreateTrapData::Create(create_trap_data) => CallCreateTrapPrepareData::Create { gas_limit, is_static, + transaction_context, trap: create_trap_data, }, })) @@ -224,13 +235,29 @@ where CallCreateTrapPrepareData::Create { gas_limit, is_static, + transaction_context, + trap, + } => enter_create_trap_stack( + gas_limit, + is_static, + transaction_context, trap, - } => enter_create_trap_stack(gas_limit, is_static, trap, handler, self.config), + handler, + self.config, + ), CallCreateTrapPrepareData::Call { gas_limit, is_static, + transaction_context, + trap, + } => enter_call_trap_stack( + gas_limit, + is_static, + transaction_context, trap, - } => enter_call_trap_stack(gas_limit, is_static, trap, handler, self.config), + handler, + self.config, + ), } } @@ -315,12 +342,13 @@ where fn enter_create_trap_stack<'config, H>( gas_limit: u64, is_static: bool, + transaction_context: Rc, trap_data: CreateTrapData, handler: &mut H, config: &'config Config, ) -> Result<(CallCreateTrapEnterData, GasedMachine<'config>), ExitError> where - H: RuntimeBackend + TransactionalBackend, + H: RuntimeHandle + TransactionalBackend, { handler.push_substate(); @@ -374,6 +402,7 @@ where config.memory_limit, RuntimeState { context, + transaction_context, retbuf: Vec::new(), gas: U256::zero(), }, @@ -411,7 +440,7 @@ fn exit_create_trap_stack_no_exit_substate<'config, H>( config: &'config Config, ) -> Result where - H: RuntimeBackend + TransactionalBackend, + H: RuntimeHandle + TransactionalBackend, { fn check_first_byte(config: &Config, code: &[u8]) -> Result<(), ExitError> { if config.disallow_executable_format && Some(&Opcode::EOFMAGIC.as_u8()) == code.first() { @@ -439,12 +468,13 @@ where fn enter_call_trap_stack<'config, H>( mut gas_limit: u64, is_static: bool, + transaction_context: Rc, trap_data: CallTrapData, handler: &mut H, config: &'config Config, ) -> Result<(CallCreateTrapEnterData, GasedMachine<'config>), ExitError> where - H: RuntimeBackend + TransactionalBackend, + H: RuntimeHandle + TransactionalBackend, { handler.push_substate(); @@ -469,6 +499,7 @@ where config.memory_limit, RuntimeState { context: trap_data.context.clone(), + transaction_context, retbuf: Vec::new(), gas: U256::zero(), },