Skip to content

Commit

Permalink
start fixing cov for trie (#692)
Browse files Browse the repository at this point in the history
closes #678
  • Loading branch information
Eikix authored Feb 7, 2025
1 parent bee0671 commit 1a30066
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
16 changes: 13 additions & 3 deletions cairo/ethereum/cancun/trie.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ func encode_node{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: Kecc
) -> Bytes {
alloc_locals;

tempvar is_none = is_zero(cast(node.value, felt));
jmp none if is_none != 0;

tempvar is_account = cast(node.value.account.value, felt);
jmp account if is_account != 0;

Expand All @@ -385,7 +388,7 @@ func encode_node{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: Kecc

none:
// None defined for type Node but actually not supported in the EELS
with_attr error_message("encode_node: node cannot be None") {
with_attr error_message("AssertionError") {
assert 0 = 1;
ret;
}
Expand All @@ -412,8 +415,15 @@ func encode_node{range_check_ptr, bitwise_ptr: BitwiseBuiltin*, keccak_ptr: Kecc
return encoded;

uint:
let encoded = encode_uint([node.value.uint]);
return encoded;
// Node is Union[Account, Bytes, LegacyTransaction, Receipt, Uint, U256, Withdrawal, None]
// but encode_node(Uint) will raise AssertionError in EELS
with_attr error_message("AssertionError") {
assert 0 = 1;
}
ret;
// TODO: use this code once Uint is supported in the EELS
// let encoded = encode_uint([node.value.uint]);
// return encoded;

u256:
let encoded = encode_u256(node.value.u256);
Expand Down
40 changes: 32 additions & 8 deletions cairo/tests/ethereum/cancun/test_trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from ethereum.cancun.blocks import LegacyTransaction, Receipt, Withdrawal
from ethereum.cancun.fork_types import Account, Address
from ethereum.cancun.trie import (
BranchNode,
ExtensionNode,
InternalNode,
LeafNode,
Node,
Trie,
bytes_to_nibble_list,
Expand All @@ -19,12 +22,12 @@
)
from ethereum_types.bytes import Bytes, Bytes32
from ethereum_types.numeric import U256, Uint
from hypothesis import assume, given
from hypothesis import example, given
from hypothesis import strategies as st

from cairo_addons.testing.hints import patch_hint
from tests.utils.assertion import sequence_equal
from tests.utils.errors import cairo_error
from tests.utils.errors import cairo_error, strict_raises
from tests.utils.strategies import bytes32, nibble, uint4


Expand All @@ -36,13 +39,17 @@ def test_encode_internal_node(self, cairo_run, node: Optional[InternalNode]):
)

@given(node=..., storage_root=...)
@example(node=None, storage_root=None)
@example(node=Uint(145), storage_root=None)
def test_encode_node(self, cairo_run, node: Node, storage_root: Optional[Bytes]):
assume(node is not None)
assume(not isinstance(node, Uint))
assume(not (isinstance(node, Account) and storage_root is None))
assert encode_node(node, storage_root) == cairo_run(
"encode_node", node, storage_root
)
try:
cairo_result = cairo_run("encode_node", node, storage_root)
except Exception as cairo_error:
with strict_raises(type(cairo_error)):
encode_node(node, storage_root)
return
result = encode_node(node, storage_root)
assert cairo_result == result

@given(node=...)
def test_encode_account_should_fail_without_storage_root(
Expand Down Expand Up @@ -140,6 +147,23 @@ def test_get_branches(self, cairo_run, obj, level):
def test_patricialize(self, cairo_run, obj: Mapping[Bytes, Bytes]):
assert patricialize(obj, Uint(0)) == cairo_run("patricialize", obj, Uint(0))

@given(leaf_node=...)
def test_internal_node_leaf_node(self, cairo_run, leaf_node: LeafNode):
result = cairo_run("InternalNodeImpl.leaf_node", leaf_node)
assert result == leaf_node

@given(extension_node=...)
def test_internal_node_extension_node(
self, cairo_run, extension_node: ExtensionNode
):
result = cairo_run("InternalNodeImpl.extension_node", extension_node)
assert result == extension_node

@given(branch_node=...)
def test_internal_node_branch_node(self, cairo_run, branch_node: BranchNode):
result = cairo_run("InternalNodeImpl.branch_node", branch_node)
assert result == branch_node


class TestTrieOperations:
class TestGet:
Expand Down

0 comments on commit 1a30066

Please sign in to comment.