Skip to content

Commit

Permalink
Add constants for precompiles addresses range. (#868)
Browse files Browse the repository at this point in the history
* add constants => is_precompile()

* add unittest

* review changes

* delete import

* change loop => for

* fix ident
  • Loading branch information
lprovenzano committed Aug 25, 2024
1 parent 112949d commit 11ac169
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use utils::helpers::{ResultExTrait};
use utils::set::{Set, SpanSet};
use utils::traits::{EthAddressDefault, ContractAddressDefault, SpanDefault};

const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100;
const LAST_ETHEREUM_PRECOMPILE_ADDRESS: u256 = 0x0a;
const ZERO: felt252 = 0x0;

#[derive(Destruct, Default)]
struct Environment {
origin: EthAddress,
Expand Down Expand Up @@ -139,7 +143,9 @@ impl AddressImpl of AddressTrait {
/// Check whether an address for a call-family opcode is a precompile.
fn is_precompile(self: EthAddress) -> bool {
let self: felt252 = self.into();
return (self != 0 && (self.into() < 10_u256 || self.into() == 256_u256));
return self != ZERO
&& (self.into() < LAST_ETHEREUM_PRECOMPILE_ADDRESS
|| self.into() == FIRST_ROLLUP_PRECOMPILE_ADDRESS);
}
}

Expand Down Expand Up @@ -176,8 +182,6 @@ mod tests {
let (_, kakarot_core) = setup_contracts_for_testing();
starknet_backend::deploy(evm_address()).expect('failed deploy eoa account',);

// When

// When
set_contract_address(kakarot_core.contract_address);
let is_deployed = evm_address().is_deployed();
Expand All @@ -194,6 +198,7 @@ mod tests {

// When
let is_deployed = evm_address().is_deployed();

// Then
assert(is_deployed, 'account should be deployed');
}
Expand Down Expand Up @@ -402,8 +407,45 @@ mod tests {
let account = AccountTrait::fetch(evm_address()).unwrap();
let balance = account.balance();

// Then
// Then
assert(balance == native_token.balanceOf(ca_address.starknet), 'wrong balance');
}

#[test]
fn test_is_precompile() {
// Given
let valid_precompiles = array![0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x100];

//When
for el in valid_precompiles {
let evm_address: EthAddress = (el).try_into().unwrap();
//Then
assert_eq!(true, evm_address.is_precompile());
};
}

#[test]
fn test_is_precompile_zero() {
// Given
let evm_address: EthAddress = 0x0.try_into().unwrap();

// When
let is_precompile = evm_address.is_precompile();

// Then
assert_eq!(false, is_precompile);
}

#[test]
fn test_is_not_precompile() {
// Given
let not_valid_precompiles = array![0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x99];

//When
for el in not_valid_precompiles {
let evm_address: EthAddress = (el).try_into().unwrap();
//Then
assert_eq!(false, evm_address.is_precompile());
};
}
}

0 comments on commit 11ac169

Please sign in to comment.