Skip to content

Commit d68fda5

Browse files
committed
core/state, tests: remove account reset operation
1 parent c170cc0 commit d68fda5

File tree

8 files changed

+355
-356
lines changed

8 files changed

+355
-356
lines changed

core/state/journal.go

Lines changed: 105 additions & 32 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,23 @@ 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
97-
98-
prevAccountOriginExist bool
99-
prevAccountOrigin []byte
100-
prevStorageOrigin map[common.Hash][]byte
101-
}
102108
selfDestructChange struct {
103109
account *common.Address
104110
prev bool // whether account had already self-destructed
@@ -136,6 +142,7 @@ type (
136142
touchChange struct {
137143
account *common.Address
138144
}
145+
139146
// Changes to the access list
140147
accessListAddAccountChange struct {
141148
address *common.Address
@@ -145,6 +152,7 @@ type (
145152
slot *common.Hash
146153
}
147154

155+
// Changes to transient storage
148156
transientStorageChange struct {
149157
account *common.Address
150158
key, prevalue common.Hash
@@ -153,36 +161,18 @@ type (
153161

154162
func (ch createObjectChange) revert(s *StateDB) {
155163
delete(s.stateObjects, *ch.account)
156-
delete(s.stateObjectsDirty, *ch.account)
157164
}
158165

159166
func (ch createObjectChange) dirtied() *common.Address {
160167
return ch.account
161168
}
162169

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
170+
func (ch createObjectChange) copy() journalEntry {
171+
return createObjectChange{
172+
account: ch.account,
179173
}
180174
}
181175

182-
func (ch resetObjectChange) dirtied() *common.Address {
183-
return ch.account
184-
}
185-
186176
func (ch selfDestructChange) revert(s *StateDB) {
187177
obj := s.getStateObject(*ch.account)
188178
if obj != nil {
@@ -195,6 +185,14 @@ func (ch selfDestructChange) dirtied() *common.Address {
195185
return ch.account
196186
}
197187

188+
func (ch selfDestructChange) copy() journalEntry {
189+
return selfDestructChange{
190+
account: ch.account,
191+
prev: ch.prev,
192+
prevbalance: new(uint256.Int).Set(ch.prevbalance),
193+
}
194+
}
195+
198196
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
199197

200198
func (ch touchChange) revert(s *StateDB) {
@@ -204,6 +202,12 @@ func (ch touchChange) dirtied() *common.Address {
204202
return ch.account
205203
}
206204

205+
func (ch touchChange) copy() journalEntry {
206+
return touchChange{
207+
account: ch.account,
208+
}
209+
}
210+
207211
func (ch balanceChange) revert(s *StateDB) {
208212
s.getStateObject(*ch.account).setBalance(ch.prev)
209213
}
@@ -212,6 +216,13 @@ func (ch balanceChange) dirtied() *common.Address {
212216
return ch.account
213217
}
214218

219+
func (ch balanceChange) copy() journalEntry {
220+
return balanceChange{
221+
account: ch.account,
222+
prev: new(uint256.Int).Set(ch.prev),
223+
}
224+
}
225+
215226
func (ch nonceChange) revert(s *StateDB) {
216227
s.getStateObject(*ch.account).setNonce(ch.prev)
217228
}
@@ -220,6 +231,13 @@ func (ch nonceChange) dirtied() *common.Address {
220231
return ch.account
221232
}
222233

234+
func (ch nonceChange) copy() journalEntry {
235+
return nonceChange{
236+
account: ch.account,
237+
prev: ch.prev,
238+
}
239+
}
240+
223241
func (ch codeChange) revert(s *StateDB) {
224242
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
225243
}
@@ -228,6 +246,14 @@ func (ch codeChange) dirtied() *common.Address {
228246
return ch.account
229247
}
230248

249+
func (ch codeChange) copy() journalEntry {
250+
return codeChange{
251+
account: ch.account,
252+
prevhash: common.CopyBytes(ch.prevhash),
253+
prevcode: common.CopyBytes(ch.prevcode),
254+
}
255+
}
256+
231257
func (ch storageChange) revert(s *StateDB) {
232258
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
233259
}
@@ -236,6 +262,14 @@ func (ch storageChange) dirtied() *common.Address {
236262
return ch.account
237263
}
238264

265+
func (ch storageChange) copy() journalEntry {
266+
return storageChange{
267+
account: ch.account,
268+
key: ch.key,
269+
prevalue: ch.prevalue,
270+
}
271+
}
272+
239273
func (ch transientStorageChange) revert(s *StateDB) {
240274
s.setTransientState(*ch.account, ch.key, ch.prevalue)
241275
}
@@ -244,6 +278,14 @@ func (ch transientStorageChange) dirtied() *common.Address {
244278
return nil
245279
}
246280

281+
func (ch transientStorageChange) copy() journalEntry {
282+
return transientStorageChange{
283+
account: ch.account,
284+
key: ch.key,
285+
prevalue: ch.prevalue,
286+
}
287+
}
288+
247289
func (ch refundChange) revert(s *StateDB) {
248290
s.refund = ch.prev
249291
}
@@ -252,6 +294,12 @@ func (ch refundChange) dirtied() *common.Address {
252294
return nil
253295
}
254296

297+
func (ch refundChange) copy() journalEntry {
298+
return refundChange{
299+
prev: ch.prev,
300+
}
301+
}
302+
255303
func (ch addLogChange) revert(s *StateDB) {
256304
logs := s.logs[ch.txhash]
257305
if len(logs) == 1 {
@@ -266,6 +314,12 @@ func (ch addLogChange) dirtied() *common.Address {
266314
return nil
267315
}
268316

317+
func (ch addLogChange) copy() journalEntry {
318+
return addLogChange{
319+
txhash: ch.txhash,
320+
}
321+
}
322+
269323
func (ch addPreimageChange) revert(s *StateDB) {
270324
delete(s.preimages, ch.hash)
271325
}
@@ -274,6 +328,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
274328
return nil
275329
}
276330

331+
func (ch addPreimageChange) copy() journalEntry {
332+
return addPreimageChange{
333+
hash: ch.hash,
334+
}
335+
}
336+
277337
func (ch accessListAddAccountChange) revert(s *StateDB) {
278338
/*
279339
One important invariant here, is that whenever a (addr, slot) is added, if the
@@ -291,10 +351,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
291351
return nil
292352
}
293353

354+
func (ch accessListAddAccountChange) copy() journalEntry {
355+
return accessListAddAccountChange{
356+
address: ch.address,
357+
}
358+
}
359+
294360
func (ch accessListAddSlotChange) revert(s *StateDB) {
295361
s.accessList.DeleteSlot(*ch.address, *ch.slot)
296362
}
297363

298364
func (ch accessListAddSlotChange) dirtied() *common.Address {
299365
return nil
300366
}
367+
368+
func (ch accessListAddSlotChange) copy() journalEntry {
369+
return accessListAddSlotChange{
370+
address: ch.address,
371+
slot: ch.slot,
372+
}
373+
}

core/state/state_object.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,8 @@ import (
3232
"github.com/holiman/uint256"
3333
)
3434

35-
type Code []byte
36-
37-
func (c Code) String() string {
38-
return string(c) //strings.Join(Disassemble(c), " ")
39-
}
40-
4135
type Storage map[common.Hash]common.Hash
4236

43-
func (s Storage) String() (str string) {
44-
for key, value := range s {
45-
str += fmt.Sprintf("%X : %X\n", key, value)
46-
}
47-
return
48-
}
49-
5037
func (s Storage) Copy() Storage {
5138
return maps.Clone(s)
5239
}
@@ -65,8 +52,8 @@ type stateObject struct {
6552
data types.StateAccount // Account data with all mutations applied in the scope of block
6653

6754
// Write caches.
68-
trie Trie // storage trie, which becomes non-nil on first access
69-
code Code // contract bytecode, which gets set when code is loaded
55+
trie Trie // storage trie, which becomes non-nil on first access
56+
code []byte // contract bytecode, which gets set when code is loaded
7057

7158
originStorage Storage // Storage cache of original entries to dedup rewrites
7259
pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block
@@ -75,15 +62,10 @@ type stateObject struct {
7562
// Cache flags.
7663
dirtyCode bool // true if the code was updated
7764

78-
// Flag whether the account was marked as self-destructed. The self-destructed account
79-
// is still accessible in the scope of same transaction.
65+
// Flag whether the account was marked as self-destructed. The self-destructed
66+
// account is still accessible in the scope of same transaction.
8067
selfDestructed bool
8168

82-
// Flag whether the account was marked as deleted. A self-destructed account
83-
// or an account that is considered as empty will be marked as deleted at
84-
// the end of transaction and no longer accessible anymore.
85-
deleted bool
86-
8769
// Flag whether the object was created in the current transaction
8870
created bool
8971
}
@@ -463,12 +445,12 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
463445
obj.trie = db.db.CopyTrie(s.trie)
464446
}
465447
obj.code = s.code
466-
obj.dirtyStorage = s.dirtyStorage.Copy()
467448
obj.originStorage = s.originStorage.Copy()
468449
obj.pendingStorage = s.pendingStorage.Copy()
469-
obj.selfDestructed = s.selfDestructed
450+
obj.dirtyStorage = s.dirtyStorage.Copy()
470451
obj.dirtyCode = s.dirtyCode
471-
obj.deleted = s.deleted
452+
obj.selfDestructed = s.selfDestructed
453+
obj.created = s.created
472454
return obj
473455
}
474456

@@ -483,7 +465,7 @@ func (s *stateObject) Address() common.Address {
483465

484466
// Code returns the contract code associated with this object, if any.
485467
func (s *stateObject) Code() []byte {
486-
if s.code != nil {
468+
if len(s.code) != 0 {
487469
return s.code
488470
}
489471
if bytes.Equal(s.CodeHash(), types.EmptyCodeHash.Bytes()) {
@@ -501,7 +483,7 @@ func (s *stateObject) Code() []byte {
501483
// or zero if none. This method is an almost mirror of Code, but uses a cache
502484
// inside the database to avoid loading codes seen recently.
503485
func (s *stateObject) CodeSize() int {
504-
if s.code != nil {
486+
if len(s.code) != 0 {
505487
return len(s.code)
506488
}
507489
if bytes.Equal(s.CodeHash(), types.EmptyCodeHash.Bytes()) {

0 commit comments

Comments
 (0)