Skip to content

Commit 6814140

Browse files
committed
unify coinbase state clearing of gst, evmstate, and t8n
it is troublesome if we have to fix it in three places in case of a bug, it also reduce code duplication.
1 parent e2601e7 commit 6814140

File tree

6 files changed

+58
-37
lines changed

6 files changed

+58
-37
lines changed

nimbus/common/common.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ func toEVMFork*(com: CommonRef, number: BlockNumber): EVMFork =
295295
let fork = com.toHardFork(number)
296296
ToEVMFork[fork]
297297

298+
func toEVMFork*(com: CommonRef): EVMFork =
299+
ToEVMFork[com.currentFork]
300+
298301
func isLondon*(com: CommonRef, number: BlockNumber): bool =
299302
# TODO: Fixme, use only London comparator
300303
com.toHardFork(number) >= London

tests/test_blockchain_json.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus, debugMode = fal
390390
# state root to test against 'postState'
391391
let stateDB = AccountsCache.init(memDB, header.stateRoot, pruneTrie)
392392
verifyStateDB(fixture["postState"], ReadOnlyStateDB(stateDB))
393+
394+
success = lastBlockHash == tester.lastBlockHash
393395
except ValidationError as E:
394396
echo fixtureName, " ERROR: ", E.msg
395397
success = false

tests/test_generalstate_json.nim

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import
1616
../nimbus/utils/utils,
1717
../tools/common/helpers as chp,
1818
../tools/evmstate/helpers,
19+
../tools/common/state_clearing,
1920
chronicles,
2021
eth/rlp,
2122
eth/trie/trie_defs,
@@ -124,28 +125,8 @@ proc testFixtureIndexes(tester: Tester, testStatusIMPL: var TestStatus) =
124125
if rc.isOk:
125126
gasUsed = rc.value
126127

127-
# This is necessary due to the manner in which the state tests are
128-
# generated. State tests are generated from the BlockChainTest tests
129-
# in which these transactions are included in the larger context of a
130-
# block and thus, the mechanisms which would touch/create/clear the
131-
# coinbase account based on the mining reward are present during test
132-
# generation, but not part of the execution, thus we must artificially
133-
# create the account in VMs prior to the state clearing rules,
134-
# as well as conditionally cleaning up the coinbase account when left
135-
# empty in VMs after the state clearing rules came into effect.
136128
let miner = tester.header.coinbase
137-
if miner in vmState.selfDestructs:
138-
vmState.mutateStateDB:
139-
db.addBalance(miner, 0.u256)
140-
if fork >= FkSpurious:
141-
if db.isEmptyAccount(miner):
142-
db.deleteAccount(miner)
143-
144-
# this is an important step when using accounts_cache
145-
# it will affect the account storage's location
146-
# during the next call to `getComittedStorage`
147-
# and the result of rootHash
148-
db.persist()
129+
coinbaseStateClearing(vmState, miner, fork)
149130

150131
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus,
151132
trace = false, debugMode = false) =

tools/common/state_clearing.nim

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Nimbus
2+
# Copyright (c) 2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
5+
# http://www.apache.org/licenses/LICENSE-2.0)
6+
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
7+
# http://opensource.org/licenses/MIT)
8+
# at your option. This file may not be copied, modified, or distributed except
9+
# according to those terms.
10+
11+
import
12+
../../nimbus/common/common,
13+
../../nimbus/[vm_state, vm_types],
14+
../../nimbus/db/accounts_cache
15+
16+
proc coinbaseStateClearing*(vmState: BaseVMState,
17+
miner: EthAddress,
18+
fork: EVMFork,
19+
touched = true) =
20+
# This is necessary due to the manner in which the state tests are
21+
# generated. State tests are generated from the BlockChainTest tests
22+
# in which these transactions are included in the larger context of a
23+
# block and thus, the mechanisms which would touch/create/clear the
24+
# coinbase account based on the mining reward are present during test
25+
# generation, but not part of the execution, thus we must artificially
26+
# create the account in VMs prior to the state clearing rules,
27+
# as well as conditionally cleaning up the coinbase account when left
28+
# empty in VMs after the state clearing rules came into effect.
29+
30+
vmState.mutateStateDB:
31+
if touched:
32+
db.addBalance(miner, 0.u256)
33+
if fork >= FkSpurious:
34+
# EIP158/161 state clearing
35+
if db.accountExists(miner) and db.isEmptyAccount(miner):
36+
db.deleteAccount(miner)
37+
38+
# db.persist is an important step when using accounts_cache
39+
# it will affect the account storage's location
40+
# during the next call to `getComittedStorage`
41+
# and the result of rootHash
42+
43+
# do not clear cache, we need the cache when constructing
44+
# post state
45+
db.persist(clearCache = false)

tools/evmstate/evmstate.nim

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import
2121
../../nimbus/core/executor,
2222
../../nimbus/common/common,
2323
../common/helpers as chp,
24-
"."/[config, helpers]
24+
"."/[config, helpers],
25+
../common/state_clearing
2526

2627
type
2728
StateContext = object
@@ -235,13 +236,7 @@ proc runExecution(ctx: var StateContext, conf: StateConf, pre: JsonNode): StateR
235236
gasUsed = rc.value
236237

237238
let miner = ctx.header.coinbase
238-
if miner in vmState.selfDestructs:
239-
vmState.mutateStateDB:
240-
db.addBalance(miner, 0.u256)
241-
if fork >= FkSpurious:
242-
if db.isEmptyAccount(miner):
243-
db.deleteAccount(miner)
244-
db.persist(clearCache = false)
239+
coinbaseStateClearing(vmState, miner, fork)
245240

246241
proc toTracerFlags(conf: Stateconf): set[TracerFlags] =
247242
result = {

tools/t8n/transition.nim

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import
1313
eth/[rlp, trie, eip1559],
1414
stint, chronicles, stew/results,
1515
"."/[config, types, helpers],
16+
../common/state_clearing,
1617
../../nimbus/[vm_types, vm_state, transaction],
1718
../../nimbus/common/common,
1819
../../nimbus/db/accounts_cache,
@@ -222,14 +223,8 @@ proc exec(ctx: var TransContext,
222223
db.persist(clearCache = false)
223224
224225
let miner = ctx.env.currentCoinbase
225-
if vmState.com.forkGTE(Spurious):
226-
# EIP158/161 state clearing
227-
vmState.mutateStateDB:
228-
if db.accountExists(miner) and db.isEmptyAccount(miner):
229-
db.deleteAccount(miner)
230-
# do not clear cache, we need the cache when constructing
231-
# post state
232-
db.persist(clearCache = false)
226+
let fork = vmState.com.toEVMFork
227+
coinbaseStateClearing(vmState, miner, fork, stateReward.isSome())
233228
234229
let stateDB = vmState.stateDB
235230
stateDB.postState(result.alloc)

0 commit comments

Comments
 (0)