mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
trie: use dedicated committer/hasher
This commit is contained in:
parent
39025991a3
commit
4670907ebe
2 changed files with 51 additions and 4 deletions
|
|
@ -67,6 +67,7 @@ func newHasher(onleaf LeafCallback) *hasher {
|
||||||
}
|
}
|
||||||
|
|
||||||
func returnHasherToPool(h *hasher) {
|
func returnHasherToPool(h *hasher) {
|
||||||
|
h.onleaf = nil
|
||||||
hasherPool.Put(h)
|
hasherPool.Put(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
54
trie/trie.go
54
trie/trie.go
|
|
@ -20,6 +20,7 @@ package trie
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
|
@ -409,13 +410,20 @@ func (t *Trie) Hash() common.Hash {
|
||||||
return common.BytesToHash(hash.(hashNode))
|
return common.BytesToHash(hash.(hashNode))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit writes all nodes to the trie's memory database, tracking the internal
|
// oldCommit is the old implementation of Commit, which uses the
|
||||||
|
// regular hasher.
|
||||||
|
// It writes all nodes to the trie's memory database, tracking the internal
|
||||||
// and external (for account tries) references.
|
// and external (for account tries) references.
|
||||||
func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
func (t *Trie) oldCommit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||||
if t.db == nil {
|
if t.db == nil {
|
||||||
panic("commit called on trie with nil database")
|
panic("commit called on trie with nil database")
|
||||||
}
|
}
|
||||||
hash, cached, err := t.hashRoot(t.db, onleaf)
|
if t.root == nil {
|
||||||
|
return emptyRoot, nil
|
||||||
|
}
|
||||||
|
h := newHasher(onleaf)
|
||||||
|
defer returnHasherToPool(h)
|
||||||
|
hash, cached, err := h.hash(t.root, t.db, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Hash{}, err
|
return common.Hash{}, err
|
||||||
}
|
}
|
||||||
|
|
@ -423,7 +431,34 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||||
return common.BytesToHash(hash.(hashNode)), nil
|
return common.BytesToHash(hash.(hashNode)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) {
|
func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
||||||
|
if t.db == nil {
|
||||||
|
panic("commit called on trie with nil database")
|
||||||
|
}
|
||||||
|
if t.root == nil {
|
||||||
|
return emptyRoot, nil
|
||||||
|
}
|
||||||
|
rootHash := t.Hash()
|
||||||
|
h := newCommitter(onleaf)
|
||||||
|
defer returnCommitterToPool(h)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
if onleaf != nil {
|
||||||
|
wg.Add(1)
|
||||||
|
go h.commitLoop(t.db, &wg)
|
||||||
|
}
|
||||||
|
_, err = h.commit(t.root, t.db, true)
|
||||||
|
if onleaf != nil {
|
||||||
|
close(h.leafCh)
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
return rootHash, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// oldHashRoot is the old implementation of hashRoot, which uses the regular hasher
|
||||||
|
func (t *Trie) oldHashRoot(db *Database, onleaf LeafCallback) (node, node, error) {
|
||||||
if t.root == nil {
|
if t.root == nil {
|
||||||
return hashNode(emptyRoot.Bytes()), nil, nil
|
return hashNode(emptyRoot.Bytes()), nil, nil
|
||||||
}
|
}
|
||||||
|
|
@ -431,3 +466,14 @@ func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) {
|
||||||
defer returnHasherToPool(h)
|
defer returnHasherToPool(h)
|
||||||
return h.hash(t.root, db, true)
|
return h.hash(t.root, db, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hashRoot calculates the root hash of the given trie
|
||||||
|
func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) {
|
||||||
|
if t.root == nil {
|
||||||
|
return hashNode(emptyRoot.Bytes()), nil, nil
|
||||||
|
}
|
||||||
|
h := newPureHasher()
|
||||||
|
defer returnPureHasherToPool(h)
|
||||||
|
hashed, cached := h.hash(t.root, true)
|
||||||
|
return hashed, cached, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue