Skip to content

Commit

Permalink
Partial MPT: account trie (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter authored Dec 17, 2024
1 parent 5e62feb commit 8877213
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
33 changes: 27 additions & 6 deletions cairo/ethereum/cancun/fork_types.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.math_cmp import is_le
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, KeccakBuiltin
from starkware.cairo.common.memcpy import memcpy
from src.utils.bytes import felt_to_bytes_little, uint256_to_bytes32_little, felt_to_bytes20_little
from ethereum.crypto.hash import Hash32, keccak256

from ethereum_types.bytes import Bytes20, Bytes256, Bytes, BytesStruct
from ethereum_types.numeric import Uint, U256
from ethereum_types.numeric import Uint, U256, U256Struct
from ethereum.crypto.hash import Hash32

using Address = Bytes20;
using Root = Hash32;

Expand All @@ -30,3 +28,26 @@ struct AccountStruct {
struct Account {
value: AccountStruct*,
}

struct AddressAccountDictAccess {
key: Address,
prev_value: Account,
new_value: Account,
}

struct MappingAddressAccountStruct {
dict_ptr_start: AddressAccountDictAccess*,
dict_ptr: AddressAccountDictAccess*,
}

struct MappingAddressAccount {
value: MappingAddressAccountStruct*,
}

func EMPTY_ACCOUNT() -> Account {
tempvar balance = U256(new U256Struct(0, 0));
let (data) = alloc();
tempvar code = Bytes(new BytesStruct(data=data, len=0));
tempvar account = Account(value=new AccountStruct(nonce=Uint(0), balance=balance, code=code));
return account;
}
16 changes: 10 additions & 6 deletions cairo/ethereum/cancun/trie.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from ethereum_types.bytes import (
TupleMappingBytesBytesStruct,
)
from ethereum.cancun.blocks import Receipt, Withdrawal
from ethereum.cancun.fork_types import Account, Address, Root
from ethereum.cancun.fork_types import Account, MappingAddressAccount
from ethereum.cancun.transactions import LegacyTransaction
from ethereum.rlp import (
Extended,
Expand Down Expand Up @@ -126,11 +126,15 @@ struct Node {
value: NodeEnum*,
}

// struct TrieStruct {
// secured: bool,
// default: V,
// _data: Dict[K, V],
// }
struct TrieAddressAccountStruct {
secured: bool,
default: Account,
_data: MappingAddressAccount,
}

struct TrieAddressAccount {
value: TrieAddressAccountStruct*,
}

func encode_internal_node{
range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: KeccakBuiltin*
Expand Down
3 changes: 2 additions & 1 deletion cairo/ethereum_types/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ struct Bytes8 {
struct Bytes20 {
value: felt,
}
using Bytes32Struct = Uint256;
struct Bytes32 {
value: Uint256*,
value: Bytes32Struct*,
}
struct Bytes256 {
value: U128*,
Expand Down
4 changes: 3 additions & 1 deletion cairo/ethereum_types/numeric.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ struct U128 {
struct Uint {
value: felt,
}

using U256Struct = Uint256;
struct U256 {
value: Uint256*,
value: U256Struct*,
}

struct SetUintDictAccess {
Expand Down
6 changes: 6 additions & 0 deletions cairo/tests/ethereum/cancun/test_fork_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from ethereum.cancun.fork_types import EMPTY_ACCOUNT


class TestForkTypes:
def test_account_default(self, cairo_run):
assert EMPTY_ACCOUNT == cairo_run("EMPTY_ACCOUNT")
1 change: 1 addition & 0 deletions cairo/tests/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_type(
Mapping[Bytes, Bytes],
Tuple[Mapping[Bytes, Bytes], ...],
Set[Uint],
Mapping[Address, Account],
],
):
assume(no_empty_sequence(b))
Expand Down
3 changes: 3 additions & 0 deletions cairo/tests/utils/args_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
("ethereum", "cancun", "trie", "BranchNode"): BranchNode,
("ethereum", "cancun", "trie", "InternalNode"): InternalNode,
("ethereum", "cancun", "trie", "Node"): Node,
("ethereum", "cancun", "fork_types", "MappingAddressAccount"): Mapping[
Address, Account
],
}

# In the EELS, some functions are annotated with Sequence while it's actually just Bytes.
Expand Down

0 comments on commit 8877213

Please sign in to comment.