From 2ff10d84263a3476e247804e9b71d7b61eb48ba0 Mon Sep 17 00:00:00 2001 From: Elias Tazartes Date: Fri, 17 Jan 2025 11:51:02 +0100 Subject: [PATCH] feat: increment nonce --- cairo/ethereum/cancun/state.cairo | 15 ++++++++++++++- cairo/tests/ethereum/cancun/test_state.py | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cairo/ethereum/cancun/state.cairo b/cairo/ethereum/cancun/state.cairo index 8dbe2c4d..12ef3a52 100644 --- a/cairo/ethereum/cancun/state.cairo +++ b/cairo/ethereum/cancun/state.cairo @@ -31,7 +31,7 @@ from ethereum.cancun.trie import ( copy_trieBytes32U256, ) from ethereum_types.bytes import Bytes, Bytes32 -from ethereum_types.numeric import U256, U256Struct, Bool, bool +from ethereum_types.numeric import U256, U256Struct, Bool, bool, Uint from ethereum.utils.numeric import is_zero from src.utils.dict import hashdict_read, hashdict_write, hashdict_get, dict_new_empty @@ -267,6 +267,19 @@ func destroy_account{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Addr return (); } +func increment_nonce{poseidon_ptr: PoseidonBuiltin*, state: State}(address: Address) { + alloc_locals; + let account = get_account(address); + // This increment is safe since + // `validate_transaction` will not allow a transaction + // with a nonce equal to max nonce (u64 as of today) + let new_nonce = account.value.nonce.value + 1; + tempvar new_account = OptionalAccount( + new AccountStruct(Uint(new_nonce), account.value.balance, account.value.code) + ); + set_account(address, new_account); + return (); +} func set_storage{poseidon_ptr: PoseidonBuiltin*, state: State}( address: Address, key: Bytes32, value: U256 ) { diff --git a/cairo/tests/ethereum/cancun/test_state.py b/cairo/tests/ethereum/cancun/test_state.py index d26ac552..24b6c126 100644 --- a/cairo/tests/ethereum/cancun/test_state.py +++ b/cairo/tests/ethereum/cancun/test_state.py @@ -19,6 +19,7 @@ get_storage, get_storage_original, get_transient_storage, + increment_nonce, is_account_alive, is_account_empty, mark_account_created, @@ -189,6 +190,13 @@ def test_set_account_balance(self, cairo_run, data, amount: U256): set_account_balance(state, address, amount) assert state_cairo == state + @given(data=state_and_address_and_optional_key()) + def test_increment_nonce(self, cairo_run, data): + state, address = data + state_cairo = cairo_run("increment_nonce", state, address) + increment_nonce(state, address) + assert state_cairo == state + class TestStateStorage: @given(state_and_address_and_optional_key(key_strategy=bytes32))