Skip to content

Commit 8d5ceb6

Browse files
committed
core/state: refactor journalling by implementing journal API
1 parent df645e7 commit 8d5ceb6

File tree

3 files changed

+105
-51
lines changed

3 files changed

+105
-51
lines changed

core/state/journal.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,91 @@ func (j *journal) copy() *journal {
100100
}
101101
}
102102

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

core/state/state_object.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,7 @@ func (s *stateObject) markSelfdestructed() {
114114
}
115115

116116
func (s *stateObject) touch() {
117-
s.db.journal.append(touchChange{
118-
account: &s.address,
119-
})
120-
if s.address == ripemd {
121-
// Explicitly put it in the dirty-cache, which is otherwise generated from
122-
// flattened journals.
123-
s.db.journal.dirty(s.address)
124-
}
117+
s.db.journal.JournalTouch(s.address)
125118
}
126119

127120
// getTrie returns the associated storage trie. The trie will be opened if it's
@@ -251,16 +244,11 @@ func (s *stateObject) SetState(key, value common.Hash) {
251244
return
252245
}
253246
// New value is different, update and journal the change
254-
s.db.journal.append(storageChange{
255-
account: &s.address,
256-
key: key,
257-
prevvalue: prev,
258-
origvalue: origin,
259-
})
247+
s.db.journal.JournalSetState(s.address, key, prev, origin)
248+
s.setState(key, value, origin)
260249
if s.db.logger != nil && s.db.logger.OnStorageChange != nil {
261250
s.db.logger.OnStorageChange(s.address, key, prev, value)
262251
}
263-
s.setState(key, value, origin)
264252
}
265253

266254
// setState updates a value in account dirty storage. The dirtiness will be
@@ -510,10 +498,7 @@ func (s *stateObject) SubBalance(amount *uint256.Int, reason tracing.BalanceChan
510498
}
511499

512500
func (s *stateObject) SetBalance(amount *uint256.Int, reason tracing.BalanceChangeReason) {
513-
s.db.journal.append(balanceChange{
514-
account: &s.address,
515-
prev: new(uint256.Int).Set(s.data.Balance),
516-
})
501+
s.db.journal.JournalBalanceChange(s.address, s.data.Balance)
517502
if s.db.logger != nil && s.db.logger.OnBalanceChange != nil {
518503
s.db.logger.OnBalanceChange(s.address, s.Balance().ToBig(), amount.ToBig(), reason)
519504
}
@@ -589,14 +574,10 @@ func (s *stateObject) CodeSize() int {
589574
}
590575

591576
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
592-
prevcode := s.Code()
593-
s.db.journal.append(codeChange{
594-
account: &s.address,
595-
prevhash: s.CodeHash(),
596-
prevcode: prevcode,
597-
})
577+
s.db.journal.JournalSetCode(s.address, s.Code(), s.CodeHash())
598578
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
599-
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), prevcode, codeHash, code)
579+
// TODO remove prevcode from this callback
580+
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)
600581
}
601582
s.setCode(codeHash, code)
602583
}
@@ -608,10 +589,7 @@ func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
608589
}
609590

610591
func (s *stateObject) SetNonce(nonce uint64) {
611-
s.db.journal.append(nonceChange{
612-
account: &s.address,
613-
prev: s.data.Nonce,
614-
})
592+
s.db.journal.JournalNonceChange(s.address, s.data.Nonce)
615593
if s.db.logger != nil && s.db.logger.OnNonceChange != nil {
616594
s.db.logger.OnNonceChange(s.address, s.data.Nonce, nonce)
617595
}

core/state/statedb.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (s *StateDB) Error() error {
256256
}
257257

258258
func (s *StateDB) AddLog(log *types.Log) {
259-
s.journal.append(addLogChange{txhash: s.thash})
259+
s.journal.JournalLog(s.thash)
260260

261261
log.TxHash = s.thash
262262
log.TxIndex = uint(s.txIndex)
@@ -290,7 +290,7 @@ func (s *StateDB) Logs() []*types.Log {
290290
// AddPreimage records a SHA3 preimage seen by the VM.
291291
func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
292292
if _, ok := s.preimages[hash]; !ok {
293-
s.journal.append(addPreimageChange{hash: hash})
293+
s.journal.JournalAddPreimage(hash)
294294
s.preimages[hash] = slices.Clone(preimage)
295295
}
296296
}
@@ -302,14 +302,14 @@ func (s *StateDB) Preimages() map[common.Hash][]byte {
302302

303303
// AddRefund adds gas to the refund counter
304304
func (s *StateDB) AddRefund(gas uint64) {
305-
s.journal.append(refundChange{prev: s.refund})
305+
s.journal.JournalRefundChange(s.refund)
306306
s.refund += gas
307307
}
308308

309309
// SubRefund removes gas from the refund counter.
310310
// This method will panic if the refund counter goes below zero
311311
func (s *StateDB) SubRefund(gas uint64) {
312-
s.journal.append(refundChange{prev: s.refund})
312+
s.journal.JournalRefundChange(s.refund)
313313
if gas > s.refund {
314314
panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
315315
}
@@ -510,14 +510,12 @@ func (s *StateDB) SelfDestruct(addr common.Address) {
510510
prev = new(uint256.Int).Set(stateObject.Balance())
511511
n = new(uint256.Int)
512512
)
513-
s.journal.append(selfDestructChange{
514-
account: &addr,
515-
prev: stateObject.selfDestructed,
516-
prevbalance: prev,
517-
})
513+
s.journal.JournalDestruct(addr, stateObject.selfDestructed, prev)
514+
518515
if s.logger != nil && s.logger.OnBalanceChange != nil && prev.Sign() > 0 {
519516
s.logger.OnBalanceChange(addr, prev.ToBig(), n.ToBig(), tracing.BalanceDecreaseSelfdestruct)
520517
}
518+
521519
stateObject.markSelfdestructed()
522520
stateObject.data.Balance = n
523521
}
@@ -540,11 +538,7 @@ func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash)
540538
if prev == value {
541539
return
542540
}
543-
s.journal.append(transientStorageChange{
544-
account: &addr,
545-
key: key,
546-
prevalue: prev,
547-
})
541+
s.journal.JournalSetTransientState(addr, key, prev)
548542
s.setTransientState(addr, key, value)
549543
}
550544

@@ -663,7 +657,7 @@ func (s *StateDB) getOrNewStateObject(addr common.Address) *stateObject {
663657
// existing account with the given address, otherwise it will be silently overwritten.
664658
func (s *StateDB) createObject(addr common.Address) *stateObject {
665659
obj := newObject(s, addr, nil)
666-
s.journal.append(createObjectChange{account: &addr})
660+
s.journal.JournalCreate(addr)
667661
s.setStateObject(obj)
668662
return obj
669663
}
@@ -1419,7 +1413,7 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
14191413
// AddAddressToAccessList adds the given address to the access list
14201414
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
14211415
if s.accessList.AddAddress(addr) {
1422-
s.journal.append(accessListAddAccountChange{&addr})
1416+
s.journal.JournalAccessListAddAccount(addr)
14231417
}
14241418
}
14251419

@@ -1431,13 +1425,10 @@ func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
14311425
// scope of 'address' without having the 'address' become already added
14321426
// to the access list (via call-variant, create, etc).
14331427
// Better safe than sorry, though
1434-
s.journal.append(accessListAddAccountChange{&addr})
1428+
s.journal.JournalAccessListAddAccount(addr)
14351429
}
14361430
if slotMod {
1437-
s.journal.append(accessListAddSlotChange{
1438-
address: &addr,
1439-
slot: &slot,
1440-
})
1431+
s.journal.JournalAccessListAddSlot(addr, slot)
14411432
}
14421433
}
14431434

0 commit comments

Comments
 (0)