@@ -20,6 +20,7 @@ import (
20
20
"math/big"
21
21
22
22
"github.com/CortexFoundation/CortexTheseus/common"
23
+ "maps"
23
24
)
24
25
25
26
// journalEntry is a modification entry in the state change journal that can be
@@ -30,6 +31,9 @@ type journalEntry interface {
30
31
31
32
// dirtied returns the Cortex address modified by this journal entry.
32
33
dirtied () * common.Address
34
+
35
+ // copy returns a deep-copied journal entry.
36
+ copy () journalEntry
33
37
}
34
38
35
39
// journal contains the list of state modifications applied since the last state
@@ -84,22 +88,31 @@ func (j *journal) length() int {
84
88
return len (j .entries )
85
89
}
86
90
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
+
87
103
type (
88
104
// Changes to the account trie.
89
105
createObjectChange struct {
90
106
account * common.Address
91
107
}
92
- resetObjectChange struct {
93
- account * common.Address
94
- prev * stateObject
95
- prevdestruct bool
96
- prevAccount []byte
97
- prevStorage map [common.Hash ][]byte
98
108
99
- prevAccountOriginExist bool
100
- prevAccountOrigin []byte
101
- 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
102
114
}
115
+
103
116
selfDestructChange struct {
104
117
account * common.Address
105
118
prev bool // whether account had already self-destructed
@@ -147,6 +160,7 @@ type (
147
160
touchChange struct {
148
161
account * common.Address
149
162
}
163
+
150
164
// Changes to the access list
151
165
accessListAddAccountChange struct {
152
166
address * common.Address
@@ -156,6 +170,7 @@ type (
156
170
slot * common.Hash
157
171
}
158
172
173
+ // Changes to transient storage
159
174
transientStorageChange struct {
160
175
account * common.Address
161
176
key , prevalue common.Hash
@@ -164,34 +179,30 @@ type (
164
179
165
180
func (ch createObjectChange ) revert (s * StateDB ) {
166
181
delete (s .stateObjects , * ch .account )
167
- delete (s .stateObjectsDirty , * ch .account )
168
182
}
169
183
170
184
func (ch createObjectChange ) dirtied () * common.Address {
171
185
return ch .account
172
186
}
173
187
174
- func (ch resetObjectChange ) revert (s * StateDB ) {
175
- s .setStateObject (ch .prev )
176
- if ! ch .prevdestruct {
177
- delete (s .stateObjectsDestruct , ch .prev .address )
178
- }
179
- if ch .prevAccount != nil {
180
- s .accounts [ch .prev .addrHash ] = ch .prevAccount
181
- }
182
- if ch .prevStorage != nil {
183
- s .storages [ch .prev .addrHash ] = ch .prevStorage
184
- }
185
- if ch .prevAccountOriginExist {
186
- s .accountsOrigin [ch .prev .address ] = ch .prevAccountOrigin
187
- }
188
- if ch .prevStorageOrigin != nil {
189
- s .storagesOrigin [ch .prev .address ] = ch .prevStorageOrigin
188
+ func (ch createObjectChange ) copy () journalEntry {
189
+ return createObjectChange {
190
+ account : ch .account ,
190
191
}
191
192
}
192
193
193
- func (ch resetObjectChange ) dirtied () * common.Address {
194
- return ch .account
194
+ func (ch createContractChange ) revert (s * StateDB ) {
195
+ s .getStateObject (ch .account ).newContract = false
196
+ }
197
+
198
+ func (ch createContractChange ) dirtied () * common.Address {
199
+ return nil
200
+ }
201
+
202
+ func (ch createContractChange ) copy () journalEntry {
203
+ return createContractChange {
204
+ account : ch .account ,
205
+ }
195
206
}
196
207
197
208
func (ch selfDestructChange ) revert (s * StateDB ) {
@@ -208,6 +219,16 @@ func (ch selfDestructChange) dirtied() *common.Address {
208
219
return ch .account
209
220
}
210
221
222
+ func (ch selfDestructChange ) copy () journalEntry {
223
+ return selfDestructChange {
224
+ account : ch .account ,
225
+ prev : ch .prev ,
226
+ prevbalance : new (big.Int ).Set (ch .prevbalance ),
227
+ prevupload : new (big.Int ).Set (ch .prevupload ),
228
+ prevnum : new (big.Int ).Set (ch .prevnum ),
229
+ }
230
+ }
231
+
211
232
var ripemd = common .HexToAddress ("0000000000000000000000000000000000000003" )
212
233
213
234
func (ch touchChange ) revert (s * StateDB ) {
@@ -217,6 +238,12 @@ func (ch touchChange) dirtied() *common.Address {
217
238
return ch .account
218
239
}
219
240
241
+ func (ch touchChange ) copy () journalEntry {
242
+ return touchChange {
243
+ account : ch .account ,
244
+ }
245
+ }
246
+
220
247
func (ch balanceChange ) revert (s * StateDB ) {
221
248
s .getStateObject (* ch .account ).setBalance (ch .prev )
222
249
}
@@ -241,6 +268,13 @@ func (ch numChange) dirtied() *common.Address {
241
268
return ch .account
242
269
}
243
270
271
+ func (ch balanceChange ) copy () journalEntry {
272
+ return balanceChange {
273
+ account : ch .account ,
274
+ prev : new (big.Int ).Set (ch .prev ),
275
+ }
276
+ }
277
+
244
278
func (ch nonceChange ) revert (s * StateDB ) {
245
279
s .getStateObject (* ch .account ).setNonce (ch .prev )
246
280
}
@@ -249,6 +283,13 @@ func (ch nonceChange) dirtied() *common.Address {
249
283
return ch .account
250
284
}
251
285
286
+ func (ch nonceChange ) copy () journalEntry {
287
+ return nonceChange {
288
+ account : ch .account ,
289
+ prev : ch .prev ,
290
+ }
291
+ }
292
+
252
293
func (ch codeChange ) revert (s * StateDB ) {
253
294
s .getStateObject (* ch .account ).setCode (common .BytesToHash (ch .prevhash ), ch .prevcode )
254
295
}
@@ -257,6 +298,14 @@ func (ch codeChange) dirtied() *common.Address {
257
298
return ch .account
258
299
}
259
300
301
+ func (ch codeChange ) copy () journalEntry {
302
+ return codeChange {
303
+ account : ch .account ,
304
+ prevhash : common .CopyBytes (ch .prevhash ),
305
+ prevcode : common .CopyBytes (ch .prevcode ),
306
+ }
307
+ }
308
+
260
309
func (ch storageChange ) revert (s * StateDB ) {
261
310
s .getStateObject (* ch .account ).setState (ch .key , ch .prevalue )
262
311
}
@@ -265,8 +314,12 @@ func (ch storageChange) dirtied() *common.Address {
265
314
return ch .account
266
315
}
267
316
268
- func (ch refundChange ) revert (s * StateDB ) {
269
- s .refund = ch .prev
317
+ func (ch storageChange ) copy () journalEntry {
318
+ return storageChange {
319
+ account : ch .account ,
320
+ key : ch .key ,
321
+ prevalue : ch .prevalue ,
322
+ }
270
323
}
271
324
272
325
func (ch transientStorageChange ) revert (s * StateDB ) {
@@ -277,10 +330,28 @@ func (ch transientStorageChange) dirtied() *common.Address {
277
330
return nil
278
331
}
279
332
333
+ func (ch transientStorageChange ) copy () journalEntry {
334
+ return transientStorageChange {
335
+ account : ch .account ,
336
+ key : ch .key ,
337
+ prevalue : ch .prevalue ,
338
+ }
339
+ }
340
+
341
+ func (ch refundChange ) revert (s * StateDB ) {
342
+ s .refund = ch .prev
343
+ }
344
+
280
345
func (ch refundChange ) dirtied () * common.Address {
281
346
return nil
282
347
}
283
348
349
+ func (ch refundChange ) copy () journalEntry {
350
+ return refundChange {
351
+ prev : ch .prev ,
352
+ }
353
+ }
354
+
284
355
func (ch addLogChange ) revert (s * StateDB ) {
285
356
logs := s .logs [ch .txhash ]
286
357
if len (logs ) == 1 {
@@ -295,6 +366,12 @@ func (ch addLogChange) dirtied() *common.Address {
295
366
return nil
296
367
}
297
368
369
+ func (ch addLogChange ) copy () journalEntry {
370
+ return addLogChange {
371
+ txhash : ch .txhash ,
372
+ }
373
+ }
374
+
298
375
func (ch addPreimageChange ) revert (s * StateDB ) {
299
376
delete (s .preimages , ch .hash )
300
377
}
@@ -303,6 +380,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
303
380
return nil
304
381
}
305
382
383
+ func (ch addPreimageChange ) copy () journalEntry {
384
+ return addPreimageChange {
385
+ hash : ch .hash ,
386
+ }
387
+ }
388
+
306
389
func (ch accessListAddAccountChange ) revert (s * StateDB ) {
307
390
/*
308
391
One important invariant here, is that whenever a (addr, slot) is added, if the
@@ -320,10 +403,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
320
403
return nil
321
404
}
322
405
406
+ func (ch accessListAddAccountChange ) copy () journalEntry {
407
+ return accessListAddAccountChange {
408
+ address : ch .address ,
409
+ }
410
+ }
411
+
323
412
func (ch accessListAddSlotChange ) revert (s * StateDB ) {
324
413
s .accessList .DeleteSlot (* ch .address , * ch .slot )
325
414
}
326
415
327
416
func (ch accessListAddSlotChange ) dirtied () * common.Address {
328
417
return nil
329
418
}
419
+
420
+ func (ch accessListAddSlotChange ) copy () journalEntry {
421
+ return accessListAddSlotChange {
422
+ address : ch .address ,
423
+ slot : ch .slot ,
424
+ }
425
+ }
0 commit comments