Skip to content

Commit

Permalink
fixed swap
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed May 5, 2024
1 parent f3dd2bf commit d1eb2f2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/evm_logic/evm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn handle_return_data(
ret_offset: usize,
ret_size: usize,
) {
evm.last_return_data = Memory::from(&return_data.clone(), None);
evm.last_return_data = Memory::from(&return_data, Some(&mut evm.gas_recorder));
evm.memory.copy_from_bytes(
return_data,
U256::from(0),
Expand Down
56 changes: 38 additions & 18 deletions src/evm_logic/evm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,20 @@ pub fn decode_instruction(
push!(evm, U256::zero());
}
_ => {
let result = a.checked_rem(c)
.unwrap()
.overflowing_add(b.checked_rem(c).unwrap())
.0;
let result = a
.checked_rem(c)
.unwrap()
.overflowing_add(b.checked_rem(c).unwrap())
.0;
println!("result: {:?}", result);
push!(
evm,
a.checked_rem(c)
.unwrap()
.overflowing_add(b.checked_rem(c).unwrap())
.0.checked_rem(c).unwrap()
.0
.checked_rem(c)
.unwrap()
);
}
}
Expand Down Expand Up @@ -312,12 +315,10 @@ pub fn decode_instruction(
let (shift, value) = (pop!(evm), pop!(evm));
let sign = value.bit(255);
if shift >= 256.into() {
push!(evm, if sign {U256::MAX} else {ZERO});
}
else if !sign {
push!(evm, if sign { U256::MAX } else { ZERO });
} else if !sign {
push!(evm, value.shr(shift.as_u64()));
}
else {
} else {
let value = value.shr(shift);
let mask = U256::MAX;
let mask = mask.shl(256 as u64 - shift.as_u64());
Expand Down Expand Up @@ -603,8 +604,13 @@ pub fn decode_instruction(
u256_to_h256(value),
);
runtime.set_storage(evm.contract_address, key, u256_to_h256(value));
println!("v_ord: {:?}, v_cur: {:?}, v_new: {:?}, target_is_cold: {:?}",
v_org, v_cur, v_new, runtime.is_cold_index(evm.contract_address, key));
println!(
"v_ord: {:?}, v_cur: {:?}, v_new: {:?}, target_is_cold: {:?}",
v_org,
v_cur,
v_new,
runtime.is_cold_index(evm.contract_address, key)
);
let dynamic_cost = DynamicCosts::SStore {
original: v_org,
current: v_cur,
Expand Down Expand Up @@ -665,6 +671,7 @@ pub fn decode_instruction(
evm.program_counter += push_number as usize;
push!(evm, U256::from_big_endian(bytes.as_slice()));
evm.gas_recorder.record_gas_usage(static_costs::G_VERY_LOW);
println!("val {:?}",U256::from_big_endian(bytes.as_slice()));
}

opcodes::DUP_1..=opcodes::DUP_16 => {
Expand All @@ -675,12 +682,18 @@ pub fn decode_instruction(
}

opcodes::SWAP_1..=opcodes::SWAP_16 => {
// println!("stack: {:?}", evm.stack.data);
// println!("stack pointer: {:?}", evm.stack.stack_pointer);
let swap_number: usize = (opcode - opcodes::SWAP_1 + 1) as usize;
// println!("swap_number: {:?}", swap_number);
let bottom_value = evm.stack.read_nth(swap_number);
let top_value = pop!(evm);
evm.stack.write_nth(swap_number - 1, top_value);
push!(evm, bottom_value);
let top_value = evm.stack.read_nth(0);
evm.stack.write_nth(swap_number, top_value);
evm.stack.write_nth(0, bottom_value);
// push!(evm, bottom_value);
evm.gas_recorder.record_gas_usage(static_costs::G_VERY_LOW);
// println!("top_value: {:?}, bottom_value: {:?}", top_value, bottom_value);
// println!("stack: {:?}", evm.stack.data);
}

// TODO log
Expand Down Expand Up @@ -730,13 +743,17 @@ pub fn decode_instruction(
let len = offset.checked_add(size);
match len {
Some(len) => {
evm.gas_recorder
.record_memory_gas_usage(evm.memory.len(), len);
if size > 0 {
evm.gas_recorder
.record_memory_gas_usage(evm.memory.len(), len);
println!("Recorded gas usage return");
}
}
None => {
evm.gas_recorder.set_gas_usage_to_max();
return ExecutionResult::Error(ExecutionError::InvalidMemoryAccess);
}
_ => {}
}
}
return_if_error!(evm.check_gas_usage());
Expand Down Expand Up @@ -775,7 +792,10 @@ pub fn decode_instruction(
}
}
return_if_error!(evm.check_gas_usage());
println!("evm.memory: {:?}", evm.memory.to_sub_vec(offset, offset + size).len());
println!(
"evm.memory: {:?}",
evm.memory.to_sub_vec(offset, offset + size).len()
);
return ExecutionResult::Error(ExecutionError::Revert(
evm.memory.to_sub_vec(offset, offset + size),
));
Expand Down
8 changes: 4 additions & 4 deletions src/evm_logic/state/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::evm_logic::util::ZERO;
const STACK_SIZE: usize = 1024;

pub struct Stack {
data: [U256; STACK_SIZE],
stack_pointer: usize,
pub data: [U256; STACK_SIZE],
pub stack_pointer: usize,
}

impl Stack {
Expand Down Expand Up @@ -40,12 +40,12 @@ impl Stack {
// TODO add error handling here
#[inline]
pub fn read_nth(&self, offset: usize) -> U256 {
self.data[self.stack_pointer - offset]
self.data[self.stack_pointer - offset - 1]
}

// TODO add error handling here
#[inline]
pub fn write_nth(&mut self, offset: usize, value: U256) {
self.data[self.stack_pointer - offset] = value;
self.data[self.stack_pointer - offset - 1] = value;
}
}
8 changes: 4 additions & 4 deletions tests/official_tests/official_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use crate::mocks::mock_runtime::{Contract, MockRuntime};

use official_test_types::types::{TestState, TestStateMulti};

generate_official_tests!(
"./tests/official_tests/tests/GeneralStateTests/VMTests/vmTests/calldatacopy.json"
);
// generate_official_tests!(
// "./tests/official_tests/tests/GeneralStateTests/VMTests"
// "./tests/official_tests/tests/GeneralStateTests/VMTests/vmIOandFlowOperations"
// );
generate_official_tests!(
"./tests/official_tests/tests/GeneralStateTests/VMTests/vmTests/"
);
// generate_official_tests!("./tests/official_tests/tests/GeneralStateTests/stMemoryTest");

pub fn run_test_file(filename: String, debug: bool, index: usize) {
Expand Down

0 comments on commit d1eb2f2

Please sign in to comment.