From be44bde6ebbf2560e30e638a57071cb1ac8bfe0d Mon Sep 17 00:00:00 2001 From: joeylichang Date: Thu, 6 Jun 2024 17:21:51 +0800 Subject: [PATCH] chore: add notrie check in statedb commit --- core/state/statedb.go | 80 +++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 630130cf2b..41f988e3ae 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1295,7 +1295,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er // which will affect performance, so it is discarded //storageTrieNodesUpdated int //storageTrieNodesDeleted int - + nodes = trienode.NewMergedNodeSet() incomplete map[common.Address]struct{} ) @@ -1368,13 +1368,16 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er if obj := s.stateObjects[addr]; !obj.deleted { tasks <- func() { // Write any storage changes in the state object to its storage trie - if set, err := obj.commit(); err != nil { - taskResults <- taskResult{err, nil} - return + if !s.noTrie { + if set, err := obj.commit(); err != nil { + taskResults <- taskResult{err, nil} + return + } else { + taskResults <- taskResult{nil, set} + } } else { - taskResults <- taskResult{nil, set} + taskResults <- taskResult{nil, nil} } - } tasksNum++ } @@ -1397,45 +1400,46 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er } close(finishCh) - var start time.Time - if metrics.EnabledExpensive { - start = time.Now() - } - root, set, err := s.trie.Commit(true) - if err != nil { - return err - } - // Merge the dirty nodes of account trie into global set - if set != nil { - if err := nodes.Merge(set); err != nil { - return err + if !s.noTrie { + var start time.Time + if metrics.EnabledExpensive { + start = time.Now() } - accountTrieNodesUpdated, accountTrieNodesDeleted = set.Size() - } - if metrics.EnabledExpensive { - s.AccountCommits += time.Since(start) - } - - origin := s.originalRoot - if origin == (common.Hash{}) { - origin = types.EmptyRootHash - } - - if root != origin { - start := time.Now() - set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete) - if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil { + root, set, err := s.trie.Commit(true) + if err != nil { return err } - s.originalRoot = root + // Merge the dirty nodes of account trie into global set + if set != nil { + if err := nodes.Merge(set); err != nil { + return err + } + accountTrieNodesUpdated, accountTrieNodesDeleted = set.Size() + } if metrics.EnabledExpensive { - s.TrieDBCommits += time.Since(start) + s.AccountCommits += time.Since(start) + } + + origin := s.originalRoot + if origin == (common.Hash{}) { + origin = types.EmptyRootHash } - if s.onCommit != nil { - s.onCommit(set) + + if root != origin { + start := time.Now() + set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete) + if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil { + return err + } + s.originalRoot = root + if metrics.EnabledExpensive { + s.TrieDBCommits += time.Since(start) + } + if s.onCommit != nil { + s.onCommit(set) + } } } - wg.Wait() return nil },