Skip to content

Commit

Permalink
Merge eip2929 fix (rust-ethereum#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolsky committed Aug 26, 2024
1 parent 2f3eee7 commit 4ad76ec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
7 changes: 0 additions & 7 deletions interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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 @@ -128,7 +127,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 @@ -140,7 +138,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 @@ -153,8 +150,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 @@ -264,7 +259,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 @@ -276,7 +270,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
84 changes: 65 additions & 19 deletions src/standard/gasometer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,40 +303,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 @@ -345,8 +364,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 @@ -358,16 +382,24 @@ 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::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 @@ -383,11 +415,15 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
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::LOG0 if !is_static => GasCost::Log {
Expand Down Expand Up @@ -416,9 +452,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 @@ -428,10 +469,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

0 comments on commit 4ad76ec

Please sign in to comment.