From 6540db3fc3580ac55105dc64cbf791cd02a7ef59 Mon Sep 17 00:00:00 2001 From: Melanie Riise Date: Thu, 2 Feb 2023 21:24:40 -0800 Subject: [PATCH] EVM: Test context opcodes (#1155) * add tests for first half of context * remove blockhash from this PR, fmt * add remaining context tests --- .../src/interpreter/instructions/context.rs | 116 +++++++++++++++++- .../evm/src/interpreter/instructions/state.rs | 5 - actors/evm/src/interpreter/test_util.rs | 1 + 3 files changed, 116 insertions(+), 6 deletions(-) diff --git a/actors/evm/src/interpreter/instructions/context.rs b/actors/evm/src/interpreter/instructions/context.rs index a06d5687d..077f86b78 100644 --- a/actors/evm/src/interpreter/instructions/context.rs +++ b/actors/evm/src/interpreter/instructions/context.rs @@ -75,7 +75,7 @@ pub fn coinbase( _state: &mut ExecutionState, _system: &System, ) -> Result { - // TODO do we want to return the zero ID address, or just a plain 0? + // Eth zero address, beneficiary of the current block doesn't make much sense in Filecoin due to multiple winners in each block. Ok(U256::zero()) } @@ -106,6 +106,7 @@ pub fn block_number( _state: &mut ExecutionState, system: &System, ) -> Result { + // NOTE: Panics if current epoch is negative, which should never happen in the network Ok(U256::from(system.rt.curr_epoch())) } @@ -217,4 +218,117 @@ mod tests { assert_eq!(m.state.stack.pop().unwrap(), U256::from(123)); }; } + + #[test] + fn test_number() { + for epoch in [1234, i64::MAX, 0, 1] { + evm_unit_test! { + (rt) { + rt.set_epoch(epoch); + } + (m) { + NUMBER; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), U256::from(epoch)); + }; + } + } + + #[test] + fn test_chainid() { + for chainid in [31415, 3141, 0, 1] { + evm_unit_test! { + (rt) { + rt.chain_id = chainid.into(); + } + (m) { + CHAINID; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), U256::from(chainid)); + }; + } + } + + #[test] + fn test_basefee() { + for basefee in [12345, u128::MAX, 0, 1].map(U256::from) { + evm_unit_test! { + (rt) { + rt.base_fee = TokenAmount::from(&basefee); + } + (m) { + BASEFEE; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), basefee); + }; + } + } + + #[test] + fn test_coinbase() { + evm_unit_test! { + (m) { + COINBASE; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), U256::ZERO); + }; + } + + #[test] + fn test_timestamp() { + evm_unit_test! { + (rt) { + rt.tipset_timestamp = 12345; + } + (m) { + TIMESTAMP; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), U256::from(12345)); + }; + } + + #[test] + fn test_prevrandao() { + let epoch = 1234; + evm_unit_test! { + (rt) { + rt.set_epoch(epoch); + rt.expect_get_randomness_from_beacon(fil_actors_runtime::runtime::DomainSeparationTag::EvmPrevRandao, epoch, Vec::from(*b"prevrandao"), [0xff; 32]); + } + (m) { + PREVRANDAO; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + assert_eq!(m.state.stack.pop().unwrap(), U256::MAX); + }; + } + + #[test] + fn test_gas_limit() { + for limit in [12345, 0, u64::MAX] { + evm_unit_test! { + (rt) { + rt.gas_limit = limit; + } + (m) { + GASLIMIT; + } + m.step().expect("execution step failed"); + assert_eq!(m.state.stack.len(), 1); + // always block gas limit + assert_eq!(m.state.stack.pop().unwrap(), U256::from(10_000_000_000u64)); + }; + } + } } diff --git a/actors/evm/src/interpreter/instructions/state.rs b/actors/evm/src/interpreter/instructions/state.rs index 819130b1e..7571ad345 100644 --- a/actors/evm/src/interpreter/instructions/state.rs +++ b/actors/evm/src/interpreter/instructions/state.rs @@ -55,8 +55,6 @@ mod test { ] { evm_unit_test! { (rt) { - rt.in_call = true; - let id_address = 1111; if has_id { let addr: EthAddress = addr.into(); @@ -100,8 +98,6 @@ mod test { evm_unit_test! { (rt) { - rt.in_call = true; - // 0xff id address gets balance rt.actor_balances.insert(id as u64, TokenAmount::from_atto(balance)); } @@ -130,7 +126,6 @@ mod test { let balance = U256::ONE << i; evm_unit_test! { (rt) { - rt.in_call = true; rt.add_balance(TokenAmount::from(&balance)); } (m) { diff --git a/actors/evm/src/interpreter/test_util.rs b/actors/evm/src/interpreter/test_util.rs index 4d78dbe93..2713b24b8 100644 --- a/actors/evm/src/interpreter/test_util.rs +++ b/actors/evm/src/interpreter/test_util.rs @@ -20,6 +20,7 @@ macro_rules! evm_unit_test { use $crate::{Bytecode, Bytes, EthAddress, ExecutionState}; let mut $rt = MockRuntime::default(); + $rt.in_call = true; $init let mut state = ExecutionState::new(