Skip to content

Commit

Permalink
feat: calldatasize
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Jan 21, 2025
1 parent 3f90598 commit 38947a4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cairo/ethereum/cancun/vm/instructions/environment.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,30 @@ func calldatacopy{range_check_ptr, evm: Evm}() -> ExceptionalHalt* {
let ok = cast(0, ExceptionalHalt*);
return ok;
}

func calldatasize{range_check_ptr, evm: Evm}() -> ExceptionalHalt* {
alloc_locals;
// STACK
// No stack input
let stack = evm.value.stack;
// GAS
let err = charge_gas(Uint(GasConstants.GAS_BASE));
if (cast(err, felt) != 0) {
return err;
}

// OPERATION
let calldata_len = evm.value.message.value.data.value.len;
with stack {
let err = push(U256(new U256Struct(calldata_len, 0)));
if (cast(err, felt) != 0) {
return err;
}
}

// PROGRAM COUNTER
EvmImpl.set_pc_stack(Uint(evm.value.pc.value + 1), stack);

let ok = cast(0, ExceptionalHalt*);
return ok;
}
15 changes: 15 additions & 0 deletions cairo/tests/ethereum/cancun/vm/instructions/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
blob_hash,
calldatacopy,
calldataload,
calldatasize,
caller,
callvalue,
codecopy,
Expand Down Expand Up @@ -384,3 +385,17 @@ def test_calldatacopy(self, cairo_run, evm: Evm):

calldatacopy(evm)
assert evm == cairo_result

@given(
evm=EvmBuilder().with_stack().with_gas_left().with_message_calldata().build()
)
def test_calldatasize(self, cairo_run, evm: Evm):
try:
cairo_result = cairo_run("calldatasize", evm)
except ExceptionalHalt as cairo_error:
with strict_raises(type(cairo_error)):
calldatasize(evm)
return

calldatasize(evm)
assert evm == cairo_result

0 comments on commit 38947a4

Please sign in to comment.