@@ -66,6 +66,7 @@ func (self Storage) Copy() Storage {
6666type StateObject struct {
6767 address common.Address // Ethereum address of this account
6868 data Account
69+ db * StateDB
6970
7071 // DB error.
7172 // State objects are used by the consensus core and VM which are
@@ -99,15 +100,15 @@ type Account struct {
99100 CodeHash []byte
100101}
101102
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 {
104105 if data .Balance == nil {
105106 data .Balance = new (big.Int )
106107 }
107108 if data .CodeHash == nil {
108109 data .CodeHash = emptyCodeHash
109110 }
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 }
111112}
112113
113114// EncodeRLP implements rlp.Encoder.
@@ -122,7 +123,7 @@ func (self *StateObject) setError(err error) {
122123 }
123124}
124125
125- func (self * StateObject ) MarkForDeletion () {
126+ func (self * StateObject ) markForDeletion () {
126127 self .remove = true
127128 if self .onDirty != nil {
128129 self .onDirty (self .Address ())
@@ -163,7 +164,16 @@ func (self *StateObject) GetState(db trie.Database, key common.Hash) common.Hash
163164}
164165
165166// 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 ) {
167177 self .cachedStorage [key ] = value
168178 self .dirtyStorage [key ] = value
169179
@@ -189,7 +199,7 @@ func (self *StateObject) updateTrie(db trie.Database) {
189199}
190200
191201// 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 ) {
193203 self .updateTrie (db )
194204 self .data .Root = self .trie .Hash ()
195205}
@@ -232,6 +242,14 @@ func (c *StateObject) SubBalance(amount *big.Int) {
232242}
233243
234244func (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 ) {
235253 self .data .Balance = amount
236254 if self .onDirty != nil {
237255 self .onDirty (self .Address ())
@@ -242,8 +260,8 @@ func (self *StateObject) SetBalance(amount *big.Int) {
242260// Return the gas back to the origin. Used by the Virtual machine or Closures
243261func (c * StateObject ) ReturnGas (gas , price * big.Int ) {}
244262
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 )
247265 stateObject .trie = self .trie
248266 stateObject .code = self .code
249267 stateObject .dirtyStorage = self .dirtyStorage .Copy ()
@@ -280,6 +298,16 @@ func (self *StateObject) Code(db trie.Database) []byte {
280298}
281299
282300func (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 ) {
283311 self .code = code
284312 self .data .CodeHash = codeHash [:]
285313 self .dirtyCode = true
@@ -290,6 +318,14 @@ func (self *StateObject) SetCode(codeHash common.Hash, code []byte) {
290318}
291319
292320func (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 ) {
293329 self .data .Nonce = nonce
294330 if self .onDirty != nil {
295331 self .onDirty (self .Address ())
@@ -322,7 +358,7 @@ func (self *StateObject) ForEachStorage(cb func(key, value common.Hash) bool) {
322358 cb (h , value )
323359 }
324360
325- it := self .trie .Iterator ()
361+ it := self .getTrie ( self . db . db ) .Iterator ()
326362 for it .Next () {
327363 // ignore cached values
328364 key := common .BytesToHash (self .trie .GetKey (it .Key ))
0 commit comments