Skip to content

Commit

Permalink
check account storage based on internal key
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Dec 11, 2024
1 parent 0feacf7 commit 4be3e70
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cairo/src/account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Account {
// @dev This is used to initialize a new, empty account in the state
// @dev when an account is part of a transaction, but was never interacted with before.
// @return The new account
func new_empty() -> model.Account* {
func default() -> model.Account* {
let (code) = alloc();
tempvar empty_code_hash = new Uint256(
low=Constants.EMPTY_CODE_HASH_LOW, high=Constants.EMPTY_CODE_HASH_HIGH
Expand Down
2 changes: 1 addition & 1 deletion cairo/src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace State {
}

// Initialize a new account, empty otherwise
let account = Account.new_empty();
let account = Account.default();
dict_write{dict_ptr=accounts}(key=evm_address, new_value=cast(account, felt));
tempvar state = new model.State(
accounts_start=state.accounts_start,
Expand Down
8 changes: 7 additions & 1 deletion cairo/tests/src/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ from starkware.cairo.common.uint256 import Uint256, assert_uint256_eq
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.memcpy import memcpy

from ethereum.base_types import U256
from src.model import model
from src.account import Account
from src.account import Account, Internals
from tests.utils.helpers import TestHelpers

func test__init__should_return_account_with_default_dict_as_storage{}() {
Expand Down Expand Up @@ -185,3 +186,8 @@ func test__compute_code_hash{
// Then
return result;
}

func test___storage_addr{pedersen_ptr: HashBuiltin*}(key: U256) -> felt {
let (res) = Internals._storage_addr(key.value);
return res;
}
9 changes: 9 additions & 0 deletions cairo/tests/src/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from hypothesis import given
from hypothesis.strategies import binary

from ethereum.base_types import U256
from src.utils.uint256 import int_to_uint256
from tests.utils.helpers import get_internal_storage_key


class TestAccount:
Expand Down Expand Up @@ -60,3 +62,10 @@ def test_should_compute_code_hash(self, cairo_run, bytecode):
output = cairo_run("test__compute_code_hash", code=bytecode)
code_hash = int.from_bytes(keccak(bytecode), byteorder="big")
assert output["low"] + 2**128 * output["high"] == code_hash

class TestInternals:
@given(key=...)
def test_should_compute_storage_address(self, cairo_run, key: U256):
assert get_internal_storage_key(key) == cairo_run(
"test___storage_addr", key
)
8 changes: 6 additions & 2 deletions cairo/tests/src/test_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ethereum.cancun.fork_types import EMPTY_ACCOUNT
from ethereum.crypto.hash import keccak256
from tests.utils.constants import OTHER, OWNER
from tests.utils.helpers import get_internal_storage_key
from tests.utils.models import State


Expand Down Expand Up @@ -47,15 +48,18 @@ def test_should_return_account_when_account_in_state(self, cairo_run):
),
"balance": initial_state[OWNER]["balance"],
"nonce": initial_state[OWNER]["nonce"],
"storage": initial_state[OWNER]["storage"],
"storage": {
get_internal_storage_key(k): v
for k, v in initial_state[OWNER]["storage"].items()
},
"transient_storage": {},
"valid_jumpdests": {},
"selfdestruct": 0,
"created": 0,
}
assert account == expected

def test_should_return_new_empty_account_when_account_not_in_state(
def test_should_return_default_account_when_account_not_in_state(
self, cairo_run
):
account = cairo_run(
Expand Down
26 changes: 6 additions & 20 deletions cairo/tests/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import random
from collections import defaultdict
from textwrap import wrap
from typing import Iterable, List, Tuple, Union

Expand All @@ -10,6 +9,7 @@
from eth_utils import decode_hex, keccak, to_checksum_address
from starkware.cairo.lang.vm.crypto import pedersen_hash

from ethereum.base_types import U256
from src.utils.uint256 import int_to_uint256
from tests.utils.parsers import to_bytes, to_int

Expand Down Expand Up @@ -93,10 +93,6 @@ def ec_sign(
)


def pack_64_bits_little(input: List[int]):
return sum([x * 256**i for (i, x) in enumerate(input)])


def flatten(data):
result = []

Expand Down Expand Up @@ -125,21 +121,6 @@ def flatten_tx_access_list(access_list):
return result


def merge_access_list(access_list):
"""
Merge all entries of the access list to get one entry per account with all its storage keys.
"""
merged_list = defaultdict(set)
for access in access_list:
merged_list[access["address"]] = merged_list[access["address"]].union(
{
pedersen_hash(*int_to_uint256(int(key, 16)))
for key in access["storageKeys"]
}
)
return merged_list


def pack_calldata(data: bytes) -> List[int]:
"""
Pack the incoming calldata bytes 31-bytes at a time in big-endian order.
Expand All @@ -150,3 +131,8 @@ def pack_calldata(data: bytes) -> List[int]:
"""

return [len(data), *[int(chunk, 16) for chunk in wrap(data.hex(), 2 * 31)]]


def get_internal_storage_key(key: U256) -> int:
low, high = int_to_uint256(key)
return pedersen_hash(low, high)

0 comments on commit 4be3e70

Please sign in to comment.