@@ -66,6 +66,7 @@ func (self Storage) Copy() Storage {
66
66
type StateObject struct {
67
67
address common.Address // Ethereum address of this account
68
68
data Account
69
+ db * StateDB
69
70
70
71
// DB error.
71
72
// State objects are used by the consensus core and VM which are
@@ -99,15 +100,15 @@ type Account struct {
99
100
CodeHash []byte
100
101
}
101
102
102
- // NewObject creates a state object.
103
- func NewObject ( address common.Address , data Account , onDirty func (addr common.Address )) * StateObject {
103
+ // newObject creates a state object.
104
+ func newObject ( db * StateDB , address common.Address , data Account , onDirty func (addr common.Address )) * StateObject {
104
105
if data .Balance == nil {
105
106
data .Balance = new (big.Int )
106
107
}
107
108
if data .CodeHash == nil {
108
109
data .CodeHash = emptyCodeHash
109
110
}
110
- return & StateObject {address : address , data : data , cachedStorage : make (Storage ), dirtyStorage : make (Storage ), onDirty : onDirty }
111
+ return & StateObject {db : db , address : address , data : data , cachedStorage : make (Storage ), dirtyStorage : make (Storage ), onDirty : onDirty }
111
112
}
112
113
113
114
// EncodeRLP implements rlp.Encoder.
@@ -122,7 +123,7 @@ func (self *StateObject) setError(err error) {
122
123
}
123
124
}
124
125
125
- func (self * StateObject ) MarkForDeletion () {
126
+ func (self * StateObject ) markForDeletion () {
126
127
self .remove = true
127
128
if self .onDirty != nil {
128
129
self .onDirty (self .Address ())
@@ -163,7 +164,16 @@ func (self *StateObject) GetState(db trie.Database, key common.Hash) common.Hash
163
164
}
164
165
165
166
// SetState updates a value in account storage.
166
- func (self * StateObject ) SetState (key , value common.Hash ) {
167
+ func (self * StateObject ) SetState (db trie.Database , key , value common.Hash ) {
168
+ self .db .journal = append (self .db .journal , storageChange {
169
+ account : & self .address ,
170
+ key : key ,
171
+ prevalue : self .GetState (db , key ),
172
+ })
173
+ self .setState (key , value )
174
+ }
175
+
176
+ func (self * StateObject ) setState (key , value common.Hash ) {
167
177
self .cachedStorage [key ] = value
168
178
self .dirtyStorage [key ] = value
169
179
@@ -189,7 +199,7 @@ func (self *StateObject) updateTrie(db trie.Database) {
189
199
}
190
200
191
201
// UpdateRoot sets the trie root to the current root hash of
192
- func (self * StateObject ) UpdateRoot (db trie.Database ) {
202
+ func (self * StateObject ) updateRoot (db trie.Database ) {
193
203
self .updateTrie (db )
194
204
self .data .Root = self .trie .Hash ()
195
205
}
@@ -232,6 +242,14 @@ func (c *StateObject) SubBalance(amount *big.Int) {
232
242
}
233
243
234
244
func (self * StateObject ) SetBalance (amount * big.Int ) {
245
+ self .db .journal = append (self .db .journal , balanceChange {
246
+ account : & self .address ,
247
+ prev : new (big.Int ).Set (self .data .Balance ),
248
+ })
249
+ self .setBalance (amount )
250
+ }
251
+
252
+ func (self * StateObject ) setBalance (amount * big.Int ) {
235
253
self .data .Balance = amount
236
254
if self .onDirty != nil {
237
255
self .onDirty (self .Address ())
@@ -242,8 +260,8 @@ func (self *StateObject) SetBalance(amount *big.Int) {
242
260
// Return the gas back to the origin. Used by the Virtual machine or Closures
243
261
func (c * StateObject ) ReturnGas (gas , price * big.Int ) {}
244
262
245
- func (self * StateObject ) Copy (db trie. Database , onDirty func (addr common.Address )) * StateObject {
246
- stateObject := NewObject ( self .address , self .data , onDirty )
263
+ func (self * StateObject ) deepCopy (db * StateDB , onDirty func (addr common.Address )) * StateObject {
264
+ stateObject := newObject ( db , self .address , self .data , onDirty )
247
265
stateObject .trie = self .trie
248
266
stateObject .code = self .code
249
267
stateObject .dirtyStorage = self .dirtyStorage .Copy ()
@@ -280,6 +298,16 @@ func (self *StateObject) Code(db trie.Database) []byte {
280
298
}
281
299
282
300
func (self * StateObject ) SetCode (codeHash common.Hash , code []byte ) {
301
+ prevcode := self .Code (self .db .db )
302
+ self .db .journal = append (self .db .journal , codeChange {
303
+ account : & self .address ,
304
+ prevhash : self .CodeHash (),
305
+ prevcode : prevcode ,
306
+ })
307
+ self .setCode (codeHash , code )
308
+ }
309
+
310
+ func (self * StateObject ) setCode (codeHash common.Hash , code []byte ) {
283
311
self .code = code
284
312
self .data .CodeHash = codeHash [:]
285
313
self .dirtyCode = true
@@ -290,6 +318,14 @@ func (self *StateObject) SetCode(codeHash common.Hash, code []byte) {
290
318
}
291
319
292
320
func (self * StateObject ) SetNonce (nonce uint64 ) {
321
+ self .db .journal = append (self .db .journal , nonceChange {
322
+ account : & self .address ,
323
+ prev : self .data .Nonce ,
324
+ })
325
+ self .setNonce (nonce )
326
+ }
327
+
328
+ func (self * StateObject ) setNonce (nonce uint64 ) {
293
329
self .data .Nonce = nonce
294
330
if self .onDirty != nil {
295
331
self .onDirty (self .Address ())
@@ -322,7 +358,7 @@ func (self *StateObject) ForEachStorage(cb func(key, value common.Hash) bool) {
322
358
cb (h , value )
323
359
}
324
360
325
- it := self .trie .Iterator ()
361
+ it := self .getTrie ( self . db . db ) .Iterator ()
326
362
for it .Next () {
327
363
// ignore cached values
328
364
key := common .BytesToHash (self .trie .GetKey (it .Key ))
0 commit comments