Skip to content

Commit

Permalink
feat: add test for coinbase + unwrap machine calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Bajpai authored and Harsh Bajpai committed Nov 28, 2023
1 parent 6aaef63 commit 211c7c9
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions crates/evm/src/tests/test_instructions/test_block_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ use contracts::kakarot_core::interface::{
};

use contracts::tests::test_utils::{
setup_contracts_for_testing, fund_account_with_native_token, deploy_contract_account
setup_contracts_for_testing, fund_account_with_native_token, deploy_contract_account,
};
use core::clone::Clone;
use core::result::ResultTrait;
use core::traits::TryInto;
use evm::instructions::BlockInformationTrait;
use evm::model::contract_account::ContractAccountTrait;
use evm::stack::StackTrait;
use evm::tests::test_utils::{evm_address, MachineBuilderTestTrait};
use openzeppelin::token::erc20::interface::IERC20CamelDispatcherTrait;
use starknet::testing::{set_block_timestamp, set_block_number, set_contract_address};
use starknet::testing::{
set_block_timestamp, set_block_number, set_contract_address, set_sequencer_address,
ContractAddress
};

/// 0x40 - BLOCKHASH
#[test]
Expand All @@ -22,7 +28,7 @@ fn test_exec_blockhash_below_bounds() {

// When
machine.stack.push(243).unwrap();
machine.exec_blockhash();
machine.exec_blockhash().unwrap();

// Then
assert(machine.stack.peek().unwrap() == 0, 'stack top should be 0');
Expand All @@ -37,7 +43,7 @@ fn test_exec_blockhash_above_bounds() {

// When
machine.stack.push(491).unwrap();
machine.exec_blockhash();
machine.exec_blockhash().unwrap();

// Then
assert(machine.stack.peek().unwrap() == 0, 'stack top should be 0');
Expand Down Expand Up @@ -72,7 +78,7 @@ fn test_block_timestamp_set_to_1692873993() {
// If not set the default timestamp is 0.
set_block_timestamp(1692873993);
// When
machine.exec_timestamp();
machine.exec_timestamp().unwrap();

// Then
assert(machine.stack.len() == 1, 'stack should have one element');
Expand All @@ -86,7 +92,7 @@ fn test_block_number_set_to_32() {
// If not set the default block number is 0.
set_block_number(32);
// When
machine.exec_number();
machine.exec_number().unwrap();

// Then
assert(machine.stack.len() == 1, 'stack should have one element');
Expand All @@ -98,7 +104,7 @@ fn test_gaslimit() {
// Given
let mut machine = MachineBuilderTestTrait::new_with_presets().build();
// When
machine.exec_gaslimit();
machine.exec_gaslimit().unwrap();

// Then
assert(machine.stack.len() == 1, 'stack should have one element');
Expand All @@ -122,7 +128,7 @@ fn test_exec_selfbalance_eoa() {

// When
set_contract_address(kakarot_core.contract_address);
machine.exec_selfbalance();
machine.exec_selfbalance().unwrap();

// Then
assert(machine.stack.peek().unwrap() == native_token.balanceOf(eoa), 'wrong balance');
Expand All @@ -138,7 +144,7 @@ fn test_exec_selfbalance_zero() {

// When
set_contract_address(kakarot_core.contract_address);
machine.exec_selfbalance();
machine.exec_selfbalance().unwrap();

// Then
assert(machine.stack.peek().unwrap() == 0x00, 'wrong balance');
Expand All @@ -155,7 +161,7 @@ fn test_exec_selfbalance_contract_account() {

// When
set_contract_address(kakarot_core.contract_address);
machine.exec_selfbalance();
machine.exec_selfbalance().unwrap();

// Then
assert(machine.stack.peek().unwrap() == 0x1, 'wrong balance');
Expand All @@ -167,7 +173,7 @@ fn test_basefee() {
// Given
let mut machine = MachineBuilderTestTrait::new_with_presets().build();
// When
machine.exec_basefee();
machine.exec_basefee().unwrap();

// Then
assert(machine.stack.len() == 1, 'stack should have one element');
Expand All @@ -188,7 +194,7 @@ fn test_chainid_should_push_chain_id_to_stack() {
.into();

// When
machine.exec_chainid();
machine.exec_chainid().unwrap();

// Then
let result = machine.stack.peek().unwrap();
Expand All @@ -208,3 +214,27 @@ fn test_randao_should_push_zero_to_stack() {
let result = machine.stack.peek().unwrap();
assert(result == 0x00, 'stack top should be zero');
}

// *************************************************************************
// 0x41: COINBASE
// *************************************************************************
#[test]
#[available_gas(5000000)]
fn test_exec_coinbase() {
// Given
let (native_token, kakarot_core) = setup_contracts_for_testing();
let sequencer_eoa = kakarot_core.deploy_eoa(evm_address());
fund_account_with_native_token(sequencer_eoa, native_token, 0x1);

// And
let mut machine = MachineBuilderTestTrait::new_with_presets().build();

// When
set_sequencer_address(sequencer_eoa);
machine.exec_coinbase().unwrap();

let sequencer_address = machine.stack.pop().unwrap();

// Then
assert(evm_address().address.into() == sequencer_address, 'wrong sequencer_address');
}

0 comments on commit 211c7c9

Please sign in to comment.