Skip to content

Commit

Permalink
EVM: Test context opcodes (#1155)
Browse files Browse the repository at this point in the history
* add tests for first half of context

* remove blockhash from this PR, fmt

* add remaining context tests
  • Loading branch information
mriise authored Feb 3, 2023
1 parent 7db5cff commit 6540db3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
116 changes: 115 additions & 1 deletion actors/evm/src/interpreter/instructions/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn coinbase(
_state: &mut ExecutionState,
_system: &System<impl Runtime>,
) -> Result<U256, ActorError> {
// 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())
}

Expand Down Expand Up @@ -106,6 +106,7 @@ pub fn block_number(
_state: &mut ExecutionState,
system: &System<impl Runtime>,
) -> Result<U256, ActorError> {
// NOTE: Panics if current epoch is negative, which should never happen in the network
Ok(U256::from(system.rt.curr_epoch()))
}

Expand Down Expand Up @@ -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));
};
}
}
}
5 changes: 0 additions & 5 deletions actors/evm/src/interpreter/instructions/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions actors/evm/src/interpreter/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 6540db3

Please sign in to comment.