Skip to content

Commit

Permalink
dev: add tests to test more cases of eth_chain_id
Browse files Browse the repository at this point in the history
  • Loading branch information
bitfalt committed Sep 17, 2024
1 parent 45bd2bf commit 6ff3620
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions crates/contracts/src/kakarot_core/eth_rpc.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,50 @@ fn is_view(self: @KakarotCore::ContractState) -> bool {
mod tests {
use contracts::kakarot_core::KakarotCore;
use contracts::kakarot_core::eth_rpc::IEthRPC;
use snforge_std::{start_cheat_chain_id_global};
use snforge_std::{start_cheat_chain_id_global, stop_cheat_chain_id_global};
use utils::constants::POW_2_53;

fn set_up() -> KakarotCore::ContractState {
// Define the kakarot state to access contract functions
let kakarot_state = KakarotCore::unsafe_new_contract_state();

// Set up environment
start_cheat_chain_id_global(1);

kakarot_state
}

fn tear_down() {
stop_cheat_chain_id_global();
}


#[test]
fn test_eth_chain_id() {
fn test_eth_chain_id_returns_input_when_less_than_pow_2_53() {
let kakarot_state = KakarotCore::unsafe_new_contract_state();
// Convert POW_2_53 - 1 to u64 since POW_2_53 is defined as u128
let chain_id: u64 = (POW_2_53 - 1).try_into().unwrap();
start_cheat_chain_id_global(chain_id.into());
assert_eq!(
kakarot_state.eth_chain_id(),
chain_id,
"Should return original chain ID when below 2^53"
);
tear_down();
}

#[test]
fn test_eth_chain_id_returns_modulo_when_greater_than_or_equal_to_pow_2_53() {
// Test with a value equal to 2^53
let kakarot_state = set_up();
assert_eq!(kakarot_state.eth_chain_id(), 1);
let chain_id: u64 = POW_2_53.try_into().unwrap();
start_cheat_chain_id_global(chain_id.into());
assert_eq!(kakarot_state.eth_chain_id(), 0, "Should return 0 when chain ID is 2^53");

// Test with a value greater than 2^53
let chain_id: u64 = (POW_2_53 + 53).try_into().unwrap();
start_cheat_chain_id_global(chain_id.into());
assert_eq!(
kakarot_state.eth_chain_id(), 53, "Should return correct value after modulo operation"
);
tear_down();
}
}

0 comments on commit 6ff3620

Please sign in to comment.