Skip to content

Commit

Permalink
dev: add append_logs util (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikix authored Feb 12, 2025
1 parent 6af4624 commit 42344a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cairo/ethereum/cancun/vm/instructions/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ from starkware.cairo.common.math_cmp import is_nn, is_in_range

from ethereum_types.numeric import U256, U256Struct, Uint
from ethereum.cancun.vm import Evm, EvmImpl
from ethereum.cancun.blocks import TupleLog, TupleLogStruct
from ethereum.exceptions import EthereumException
from ethereum.cancun.vm.gas import charge_gas, GasConstants
from ethereum.cancun.vm.stack import Stack, pop, push
from ethereum.utils.numeric import U256_from_be_bytes32
from legacy.utils.bytes import felt_to_bytes20_little
from legacy.utils.utils import Helpers
from starkware.cairo.common.memcpy import memcpy

// @notice Get the hash of one of the 256 most recent complete blocks
func block_hash{
Expand Down Expand Up @@ -303,3 +305,16 @@ namespace Internals {
return ok;
}
}

func _append_logs{logs: TupleLog}(new_logs: TupleLog) {
let src_len = new_logs.value.len;
if (src_len == 0) {
return ();
}
let len = logs.value.len;
let dst = logs.value.data + len;
let src = new_logs.value.data;
memcpy(dst, src, src_len);
tempvar logs = TupleLog(new TupleLogStruct(data=logs.value.data, len=len + src_len));
return ();
}
19 changes: 19 additions & 0 deletions cairo/tests/ethereum/cancun/vm/instructions/test_block.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Tuple

from ethereum.cancun.blocks import Log
from ethereum.cancun.state import TransientStorage
from ethereum.cancun.vm import Environment, Evm
from ethereum.cancun.vm.instructions.block import (
Expand Down Expand Up @@ -152,3 +155,19 @@ def test_chain_id(self, cairo_run, evm: Evm):

chain_id(evm)
assert evm == cairo_result


class TestUtils:
@given(logs=..., new_logs=...)
def test_append_logs(
self, cairo_run, logs: Tuple[Log, ...], new_logs: Tuple[Log, ...]
):
try:
cairo_result = cairo_run("_append_logs", logs, new_logs)
except EthereumException as cairo_error:
with strict_raises(type(cairo_error)):
logs += new_logs
return

logs += new_logs
assert logs == cairo_result
2 changes: 1 addition & 1 deletion cairo/tests/utils/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def register_type_strategies():
st.register_type_strategy(Account, account_strategy)
st.register_type_strategy(Withdrawal, st.builds(Withdrawal))
st.register_type_strategy(Header, st.builds(Header))
st.register_type_strategy(Log, st.builds(Log))
st.register_type_strategy(Log, st.builds(Log, data=small_bytes))
st.register_type_strategy(Receipt, st.builds(Receipt))
st.register_type_strategy(
LegacyTransaction, st.builds(LegacyTransaction, data=small_bytes)
Expand Down

0 comments on commit 42344a4

Please sign in to comment.