Skip to content

Commit

Permalink
clean + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Oct 19, 2023
1 parent 9a6161b commit 17f418d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl EnvironmentInformationImpl of EnvironmentInformationTrait {
let bytecode: Span<u8> = self.bytecode();

let copied: Span<u8> = if offset > bytecode.len() {
array![].span()
Default::default().span()
} else if (offset + size > bytecode.len()) {
bytecode.slice(offset, bytecode.len() - offset)
} else {
Expand Down
17 changes: 14 additions & 3 deletions crates/evm/src/tests/test_execution_context.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::nullable::{NullableTrait, null};
use evm::context::{
CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait, DefaultOptionSpanU8
CallContext, CallContextTrait, ExecutionContext, ExecutionContextId, ExecutionContextTrait,
DefaultOptionSpanU8
};
use evm::memory::{Memory, MemoryTrait};
use evm::model::Event;
Expand Down Expand Up @@ -33,9 +34,19 @@ fn test_call_context_new() {
let gas_price = 0xabde1;
let gas_limit = 0xe11a5;
let read_only = false;
let output_offset = 0;
let output_size = 0;

let call_ctx = CallContextTrait::new(
address, bytecode, calldata, value, read_only, gas_limit, gas_price
address,
bytecode,
calldata,
value,
read_only,
gas_limit,
gas_price,
output_offset,
output_size
);

// Then
Expand All @@ -52,7 +63,7 @@ fn test_call_context_new() {
fn test_execution_context_new() {
// Given
let call_ctx = setup_call_context();
let context_id = 0;
let context_id = ExecutionContextId::Root(0);
let program_counter: u32 = 0;

let stopped: bool = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn test_exec_return() {
assert(machine.exec_return().is_ok(), 'Exec return failed');

// Then
assert(1000 == load_word(32, parent_ctx_return_data(ref machine)), 'Wrong return_data');
assert(1000 == load_word(32, machine.output()), 'Wrong return_data');
assert(machine.stopped(), 'machine should be stopped')
}

Expand All @@ -42,6 +42,6 @@ fn test_exec_return_with_offset() {
assert(machine.exec_return().is_ok(), 'Exec return failed');

// Then
assert(256 == load_word(32, parent_ctx_return_data(ref machine)), 'Wrong return_data');
assert(256 == load_word(32, machine.output()), 'Wrong return_data');
assert(machine.stopped(), 'machine should be stopped')
}
12 changes: 6 additions & 6 deletions crates/evm/src/tests/test_machine.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use evm::context::CallContextTrait;
use evm::context::{CallContextTrait, ExecutionContextId, ExecutionContextTrait};
use evm::machine::{Machine, MachineCurrentContextTrait};
use evm::tests::test_utils::{
evm_address, setup_machine_with_bytecode, setup_machine, starknet_address,
Expand All @@ -23,20 +23,20 @@ fn test_set_current_ctx() {
let mut machine: Machine = Default::default();

let first_ctx = machine.current_ctx.unbox();
assert(first_ctx.id == 0, 'wrong first id');
assert(first_ctx.id() == 0, 'wrong first id');
// We need to re-box the context into the machine, otherwise we have a "Variable Moved" error.
machine.current_ctx = BoxTrait::new(first_ctx);
assert(machine.stack.active_segment == 0, 'wrong initial stack segment');
assert(machine.memory.active_segment == 0, 'wrong initial memory segment');

// Create another context with id=1
let mut second_ctx = setup_execution_context();
second_ctx.id = 1;
second_ctx.id = ExecutionContextId::Call(1);

machine.set_current_ctx(second_ctx);
assert(machine.stack.active_segment == 1, 'wrong updated stack segment');
assert(machine.memory.active_segment == 1, 'wrong updated stack segment');
assert(machine.current_ctx.unbox().id == 1, 'wrong updated id');
assert(machine.current_ctx.unbox().id() == 1, 'wrong updated id');
}

#[test]
Expand All @@ -53,7 +53,7 @@ fn test_set_pc() {
#[test]
#[available_gas(20000000)]
fn test_revert() {
let mut machine = Default::default();
let mut machine: Machine = Default::default();

machine.set_reverted();

Expand All @@ -64,7 +64,7 @@ fn test_revert() {
#[test]
#[available_gas(20000000)]
fn test_set_stopped() {
let mut machine = Default::default();
let mut machine: Machine = Default::default();

machine.set_stopped();

Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/tests/test_memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod internal {
memory.store_n(bytes_array.span(), offset);

// When
let mut elements: Array<u8> = array![];
let mut elements: Array<u8> = Default::default();
memory.load_n_internal(32, ref elements, offset);

// Then
Expand Down Expand Up @@ -173,7 +173,7 @@ fn test_store_n_2_aligned_words() {
// value [35] will be stored in final word
assert(memory.size() == 64, 'memory should be 64 bytes');

let mut stored_bytes = array![];
let mut stored_bytes = Default::default();
memory.load_n_internal(35, ref stored_bytes, 15);
assert(stored_bytes.span() == bytes_arr, 'stored bytes not == expected');
}
Expand Down
61 changes: 49 additions & 12 deletions crates/evm/src/tests/test_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use evm::context::{
CallContext, CallContextTrait, ExecutionContext, ExecutionContextTrait, DefaultOptionSpanU8
CallContext, CallContextTrait, ExecutionContext, ExecutionContextId, ExecutionContextTrait,
DefaultOptionSpanU8
};

use evm::machine::{Machine, MachineCurrentContextTrait};
Expand Down Expand Up @@ -67,12 +68,24 @@ fn setup_call_context() -> CallContext {
let read_only = false;
let gas_price = 0xaaaaaa;
let gas_limit = 0xffffff;

CallContextTrait::new(address, bytecode, calldata, value, read_only, gas_limit, gas_price)
let output_offset = 0;
let output_size = 0;

CallContextTrait::new(
address,
bytecode,
calldata,
value,
read_only,
gas_limit,
gas_price,
output_offset,
output_size
)
}

fn setup_execution_context() -> ExecutionContext {
let context_id = 0;
let context_id = ExecutionContextId::Root(0);
let call_ctx = setup_call_context();
let evm_address: EthAddress = evm_address();
let return_data = array![1, 2, 3].span();
Expand All @@ -84,7 +97,7 @@ fn setup_nested_execution_context() -> ExecutionContext {
let mut parent_context = setup_execution_context();

// Second Execution Context
let context_id = 1;
let context_id = ExecutionContextId::Call(1);
let mut child_context = setup_execution_context();
child_context.id = context_id;
child_context.parent_ctx = NullableTrait::new(parent_context);
Expand All @@ -102,12 +115,24 @@ fn setup_call_context_with_bytecode(bytecode: Span<u8>) -> CallContext {
let read_only = false;
let gas_price = 0xaaaaaa;
let gas_limit = 0xffffff;

CallContextTrait::new(address, bytecode, calldata, value, read_only, gas_limit, gas_price)
let output_offset = 0;
let output_size = 0;

CallContextTrait::new(
address,
bytecode,
calldata,
value,
read_only,
gas_limit,
gas_price,
output_offset,
output_size
)
}

fn setup_execution_context_with_bytecode(bytecode: Span<u8>) -> ExecutionContext {
let context_id = 0;
let context_id = ExecutionContextId::Root(0);
let call_ctx = setup_call_context_with_bytecode(bytecode);
let evm_address: EthAddress = evm_address();
let return_data = Default::default().span();
Expand All @@ -123,12 +148,24 @@ fn setup_call_context_with_calldata(calldata: Span<u8>) -> CallContext {
let read_only = false;
let gas_price = 0xffffff;
let gas_limit = 0xffffff;

CallContextTrait::new(address, bytecode, calldata, value, read_only, gas_price, gas_limit)
let output_offset = 0;
let output_size = 0;

CallContextTrait::new(
address,
bytecode,
calldata,
value,
read_only,
gas_price,
gas_limit,
output_offset,
output_size
)
}

fn setup_execution_context_with_calldata(calldata: Span<u8>) -> ExecutionContext {
let context_id = 0;
let context_id = ExecutionContextId::Root(0);
let call_ctx = setup_call_context_with_calldata(calldata);
let evm_address: EthAddress = evm_address();
let return_data = Default::default().span();
Expand Down Expand Up @@ -216,7 +253,7 @@ fn parent_ctx_return_data(ref self: Machine) -> Span<u8> {
// Due to ownership mechanism, both branches need to explicitly re-bind the parent_ctx.
FromNullableResult::Null => {
current_ctx.parent_ctx = Default::default();
array![].span()
Default::default().span()
},
FromNullableResult::NotNull(parent_ctx) => {
let mut parent_ctx = parent_ctx.unbox();
Expand Down
10 changes: 0 additions & 10 deletions crates/utils/src/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ impl U256TryIntoContractAddress of TryInto<u256, ContractAddress> {
}
}

impl U256TryIntoEthAddress of TryInto<u256, EthAddress> {
fn try_into(self: u256) -> Option<EthAddress> {
let maybe_value: Option<felt252> = self.try_into();
match maybe_value {
Option::Some(value) => value.try_into(),
Option::None => Option::None,
}
}
}

//TODO remove once merged in corelib
impl StorageBaseAddressIntoFelt252 of Into<StorageBaseAddress, felt252> {
fn into(self: StorageBaseAddress) -> felt252 {
Expand Down

0 comments on commit 17f418d

Please sign in to comment.