@@ -35,7 +35,6 @@ import (
3535type StateDB struct {
3636 db ethdb.Database
3737 trie * trie.SecureTrie
38- root common.Hash
3938
4039 stateObjects map [string ]* StateObject
4140
@@ -49,12 +48,19 @@ type StateDB struct {
4948
5049// Create a new state from a given trie
5150func New (root common.Hash , db ethdb.Database ) * StateDB {
52- trie := trie .NewSecure (root [:], db )
53- return & StateDB {root : root , db : db , trie : trie , stateObjects : make (map [string ]* StateObject ), refund : new (big.Int ), logs : make (map [common.Hash ]Logs )}
54- }
55-
56- func (self * StateDB ) PrintRoot () {
57- self .trie .Trie .PrintRoot ()
51+ tr , err := trie .NewSecure (root , db )
52+ if err != nil {
53+ // TODO: bubble this up
54+ tr , _ = trie .NewSecure (common.Hash {}, db )
55+ glog .Errorf ("can't create state trie with root %x: %v" , root [:], err )
56+ }
57+ return & StateDB {
58+ db : db ,
59+ trie : tr ,
60+ stateObjects : make (map [string ]* StateObject ),
61+ refund : new (big.Int ),
62+ logs : make (map [common.Hash ]Logs ),
63+ }
5864}
5965
6066func (self * StateDB ) StartRecord (thash , bhash common.Hash , ti int ) {
@@ -196,7 +202,6 @@ func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
196202 if len (stateObject .CodeHash ()) > 0 {
197203 self .db .Put (stateObject .CodeHash (), stateObject .code )
198204 }
199-
200205 addr := stateObject .Address ()
201206 self .trie .Update (addr [:], stateObject .RlpEncode ())
202207}
@@ -207,6 +212,7 @@ func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
207212
208213 addr := stateObject .Address ()
209214 self .trie .Delete (addr [:])
215+ //delete(self.stateObjects, addr.Str())
210216}
211217
212218// Retrieve a state object given my the address. Nil if not found
@@ -303,65 +309,67 @@ func (self *StateDB) Set(state *StateDB) {
303309 self .logSize = state .logSize
304310}
305311
306- func (s * StateDB ) Root () common.Hash {
307- return common .BytesToHash (s .trie .Root ())
308- }
309-
310- // Syncs the trie and all siblings
311- func (s * StateDB ) Sync () {
312- // Sync all nested states
312+ // IntermediateRoot computes the current root hash of the state trie.
313+ // It is called in between transactions to get the root hash that
314+ // goes into transaction receipts.
315+ func (s * StateDB ) IntermediateRoot () common.Hash {
316+ s .refund = new (big.Int )
313317 for _ , stateObject := range s .stateObjects {
314- stateObject .trie .Commit ()
315- }
316-
317- s .trie .Commit ()
318-
319- s .Empty ()
320- }
321-
322- func (self * StateDB ) Empty () {
323- self .stateObjects = make (map [string ]* StateObject )
324- self .refund = new (big.Int )
325- }
326-
327- func (self * StateDB ) Refunds () * big.Int {
328- return self .refund
329- }
330-
331- // SyncIntermediate updates the intermediate state and all mid steps
332- func (self * StateDB ) SyncIntermediate () {
333- self .refund = new (big.Int )
334-
335- for _ , stateObject := range self .stateObjects {
336318 if stateObject .dirty {
337319 if stateObject .remove {
338- self .DeleteStateObject (stateObject )
320+ s .DeleteStateObject (stateObject )
339321 } else {
340322 stateObject .Update ()
341-
342- self .UpdateStateObject (stateObject )
323+ s .UpdateStateObject (stateObject )
343324 }
344325 stateObject .dirty = false
345326 }
346327 }
328+ return s .trie .Hash ()
347329}
348330
349- // SyncObjects syncs the changed objects to the trie
350- func (self * StateDB ) SyncObjects () {
351- self .trie = trie .NewSecure (self .root [:], self .db )
331+ // Commit commits all state changes to the database.
332+ func (s * StateDB ) Commit () (root common.Hash , err error ) {
333+ return s .commit (s .db )
334+ }
352335
353- self .refund = new (big.Int )
336+ // CommitBatch commits all state changes to a write batch but does not
337+ // execute the batch. It is used to validate state changes against
338+ // the root hash stored in a block.
339+ func (s * StateDB ) CommitBatch () (root common.Hash , batch ethdb.Batch ) {
340+ batch = s .db .NewBatch ()
341+ root , _ = s .commit (batch )
342+ return root , batch
343+ }
354344
355- for _ , stateObject := range self .stateObjects {
345+ func (s * StateDB ) commit (db trie.DatabaseWriter ) (common.Hash , error ) {
346+ s .refund = new (big.Int )
347+
348+ for _ , stateObject := range s .stateObjects {
356349 if stateObject .remove {
357- self .DeleteStateObject (stateObject )
350+ // If the object has been removed, don't bother syncing it
351+ // and just mark it for deletion in the trie.
352+ s .DeleteStateObject (stateObject )
358353 } else {
354+ // Write any storage changes in the state object to its trie.
359355 stateObject .Update ()
360-
361- self .UpdateStateObject (stateObject )
356+ // Commit the trie of the object to the batch.
357+ // This updates the trie root internally, so
358+ // getting the root hash of the storage trie
359+ // through UpdateStateObject is fast.
360+ if _ , err := stateObject .trie .CommitTo (db ); err != nil {
361+ return common.Hash {}, err
362+ }
363+ // Update the object in the account trie.
364+ s .UpdateStateObject (stateObject )
362365 }
363366 stateObject .dirty = false
364367 }
368+ return s .trie .CommitTo (db )
369+ }
370+
371+ func (self * StateDB ) Refunds () * big.Int {
372+ return self .refund
365373}
366374
367375// Debug stuff
0 commit comments