Skip to content

Commit a5a79c4

Browse files
authored
Fix eip2929 implementation (#280)
* burn base fee after eip-1559 included in london fork * feat: add Cancun configuration * fix(eip-2929): some addresses were considered cold when they shouldn't * Revert "feat: add Cancun configuration" This reverts commit 654fb20. * Revert "burn base fee after eip-1559 included in london fork" This reverts commit 1e31869. * add eip-2929 test * remove duplicated test
1 parent 8168ca1 commit a5a79c4

File tree

4 files changed

+78
-28
lines changed

4 files changed

+78
-28
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ jobs:
5252
jsontests/res/ethtests/GeneralStateTests/VMTests/vmBitwiseLogicOperation/ \
5353
jsontests/res/ethtests/GeneralStateTests/VMTests/vmIOandFlowOperations/ \
5454
jsontests/res/ethtests/GeneralStateTests/VMTests/vmLogTest/ \
55-
jsontests/res/ethtests/GeneralStateTests/VMTests/vmTests/
55+
jsontests/res/ethtests/GeneralStateTests/VMTests/vmTests/ \
56+
jsontests/res/ethtests/GeneralStateTests/stEIP150singleCodeGasPrices/eip2929.json
57+

interpreter/src/eval/system.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub fn balance<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBackend, T
5050
handler: &mut H,
5151
) -> Control<Tr> {
5252
pop!(machine, address);
53-
handler.mark_hot(address.into(), None);
5453
push_u256!(machine, handler.balance(address.into()));
5554

5655
Control::Continue
@@ -130,7 +129,6 @@ pub fn extcodesize<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacken
130129
handler: &mut H,
131130
) -> Control<Tr> {
132131
pop!(machine, address);
133-
handler.mark_hot(address.into(), None);
134132
let code_size = handler.code_size(address.into());
135133
push_u256!(machine, code_size);
136134

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

@@ -155,8 +152,6 @@ pub fn extcodecopy<S: AsRef<RuntimeState>, H: RuntimeEnvironment + RuntimeBacken
155152
) -> Control<Tr> {
156153
pop!(machine, address);
157154
pop_u256!(machine, memory_offset, code_offset, len);
158-
159-
handler.mark_hot(address.into(), None);
160155
try_or_fail!(machine.memory.resize_offset(memory_offset, len));
161156

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

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

283276
match handler.set_storage(machine.state.as_ref().context.address, index, value) {
284277
Ok(()) => Control::Continue,

jsontests/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ fn vm_tests() {
7373
let tests_status = run::run_single(JSON_FILENAME, false).unwrap();
7474
tests_status.print_total();
7575
}
76+
77+
#[test]
78+
fn sqt_eip_2930() {
79+
const JSON_FILENAME: &str =
80+
"res/ethtests/GeneralStateTests/stEIP150singleCodeGasPrices/eip2929.json";
81+
let tests_status = run::run_single(JSON_FILENAME, false).unwrap();
82+
tests_status.print_total();
83+
}

src/standard/gasometer/mod.rs

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
283283
stack: &Stack,
284284
is_static: bool,
285285
config: &Config,
286-
handler: &H,
286+
handler: &mut H,
287287
) -> Result<(GasCost, Option<MemoryCost>), ExitError> {
288288
let gas_cost = match opcode {
289289
Opcode::RETURN => GasCost::Zero,
@@ -307,40 +307,59 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
307307

308308
Opcode::EXTCODESIZE => {
309309
let target = stack.peek(0)?.into();
310-
GasCost::ExtCodeSize {
311-
target_is_cold: handler.is_cold(target, None),
312-
}
310+
311+
// https://eips.ethereum.org/EIPS/eip-2929
312+
let target_is_cold = handler.is_cold(target, None);
313+
handler.mark_hot(target, None);
314+
315+
GasCost::ExtCodeSize { target_is_cold }
313316
}
314317
Opcode::BALANCE => {
315318
let target = stack.peek(0)?.into();
316-
GasCost::Balance {
317-
target_is_cold: handler.is_cold(target, None),
318-
}
319+
320+
// https://eips.ethereum.org/EIPS/eip-2929
321+
let target_is_cold = handler.is_cold(target, None);
322+
handler.mark_hot(target, None);
323+
324+
GasCost::Balance { target_is_cold }
319325
}
320326
Opcode::BLOCKHASH => GasCost::BlockHash,
321327

322328
Opcode::EXTCODEHASH if config.has_ext_code_hash => {
323329
let target = stack.peek(0)?.into();
324-
GasCost::ExtCodeHash {
325-
target_is_cold: handler.is_cold(target, None),
326-
}
330+
331+
// https://eips.ethereum.org/EIPS/eip-2929
332+
let target_is_cold = handler.is_cold(target, None);
333+
handler.mark_hot(target, None);
334+
335+
GasCost::ExtCodeHash { target_is_cold }
327336
}
328337
Opcode::EXTCODEHASH => GasCost::Invalid(opcode),
329338

330339
Opcode::CALLCODE => {
331340
let target = stack.peek(1)?.into();
341+
342+
// https://eips.ethereum.org/EIPS/eip-2929
343+
let target_is_cold = handler.is_cold(target, None);
344+
handler.mark_hot(target, None);
345+
332346
GasCost::CallCode {
333347
value: U256::from_big_endian(&stack.peek(2)?[..]),
334348
gas: U256::from_big_endian(&stack.peek(0)?[..]),
335-
target_is_cold: handler.is_cold(target, None),
349+
target_is_cold,
336350
target_exists: { handler.exists(target) },
337351
}
338352
}
339353
Opcode::STATICCALL => {
340354
let target = stack.peek(1)?.into();
355+
356+
// https://eips.ethereum.org/EIPS/eip-2929
357+
let target_is_cold = handler.is_cold(target, None);
358+
handler.mark_hot(target, None);
359+
341360
GasCost::StaticCall {
342361
gas: U256::from_big_endian(&stack.peek(0)?[..]),
343-
target_is_cold: handler.is_cold(target, None),
362+
target_is_cold,
344363
target_exists: { handler.exists(target) },
345364
}
346365
}
@@ -349,8 +368,13 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
349368
},
350369
Opcode::EXTCODECOPY => {
351370
let target = stack.peek(0)?.into();
371+
372+
// https://eips.ethereum.org/EIPS/eip-2929
373+
let target_is_cold = handler.is_cold(target, None);
374+
handler.mark_hot(target, None);
375+
352376
GasCost::ExtCodeCopy {
353-
target_is_cold: handler.is_cold(target, None),
377+
target_is_cold,
354378
len: U256::from_big_endian(&stack.peek(3)?[..]),
355379
}
356380
}
@@ -365,17 +389,25 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
365389
},
366390
Opcode::SLOAD => {
367391
let index = stack.peek(0)?;
368-
GasCost::SLoad {
369-
target_is_cold: handler.is_cold(address, Some(index)),
370-
}
392+
393+
// https://eips.ethereum.org/EIPS/eip-2929
394+
let target_is_cold = handler.is_cold(address, Some(index));
395+
handler.mark_hot(address, Some(index));
396+
397+
GasCost::SLoad { target_is_cold }
371398
}
372399
Opcode::TLOAD if config.eip_1153_enabled => GasCost::TLoad,
373400

374401
Opcode::DELEGATECALL if config.has_delegate_call => {
375402
let target = stack.peek(1)?.into();
403+
404+
// https://eips.ethereum.org/EIPS/eip-2929
405+
let target_is_cold = handler.is_cold(target, None);
406+
handler.mark_hot(target, None);
407+
376408
GasCost::DelegateCall {
377409
gas: U256::from_big_endian(&stack.peek(0)?[..]),
378-
target_is_cold: handler.is_cold(target, None),
410+
target_is_cold,
379411
target_exists: { handler.exists(target) },
380412
}
381413
}
@@ -390,11 +422,16 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
390422
Opcode::SSTORE if !is_static => {
391423
let index = stack.peek(0)?;
392424
let value = stack.peek(1)?;
425+
426+
// https://eips.ethereum.org/EIPS/eip-2929
427+
let target_is_cold = handler.is_cold(address, Some(index));
428+
handler.mark_hot(address, Some(index));
429+
393430
GasCost::SStore {
394431
original: handler.original_storage(address, index),
395432
current: handler.storage(address, index),
396433
new: value,
397-
target_is_cold: handler.is_cold(address, Some(index)),
434+
target_is_cold,
398435
}
399436
}
400437
Opcode::TSTORE if !is_static && config.eip_1153_enabled => GasCost::TStore,
@@ -424,9 +461,14 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
424461
},
425462
Opcode::SUICIDE if !is_static => {
426463
let target = stack.peek(0)?.into();
464+
465+
// https://eips.ethereum.org/EIPS/eip-2929
466+
let target_is_cold = handler.is_cold(target, None);
467+
handler.mark_hot(target, None);
468+
427469
GasCost::Suicide {
428470
value: handler.balance(address),
429-
target_is_cold: handler.is_cold(target, None),
471+
target_is_cold,
430472
target_exists: { handler.exists(target) },
431473
already_removed: handler.deleted(address),
432474
}
@@ -436,10 +478,15 @@ fn dynamic_opcode_cost<H: RuntimeBackend>(
436478
|| (is_static && U256::from_big_endian(&stack.peek(2)?[..]) == U256::zero()) =>
437479
{
438480
let target = stack.peek(1)?.into();
481+
482+
// https://eips.ethereum.org/EIPS/eip-2929
483+
let target_is_cold = handler.is_cold(target, None);
484+
handler.mark_hot(target, None);
485+
439486
GasCost::Call {
440487
value: U256::from_big_endian(&stack.peek(2)?[..]),
441488
gas: U256::from_big_endian(&stack.peek(0)?[..]),
442-
target_is_cold: handler.is_cold(target, None),
489+
target_is_cold,
443490
target_exists: { handler.exists(target) },
444491
}
445492
}

0 commit comments

Comments
 (0)