Skip to content

Commit

Permalink
some broken buffer tests but major refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed Mar 29, 2024
1 parent 2bab8b0 commit ec80504
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 415 deletions.
28 changes: 11 additions & 17 deletions src/evm_logic/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ mod create;
mod decoder;
pub mod macros;

use crate::evm_logic::evm::macros::{break_if_error, return_if_error};
use crate::evm_logic::gas_calculator::{call_data_gas_cost, GasRecorder};
use crate::configs::gas_costs::static_costs;
use crate::evm_logic::gas_calculator::GasRecorder;
use crate::result::{Error, ExecutionResult, ExecutionSuccess};
use crate::runtime::Runtime;
use primitive_types::U256;

use super::state::memory::Memory;
use super::state::stack::Stack;

use primitive_types::U256;

#[derive(Clone)]
struct Transaction {
pub origin: U256,
Expand All @@ -33,8 +34,6 @@ pub struct EVMContext {
transaction: Transaction,
message: Message,
last_return_data: Memory,
// TODO refactor this away into the result
result: Memory,
gas_input: u64,
gas_price: U256,
nested_index: usize,
Expand Down Expand Up @@ -73,11 +72,8 @@ impl EVMContext {
0,
false,
);
evm.gas_recorder.record_gas(21000);
if evm.message.data.len() != 0 {
evm.gas_recorder
.record_gas(call_data_gas_cost(&evm.message.data));
}
evm.gas_recorder.record_gas_usage(static_costs::G_TRANSACTION);
evm.gas_recorder.record_call_data_gas_usage(&evm.message.data);
if debug {
println!("Call Data Gas Cost: {:x}", evm.gas_recorder.gas_usage);
}
Expand All @@ -104,19 +100,18 @@ impl EVMContext {
memory: Memory::new(),
program: Memory::from(
code,
&mut GasRecorder {
Some(&mut GasRecorder {
gas_input: gas as usize,
gas_usage: 0,
gas_refunds: 0,
},
}),
),
program_counter: 0,
contract_address: address,
// TODO remove need to clone here
transaction: transaction,
message: message,
last_return_data: Memory::new(),
result: Memory::new(),
gas_input: gas,
gas_price: gas_price,
nested_index: nested_index,
Expand All @@ -134,22 +129,21 @@ impl EVMContext {
runtime.add_context();

let result = {
let mut result = ExecutionResult::Success(ExecutionSuccess::Unknown);
let mut result;
if self.program.len() != 0 {
loop {
result = self.execute_next_instruction(runtime, debug);
match result {
match &result {
ExecutionResult::Err(_) => {
break;
}
ExecutionResult::Success(success) => match success {
ExecutionSuccess::Return | ExecutionSuccess::Stop => {
ExecutionSuccess::Return(_) | ExecutionSuccess::Stop => {
break;
}
_ => {}
},
}
break_if_error!(result);
}
} else {
result = ExecutionResult::Err(Error::InvalidMemSize);
Expand Down
65 changes: 47 additions & 18 deletions src/evm_logic/evm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool) -> Ex
ret_offset: ret_offset,
ret_size: ret_size,
};
evm.gas_recorder.record_gas(
evm.gas_recorder.record_gas_usage(
DynamicCosts::Call {
value: value,
target_is_cold: runtime.is_cold(address),
Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn call_code(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool)
ret_offset: ret_offset,
ret_size: ret_size,
};
evm.gas_recorder.record_gas(
evm.gas_recorder.record_gas_usage(
DynamicCosts::Call {
value: value,
target_is_cold: runtime.is_cold(address),
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn delegate_call(
ret_offset: ret_offset,
ret_size: ret_size,
};
evm.gas_recorder.record_gas(
evm.gas_recorder.record_gas_usage(
DynamicCosts::Call {
value: evm.message.value,
target_is_cold: runtime.is_cold(address),
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn static_call(evm: &mut EVMContext, runtime: &mut impl Runtime, debug: bool
ret_offset: ret_offset,
ret_size: ret_size,
};
evm.gas_recorder.record_gas(
evm.gas_recorder.record_gas_usage(
DynamicCosts::Call {
value: ZERO,
target_is_cold: runtime.is_cold(address),
Expand Down Expand Up @@ -227,7 +227,7 @@ pub fn make_call(
.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(evm.gas_recorder.gas_input as u64);
evm.gas_recorder.record_gas_usage(evm.gas_recorder.gas_input as u64);
return ExecutionResult::Err(Error::InvalidMemSize);
}
let mut sub_evm = EVMContext::create_sub_context(
Expand All @@ -246,26 +246,44 @@ pub fn make_call(
);
println!("--------------");
let execution_result = sub_evm.execute_program(runtime, debug);
if match execution_result { ExecutionResult::Err(err) => match err { Error::Revert => {true}, _ => {false}}, _ => {true} } {
evm.last_return_data = sub_evm.result;
println!("Copy Result");
evm.memory.copy_from(
&mut evm.last_return_data,
0,
args.ret_offset,
args.ret_size,
&mut evm.gas_recorder,
);
} else {
evm.last_return_data = Memory::new();
match &execution_result {
ExecutionResult::Err(error) => match &error {
Error::Revert(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
}
_ => {
evm.last_return_data = Memory::new();
}
},
ExecutionResult::Success(success) => match success {
ExecutionSuccess::Return(result) => {
handle_return_data(evm, result, args.ret_offset, args.ret_size);
}
_ => {
evm.last_return_data = Memory::new();
}
}
}
// if match execution_result { ExecutionResult::Err(err) => match err { Error::Revert => {true}, _ => {false}}, _ => {true} } {
// evm.last_return_data = sub_evm.result;
// println!("Copy Result");
// evm.memory.copy_from(
// &mut evm.last_return_data,
// 0,
// args.ret_offset,
// args.ret_size,
// &mut evm.gas_recorder,
// );
// } else {
// evm.last_return_data = Memory::new();
// }
// println!("Gas usage {:x}", evm.gas_recorder.gas_input - evm.gas_recorder.gas_usage + evm.gas_recorder.gas_refunds);
println!("Execution Result {:?}", execution_result);
if let ExecutionResult::Success(_) = execution_result {
println!("Merging");
evm.gas_recorder.merge(&sub_evm.gas_recorder);
} else {
evm.gas_recorder.record_gas(sub_evm.gas_recorder.gas_usage as u64);
evm.gas_recorder.record_gas_usage(sub_evm.gas_recorder.gas_usage as u64);
}
// evm.gas_recorder
// .record_gas((sub_evm.gas_recorder.gas_usage - sub_evm.gas_recorder.gas_refunds.min(sub_evm.gas_recorder.gas_usage)) as u64);
Expand All @@ -279,6 +297,17 @@ pub fn make_call(
execution_result
}

fn handle_return_data(evm: &mut EVMContext, return_data: &Vec<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,
ret_offset,
ret_size,
&mut evm.gas_recorder,
);
}

// #[inline]
// pub fn _(
// evm: &mut EVMContext,
Expand Down
4 changes: 2 additions & 2 deletions src/evm_logic/evm/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn create(
// evm.gas_recorder.gas_usage -= 100;
runtime.increase_nonce(evm.message.caller);
runtime.increase_nonce(address);
if result.is_result_with_return() {
if result.has_return_result() {
runtime.set_contract_code(address, evm.last_return_data.bytes.clone());
}
println!("Created: {:?}", address);
Expand All @@ -66,7 +66,7 @@ pub fn create(
}
.cost()
);
evm.gas_recorder.record_gas(
evm.gas_recorder.record_gas_usage(
DynamicCosts::Create {
deployed_code_size: deployed_code_size,
}
Expand Down
Loading

0 comments on commit ec80504

Please sign in to comment.