Skip to content

Commit

Permalink
81 buffer tests passing and refactored call
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed Mar 28, 2024
1 parent c777a7d commit eeaece6
Show file tree
Hide file tree
Showing 14 changed files with 595 additions and 159 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"hasher",
"keccak",
"llcversion"
]
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO

- [_] Fix calling costs and split up function
- [_] Fix Calldata load, Code load etc.
- [_] Do general clean up
- [_] Better error handling
Expand Down
79 changes: 26 additions & 53 deletions src/configs/gas_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,9 @@ pub enum DynamicCosts {
},
Call {
value: U256,
gas: U256,
target_is_cold: bool,
target_exists: bool,
},
CallCode {
/// Call value.
value: U256,
/// Call gas.
gas: U256,
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
/// Whether the target exists.
target_exists: bool,
},
DelegateCall {
/// Call gas.
gas: U256,
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
/// Whether the target exists.
target_exists: bool,
empty_account: bool,
is_delegate: bool,
},
StaticCall {
/// Call gas.
Expand Down Expand Up @@ -102,9 +84,9 @@ pub enum DynamicCosts {
/// Gas cost for `LOG`.
Log {
/// Topic length.
n: u8,
topic_length: u8,
/// Data length.
len: U256,
size: usize,
},
/// Gas cost for `EXTCODECOPY`.
Exp {
Expand All @@ -115,6 +97,9 @@ pub enum DynamicCosts {
/// Length.
len: U256,
},
Create {
deployed_code_size: usize,
},
/// Gas cost for `SLOAD`.
SLoad {
/// True if target has not been previously accessed in this transaction
Expand Down Expand Up @@ -156,38 +141,23 @@ impl DynamicCosts {
}
DynamicCosts::Call {
value,
gas,
target_is_cold,
target_exists,
} => {
if *value != ZERO {
static_costs::G_CALL_VALUE
} else {
static_costs::G_CALL_STIPEND
}
}
DynamicCosts::CallCode {
value,
gas,
empty_account,
target_is_cold,
target_exists,
is_delegate,
} => {
if *value != ZERO {
static_costs::G_CALL_VALUE

0 + if *target_is_cold {
static_costs::G_COLD_ACCOUNT_ACCESS
} else {
static_costs::G_CALL_STIPEND
}
static_costs::G_WARM_ACCESS
} +
if !value.eq(&ZERO) & !is_delegate {
static_costs::G_CALL_VALUE - static_costs::G_CALL_STIPEND
} else {0} +
if *empty_account {
static_costs::G_NEW_ACCOUNT
} else {0}
}
DynamicCosts::DelegateCall {
gas,
target_is_cold,
target_exists,
} => static_costs::G_CALL_STIPEND,
DynamicCosts::StaticCall {
gas,
target_is_cold,
target_exists,
} => static_costs::G_CALL_STIPEND,
DynamicCosts::SStore {
original,
current,
Expand All @@ -203,10 +173,10 @@ impl DynamicCosts {
DynamicCosts::Keccak256 { len } => {
static_costs::G_KECCAK256 + (len.div_ceil(32)) * static_costs::G_KECCAK256_WORD
}
DynamicCosts::Log { n, len } => {
DynamicCosts::Log { topic_length, size } => {
static_costs::G_LOG
+ static_costs::G_LOG_TOPIC * (*n as u64)
+ static_costs::G_LOG_DATA * (len.as_u64() / 32)
+ static_costs::G_LOG_TOPIC * (*topic_length as u64)
+ static_costs::G_LOG_DATA * (*size as u64)
}

DynamicCosts::Exp { power } => {
Expand All @@ -229,6 +199,9 @@ impl DynamicCosts {
static_costs::G_WARM_ACCESS
}
}
DynamicCosts::Create {deployed_code_size} => {
static_costs::G_CREATE + static_costs::G_KECCAK256_WORD * (*deployed_code_size as u64).div_ceil(32)
}
_ => 0,
}
}
Expand Down
20 changes: 14 additions & 6 deletions src/evm_logic/evm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod call;
mod decoder;
mod macros;
mod create;

use crate::evm_logic::evm::macros::{break_if_error, return_if_error};
use crate::evm_logic::gas_calculator::{call_data_gas_cost, GasRecorder};
Expand Down Expand Up @@ -38,6 +39,7 @@ pub struct EVMContext {
stopped: bool,
nested_index: usize,
gas_recorder: GasRecorder,
is_static: bool
}

impl EVMContext {
Expand Down Expand Up @@ -69,8 +71,16 @@ impl EVMContext {
transaction,
gas_price,
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));
}
if debug {
println!("Call Data Gas Cost: {:x}", evm.gas_recorder.gas_usage);
}
let result = evm.execute_program(runtime, debug);
// TODO move this into gas_recorder
let gas_usage = evm.gas_recorder.gas_usage
Expand All @@ -87,6 +97,7 @@ impl EVMContext {
transaction: Transaction,
gas_price: U256,
nested_index: usize,
is_static: bool,
) -> EVMContext {
EVMContext {
stack: Stack::new(),
Expand All @@ -113,6 +124,7 @@ impl EVMContext {
gas_usage: 0,
gas_refunds: 0,
},
is_static: is_static
}
}

Expand All @@ -122,12 +134,8 @@ impl EVMContext {

let result = {
let mut result = ExecutionResult::Success;
if self.message.data.len() != 0 {
self.gas_recorder
.record_gas(call_data_gas_cost(&self.message.data));
}
if debug {
println!("Call Data Gas Cost: {:x}", self.gas_recorder.gas_usage);
if self.program.len() == 0 {
self.stopped = true;
}
while !self.stopped {
result = self.execute_next_instruction(runtime, debug);
Expand Down
Loading

0 comments on commit eeaece6

Please sign in to comment.