1717package state
1818
1919import (
20+ "maps"
2021 "math/big"
2122
2223 "github.com/XinFinOrg/XDPoSChain/common"
@@ -31,6 +32,9 @@ type journalEntry interface {
3132
3233 // dirtied returns the Ethereum address modified by this journal entry.
3334 dirtied () * common.Address
35+
36+ // copy returns a deep-copied journal entry.
37+ copy () journalEntry
3438}
3539
3640// journal contains the list of state modifications applied since the last state
@@ -85,6 +89,18 @@ func (j *journal) length() int {
8589 return len (j .entries )
8690}
8791
92+ // copy returns a deep-copied journal.
93+ func (j * journal ) copy () * journal {
94+ entries := make ([]journalEntry , 0 , j .length ())
95+ for i := 0 ; i < j .length (); i ++ {
96+ entries = append (entries , j .entries [i ].copy ())
97+ }
98+ return & journal {
99+ entries : entries ,
100+ dirties : maps .Clone (j .dirties ),
101+ }
102+ }
103+
88104func (j * journal ) createContract (addr common.Address ) {
89105 j .append (createContractChange {account : addr })
90106}
@@ -107,11 +123,6 @@ type (
107123 createContractChange struct {
108124 account common.Address
109125 }
110- resetObjectChange struct {
111- account common.Address
112- prev * stateObject
113- prevdestruct bool
114- }
115126 selfDestructChange struct {
116127 account common.Address
117128 prev bool // whether account had already self-destructed
@@ -149,6 +160,7 @@ type (
149160 touchChange struct {
150161 account common.Address
151162 }
163+
152164 // Changes to the access list
153165 accessListAddAccountChange struct {
154166 address common.Address
@@ -158,6 +170,7 @@ type (
158170 slot common.Hash
159171 }
160172
173+ // Changes to transient storage
161174 transientStorageChange struct {
162175 account common.Address
163176 key , prevalue common.Hash
@@ -166,32 +179,32 @@ type (
166179
167180func (ch createObjectChange ) revert (s * StateDB ) {
168181 delete (s .stateObjects , ch .account )
169- delete (s .stateObjectsDirty , ch .account )
170182}
171183
172184func (ch createObjectChange ) dirtied () * common.Address {
173185 return & ch .account
174186}
175187
188+ func (ch createObjectChange ) copy () journalEntry {
189+ return createObjectChange {
190+ account : ch .account ,
191+ }
192+ }
193+
176194func (ch createContractChange ) revert (s * StateDB ) {
177- s .getStateObject (ch .account ).created = false
195+ s .getStateObject (ch .account ).newContract = false
178196}
179197
180198func (ch createContractChange ) dirtied () * common.Address {
181199 return nil
182200}
183201
184- func (ch resetObjectChange ) revert (s * StateDB ) {
185- s .setStateObject (ch .prev )
186- if ! ch .prevdestruct {
187- delete (s .stateObjectsDestruct , ch .prev .address )
202+ func (ch createContractChange ) copy () journalEntry {
203+ return createContractChange {
204+ account : ch .account ,
188205 }
189206}
190207
191- func (ch resetObjectChange ) dirtied () * common.Address {
192- return & ch .account
193- }
194-
195208func (ch selfDestructChange ) revert (s * StateDB ) {
196209 obj := s .getStateObject (ch .account )
197210 if obj != nil {
@@ -204,6 +217,14 @@ func (ch selfDestructChange) dirtied() *common.Address {
204217 return & ch .account
205218}
206219
220+ func (ch selfDestructChange ) copy () journalEntry {
221+ return selfDestructChange {
222+ account : ch .account ,
223+ prev : ch .prev ,
224+ prevbalance : new (big.Int ).Set (ch .prevbalance ),
225+ }
226+ }
227+
207228var ripemd = common .HexToAddress ("0000000000000000000000000000000000000003" )
208229
209230func (ch touchChange ) revert (s * StateDB ) {
@@ -213,6 +234,12 @@ func (ch touchChange) dirtied() *common.Address {
213234 return & ch .account
214235}
215236
237+ func (ch touchChange ) copy () journalEntry {
238+ return touchChange {
239+ account : ch .account ,
240+ }
241+ }
242+
216243func (ch balanceChange ) revert (s * StateDB ) {
217244 s .getStateObject (ch .account ).setBalance (ch .prev )
218245}
@@ -221,6 +248,13 @@ func (ch balanceChange) dirtied() *common.Address {
221248 return & ch .account
222249}
223250
251+ func (ch balanceChange ) copy () journalEntry {
252+ return balanceChange {
253+ account : ch .account ,
254+ prev : new (big.Int ).Set (ch .prev ),
255+ }
256+ }
257+
224258func (ch nonceChange ) revert (s * StateDB ) {
225259 s .getStateObject (ch .account ).setNonce (ch .prev )
226260}
@@ -229,6 +263,13 @@ func (ch nonceChange) dirtied() *common.Address {
229263 return & ch .account
230264}
231265
266+ func (ch nonceChange ) copy () journalEntry {
267+ return nonceChange {
268+ account : ch .account ,
269+ prev : ch .prev ,
270+ }
271+ }
272+
232273func (ch codeChange ) revert (s * StateDB ) {
233274 s .getStateObject (ch .account ).setCode (crypto .Keccak256Hash (ch .prevCode ), ch .prevCode )
234275}
@@ -237,6 +278,13 @@ func (ch codeChange) dirtied() *common.Address {
237278 return & ch .account
238279}
239280
281+ func (ch codeChange ) copy () journalEntry {
282+ return codeChange {
283+ account : ch .account ,
284+ prevCode : common .CopyBytes (ch .prevCode ),
285+ }
286+ }
287+
240288func (ch storageChange ) revert (s * StateDB ) {
241289 s .getStateObject (ch .account ).setState (ch .key , ch .prevalue )
242290}
@@ -245,6 +293,14 @@ func (ch storageChange) dirtied() *common.Address {
245293 return & ch .account
246294}
247295
296+ func (ch storageChange ) copy () journalEntry {
297+ return storageChange {
298+ account : ch .account ,
299+ key : ch .key ,
300+ prevalue : ch .prevalue ,
301+ }
302+ }
303+
248304func (ch transientStorageChange ) revert (s * StateDB ) {
249305 s .setTransientState (ch .account , ch .key , ch .prevalue )
250306}
@@ -253,6 +309,14 @@ func (ch transientStorageChange) dirtied() *common.Address {
253309 return nil
254310}
255311
312+ func (ch transientStorageChange ) copy () journalEntry {
313+ return transientStorageChange {
314+ account : ch .account ,
315+ key : ch .key ,
316+ prevalue : ch .prevalue ,
317+ }
318+ }
319+
256320func (ch refundChange ) revert (s * StateDB ) {
257321 s .refund = ch .prev
258322}
@@ -261,6 +325,12 @@ func (ch refundChange) dirtied() *common.Address {
261325 return nil
262326}
263327
328+ func (ch refundChange ) copy () journalEntry {
329+ return refundChange {
330+ prev : ch .prev ,
331+ }
332+ }
333+
264334func (ch addLogChange ) revert (s * StateDB ) {
265335 logs := s .logs [ch .txhash ]
266336 if len (logs ) == 1 {
@@ -275,6 +345,12 @@ func (ch addLogChange) dirtied() *common.Address {
275345 return nil
276346}
277347
348+ func (ch addLogChange ) copy () journalEntry {
349+ return addLogChange {
350+ txhash : ch .txhash ,
351+ }
352+ }
353+
278354func (ch addPreimageChange ) revert (s * StateDB ) {
279355 delete (s .preimages , ch .hash )
280356}
@@ -283,6 +359,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
283359 return nil
284360}
285361
362+ func (ch addPreimageChange ) copy () journalEntry {
363+ return addPreimageChange {
364+ hash : ch .hash ,
365+ }
366+ }
367+
286368func (ch accessListAddAccountChange ) revert (s * StateDB ) {
287369 /*
288370 One important invariant here, is that whenever a (addr, slot) is added, if the
@@ -300,10 +382,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
300382 return nil
301383}
302384
385+ func (ch accessListAddAccountChange ) copy () journalEntry {
386+ return accessListAddAccountChange {
387+ address : ch .address ,
388+ }
389+ }
390+
303391func (ch accessListAddSlotChange ) revert (s * StateDB ) {
304392 s .accessList .DeleteSlot (ch .address , ch .slot )
305393}
306394
307395func (ch accessListAddSlotChange ) dirtied () * common.Address {
308396 return nil
309397}
398+
399+ func (ch accessListAddSlotChange ) copy () journalEntry {
400+ return accessListAddSlotChange {
401+ address : ch .address ,
402+ slot : ch .slot ,
403+ }
404+ }
0 commit comments