Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix call_data to calldata #274

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/evm/src/context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct CallContext {
/// The bytecode to execute.
bytecode: Span<u8>,
/// The call data.
call_data: Span<u8>,
calldata: Span<u8>,
/// Amount of native token to transfer.
value: u256,
}
Expand All @@ -32,16 +32,16 @@ struct CallContext {
// instead we should use the methods defined in the trait.
// This is not enforced until there are `pub` and `priv` visibility on struct fields.
trait CallContextTrait {
fn new(bytecode: Span<u8>, call_data: Span<u8>, value: u256) -> CallContext;
fn new(bytecode: Span<u8>, calldata: Span<u8>, value: u256) -> CallContext;
fn bytecode(self: @CallContext) -> Span<u8>;
fn call_data(self: @CallContext) -> Span<u8>;
fn calldata(self: @CallContext) -> Span<u8>;
fn value(self: @CallContext) -> u256;
}

impl CallContextImpl of CallContextTrait {
#[inline(always)]
fn new(bytecode: Span<u8>, call_data: Span<u8>, value: u256) -> CallContext {
CallContext { bytecode, call_data, value, }
fn new(bytecode: Span<u8>, calldata: Span<u8>, value: u256) -> CallContext {
CallContext { bytecode, calldata, value, }
}

#[inline(always)]
Expand All @@ -50,8 +50,8 @@ impl CallContextImpl of CallContextTrait {
}

#[inline(always)]
fn call_data(self: @CallContext) -> Span<u8> {
*self.call_data
fn calldata(self: @CallContext) -> Span<u8> {
*self.calldata
}

#[inline(always)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl EnvironmentInformationImpl of EnvironmentInformationTrait {
/// Get the size of return data.
/// # Specification: https://www.evm.codes/#36?fork=shanghai
fn exec_calldatasize(ref self: ExecutionContext) -> Result<(), EVMError> {
let result: u256 = self.call_context().call_data().len().into();
let result: u256 = self.call_context().calldata().len().into();
self.stack.push(result)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/evm/src/tests/test_execution_context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ fn no_op() {}
fn test_call_context_new() {
// When
let bytecode: Span<u8> = array![1, 2, 3].span();
let call_data: Span<u8> = array![4, 5, 6].span();
let calldata: Span<u8> = array![4, 5, 6].span();
let value: u256 = callvalue();

let call_ctx = CallContextTrait::new(bytecode, call_data, value);
let call_ctx = CallContextTrait::new(bytecode, calldata, value);
// TODO remove once no longer required (see https://github.com/starkware-libs/cairo/issues/3863)
no_op();

// Then
assert(call_ctx.bytecode() == bytecode, 'wrong bytecode');
assert(call_ctx.call_data() == call_data, 'wrong call_data');
assert(call_ctx.calldata() == calldata, 'wrong calldata');
assert(call_ctx.value() == callvalue(), 'wrong value');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ fn test_gasprice() {
fn test_calldata_size() {
// Given
let mut ctx = setup_execution_context();
let call_data: Span<u8> = ctx.call_context().call_data();
let calldata: Span<u8> = ctx.call_context().calldata();

// When
ctx.exec_calldatasize();

// Then
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.peek().unwrap() == call_data.len().into(), 'stack top is not calldatasize');
assert(ctx.stack.peek().unwrap() == calldata.len().into(), 'stack top is not calldatasize');
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ fn callvalue() -> u256 {

fn setup_call_context() -> CallContext {
let bytecode: Span<u8> = array![1, 2, 3].span();
let call_data: Span<u8> = array![4, 5, 6].span();
let calldata: Span<u8> = array![4, 5, 6].span();
let value: u256 = callvalue();

CallContextTrait::new(bytecode, call_data, value)
CallContextTrait::new(bytecode, calldata, value)
}

fn setup_execution_context() -> ExecutionContext {
Expand All @@ -43,10 +43,10 @@ fn setup_execution_context() -> ExecutionContext {
}

fn setup_call_context_with_bytecode(bytecode: Span<u8>) -> CallContext {
let call_data: Span<u8> = array![4, 5, 6].span();
let calldata: Span<u8> = array![4, 5, 6].span();
let value: u256 = 100;

CallContextTrait::new(bytecode, call_data, value)
CallContextTrait::new(bytecode, calldata, value)
}

fn setup_execution_context_with_bytecode(bytecode: Span<u8>) -> ExecutionContext {
Expand All @@ -65,7 +65,7 @@ fn setup_execution_context_with_bytecode(bytecode: Span<u8>) -> ExecutionContext

impl CallContextPartialEq of PartialEq<CallContext> {
fn eq(lhs: @CallContext, rhs: @CallContext) -> bool {
lhs.bytecode() == rhs.bytecode() && lhs.call_data == rhs.call_data && lhs.value == rhs.value
lhs.bytecode() == rhs.bytecode() && lhs.calldata == rhs.calldata && lhs.value == rhs.value
}
fn ne(lhs: @CallContext, rhs: @CallContext) -> bool {
!(lhs == rhs)
Expand Down
2 changes: 1 addition & 1 deletion docs/general/model/execution_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ classDiagram

class CallContext{
bytecode: Span~u8~,
call_data: Span~u8~,
calldata: Span~u8~,
value: u256,
}
ExecutionContext *-- CallContext
Expand Down
Loading