Skip to content

Commit

Permalink
Some refactoring and fixed add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed Mar 27, 2024
1 parent 5f92e9e commit bdd80d2
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 192 deletions.
210 changes: 110 additions & 100 deletions src/configs/gas_costs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::ops::Div;

use primitive_types::{H256, U256};

use crate::evm_logic::util::ZERO;

pub mod static_costs {
pub const G_ZERO: u64 = 0;
pub const G_JUMP_DEST: u64 = 1;
Expand Down Expand Up @@ -36,154 +42,152 @@ pub mod static_costs {
pub const G_BLOCK_HASH: u64 = 20;
}

pub mod dyn_costs {
enum DynamicCosts {
ExtCodeSize {
target_is_cold: bool,
},
Balance {
target_is_cold: bool,
},
ExtCodeHash {
target_is_cold: bool,
},
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,
},
StaticCall {
/// 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,
},
SStore {
/// Original value.
original: H256,
/// Current value.
current: H256,
/// New value.
new: H256,
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
},
/// Gas cost for `SHA3`.
Sha3 {
/// Length of the data.
len: U256,
},
/// Gas cost for `LOG`.
Log {
/// Topic length.
n: u8,
/// Data length.
len: U256,
},
/// Gas cost for `EXTCODECOPY`.
ExtCodeCopy {
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
/// Length.
len: U256,
},
Exp {
/// Power of `EXP`.
power: U256,
},
Create2 {
/// Length.
len: U256,
},
/// Gas cost for `SLOAD`.
SLoad {
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
},
}
pub enum DynamicCosts {
ExtCodeSize {
target_is_cold: bool,
},
Balance {
target_is_cold: bool,
},
ExtCodeHash {
target_is_cold: bool,
},
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,
},
StaticCall {
/// 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,
},
SStore {
/// Original value.
original: H256,
/// Current value.
current: H256,
/// New value.
new: H256,
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
},
/// Gas cost for `SHA3`.
Keccak256 {
/// Length of the data.
len: u64,
},
/// Gas cost for `LOG`.
Log {
/// Topic length.
n: u8,
/// Data length.
len: U256,
},
/// Gas cost for `EXTCODECOPY`.
ExtCodeCopy {
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
/// Length.
len: U256,
},
Exp {
/// Power of `EXP`.
power: U256,
},
Create2 {
/// Length.
len: U256,
},
/// Gas cost for `SLOAD`.
SLoad {
/// True if target has not been previously accessed in this transaction
target_is_cold: bool,
},
}

impl GasCost {
impl DynamicCosts {
// TODO add error here
pub fn cost(&self) -> u64 {
match self {
GasCost::ExtCodeSize { target_is_cold } => {
DynamicCosts::ExtCodeSize { target_is_cold } => {
if *target_is_cold {
static_costs::G_COLDS_LOAD
} else {
static_costs::G_WARM_ACCESS
}
}
GasCost::Balance { target_is_cold } => {
DynamicCosts::Balance { target_is_cold } => {
if *target_is_cold {
static_costs::G_COLDS_LOAD
} else {
static_costs::G_WARM_ACCESS
}
}
GasCost::ExtCodeHash { target_is_cold } => {
DynamicCosts::ExtCodeHash { target_is_cold } => {
if *target_is_cold {
static_costs::G_COLDS_LOAD
} else {
static_costs::G_WARM_ACCESS
}
}
GasCost::Call {
DynamicCosts::Call {
value,
gas,
target_is_cold,
target_exists,
} => {
if value != U256::zero() {
if *value != ZERO {
static_costs::G_CALL_VALUE
} else {
static_costs::G_CALL_STIPEND
}
}
GasCost::CallCode {
DynamicCosts::CallCode {
value,
gas,
target_is_cold,
target_exists,
} => {
if value != U256::zero() {
if *value != ZERO {
static_costs::G_CALL_VALUE
} else {
static_costs::G_CALL_STIPEND
}
}
GasCost::DelegateCall {
DynamicCosts::DelegateCall {
gas,
target_is_cold,
target_exists,
} => static_costs::G_CALL_STIPEND,
GasCost::StaticCall {
DynamicCosts::StaticCall {
gas,
target_is_cold,
target_exists,
} => static_costs::G_CALL_STIPEND,
GasCost::SStore {
DynamicCosts::SStore {
original,
current,
new,
Expand All @@ -195,14 +199,20 @@ impl GasCost {
static_costs::G_SRESET
}
}
GasCost::Sha3 { len } => {
static_costs::G_KECCAK256 + (len.as_u64() / 32) * static_costs::G_KECCAK256_WORD
DynamicCosts::Keccak256 { len } => {
static_costs::G_KECCAK256 + (len.div_ceil(32)) * static_costs::G_KECCAK256_WORD
}
GasCost::Log { n, len } => {
DynamicCosts::Log { n, len } => {
static_costs::G_LOG
+ static_costs::G_LOG_TOPIC * (*n as u64)
+ static_costs::G_LOG_DATA * (len.as_u64() / 32)
}

DynamicCosts::Exp { power } => {
// bytes_for_u256
let bytes = (power.bits() + 7) / 8;
static_costs::G_EXP + static_costs::G_EXP_BYTE * bytes as u64
}
_ => 0,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod bytecode_spec;
pub mod gas_costs;
Loading

0 comments on commit bdd80d2

Please sign in to comment.