Skip to content

Commit

Permalink
Merge branch 'main' into tmp/fix-preimages-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix authored Feb 13, 2025
2 parents e16ddc8 + 76a71d9 commit 0a0ceab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
43 changes: 35 additions & 8 deletions cairo/ethereum/cancun/trie.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,11 @@ func trie_get_TrieAddressOptionalAccount{
let fp_and_pc = get_fp_and_pc();
local __fp__: felt* = fp_and_pc.fp_val;

with dict_ptr {
let (pointer) = hashdict_read(1, &key.value);
let (pointer) = hashdict_read{dict_ptr=dict_ptr}(1, &key.value);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, AddressAccountDictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
Expand All @@ -567,15 +570,19 @@ func trie_get_TrieAddressOptionalAccount{
func trie_get_TrieTupleAddressBytes32U256{
poseidon_ptr: PoseidonBuiltin*, trie: TrieTupleAddressBytes32U256
}(address: Address, key: Bytes32) -> U256 {
alloc_locals;
let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*);

let (keys) = alloc();
assert keys[0] = address.value;
assert keys[1] = key.value.low;
assert keys[2] = key.value.high;

with dict_ptr {
let (pointer) = hashdict_read(3, keys);
let (pointer) = hashdict_read{dict_ptr=dict_ptr}(3, keys);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, TupleAddressBytes32U256DictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
Expand All @@ -594,14 +601,18 @@ func trie_get_TrieTupleAddressBytes32U256{
func trie_get_TrieBytes32U256{poseidon_ptr: PoseidonBuiltin*, trie: TrieBytes32U256}(
key: Bytes32
) -> U256 {
alloc_locals;
let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*);

let (keys) = alloc();
assert keys[0] = key.value.low;
assert keys[1] = key.value.high;

with dict_ptr {
let (pointer) = hashdict_read(2, keys);
let (pointer) = hashdict_read{dict_ptr=dict_ptr}(2, keys);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, Bytes32U256DictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
Expand All @@ -620,10 +631,14 @@ func trie_get_TrieBytes32U256{poseidon_ptr: PoseidonBuiltin*, trie: TrieBytes32U
func trie_get_TrieBytesOptionalUnionBytesLegacyTransaction{
poseidon_ptr: PoseidonBuiltin*, trie: TrieBytesOptionalUnionBytesLegacyTransaction
}(key: Bytes) -> OptionalUnionBytesLegacyTransaction {
alloc_locals;
let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*);

with dict_ptr {
let (pointer) = hashdict_read(key.value.len, key.value.data);
let (pointer) = hashdict_read{dict_ptr=dict_ptr}(key.value.len, key.value.data);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, BytesOptionalUnionBytesLegacyTransactionDictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
Expand All @@ -646,9 +661,15 @@ func trie_get_TrieBytesOptionalUnionBytesLegacyTransaction{
func trie_get_TrieBytesOptionalUnionBytesReceipt{
poseidon_ptr: PoseidonBuiltin*, trie: TrieBytesOptionalUnionBytesReceipt
}(key: Bytes) -> OptionalUnionBytesReceipt {
alloc_locals;
let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*);

let (pointer) = hashdict_read{dict_ptr=dict_ptr}(key.value.len, key.value.data);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, BytesOptionalUnionBytesReceiptDictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
tempvar mapping = MappingBytesOptionalUnionBytesReceipt(
Expand All @@ -668,9 +689,15 @@ func trie_get_TrieBytesOptionalUnionBytesReceipt{
func trie_get_TrieBytesOptionalUnionBytesWithdrawal{
poseidon_ptr: PoseidonBuiltin*, trie: TrieBytesOptionalUnionBytesWithdrawal
}(key: Bytes) -> OptionalUnionBytesWithdrawal {
alloc_locals;
let dict_ptr = cast(trie.value._data.value.dict_ptr, DictAccess*);

let (pointer) = hashdict_read{dict_ptr=dict_ptr}(key.value.len, key.value.data);
if (pointer == 0) {
tempvar pointer = cast(trie.value.default.value, felt);
} else {
tempvar pointer = pointer;
}
let new_dict_ptr = cast(dict_ptr, BytesOptionalUnionBytesWithdrawalDictAccess*);
let parent_dict = trie.value._data.value.parent_dict;
tempvar mapping = MappingBytesOptionalUnionBytesWithdrawal(
Expand Down
4 changes: 2 additions & 2 deletions python/cairo-addons/src/cairo_addons/hints/hashdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def hashdict_read(dict_manager: DictManager, ids: VmConsts, memory: MemoryDict):
preimage = tuple([memory[ids.key + i] for i in range(ids.key_len)])
# Not using [] here because it will register the value for that key in the tracker.
value = dict_tracker.data.get(preimage)
if value:
if value is not None:
ids.value = value
else:
ids.value = dict_tracker.data.default_factory()
Expand Down Expand Up @@ -47,7 +47,7 @@ def hashdict_write(dict_manager: DictManager, ids: VmConsts, memory: MemoryDict)
dict_tracker.current_ptr += ids.DictAccess.SIZE
preimage = tuple([memory[ids.key + i] for i in range(ids.key_len)])
prev_value = dict_tracker.data.get(preimage)
if prev_value:
if prev_value is not None:
ids.dict_ptr.prev_value = prev_value
else:
ids.dict_ptr.prev_value = dict_tracker.data.default_factory()
Expand Down

0 comments on commit 0a0ceab

Please sign in to comment.