Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increment nonce #467

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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))
Expand Down
Loading