Skip to content

Commit 163f345

Browse files
committed
core/state: refactor journalling by implementing journal API
1 parent cd0770e commit 163f345

File tree

3 files changed

+128
-58
lines changed

3 files changed

+128
-58
lines changed

core/state/journal.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,115 @@ func (j *journal) length() int {
8383
return len(j.entries)
8484
}
8585

86+
func (j *journal) JournalAccessListAddAccount(addr common.Address) {
87+
j.append(accessListAddAccountChange{&addr})
88+
}
89+
90+
func (j *journal) JournalAccessListAddSlot(addr common.Address, slot common.Hash) {
91+
j.append(accessListAddSlotChange{
92+
address: &addr,
93+
slot: &slot,
94+
})
95+
}
96+
97+
func (j *journal) JournalLog(txHash common.Hash) {
98+
j.append(addLogChange{txhash: txHash})
99+
}
100+
101+
func (j *journal) JournalAddPreimage(hash common.Hash) {
102+
j.append(addPreimageChange{hash: hash})
103+
}
104+
105+
func (j *journal) JournalRefund(prev uint64) {
106+
j.append(refundChange{prev: prev})
107+
}
108+
109+
func (j *journal) JournalCreate(addr common.Address) {
110+
j.append(createObjectChange{account: &addr})
111+
}
112+
113+
func (j *journal) JournalDestruct(addr common.Address, previouslyDestructed bool, prevBalance *uint256.Int) {
114+
j.append(selfDestructChange{
115+
account: &addr,
116+
prev: previouslyDestructed,
117+
prevbalance: prevBalance.Clone(),
118+
})
119+
}
120+
121+
func (j *journal) JournalSetState(addr common.Address, key, prev common.Hash) {
122+
j.append(storageChange{
123+
account: &addr,
124+
key: key,
125+
prevalue: prev,
126+
})
127+
}
128+
129+
func (j *journal) JournalSetTransientState(addr common.Address, key, prev common.Hash) {
130+
j.append(transientStorageChange{
131+
account: &addr,
132+
key: key,
133+
prevalue: prev,
134+
})
135+
}
136+
137+
func (j *journal) JournalRefundChange(previous uint64) {
138+
j.append(refundChange{prev: previous})
139+
}
140+
141+
func (j *journal) JournalBalanceChange(addr common.Address, previous *uint256.Int) {
142+
j.append(balanceChange{
143+
account: &addr,
144+
prev: previous.Clone(),
145+
})
146+
}
147+
148+
func (j *journal) JournalSetCode(address common.Address, prevcode, prevHash []byte) {
149+
j.append(codeChange{
150+
account: &address,
151+
prevhash: prevHash,
152+
prevcode: prevcode,
153+
})
154+
}
155+
156+
func (j *journal) JournalNonceChange(address common.Address, prev uint64) {
157+
j.append(nonceChange{
158+
account: &address,
159+
prev: prev,
160+
})
161+
}
162+
163+
func (j *journal) JournalTouch(address common.Address) {
164+
j.append(touchChange{
165+
account: &address,
166+
})
167+
if address == ripemd {
168+
// Explicitly put it in the dirty-cache, which is otherwise generated from
169+
// flattened journals.
170+
j.dirty(address)
171+
}
172+
}
173+
174+
func (j *journal) JournalReset(address common.Address,
175+
prev *stateObject,
176+
prevdestruct bool,
177+
prevAccount []byte,
178+
prevStorage map[common.Hash][]byte,
179+
prevAccountOriginExist bool,
180+
prevAccountOrigin []byte,
181+
prevStorageOrigin map[common.Hash][]byte) {
182+
183+
j.append(resetObjectChange{
184+
account: &address,
185+
prev: prev,
186+
prevdestruct: prevdestruct,
187+
prevAccount: prevAccount,
188+
prevStorage: prevStorage,
189+
prevAccountOriginExist: prevAccountOriginExist,
190+
prevAccountOrigin: prevAccountOrigin,
191+
prevStorageOrigin: prevStorageOrigin,
192+
})
193+
}
194+
86195
type (
87196
// Changes to the account trie.
88197
createObjectChange struct {

core/state/state_object.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ func (s *stateObject) markSelfdestructed() {
128128
}
129129

130130
func (s *stateObject) touch() {
131-
s.db.journal.append(touchChange{
132-
account: &s.address,
133-
})
134-
if s.address == ripemd {
135-
// Explicitly put it in the dirty-cache, which is otherwise generated from
136-
// flattened journals.
137-
s.db.journal.dirty(s.address)
138-
}
131+
s.db.journal.JournalTouch(s.address)
139132
}
140133

141134
// getTrie returns the associated storage trie. The trie will be opened
@@ -238,11 +231,7 @@ func (s *stateObject) SetState(key, value common.Hash) {
238231
return
239232
}
240233
// New value is different, update and journal the change
241-
s.db.journal.append(storageChange{
242-
account: &s.address,
243-
key: key,
244-
prevalue: prev,
245-
})
234+
s.db.journal.JournalSetState(s.address, key, prev)
246235
s.setState(key, value)
247236
}
248237

@@ -427,10 +416,7 @@ func (s *stateObject) SubBalance(amount *uint256.Int) {
427416
}
428417

429418
func (s *stateObject) SetBalance(amount *uint256.Int) {
430-
s.db.journal.append(balanceChange{
431-
account: &s.address,
432-
prev: new(uint256.Int).Set(s.data.Balance),
433-
})
419+
s.db.journal.JournalBalanceChange(s.address, s.data.Balance)
434420
s.setBalance(amount)
435421
}
436422

@@ -502,12 +488,7 @@ func (s *stateObject) CodeSize() int {
502488
}
503489

504490
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
505-
prevcode := s.Code()
506-
s.db.journal.append(codeChange{
507-
account: &s.address,
508-
prevhash: s.CodeHash(),
509-
prevcode: prevcode,
510-
})
491+
s.db.journal.JournalSetCode(s.address, s.Code(), s.CodeHash())
511492
s.setCode(codeHash, code)
512493
}
513494

@@ -518,10 +499,7 @@ func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
518499
}
519500

520501
func (s *stateObject) SetNonce(nonce uint64) {
521-
s.db.journal.append(nonceChange{
522-
account: &s.address,
523-
prev: s.data.Nonce,
524-
})
502+
s.db.journal.JournalNonceChange(s.address, s.data.Nonce)
525503
s.setNonce(nonce)
526504
}
527505

core/state/statedb.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (s *StateDB) Error() error {
207207
}
208208

209209
func (s *StateDB) AddLog(log *types.Log) {
210-
s.journal.append(addLogChange{txhash: s.thash})
210+
s.journal.JournalLog(s.thash)
211211

212212
log.TxHash = s.thash
213213
log.TxIndex = uint(s.txIndex)
@@ -238,7 +238,7 @@ func (s *StateDB) Logs() []*types.Log {
238238
// AddPreimage records a SHA3 preimage seen by the VM.
239239
func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
240240
if _, ok := s.preimages[hash]; !ok {
241-
s.journal.append(addPreimageChange{hash: hash})
241+
s.journal.JournalAddPreimage(hash)
242242
pi := make([]byte, len(preimage))
243243
copy(pi, preimage)
244244
s.preimages[hash] = pi
@@ -252,14 +252,14 @@ func (s *StateDB) Preimages() map[common.Hash][]byte {
252252

253253
// AddRefund adds gas to the refund counter
254254
func (s *StateDB) AddRefund(gas uint64) {
255-
s.journal.append(refundChange{prev: s.refund})
255+
s.journal.JournalRefund(s.refund)
256256
s.refund += gas
257257
}
258258

259259
// SubRefund removes gas from the refund counter.
260260
// This method will panic if the refund counter goes below zero
261261
func (s *StateDB) SubRefund(gas uint64) {
262-
s.journal.append(refundChange{prev: s.refund})
262+
s.journal.JournalRefundChange(s.refund)
263263
if gas > s.refund {
264264
panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
265265
}
@@ -447,11 +447,7 @@ func (s *StateDB) SelfDestruct(addr common.Address) {
447447
if stateObject == nil {
448448
return
449449
}
450-
s.journal.append(selfDestructChange{
451-
account: &addr,
452-
prev: stateObject.selfDestructed,
453-
prevbalance: new(uint256.Int).Set(stateObject.Balance()),
454-
})
450+
s.journal.JournalDestruct(addr, stateObject.selfDestructed, stateObject.Balance())
455451
stateObject.markSelfdestructed()
456452
stateObject.data.Balance = new(uint256.Int)
457453
}
@@ -475,11 +471,7 @@ func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash)
475471
if prev == value {
476472
return
477473
}
478-
s.journal.append(transientStorageChange{
479-
account: &addr,
480-
key: key,
481-
prevalue: prev,
482-
})
474+
s.journal.JournalSetTransientState(addr, key, prev)
483475
s.setTransientState(addr, key, value)
484476
}
485477

@@ -629,7 +621,7 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject)
629621
prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
630622
newobj = newObject(s, addr, nil)
631623
if prev == nil {
632-
s.journal.append(createObjectChange{account: &addr})
624+
s.journal.JournalCreate(addr)
633625
} else {
634626
// The original account should be marked as destructed and all cached
635627
// account and storage data should be cleared as well. Note, it must
@@ -643,16 +635,10 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject)
643635
// will be called for each transaction before byzantium fork which will always
644636
// cache the latest account/storage data.
645637
prevAccount, ok := s.accountsOrigin[prev.address]
646-
s.journal.append(resetObjectChange{
647-
account: &addr,
648-
prev: prev,
649-
prevdestruct: prevdestruct,
650-
prevAccount: s.accounts[prev.addrHash],
651-
prevStorage: s.storages[prev.addrHash],
652-
prevAccountOriginExist: ok,
653-
prevAccountOrigin: prevAccount,
654-
prevStorageOrigin: s.storagesOrigin[prev.address],
655-
})
638+
s.journal.JournalReset(addr, prev, prevdestruct,
639+
s.accounts[prev.addrHash], s.storages[prev.addrHash], ok,
640+
prevAccount, s.storagesOrigin[prev.address])
641+
656642
delete(s.accounts, prev.addrHash)
657643
delete(s.storages, prev.addrHash)
658644
delete(s.accountsOrigin, prev.address)
@@ -1342,7 +1328,7 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
13421328
// AddAddressToAccessList adds the given address to the access list
13431329
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
13441330
if s.accessList.AddAddress(addr) {
1345-
s.journal.append(accessListAddAccountChange{&addr})
1331+
s.journal.JournalAccessListAddAccount(addr)
13461332
}
13471333
}
13481334

@@ -1354,13 +1340,10 @@ func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
13541340
// scope of 'address' without having the 'address' become already added
13551341
// to the access list (via call-variant, create, etc).
13561342
// Better safe than sorry, though
1357-
s.journal.append(accessListAddAccountChange{&addr})
1343+
s.journal.JournalAccessListAddAccount(addr)
13581344
}
13591345
if slotMod {
1360-
s.journal.append(accessListAddSlotChange{
1361-
address: &addr,
1362-
slot: &slot,
1363-
})
1346+
s.journal.JournalAccessListAddSlot(addr, slot)
13641347
}
13651348
}
13661349

0 commit comments

Comments
 (0)