Skip to content

Commit

Permalink
fixed some call data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed May 5, 2024
1 parent 5df0391 commit f3dd2bf
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 104 deletions.
2 changes: 2 additions & 0 deletions src/configs/gas_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ impl DynamicCosts {
}

DynamicCosts::Copy { size_bytes } => {
println!("size_bytes: {}", size_bytes);
println!("size_bytes.div_ceil(32): {}", size_bytes.div_ceil(32) as u64);
static_costs::G_VERY_LOW + static_costs::G_COPY * (size_bytes.div_ceil(32) as u64)
}

Expand Down
98 changes: 63 additions & 35 deletions src/evm_logic/evm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Convenient to keep this as a macro as allows for early returns and error handlin
Should be converted to function once proper error handling is introduced
*/


use super::macros::pop_u64;
use super::{macros::pop, EVMContext, Message};
use crate::configs::gas_costs::DynamicCosts;
Expand All @@ -15,7 +14,6 @@ use crate::runtime::Runtime;

use primitive_types::U256;


#[inline]
pub fn call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool) -> ExecutionResult {
let (mut gas, address, value, args_offset, args_size, ret_offset, ret_size) = (
Expand Down Expand Up @@ -51,11 +49,16 @@ pub fn call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool) -> Ex
.cost(),
);
if evm.gas_recorder.gas_input > evm.gas_recorder.gas_usage {
println!("Gas usage {:x}", evm.gas_recorder.gas_input - evm.gas_recorder.gas_usage);
println!(
"Gas usage {:x}",
evm.gas_recorder.gas_input - evm.gas_recorder.gas_usage
);
}
return_if_gas_too_high!(evm.gas_recorder);
match make_call(evm, runtime, debug, call_args, false) {
ExecutionResult::Error(_) => ExecutionResult::Success(ExecutionSuccess::RevertedTransaction),
ExecutionResult::Error(_) => {
ExecutionResult::Success(ExecutionSuccess::RevertedTransaction)
}
ExecutionResult::Success(_) => ExecutionResult::InProgress,
ExecutionResult::InProgress => panic!("Call should not be still in progress"),
}
Expand Down Expand Up @@ -100,7 +103,9 @@ pub fn call_code(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool)
);
return_if_gas_too_high!(evm.gas_recorder);
match make_call(evm, runtime, debug, call_args, false) {
ExecutionResult::Error(_) => ExecutionResult::Success(ExecutionSuccess::RevertedTransaction),
ExecutionResult::Error(_) => {
ExecutionResult::Success(ExecutionSuccess::RevertedTransaction)
}
ExecutionResult::Success(_) => ExecutionResult::InProgress,
ExecutionResult::InProgress => panic!("Call should not be still in progress"),
}
Expand Down Expand Up @@ -145,15 +150,20 @@ pub fn delegate_call(
);
return_if_gas_too_high!(evm.gas_recorder);
match make_call(evm, runtime, debug, call_args, false) {
ExecutionResult::Error(_) => ExecutionResult::Success(ExecutionSuccess::RevertedTransaction),
ExecutionResult::Error(_) => {
ExecutionResult::Success(ExecutionSuccess::RevertedTransaction)
}
ExecutionResult::Success(_) => ExecutionResult::InProgress,
ExecutionResult::InProgress => panic!("Call should not be still in progress"),
}
}


#[inline]
pub fn static_call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool) -> ExecutionResult {
pub fn static_call(
evm: &mut EVMContext,
runtime: &mut impl Runtime,
debug: bool,
) -> ExecutionResult {
let (mut gas, address, args_offset, args_size, ret_offset, ret_size) = (
pop!(evm).as_u64(),
pop!(evm),
Expand Down Expand Up @@ -185,13 +195,14 @@ pub fn static_call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool
return_if_gas_too_high!(evm.gas_recorder);
runtime.mark_hot(address);
match make_call(evm, runtime, debug, call_args, true) {
ExecutionResult::Error(_) => ExecutionResult::Success(ExecutionSuccess::RevertedTransaction),
ExecutionResult::Error(_) => {
ExecutionResult::Success(ExecutionSuccess::RevertedTransaction)
}
ExecutionResult::Success(_) => ExecutionResult::InProgress,
ExecutionResult::InProgress => panic!("Call should not be still in progress"),
}
}


pub struct CallArgs {
pub gas: u64,
pub code_address: U256,
Expand All @@ -210,14 +221,17 @@ pub fn make_call(
runtime: &mut impl Runtime,
debug: bool,
args: CallArgs,
is_static: bool
is_static: bool,
) -> ExecutionResult {
let code: Vec<u8> = runtime.code(args.code_address);
let gas = args
.gas
.min((evm.gas_input - evm.gas_recorder.gas_usage.clone() as u64) * 63 / 64);
if args.args_offset.checked_add( args.args_size).is_none() || (args.args_offset + args.args_size > evm.memory.bytes.len()) {
evm.gas_recorder.record_gas_usage(evm.gas_recorder.gas_input as u64);
if args.args_offset.checked_add(args.args_size).is_none()
|| (args.args_offset + args.args_size > evm.memory.bytes.len())
{
evm.gas_recorder
.record_gas_usage(evm.gas_recorder.gas_input as u64);
return ExecutionResult::Error(ExecutionError::InvalidMemSize);
}
let mut sub_evm = EVMContext::create_sub_context(
Expand All @@ -232,52 +246,66 @@ pub fn make_call(
evm.transaction.clone(),
evm.gas_price,
evm.nested_index + 1,
is_static
is_static,
);
runtime.add_context();
let execution_result = sub_evm.execute_program(runtime, debug);
match &execution_result {
ExecutionResult::Error(error) => {
runtime.revert_context();
match &error {
ExecutionError::Revert(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
ExecutionError::Revert(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
}
_ => {
evm.last_return_data = Memory::new();
}
}
_ => {
evm.last_return_data = Memory::new();
}
}},
}
ExecutionResult::Success(success) => {
println!("Merging context");
runtime.merge_context();
match success {
ExecutionSuccess::Return(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
ExecutionSuccess::Return(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
}
_ => {
evm.last_return_data = Memory::new();
}
}
_ => {
evm.last_return_data = Memory::new();
}
}},
}
ExecutionResult::InProgress => {
panic!("Program shouldn't have excited whilst in progress");
}
}
evm.gas_recorder.merge(&sub_evm.gas_recorder, &execution_result);
push!(evm,U256::from(match execution_result {
ExecutionResult::Success(_) => true,
_ => false,
} as u64));
evm.gas_recorder
.merge(&sub_evm.gas_recorder, &execution_result);
push!(
evm,
U256::from(match execution_result {
ExecutionResult::Success(_) => true,
_ => false,
} as u64)
);
if evm.gas_recorder.gas_input > evm.gas_recorder.gas_usage {
println!("Gas usage 1 {:x}", evm.gas_recorder.gas_input - evm.gas_recorder.gas_usage);
println!(
"Gas usage 1 {:x}",
evm.gas_recorder.gas_input - evm.gas_recorder.gas_usage
);
}
execution_result
}

fn handle_return_data(evm: &mut EVMContext, return_data: &Vec<u8>, ret_offset: usize, ret_size: usize) {
fn handle_return_data(
evm: &mut EVMContext,
return_data: &[u8],
ret_offset: usize,
ret_size: usize,
) {
evm.last_return_data = Memory::from(&return_data.clone(), None);
evm.memory.copy_from_bytes(
&return_data,
0,
return_data,
U256::from(0),
ret_offset,
ret_size,
&mut evm.gas_recorder,
Expand Down
14 changes: 8 additions & 6 deletions src/evm_logic/evm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ pub fn decode_instruction(

opcodes::CALLDATACOPY => {
// TODO fix
let (dest_offset, offset, size) = (pop_usize!(evm), pop_usize!(evm), pop_usize!(evm));
let (dest_offset, offset, size) = (pop_usize!(evm), pop!(evm), pop_usize!(evm));
evm.gas_recorder
.record_gas_usage(DynamicCosts::Copy { size_bytes: size }.cost());
return_if_gas_too_high!(evm.gas_recorder);
Expand All @@ -417,9 +417,11 @@ pub fn decode_instruction(
evm.gas_recorder
.record_gas_usage(DynamicCosts::Copy { size_bytes: size }.cost());
return_if_gas_too_high!(evm.gas_recorder);
return_if_error!(evm.memory.copy_from(
&mut evm.program,
offset,
println!("Not copied yet");
// Should not expand the program
return_if_error!(evm.memory.copy_from_bytes(
&mut evm.program.bytes,
U256::from(offset),
dest_offset,
size,
&mut evm.gas_recorder
Expand Down Expand Up @@ -454,8 +456,8 @@ pub fn decode_instruction(
.cost(),
);
return_if_error!(evm.memory.copy_from_bytes(
&runtime.code(addr),
offset,
&mut runtime.code(addr),
U256::from(offset),
dest_offset,
size,
&mut evm.gas_recorder
Expand Down
3 changes: 3 additions & 0 deletions src/evm_logic/gas_recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl GasRecorder {
self.gas_usage = u64::MAX as usize;
return;
}
println!("Memory expansion cost: {:x}", memory_expansion_cost);
self.gas_usage += memory_expansion_cost;
}

Expand Down Expand Up @@ -78,8 +79,10 @@ fn call_data_gas_cost(data: &[u8]) -> u64 {
let mut cost = 0;
for byte in data {
if *byte == 0 {
// TODO put into static costs
cost += 4;
} else {
// TODO put into static costs
cost += 16;
}
}
Expand Down
Loading

0 comments on commit f3dd2bf

Please sign in to comment.