Skip to content

Commit 4386abd

Browse files
holimanrjl493456442
authored andcommitted
core/state: remove account reset operation v2 (ethereum#29520)
* core/state, tests: remove account reset operation * core/state, core/vm: implement createcontract journal event * core/state: make createcontract not emit dirtied account, unskip tests * core/state: add createcontract to journal fuzzing * core/state: fix journal * core/state: address comments * core/state: remove useless code --------- Co-authored-by: Gary Rong <[email protected]>
1 parent 0f8d7de commit 4386abd

File tree

9 files changed

+428
-360
lines changed

9 files changed

+428
-360
lines changed

core/state/journal.go

Lines changed: 123 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package state
1818

1919
import (
20+
"maps"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/holiman/uint256"
2224
)
@@ -29,6 +31,9 @@ type journalEntry interface {
2931

3032
// dirtied returns the Ethereum address modified by this journal entry.
3133
dirtied() *common.Address
34+
35+
// copy returns a deep-copied journal entry.
36+
copy() journalEntry
3237
}
3338

3439
// journal contains the list of state modifications applied since the last state
@@ -83,22 +88,31 @@ func (j *journal) length() int {
8388
return len(j.entries)
8489
}
8590

91+
// copy returns a deep-copied journal.
92+
func (j *journal) copy() *journal {
93+
entries := make([]journalEntry, 0, j.length())
94+
for i := 0; i < j.length(); i++ {
95+
entries = append(entries, j.entries[i].copy())
96+
}
97+
return &journal{
98+
entries: entries,
99+
dirties: maps.Clone(j.dirties),
100+
}
101+
}
102+
86103
type (
87104
// Changes to the account trie.
88105
createObjectChange struct {
89106
account *common.Address
90107
}
91-
resetObjectChange struct {
92-
account *common.Address
93-
prev *stateObject
94-
prevdestruct bool
95-
prevAccount []byte
96-
prevStorage map[common.Hash][]byte
97108

98-
prevAccountOriginExist bool
99-
prevAccountOrigin []byte
100-
prevStorageOrigin map[common.Hash][]byte
109+
// createContractChange represents an account becoming a contract-account.
110+
// This event happens prior to executing initcode. The journal-event simply
111+
// manages the created-flag, in order to allow same-tx destruction.
112+
createContractChange struct {
113+
account common.Address
101114
}
115+
102116
selfDestructChange struct {
103117
account *common.Address
104118
prev bool // whether account had already self-destructed
@@ -136,6 +150,7 @@ type (
136150
touchChange struct {
137151
account *common.Address
138152
}
153+
139154
// Changes to the access list
140155
accessListAddAccountChange struct {
141156
address *common.Address
@@ -145,6 +160,7 @@ type (
145160
slot *common.Hash
146161
}
147162

163+
// Changes to transient storage
148164
transientStorageChange struct {
149165
account *common.Address
150166
key, prevalue common.Hash
@@ -153,34 +169,30 @@ type (
153169

154170
func (ch createObjectChange) revert(s *StateDB) {
155171
delete(s.stateObjects, *ch.account)
156-
delete(s.stateObjectsDirty, *ch.account)
157172
}
158173

159174
func (ch createObjectChange) dirtied() *common.Address {
160175
return ch.account
161176
}
162177

163-
func (ch resetObjectChange) revert(s *StateDB) {
164-
s.setStateObject(ch.prev)
165-
if !ch.prevdestruct {
166-
delete(s.stateObjectsDestruct, ch.prev.address)
167-
}
168-
if ch.prevAccount != nil {
169-
s.accounts[ch.prev.addrHash] = ch.prevAccount
170-
}
171-
if ch.prevStorage != nil {
172-
s.storages[ch.prev.addrHash] = ch.prevStorage
173-
}
174-
if ch.prevAccountOriginExist {
175-
s.accountsOrigin[ch.prev.address] = ch.prevAccountOrigin
176-
}
177-
if ch.prevStorageOrigin != nil {
178-
s.storagesOrigin[ch.prev.address] = ch.prevStorageOrigin
178+
func (ch createObjectChange) copy() journalEntry {
179+
return createObjectChange{
180+
account: ch.account,
179181
}
180182
}
181183

182-
func (ch resetObjectChange) dirtied() *common.Address {
183-
return ch.account
184+
func (ch createContractChange) revert(s *StateDB) {
185+
s.getStateObject(ch.account).newContract = false
186+
}
187+
188+
func (ch createContractChange) dirtied() *common.Address {
189+
return nil
190+
}
191+
192+
func (ch createContractChange) copy() journalEntry {
193+
return createContractChange{
194+
account: ch.account,
195+
}
184196
}
185197

186198
func (ch selfDestructChange) revert(s *StateDB) {
@@ -195,6 +207,14 @@ func (ch selfDestructChange) dirtied() *common.Address {
195207
return ch.account
196208
}
197209

210+
func (ch selfDestructChange) copy() journalEntry {
211+
return selfDestructChange{
212+
account: ch.account,
213+
prev: ch.prev,
214+
prevbalance: new(uint256.Int).Set(ch.prevbalance),
215+
}
216+
}
217+
198218
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
199219

200220
func (ch touchChange) revert(s *StateDB) {
@@ -204,6 +224,12 @@ func (ch touchChange) dirtied() *common.Address {
204224
return ch.account
205225
}
206226

227+
func (ch touchChange) copy() journalEntry {
228+
return touchChange{
229+
account: ch.account,
230+
}
231+
}
232+
207233
func (ch balanceChange) revert(s *StateDB) {
208234
s.getStateObject(*ch.account).setBalance(ch.prev)
209235
}
@@ -212,6 +238,13 @@ func (ch balanceChange) dirtied() *common.Address {
212238
return ch.account
213239
}
214240

241+
func (ch balanceChange) copy() journalEntry {
242+
return balanceChange{
243+
account: ch.account,
244+
prev: new(uint256.Int).Set(ch.prev),
245+
}
246+
}
247+
215248
func (ch nonceChange) revert(s *StateDB) {
216249
s.getStateObject(*ch.account).setNonce(ch.prev)
217250
}
@@ -220,6 +253,13 @@ func (ch nonceChange) dirtied() *common.Address {
220253
return ch.account
221254
}
222255

256+
func (ch nonceChange) copy() journalEntry {
257+
return nonceChange{
258+
account: ch.account,
259+
prev: ch.prev,
260+
}
261+
}
262+
223263
func (ch codeChange) revert(s *StateDB) {
224264
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
225265
}
@@ -228,6 +268,14 @@ func (ch codeChange) dirtied() *common.Address {
228268
return ch.account
229269
}
230270

271+
func (ch codeChange) copy() journalEntry {
272+
return codeChange{
273+
account: ch.account,
274+
prevhash: common.CopyBytes(ch.prevhash),
275+
prevcode: common.CopyBytes(ch.prevcode),
276+
}
277+
}
278+
231279
func (ch storageChange) revert(s *StateDB) {
232280
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
233281
}
@@ -236,6 +284,14 @@ func (ch storageChange) dirtied() *common.Address {
236284
return ch.account
237285
}
238286

287+
func (ch storageChange) copy() journalEntry {
288+
return storageChange{
289+
account: ch.account,
290+
key: ch.key,
291+
prevalue: ch.prevalue,
292+
}
293+
}
294+
239295
func (ch transientStorageChange) revert(s *StateDB) {
240296
s.setTransientState(*ch.account, ch.key, ch.prevalue)
241297
}
@@ -244,6 +300,14 @@ func (ch transientStorageChange) dirtied() *common.Address {
244300
return nil
245301
}
246302

303+
func (ch transientStorageChange) copy() journalEntry {
304+
return transientStorageChange{
305+
account: ch.account,
306+
key: ch.key,
307+
prevalue: ch.prevalue,
308+
}
309+
}
310+
247311
func (ch refundChange) revert(s *StateDB) {
248312
s.refund = ch.prev
249313
}
@@ -252,6 +316,12 @@ func (ch refundChange) dirtied() *common.Address {
252316
return nil
253317
}
254318

319+
func (ch refundChange) copy() journalEntry {
320+
return refundChange{
321+
prev: ch.prev,
322+
}
323+
}
324+
255325
func (ch addLogChange) revert(s *StateDB) {
256326
logs := s.logs[ch.txhash]
257327
if len(logs) == 1 {
@@ -266,6 +336,12 @@ func (ch addLogChange) dirtied() *common.Address {
266336
return nil
267337
}
268338

339+
func (ch addLogChange) copy() journalEntry {
340+
return addLogChange{
341+
txhash: ch.txhash,
342+
}
343+
}
344+
269345
func (ch addPreimageChange) revert(s *StateDB) {
270346
delete(s.preimages, ch.hash)
271347
}
@@ -274,6 +350,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
274350
return nil
275351
}
276352

353+
func (ch addPreimageChange) copy() journalEntry {
354+
return addPreimageChange{
355+
hash: ch.hash,
356+
}
357+
}
358+
277359
func (ch accessListAddAccountChange) revert(s *StateDB) {
278360
/*
279361
One important invariant here, is that whenever a (addr, slot) is added, if the
@@ -291,10 +373,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
291373
return nil
292374
}
293375

376+
func (ch accessListAddAccountChange) copy() journalEntry {
377+
return accessListAddAccountChange{
378+
address: ch.address,
379+
}
380+
}
381+
294382
func (ch accessListAddSlotChange) revert(s *StateDB) {
295383
s.accessList.DeleteSlot(*ch.address, *ch.slot)
296384
}
297385

298386
func (ch accessListAddSlotChange) dirtied() *common.Address {
299387
return nil
300388
}
389+
390+
func (ch accessListAddSlotChange) copy() journalEntry {
391+
return accessListAddSlotChange{
392+
address: ch.address,
393+
slot: ch.slot,
394+
}
395+
}

0 commit comments

Comments
 (0)