From 9d9c64b950d863c86b3f09bc3cb89a5b9c585484 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 23 Sep 2016 04:16:42 +0200 Subject: [PATCH] core/state: robustify commit error handling --- core/state/statedb.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index f21c6b0efd..9b3cf0eb00 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -427,8 +427,14 @@ func (s *StateDB) CommitBatch() (root common.Hash, batch ethdb.Batch) { return root, batch } -func (s *StateDB) commit(dbw trie.DatabaseWriter) (common.Hash, error) { +func (s *StateDB) commit(dbw trie.DatabaseWriter) (root common.Hash, err error) { s.refund = new(big.Int) + defer func() { + if err != nil { + // Committing failed, any updates to the canon state are invalid. + s.all = make(map[common.Address]Account) + } + }() // Commit objects to the trie. for addr, stateObject := range s.stateObjects { @@ -444,7 +450,6 @@ func (s *StateDB) commit(dbw trie.DatabaseWriter) (common.Hash, error) { return common.Hash{}, err } } - // Write any storage changes in the state object to its storage trie. if err := stateObject.CommitTrie(s.db, dbw); err != nil { return common.Hash{}, err @@ -455,14 +460,8 @@ func (s *StateDB) commit(dbw trie.DatabaseWriter) (common.Hash, error) { } stateObject.dirty = false } - // Write trie changes. - root, err := s.trie.CommitTo(dbw) - if err != nil { - // Committing failed, any updates to the canon state are invalid. - s.all = make(map[common.Address]Account) - } - return root, err + return s.trie.CommitTo(dbw) } func (self *StateDB) Refunds() *big.Int {