Skip to content

Commit 958ed4f

Browse files
holimankaralabe
authored andcommitted
core/state: rework dirty handling to avoid quadratic overhead
1 parent 1a8894b commit 958ed4f

6 files changed

Lines changed: 112 additions & 79 deletions

File tree

‎core/state/dump.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (self *StateDB) RawDump() Dump {
5353
panic(err)
5454
}
5555

56-
obj := newObject(nil, common.BytesToAddress(addr), data, nil)
56+
obj := newObject(nil, common.BytesToAddress(addr), data)
5757
account := DumpAccount{
5858
Balance: data.Balance.String(),
5959
Nonce: data.Nonce,

‎core/state/journal.go‎

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,40 @@ import (
2424

2525
type journalEntry interface {
2626
undo(*StateDB)
27+
getAccount() *common.Address
2728
}
2829

29-
type journal []journalEntry
30+
type journal struct {
31+
entries []journalEntry
32+
dirtyOverrides []common.Address
33+
}
34+
35+
func (j *journal) append(entry journalEntry) {
36+
j.entries = append(j.entries, entry)
37+
}
38+
39+
func (j *journal) flatten() map[common.Address]struct{} {
40+
41+
dirtyObjects := make(map[common.Address]struct{})
42+
for _, journalEntry := range j.entries {
43+
if addr := journalEntry.getAccount(); addr != nil {
44+
dirtyObjects[*addr] = struct{}{}
45+
}
46+
}
47+
for _, addr := range j.dirtyOverrides {
48+
dirtyObjects[addr] = struct{}{}
49+
}
50+
return dirtyObjects
51+
}
52+
53+
// Length returns the number of journal entries in the journal
54+
func (j *journal) Length() int {
55+
return len(j.entries)
56+
}
57+
58+
func (j *journal) dirtyOverride(address common.Address) {
59+
j.dirtyOverrides = append(j.dirtyOverrides, address)
60+
}
3061

3162
type (
3263
// Changes to the account trie.
@@ -82,48 +113,71 @@ func (ch createObjectChange) undo(s *StateDB) {
82113
delete(s.stateObjectsDirty, *ch.account)
83114
}
84115

116+
func (ch createObjectChange) getAccount() *common.Address {
117+
return ch.account
118+
}
119+
85120
func (ch resetObjectChange) undo(s *StateDB) {
86121
s.setStateObject(ch.prev)
87122
}
88123

124+
func (ch resetObjectChange) getAccount() *common.Address {
125+
return nil
126+
}
127+
89128
func (ch suicideChange) undo(s *StateDB) {
90129
obj := s.getStateObject(*ch.account)
91130
if obj != nil {
92131
obj.suicided = ch.prev
93132
obj.setBalance(ch.prevbalance)
94133
}
95134
}
135+
func (ch suicideChange) getAccount() *common.Address {
136+
return ch.account
137+
}
96138

97139
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
98140

99141
func (ch touchChange) undo(s *StateDB) {
100-
if !ch.prev && *ch.account != ripemd {
101-
s.getStateObject(*ch.account).touched = ch.prev
102-
if !ch.prevDirty {
103-
delete(s.stateObjectsDirty, *ch.account)
104-
}
105-
}
142+
}
143+
func (ch touchChange) getAccount() *common.Address {
144+
return ch.account
106145
}
107146

108147
func (ch balanceChange) undo(s *StateDB) {
109148
s.getStateObject(*ch.account).setBalance(ch.prev)
110149
}
150+
func (ch balanceChange) getAccount() *common.Address {
151+
return ch.account
152+
}
111153

112154
func (ch nonceChange) undo(s *StateDB) {
113155
s.getStateObject(*ch.account).setNonce(ch.prev)
114156
}
115157

158+
func (ch nonceChange) getAccount() *common.Address {
159+
return ch.account
160+
}
116161
func (ch codeChange) undo(s *StateDB) {
117162
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
118163
}
164+
func (ch codeChange) getAccount() *common.Address {
165+
return ch.account
166+
}
119167

120168
func (ch storageChange) undo(s *StateDB) {
121169
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
122170
}
171+
func (ch storageChange) getAccount() *common.Address {
172+
return ch.account
173+
}
123174

124175
func (ch refundChange) undo(s *StateDB) {
125176
s.refund = ch.prev
126177
}
178+
func (ch refundChange) getAccount() *common.Address {
179+
return nil
180+
}
127181

128182
func (ch addLogChange) undo(s *StateDB) {
129183
logs := s.logs[ch.txhash]
@@ -134,7 +188,14 @@ func (ch addLogChange) undo(s *StateDB) {
134188
}
135189
s.logSize--
136190
}
191+
func (ch addLogChange) getAccount() *common.Address {
192+
return nil
193+
}
137194

138195
func (ch addPreimageChange) undo(s *StateDB) {
139196
delete(s.preimages, ch.hash)
140197
}
198+
199+
func (ch addPreimageChange) getAccount() *common.Address {
200+
return nil
201+
}

‎core/state/state_object.go‎

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ type stateObject struct {
8585
// during the "update" phase of the state transition.
8686
dirtyCode bool // true if the code was updated
8787
suicided bool
88-
touched bool
8988
deleted bool
90-
onDirty func(addr common.Address) // Callback method to mark a state object newly dirty
9189
}
9290

9391
// empty returns whether the account is considered empty.
@@ -105,7 +103,7 @@ type Account struct {
105103
}
106104

107105
// newObject creates a state object.
108-
func newObject(db *StateDB, address common.Address, data Account, onDirty func(addr common.Address)) *stateObject {
106+
func newObject(db *StateDB, address common.Address, data Account) *stateObject {
109107
if data.Balance == nil {
110108
data.Balance = new(big.Int)
111109
}
@@ -119,7 +117,6 @@ func newObject(db *StateDB, address common.Address, data Account, onDirty func(a
119117
data: data,
120118
cachedStorage: make(Storage),
121119
dirtyStorage: make(Storage),
122-
onDirty: onDirty,
123120
}
124121
}
125122

@@ -137,23 +134,17 @@ func (self *stateObject) setError(err error) {
137134

138135
func (self *stateObject) markSuicided() {
139136
self.suicided = true
140-
if self.onDirty != nil {
141-
self.onDirty(self.Address())
142-
self.onDirty = nil
143-
}
144137
}
145138

146139
func (c *stateObject) touch() {
147-
c.db.journal = append(c.db.journal, touchChange{
148-
account: &c.address,
149-
prev: c.touched,
150-
prevDirty: c.onDirty == nil,
140+
c.db.journal.append(touchChange{
141+
account: &c.address,
151142
})
152-
if c.onDirty != nil {
153-
c.onDirty(c.Address())
154-
c.onDirty = nil
143+
if c.address == ripemd {
144+
//Explicitly put it in the dirty-cache, which is otherwise
145+
// generated from flattened journals
146+
c.db.journal.dirtyOverride(c.address)
155147
}
156-
c.touched = true
157148
}
158149

159150
func (c *stateObject) getTrie(db Database) Trie {
@@ -195,7 +186,7 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
195186

196187
// SetState updates a value in account storage.
197188
func (self *stateObject) SetState(db Database, key, value common.Hash) {
198-
self.db.journal = append(self.db.journal, storageChange{
189+
self.db.journal.append(storageChange{
199190
account: &self.address,
200191
key: key,
201192
prevalue: self.GetState(db, key),
@@ -207,10 +198,6 @@ func (self *stateObject) setState(key, value common.Hash) {
207198
self.cachedStorage[key] = value
208199
self.dirtyStorage[key] = value
209200

210-
if self.onDirty != nil {
211-
self.onDirty(self.Address())
212-
self.onDirty = nil
213-
}
214201
}
215202

216203
// updateTrie writes cached storage modifications into the object's storage trie.
@@ -274,7 +261,7 @@ func (c *stateObject) SubBalance(amount *big.Int) {
274261
}
275262

276263
func (self *stateObject) SetBalance(amount *big.Int) {
277-
self.db.journal = append(self.db.journal, balanceChange{
264+
self.db.journal.append(balanceChange{
278265
account: &self.address,
279266
prev: new(big.Int).Set(self.data.Balance),
280267
})
@@ -283,17 +270,13 @@ func (self *stateObject) SetBalance(amount *big.Int) {
283270

284271
func (self *stateObject) setBalance(amount *big.Int) {
285272
self.data.Balance = amount
286-
if self.onDirty != nil {
287-
self.onDirty(self.Address())
288-
self.onDirty = nil
289-
}
290273
}
291274

292275
// Return the gas back to the origin. Used by the Virtual machine or Closures
293276
func (c *stateObject) ReturnGas(gas *big.Int) {}
294277

295-
func (self *stateObject) deepCopy(db *StateDB, onDirty func(addr common.Address)) *stateObject {
296-
stateObject := newObject(db, self.address, self.data, onDirty)
278+
func (self *stateObject) deepCopy(db *StateDB) *stateObject {
279+
stateObject := newObject(db, self.address, self.data)
297280
if self.trie != nil {
298281
stateObject.trie = db.db.CopyTrie(self.trie)
299282
}
@@ -333,7 +316,7 @@ func (self *stateObject) Code(db Database) []byte {
333316

334317
func (self *stateObject) SetCode(codeHash common.Hash, code []byte) {
335318
prevcode := self.Code(self.db.db)
336-
self.db.journal = append(self.db.journal, codeChange{
319+
self.db.journal.append(codeChange{
337320
account: &self.address,
338321
prevhash: self.CodeHash(),
339322
prevcode: prevcode,
@@ -345,14 +328,10 @@ func (self *stateObject) setCode(codeHash common.Hash, code []byte) {
345328
self.code = code
346329
self.data.CodeHash = codeHash[:]
347330
self.dirtyCode = true
348-
if self.onDirty != nil {
349-
self.onDirty(self.Address())
350-
self.onDirty = nil
351-
}
352331
}
353332

354333
func (self *stateObject) SetNonce(nonce uint64) {
355-
self.db.journal = append(self.db.journal, nonceChange{
334+
self.db.journal.append(nonceChange{
356335
account: &self.address,
357336
prev: self.data.Nonce,
358337
})
@@ -361,10 +340,6 @@ func (self *stateObject) SetNonce(nonce uint64) {
361340

362341
func (self *stateObject) setNonce(nonce uint64) {
363342
self.data.Nonce = nonce
364-
if self.onDirty != nil {
365-
self.onDirty(self.Address())
366-
self.onDirty = nil
367-
}
368343
}
369344

370345
func (self *stateObject) CodeHash() []byte {

0 commit comments

Comments
 (0)