From 81dc3c7936343c3dc9fee17b591a285566b15ce9 Mon Sep 17 00:00:00 2001 From: Gerson <71728860+Gerson2102@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:52:09 -0600 Subject: [PATCH] Implementation of eth_get_transaction_count function (#983) * Implementation of eth_get_transaction_count function * Refactoring validate nonce in validation.cairo * Changing the deprecated function * Validating nonce without new function * Adding method to interface and test it * fix tests * fmt --------- Co-authored-by: enitrat --- .../contracts/src/kakarot_core/eth_rpc.cairo | 27 ++++++++++++++++--- .../src/kakarot_core/interface.cairo | 3 +++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/contracts/src/kakarot_core/eth_rpc.cairo b/crates/contracts/src/kakarot_core/eth_rpc.cairo index e7180d44b..c824fb4f6 100644 --- a/crates/contracts/src/kakarot_core/eth_rpc.cairo +++ b/crates/contracts/src/kakarot_core/eth_rpc.cairo @@ -132,7 +132,12 @@ pub impl EthRPC< } fn eth_get_transaction_count(self: @TContractState, address: EthAddress) -> u64 { - panic!("unimplemented") + let kakarot_state = KakarotState::get_state(); + let starknet_address = kakarot_state.get_starknet_address(address); + println!("starknet_address: {:?}", starknet_address); + let account = IAccountDispatcher { contract_address: starknet_address }; + let nonce = account.get_nonce(); + nonce } fn eth_chain_id(self: @TContractState) -> u64 { @@ -225,9 +230,12 @@ mod tests { use crate::kakarot_core::eth_rpc::IEthRPC; use crate::kakarot_core::interface::IExtendedKakarotCoreDispatcherTrait; use crate::test_utils::{setup_contracts_for_testing, fund_account_with_native_token}; - use evm::test_utils::{sequencer_evm_address, evm_address}; - use snforge_std::{start_cheat_chain_id_global, stop_cheat_chain_id_global}; + use evm::test_utils::{sequencer_evm_address, evm_address, uninitialized_account}; + use snforge_std::{ + start_mock_call, start_cheat_chain_id_global, stop_cheat_chain_id_global, test_address + }; use utils::constants::POW_2_53; + use utils::helpers::compute_starknet_address; fn set_up() -> KakarotCore::ContractState { // Define the kakarot state to access contract functions @@ -240,6 +248,19 @@ mod tests { stop_cheat_chain_id_global(); } + #[test] + fn test_eth_get_transaction_count() { + let kakarot_state = set_up(); + // Deployed eoa should return a zero nonce + let starknet_address = compute_starknet_address( + test_address(), + evm_address(), + 0.try_into().unwrap() // Using 0 as the kakarot storage is empty + ); + start_mock_call::(starknet_address, selector!("get_nonce"), 1); + assert_eq!(kakarot_state.eth_get_transaction_count(evm_address()), 1); + } + #[test] fn test_eth_get_balance() { let (native_token, kakarot_core) = setup_contracts_for_testing(); diff --git a/crates/contracts/src/kakarot_core/interface.cairo b/crates/contracts/src/kakarot_core/interface.cairo index 603a52f42..aa5e8e9fa 100644 --- a/crates/contracts/src/kakarot_core/interface.cairo +++ b/crates/contracts/src/kakarot_core/interface.cairo @@ -88,6 +88,9 @@ pub trait IExtendedKakarotCore { /// Executes an EVM transaction and possibly modifies the state fn eth_send_transaction(ref self: TContractState, tx: Transaction) -> (bool, Span, u64); + // Returns the transaction count (nonce) of the specified address + fn eth_get_transaction_count(self: @TContractState, address: EthAddress) -> u64; + /// Upgrade the KakarotCore smart contract /// Using replace_class_syscall fn upgrade(ref self: TContractState, new_class_hash: ClassHash);