Skip to content

Commit

Permalink
Merge branch 'master' into rq/add-cancun-config
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Jun 4, 2024
2 parents a074f00 + a5a79c4 commit 6f535fa
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ jobs:
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmIOandFlowOperations/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmLogTest/ \
jsontests/res/ethtests/GeneralStateTests/VMTests/vmTests/
jsontests/res/ethtests/GeneralStateTests/VMTests/vmTests/ \
jsontests/res/ethtests/GeneralStateTests/stEIP150singleCodeGasPrices/eip2929.json
7 changes: 0 additions & 7 deletions interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn balance<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, T
handler: &mut H,
) -> Control<Tr> {
pop!(machine, address);
handler.mark_hot(address.into(), None);
push_u256!(machine, handler.balance(address.into()));

Control::Continue
Expand Down Expand Up @@ -130,7 +129,6 @@ pub fn extcodesize<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacken
handler: &mut H,
) -> Control<Tr> {
pop!(machine, address);
handler.mark_hot(address.into(), None);
let code_size = handler.code_size(address.into());
push_u256!(machine, code_size);

Expand All @@ -142,7 +140,6 @@ pub fn extcodehash<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacken
handler: &mut H,
) -> Control<Tr> {
pop!(machine, address);
handler.mark_hot(address.into(), None);
let code_hash = handler.code_hash(address.into());
push!(machine, code_hash);

Expand All @@ -155,8 +152,6 @@ pub fn extcodecopy<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacken
) -> Control<Tr> {
pop!(machine, address);
pop_u256!(machine, memory_offset, code_offset, len);

handler.mark_hot(address.into(), None);
try_or_fail!(machine.memory.resize_offset(memory_offset, len));

let code = handler.code(address.into());
Expand Down Expand Up @@ -266,7 +261,6 @@ pub fn sload<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr>
handler: &mut H,
) -> Control<Tr> {
pop!(machine, index);
handler.mark_hot(machine.state.as_ref().context.address, Some(index));
let value = handler.storage(machine.state.as_ref().context.address, index);
push!(machine, value);

Expand All @@ -278,7 +272,6 @@ pub fn sstore<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, Tr
handler: &mut H,
) -> Control<Tr> {
pop!(machine, index, value);
handler.mark_hot(machine.state.as_ref().context.address, Some(index));

match handler.set_storage(machine.state.as_ref().context.address, index, value) {
Ok(()) => Control::Continue,
Expand Down
8 changes: 8 additions & 0 deletions jsontests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ fn vm_tests() {
let tests_status = run::run_single(JSON_FILENAME, false).unwrap();
tests_status.print_total();
}

#[test]
fn sqt_eip_2930() {
const JSON_FILENAME: &str =
"res/ethtests/GeneralStateTests/stEIP150singleCodeGasPrices/eip2929.json";
let tests_status = run::run_single(JSON_FILENAME, false).unwrap();
tests_status.print_total();
}
4 changes: 2 additions & 2 deletions jsontests/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ pub fn run_test(
block_difficulty: test.env.current_difficulty,
block_randomness: Some(test.env.current_random),
block_gas_limit: test.env.current_gas_limit,
block_base_fee_per_gas: U256::zero(), // TODO: fill in this field.
chain_id: U256::zero(), // TODO: fill in this field.
block_base_fee_per_gas: test.transaction.gas_price,
chain_id: U256::zero(), // TODO: fill in this field.
};

let state = test
Expand Down
7 changes: 6 additions & 1 deletion jsontests/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ impl TestMulti {
transaction: TestTransaction {
data: self.transaction.data[post_state.indexes.data].0.clone(),
gas_limit: self.transaction.gas_limit[post_state.indexes.gas],
gas_price: self.transaction.gas_price.unwrap_or(U256::zero()),
gas_price: self
.transaction
.gas_price
.unwrap_or(self.env.current_base_fee),
gas_priority_fee: self.transaction.max_priority_fee_per_gas,
nonce: self.transaction.nonce,
secret_key: self.transaction.secret_key,
sender: self.transaction.sender,
Expand Down Expand Up @@ -236,6 +240,7 @@ pub struct TestTransaction {
pub data: Vec<u8>,
pub gas_limit: U256,
pub gas_price: U256,
pub gas_priority_fee: Option<U256>,
pub nonce: U256,
pub secret_key: H256,
pub sender: H160,
Expand Down
11 changes: 11 additions & 0 deletions src/standard/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct Config {
pub eip_1153_enabled: bool,
/// Enables MCOPY instruction. See [EIP-5656](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-5656.md)
pub eip_5656_enabled: bool,
/// Uses EIP-1559 (Base fee is burned when this flag is enabled) [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md)
pub eip_1559_enabled: bool,
}

impl Config {
Expand Down Expand Up @@ -156,6 +158,7 @@ impl Config {
has_push0: false,
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
}
}

Expand Down Expand Up @@ -211,6 +214,7 @@ impl Config {
has_push0: false,
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
}
}

Expand Down Expand Up @@ -252,6 +256,7 @@ impl Config {
max_initcode_size,
eip_1153_enabled,
eip_5656_enabled,
eip_1559_enabled,
} = inputs;

// See https://eips.ethereum.org/EIPS/eip-2929
Expand Down Expand Up @@ -316,6 +321,7 @@ impl Config {
has_push0,
eip_1153_enabled,
eip_5656_enabled,
eip_1559_enabled,
}
}
}
Expand All @@ -337,6 +343,7 @@ struct DerivedConfigInputs {
max_initcode_size: Option<usize>,
eip_1153_enabled: bool,
eip_5656_enabled: bool,
eip_1559_enabled: bool,
}

impl DerivedConfigInputs {
Expand All @@ -353,6 +360,7 @@ impl DerivedConfigInputs {
max_initcode_size: None,
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: false,
}
}

Expand All @@ -369,6 +377,7 @@ impl DerivedConfigInputs {
max_initcode_size: None,
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
}
}

Expand All @@ -385,6 +394,7 @@ impl DerivedConfigInputs {
max_initcode_size: None,
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
}
}

Expand All @@ -402,6 +412,7 @@ impl DerivedConfigInputs {
max_initcode_size: Some(0xC000),
eip_1153_enabled: false,
eip_5656_enabled: false,
eip_1559_enabled: true,
}
}

Expand Down
87 changes: 67 additions & 20 deletions src/standard/gasometer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
stack: &Stack,
is_static: bool,
config: &Config,
handler: &H,
handler: &mut H,
) -> Result<(GasCost, Option<MemoryCost>), ExitError> {
let gas_cost = match opcode {
Opcode::RETURN => GasCost::Zero,
Expand All @@ -307,40 +307,59 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(

Opcode::EXTCODESIZE => {
let target = stack.peek(0)?.into();
GasCost::ExtCodeSize {
target_is_cold: handler.is_cold(target, None),
}

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::ExtCodeSize { target_is_cold }
}
Opcode::BALANCE => {
let target = stack.peek(0)?.into();
GasCost::Balance {
target_is_cold: handler.is_cold(target, None),
}

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::Balance { target_is_cold }
}
Opcode::BLOCKHASH => GasCost::BlockHash,

Opcode::EXTCODEHASH if config.has_ext_code_hash => {
let target = stack.peek(0)?.into();
GasCost::ExtCodeHash {
target_is_cold: handler.is_cold(target, None),
}

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::ExtCodeHash { target_is_cold }
}
Opcode::EXTCODEHASH => GasCost::Invalid(opcode),

Opcode::CALLCODE => {
let target = stack.peek(1)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::CallCode {
value: U256::from_big_endian(&stack.peek(2)?[..]),
gas: U256::from_big_endian(&stack.peek(0)?[..]),
target_is_cold: handler.is_cold(target, None),
target_is_cold,
target_exists: { handler.exists(target) },
}
}
Opcode::STATICCALL => {
let target = stack.peek(1)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::StaticCall {
gas: U256::from_big_endian(&stack.peek(0)?[..]),
target_is_cold: handler.is_cold(target, None),
target_is_cold,
target_exists: { handler.exists(target) },
}
}
Expand All @@ -349,8 +368,13 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
},
Opcode::EXTCODECOPY => {
let target = stack.peek(0)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::ExtCodeCopy {
target_is_cold: handler.is_cold(target, None),
target_is_cold,
len: U256::from_big_endian(&stack.peek(3)?[..]),
}
}
Expand All @@ -365,17 +389,25 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
},
Opcode::SLOAD => {
let index = stack.peek(0)?;
GasCost::SLoad {
target_is_cold: handler.is_cold(address, Some(index)),
}

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(address, Some(index));
handler.mark_hot(address, Some(index));

GasCost::SLoad { target_is_cold }
}
Opcode::TLOAD if config.eip_1153_enabled => GasCost::TLoad,

Opcode::DELEGATECALL if config.has_delegate_call => {
let target = stack.peek(1)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::DelegateCall {
gas: U256::from_big_endian(&stack.peek(0)?[..]),
target_is_cold: handler.is_cold(target, None),
target_is_cold,
target_exists: { handler.exists(target) },
}
}
Expand All @@ -390,11 +422,16 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
Opcode::SSTORE if !is_static => {
let index = stack.peek(0)?;
let value = stack.peek(1)?;

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(address, Some(index));
handler.mark_hot(address, Some(index));

GasCost::SStore {
original: handler.original_storage(address, index),
current: handler.storage(address, index),
new: value,
target_is_cold: handler.is_cold(address, Some(index)),
target_is_cold,
}
}
Opcode::TSTORE if !is_static && config.eip_1153_enabled => GasCost::TStore,
Expand Down Expand Up @@ -424,9 +461,14 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
},
Opcode::SUICIDE if !is_static => {
let target = stack.peek(0)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::Suicide {
value: handler.balance(address),
target_is_cold: handler.is_cold(target, None),
target_is_cold,
target_exists: { handler.exists(target) },
already_removed: handler.deleted(address),
}
Expand All @@ -436,10 +478,15 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
|| (is_static && U256::from_big_endian(&stack.peek(2)?[..]) == U256::zero()) =>
{
let target = stack.peek(1)?.into();

// https://eips.ethereum.org/EIPS/eip-2929
let target_is_cold = handler.is_cold(target, None);
handler.mark_hot(target, None);

GasCost::Call {
value: U256::from_big_endian(&stack.peek(2)?[..]),
gas: U256::from_big_endian(&stack.peek(0)?[..]),
target_is_cold: handler.is_cold(target, None),
target_is_cold,
target_exists: { handler.exists(target) },
}
}
Expand Down
Loading

0 comments on commit 6f535fa

Please sign in to comment.