Skip to content

Commit

Permalink
feat: increment nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix committed Jan 17, 2025
1 parent 9af49dd commit b7c097b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cairo/ethereum/cancun/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
) {
Expand Down
8 changes: 8 additions & 0 deletions cairo/tests/ethereum/cancun/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_storage,
get_storage_original,
get_transient_storage,
increment_nonce,
is_account_alive,
is_account_empty,
mark_account_created,
Expand Down Expand Up @@ -167,6 +168,13 @@ def test_is_account_alive(self, cairo_run, data):
assert result_cairo == is_account_alive(state, address)
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))
Expand Down

0 comments on commit b7c097b

Please sign in to comment.