Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix coinbase #581

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ mod KakarotCore {
new_class_hash: ClassHash,
}

// TODO: add ability to pass Span<EthAddress>, which should be deployed along with Kakarot
// this can be done once https://github.com/starkware-libs/cairo/issues/4488 is resolved
#[constructor]
fn constructor(
ref self: ContractState,
Expand Down Expand Up @@ -188,7 +190,6 @@ mod KakarotCore {
}
}


fn contract_account_nonce(self: @ContractState, evm_address: EthAddress) -> u64 {
let ca_address = ContractAccountTrait::at(evm_address)
.expect('Fetching CA failed')
Expand Down
1 change: 1 addition & 0 deletions crates/contracts/src/tests/test_eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod test_external_owned_account {
use core::array::SpanTrait;
use core::starknet::account::{Call, AccountContractDispatcher, AccountContractDispatcherTrait};


use evm::model::{Address, AddressTrait, ContractAccountTrait};
use evm::tests::test_utils::{
kakarot_address, evm_address, other_evm_address, other_starknet_address, eoa_address,
Expand Down
14 changes: 11 additions & 3 deletions crates/contracts/src/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use contracts::contract_account::ContractAccount::TEST_CLASS_HASH as ContractAccountTestClassHash;
use contracts::contract_account::ContractAccount;
use contracts::contract_account::{IContractAccountDispatcher, IContractAccountDispatcherTrait};
use contracts::eoa::ExternallyOwnedAccount;
use contracts::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait;
use contracts::eoa::{
ExternallyOwnedAccount, IExternallyOwnedAccountDispatcher,
IExternallyOwnedAccountDispatcherTrait
};
use contracts::kakarot_core::interface::IKakarotCore;
use contracts::kakarot_core::interface::{
IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait
};
use contracts::kakarot_core::kakarot::StoredAccountType;
use contracts::kakarot_core::{
interface::IExtendedKakarotCoreDispatcherImpl, KakarotCore, KakarotCore::{KakarotCoreInternal},
Expand All @@ -14,6 +20,9 @@ use contracts::tests::test_upgradeable::{
};
use contracts::tests::test_utils as contract_utils;
use contracts::uninitialized_account::UninitializedAccount;
use core::option::OptionTrait;


use core::traits::TryInto;
use evm::machine::Status;
use evm::model::contract_account::ContractAccountTrait;
Expand Down Expand Up @@ -468,4 +477,3 @@ fn test_eoa_class_hash() {
assert(event.old_class_hash == class_hash, 'wrong old hash');
assert(event.new_class_hash == kakarot_core.eoa_class_hash(), 'wrong new hash');
}

11 changes: 6 additions & 5 deletions crates/evm/src/instructions/block_information.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use contracts::eoa::{IExternallyOwnedAccountDispatcher, IExternallyOwnedAccountDispatcherTrait};
//! Block Information.

use contracts::kakarot_core::{KakarotCore, IKakarotCore};
Expand Down Expand Up @@ -54,11 +55,11 @@ impl BlockInformation of BlockInformationTrait {
let execution_info = get_execution_info_syscall()
.map_err(EVMError::SyscallFailed(EXECUTION_INFO_SYSCALL_FAILED))?
.unbox();
let coinbase: EthAddress = execution_info
.block_info
.unbox()
.sequencer_address
.try_into_result()?;

let coinbase = execution_info.block_info.unbox().sequencer_address;
let eoa = IExternallyOwnedAccountDispatcher { contract_address: coinbase };

let coinbase = eoa.evm_address();
self.stack.push(coinbase.into())
}

Expand Down
2 changes: 2 additions & 0 deletions crates/evm/src/model/eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use contracts::kakarot_core::{IKakarotCore, KakarotCore, KakarotCore::KakarotCor
use contracts::uninitialized_account::{
IUninitializedAccountDispatcher, IUninitializedAccountDispatcherTrait
};

use evm::errors::{EVMError, CONTRACT_SYSCALL_FAILED, EOA_EXISTS};
use evm::model::{Address, AddressTrait};
use starknet::{EthAddress, ContractAddress, get_contract_address, deploy_syscall};
Expand All @@ -29,6 +30,7 @@ impl EOAImpl of EOATrait {
let calldata: Span<felt252> = array![kakarot_address.into(), evm_address.into()].span();

let maybe_address = deploy_syscall(account_class_hash, evm_address.into(), calldata, false);

// Panic with err as syscall failure can't be caught, so we can't manage
// the error
match maybe_address {
Expand Down
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,26 @@ 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());

// 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');
}
Loading