core/state, trie: remove unused error-return from trie Commit operation (#26641)

This commit is contained in:
Martin Holst Swende 2023-02-09 08:56:59 -05:00 committed by GitHub
parent 3086c256c9
commit 22c3ad1d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 72 additions and 131 deletions

View File

@ -109,7 +109,7 @@ type Trie interface {
// The returned nodeset can be nil if the trie is clean(nothing to commit). // The returned nodeset can be nil if the trie is clean(nothing to commit).
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
Commit(collectLeaf bool) (common.Hash, *trie.NodeSet, error) Commit(collectLeaf bool) (common.Hash, *trie.NodeSet)
// NodeIterator returns an iterator that returns nodes of the trie. Iteration // NodeIterator returns an iterator that returns nodes of the trie. Iteration
// starts at the key after the given start key. // starts at the key after the given start key.

View File

@ -367,8 +367,8 @@ func (dl *diskLayer) generateRange(ctx *generatorContext, trieId *trie.ID, prefi
for i, key := range result.keys { for i, key := range result.keys {
snapTrie.Update(key, result.vals[i]) snapTrie.Update(key, result.vals[i])
} }
root, nodes, err := snapTrie.Commit(false) root, nodes := snapTrie.Commit(false)
if err == nil && nodes != nil { if nodes != nil {
tdb.Update(trie.NewWithNodeSet(nodes)) tdb.Update(trie.NewWithNodeSet(nodes))
tdb.Commit(root, false) tdb.Commit(root, false)
} }

View File

@ -190,7 +190,7 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string
if !commit { if !commit {
return stTrie.Hash().Bytes() return stTrie.Hash().Bytes()
} }
root, nodes, _ := stTrie.Commit(false) root, nodes := stTrie.Commit(false)
if nodes != nil { if nodes != nil {
t.nodes.Merge(nodes) t.nodes.Merge(nodes)
} }
@ -198,7 +198,7 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string
} }
func (t *testHelper) Commit() common.Hash { func (t *testHelper) Commit() common.Hash {
root, nodes, _ := t.accTrie.Commit(true) root, nodes := t.accTrie.Commit(true)
if nodes != nil { if nodes != nil {
t.nodes.Merge(nodes) t.nodes.Merge(nodes)
} }

View File

@ -385,10 +385,8 @@ func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) {
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())
} }
root, nodes, err := tr.Commit(false) root, nodes := tr.Commit(false)
if err == nil { s.data.Root = root
s.data.Root = root
}
return nodes, err return nodes, err
} }

View File

@ -1019,10 +1019,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
start = time.Now() start = time.Now()
} }
root, set, err := s.trie.Commit(true) root, set := s.trie.Commit(true)
if err != nil {
return common.Hash{}, err
}
// Merge the dirty nodes of account trie into global set // Merge the dirty nodes of account trie into global set
if set != nil { if set != nil {
if err := nodes.Merge(set); err != nil { if err := nodes.Merge(set); err != nil {

View File

@ -1388,7 +1388,7 @@ func makeAccountTrieNoStorage(n int) (string, *trie.Trie, entrySlice) {
// Commit the state changes into db and re-create the trie // Commit the state changes into db and re-create the trie
// for accessing later. // for accessing later.
root, nodes, _ := accTrie.Commit(false) root, nodes := accTrie.Commit(false)
db.Update(trie.NewWithNodeSet(nodes)) db.Update(trie.NewWithNodeSet(nodes))
accTrie, _ = trie.New(trie.StateTrieID(root), db) accTrie, _ = trie.New(trie.StateTrieID(root), db)
@ -1450,7 +1450,7 @@ func makeBoundaryAccountTrie(n int) (string, *trie.Trie, entrySlice) {
// Commit the state changes into db and re-create the trie // Commit the state changes into db and re-create the trie
// for accessing later. // for accessing later.
root, nodes, _ := accTrie.Commit(false) root, nodes := accTrie.Commit(false)
db.Update(trie.NewWithNodeSet(nodes)) db.Update(trie.NewWithNodeSet(nodes))
accTrie, _ = trie.New(trie.StateTrieID(root), db) accTrie, _ = trie.New(trie.StateTrieID(root), db)
@ -1496,7 +1496,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(accounts, slots int, code bool)
sort.Sort(entries) sort.Sort(entries)
// Commit account trie // Commit account trie
root, set, _ := accTrie.Commit(true) root, set := accTrie.Commit(true)
nodes.Merge(set) nodes.Merge(set)
// Commit gathered dirty nodes into database // Commit gathered dirty nodes into database
@ -1561,7 +1561,7 @@ func makeAccountTrieWithStorage(accounts, slots int, code, boundary bool) (strin
sort.Sort(entries) sort.Sort(entries)
// Commit account trie // Commit account trie
root, set, _ := accTrie.Commit(true) root, set := accTrie.Commit(true)
nodes.Merge(set) nodes.Merge(set)
// Commit gathered dirty nodes into database // Commit gathered dirty nodes into database
@ -1603,7 +1603,7 @@ func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Databas
entries = append(entries, elem) entries = append(entries, elem)
} }
sort.Sort(entries) sort.Sort(entries)
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
return root, nodes, entries return root, nodes, entries
} }
@ -1654,7 +1654,7 @@ func makeBoundaryStorageTrie(owner common.Hash, n int, db *trie.Database) (commo
entries = append(entries, elem) entries = append(entries, elem)
} }
sort.Sort(entries) sort.Sort(entries)
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
return root, nodes, entries return root, nodes, entries
} }

View File

@ -212,10 +212,7 @@ func (c *ChtIndexerBackend) Process(ctx context.Context, header *types.Header) e
// Commit implements core.ChainIndexerBackend // Commit implements core.ChainIndexerBackend
func (c *ChtIndexerBackend) Commit() error { func (c *ChtIndexerBackend) Commit() error {
root, nodes, err := c.trie.Commit(false) root, nodes := c.trie.Commit(false)
if err != nil {
return err
}
// Commit trie changes into trie database in case it's not nil. // Commit trie changes into trie database in case it's not nil.
if nodes != nil { if nodes != nil {
if err := c.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := c.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil {
@ -226,6 +223,7 @@ func (c *ChtIndexerBackend) Commit() error {
} }
} }
// Re-create trie with newly generated root and updated database. // Re-create trie with newly generated root and updated database.
var err error
c.trie, err = trie.New(trie.TrieID(root), c.triedb) c.trie, err = trie.New(trie.TrieID(root), c.triedb)
if err != nil { if err != nil {
return err return err
@ -458,10 +456,7 @@ func (b *BloomTrieIndexerBackend) Commit() error {
b.trie.Delete(encKey[:]) b.trie.Delete(encKey[:])
} }
} }
root, nodes, err := b.trie.Commit(false) root, nodes := b.trie.Commit(false)
if err != nil {
return err
}
// Commit trie changes into trie database in case it's not nil. // Commit trie changes into trie database in case it's not nil.
if nodes != nil { if nodes != nil {
if err := b.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := b.triedb.Update(trie.NewWithNodeSet(nodes)); err != nil {
@ -472,6 +467,7 @@ func (b *BloomTrieIndexerBackend) Commit() error {
} }
} }
// Re-create trie with newly generated root and updated database. // Re-create trie with newly generated root and updated database.
var err error
b.trie, err = trie.New(trie.TrieID(root), b.triedb) b.trie, err = trie.New(trie.TrieID(root), b.triedb)
if err != nil { if err != nil {
return err return err

View File

@ -164,9 +164,9 @@ func (t *odrTrie) TryDeleteAccount(address common.Address) error {
}) })
} }
func (t *odrTrie) Commit(collectLeaf bool) (common.Hash, *trie.NodeSet, error) { func (t *odrTrie) Commit(collectLeaf bool) (common.Hash, *trie.NodeSet) {
if t.trie == nil { if t.trie == nil {
return t.id.Root, nil, nil return t.id.Root, nil
} }
return t.trie.Commit(collectLeaf) return t.trie.Commit(collectLeaf)
} }

View File

@ -182,10 +182,7 @@ func (f *fuzzer) fuzz() int {
return 0 return 0
} }
// Flush trie -> database // Flush trie -> database
rootA, nodes, err := trieA.Commit(false) rootA, nodes := trieA.Commit(false)
if err != nil {
panic(err)
}
if nodes != nil { if nodes != nil {
dbA.Update(trie.NewWithNodeSet(nodes)) dbA.Update(trie.NewWithNodeSet(nodes))
} }
@ -201,9 +198,7 @@ func (f *fuzzer) fuzz() int {
trieB.Update(kv.k, kv.v) trieB.Update(kv.k, kv.v)
} }
rootB := trieB.Hash() rootB := trieB.Hash()
if _, err := trieB.Commit(); err != nil { trieB.Commit()
panic(err)
}
if rootA != rootB { if rootA != rootB {
panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB)) panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB))
} }

View File

@ -161,10 +161,7 @@ func runRandTest(rt randTest) error {
case opHash: case opHash:
tr.Hash() tr.Hash()
case opCommit: case opCommit:
hash, nodes, err := tr.Commit(false) hash, nodes := tr.Commit(false)
if err != nil {
return err
}
if nodes != nil { if nodes != nil {
if err := triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { if err := triedb.Update(trie.NewWithNodeSet(nodes)); err != nil {
return err return err

View File

@ -48,25 +48,22 @@ func newCommitter(owner common.Hash, tracer *tracer, collectLeaf bool) *committe
// Commit collapses a node down into a hash node and returns it along with // Commit collapses a node down into a hash node and returns it along with
// the modified nodeset. // the modified nodeset.
func (c *committer) Commit(n node) (hashNode, *NodeSet, error) { func (c *committer) Commit(n node) (hashNode, *NodeSet) {
h, err := c.commit(nil, n) h := c.commit(nil, n)
if err != nil {
return nil, nil, err
}
// Some nodes can be deleted from trie which can't be captured // Some nodes can be deleted from trie which can't be captured
// by committer itself. Iterate all deleted nodes tracked by // by committer itself. Iterate all deleted nodes tracked by
// tracer and marked them as deleted only if they are present // tracer and marked them as deleted only if they are present
// in database previously. // in database previously.
c.tracer.markDeletions(c.nodes) c.tracer.markDeletions(c.nodes)
return h.(hashNode), c.nodes, nil return h.(hashNode), c.nodes
} }
// commit collapses a node down into a hash node and returns it. // commit collapses a node down into a hash node and returns it.
func (c *committer) commit(path []byte, n node) (node, error) { func (c *committer) commit(path []byte, n node) node {
// if this path is clean, use available cached data // if this path is clean, use available cached data
hash, dirty := n.cache() hash, dirty := n.cache()
if hash != nil && !dirty { if hash != nil && !dirty {
return hash, nil return hash
} }
// Commit children, then parent, and remove the dirty flag. // Commit children, then parent, and remove the dirty flag.
switch cn := n.(type) { switch cn := n.(type) {
@ -77,10 +74,8 @@ func (c *committer) commit(path []byte, n node) (node, error) {
// If the child is fullNode, recursively commit, // If the child is fullNode, recursively commit,
// otherwise it can only be hashNode or valueNode. // otherwise it can only be hashNode or valueNode.
if _, ok := cn.Val.(*fullNode); ok { if _, ok := cn.Val.(*fullNode); ok {
childV, err := c.commit(append(path, cn.Key...), cn.Val) childV := c.commit(append(path, cn.Key...), cn.Val)
if err != nil {
return nil, err
}
collapsed.Val = childV collapsed.Val = childV
} }
// The key needs to be copied, since we're adding it to the // The key needs to be copied, since we're adding it to the
@ -88,7 +83,7 @@ func (c *committer) commit(path []byte, n node) (node, error) {
collapsed.Key = hexToCompact(cn.Key) collapsed.Key = hexToCompact(cn.Key)
hashedNode := c.store(path, collapsed) hashedNode := c.store(path, collapsed)
if hn, ok := hashedNode.(hashNode); ok { if hn, ok := hashedNode.(hashNode); ok {
return hn, nil return hn
} }
// The short node now is embedded in its parent. Mark the node as // The short node now is embedded in its parent. Mark the node as
// deleted if it's present in database previously. It's equivalent // deleted if it's present in database previously. It's equivalent
@ -96,18 +91,15 @@ func (c *committer) commit(path []byte, n node) (node, error) {
if prev := c.tracer.getPrev(path); len(prev) != 0 { if prev := c.tracer.getPrev(path); len(prev) != 0 {
c.nodes.markDeleted(path, prev) c.nodes.markDeleted(path, prev)
} }
return collapsed, nil return collapsed
case *fullNode: case *fullNode:
hashedKids, err := c.commitChildren(path, cn) hashedKids := c.commitChildren(path, cn)
if err != nil {
return nil, err
}
collapsed := cn.copy() collapsed := cn.copy()
collapsed.Children = hashedKids collapsed.Children = hashedKids
hashedNode := c.store(path, collapsed) hashedNode := c.store(path, collapsed)
if hn, ok := hashedNode.(hashNode); ok { if hn, ok := hashedNode.(hashNode); ok {
return hn, nil return hn
} }
// The full node now is embedded in its parent. Mark the node as // The full node now is embedded in its parent. Mark the node as
// deleted if it's present in database previously. It's equivalent // deleted if it's present in database previously. It's equivalent
@ -115,9 +107,9 @@ func (c *committer) commit(path []byte, n node) (node, error) {
if prev := c.tracer.getPrev(path); len(prev) != 0 { if prev := c.tracer.getPrev(path); len(prev) != 0 {
c.nodes.markDeleted(path, prev) c.nodes.markDeleted(path, prev)
} }
return collapsed, nil return collapsed
case hashNode: case hashNode:
return cn, nil return cn
default: default:
// nil, valuenode shouldn't be committed // nil, valuenode shouldn't be committed
panic(fmt.Sprintf("%T: invalid node: %v", n, n)) panic(fmt.Sprintf("%T: invalid node: %v", n, n))
@ -125,7 +117,7 @@ func (c *committer) commit(path []byte, n node) (node, error) {
} }
// commitChildren commits the children of the given fullnode // commitChildren commits the children of the given fullnode
func (c *committer) commitChildren(path []byte, n *fullNode) ([17]node, error) { func (c *committer) commitChildren(path []byte, n *fullNode) [17]node {
var children [17]node var children [17]node
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
child := n.Children[i] child := n.Children[i]
@ -142,17 +134,14 @@ func (c *committer) commitChildren(path []byte, n *fullNode) ([17]node, error) {
// Commit the child recursively and store the "hashed" value. // Commit the child recursively and store the "hashed" value.
// Note the returned node can be some embedded nodes, so it's // Note the returned node can be some embedded nodes, so it's
// possible the type is not hashNode. // possible the type is not hashNode.
hashed, err := c.commit(append(path, byte(i)), child) hashed := c.commit(append(path, byte(i)), child)
if err != nil {
return children, err
}
children[i] = hashed children[i] = hashed
} }
// For the 17th child, it's possible the type is valuenode. // For the 17th child, it's possible the type is valuenode.
if n.Children[16] != nil { if n.Children[16] != nil {
children[16] = n.Children[16] children[16] = n.Children[16]
} }
return children, nil return children
} }
// store hashes the node n and adds it to the modified nodeset. If leaf collection // store hashes the node n and adds it to the modified nodeset. If leaf collection

View File

@ -60,10 +60,7 @@ func TestIterator(t *testing.T) {
all[val.k] = val.v all[val.k] = val.v
trie.Update([]byte(val.k), []byte(val.v)) trie.Update([]byte(val.k), []byte(val.v))
} }
root, nodes, err := trie.Commit(false) root, nodes := trie.Commit(false)
if err != nil {
t.Fatalf("Failed to commit trie %v", err)
}
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
@ -225,7 +222,7 @@ func TestDifferenceIterator(t *testing.T) {
for _, val := range testdata1 { for _, val := range testdata1 {
triea.Update([]byte(val.k), []byte(val.v)) triea.Update([]byte(val.k), []byte(val.v))
} }
rootA, nodesA, _ := triea.Commit(false) rootA, nodesA := triea.Commit(false)
dba.Update(NewWithNodeSet(nodesA)) dba.Update(NewWithNodeSet(nodesA))
triea, _ = New(TrieID(rootA), dba) triea, _ = New(TrieID(rootA), dba)
@ -234,7 +231,7 @@ func TestDifferenceIterator(t *testing.T) {
for _, val := range testdata2 { for _, val := range testdata2 {
trieb.Update([]byte(val.k), []byte(val.v)) trieb.Update([]byte(val.k), []byte(val.v))
} }
rootB, nodesB, _ := trieb.Commit(false) rootB, nodesB := trieb.Commit(false)
dbb.Update(NewWithNodeSet(nodesB)) dbb.Update(NewWithNodeSet(nodesB))
trieb, _ = New(TrieID(rootB), dbb) trieb, _ = New(TrieID(rootB), dbb)
@ -267,7 +264,7 @@ func TestUnionIterator(t *testing.T) {
for _, val := range testdata1 { for _, val := range testdata1 {
triea.Update([]byte(val.k), []byte(val.v)) triea.Update([]byte(val.k), []byte(val.v))
} }
rootA, nodesA, _ := triea.Commit(false) rootA, nodesA := triea.Commit(false)
dba.Update(NewWithNodeSet(nodesA)) dba.Update(NewWithNodeSet(nodesA))
triea, _ = New(TrieID(rootA), dba) triea, _ = New(TrieID(rootA), dba)
@ -276,7 +273,7 @@ func TestUnionIterator(t *testing.T) {
for _, val := range testdata2 { for _, val := range testdata2 {
trieb.Update([]byte(val.k), []byte(val.v)) trieb.Update([]byte(val.k), []byte(val.v))
} }
rootB, nodesB, _ := trieb.Commit(false) rootB, nodesB := trieb.Commit(false)
dbb.Update(NewWithNodeSet(nodesB)) dbb.Update(NewWithNodeSet(nodesB))
trieb, _ = New(TrieID(rootB), dbb) trieb, _ = New(TrieID(rootB), dbb)
@ -334,7 +331,7 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool) {
for _, val := range testdata1 { for _, val := range testdata1 {
tr.Update([]byte(val.k), []byte(val.v)) tr.Update([]byte(val.k), []byte(val.v))
} }
_, nodes, _ := tr.Commit(false) _, nodes := tr.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
if !memonly { if !memonly {
triedb.Commit(tr.Hash(), false) triedb.Commit(tr.Hash(), false)
@ -426,7 +423,7 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
for _, val := range testdata1 { for _, val := range testdata1 {
ctr.Update([]byte(val.k), []byte(val.v)) ctr.Update([]byte(val.k), []byte(val.v))
} }
root, nodes, _ := ctr.Commit(false) root, nodes := ctr.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
if !memonly { if !memonly {
triedb.Commit(root, false) triedb.Commit(root, false)
@ -545,7 +542,7 @@ func makeLargeTestTrie() (*Database, *StateTrie, *loggingDb) {
val = crypto.Keccak256(val) val = crypto.Keccak256(val)
trie.Update(key, val) trie.Update(key, val)
} }
_, nodes, _ := trie.Commit(false) _, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
// Return the generated trie // Return the generated trie
return triedb, trie, logDb return triedb, trie, logDb
@ -585,7 +582,7 @@ func TestIteratorNodeBlob(t *testing.T) {
all[val.k] = val.v all[val.k] = val.v
trie.Update([]byte(val.k), []byte(val.v)) trie.Update([]byte(val.k), []byte(val.v))
} }
_, nodes, _ := trie.Commit(false) _, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
triedb.Cap(0) triedb.Cap(0)

View File

@ -211,7 +211,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
// All cached preimages will be also flushed if preimages recording is enabled. // All cached preimages will be also flushed if preimages recording is enabled.
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) { func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
if t.preimages != nil { if t.preimages != nil {

View File

@ -58,10 +58,7 @@ func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
trie.Update(key, val) trie.Update(key, val)
} }
} }
root, nodes, err := trie.Commit(false) root, nodes := trie.Commit(false)
if err != nil {
panic(fmt.Errorf("failed to commit trie %v", err))
}
if err := triedb.Update(NewWithNodeSet(nodes)); err != nil { if err := triedb.Update(NewWithNodeSet(nodes)); err != nil {
panic(fmt.Errorf("failed to commit db %v", err)) panic(fmt.Errorf("failed to commit db %v", err))
} }

View File

@ -52,10 +52,7 @@ func makeTestTrie() (*Database, *StateTrie, map[string][]byte) {
trie.Update(key, val) trie.Update(key, val)
} }
} }
root, nodes, err := trie.Commit(false) root, nodes := trie.Commit(false)
if err != nil {
panic(fmt.Errorf("failed to commit trie %v", err))
}
if err := triedb.Update(NewWithNodeSet(nodes)); err != nil { if err := triedb.Update(NewWithNodeSet(nodes)); err != nil {
panic(fmt.Errorf("failed to commit db %v", err)) panic(fmt.Errorf("failed to commit db %v", err))
} }

View File

@ -566,7 +566,7 @@ func (t *Trie) Hash() common.Hash {
// The returned nodeset can be nil if the trie is clean (nothing to commit). // The returned nodeset can be nil if the trie is clean (nothing to commit).
// Once the trie is committed, it's not usable anymore. A new trie must // Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage // be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) { func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet) {
defer t.tracer.reset() defer t.tracer.reset()
// Trie is empty and can be classified into two types of situations: // Trie is empty and can be classified into two types of situations:
@ -576,7 +576,7 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
// Wrap tracked deletions as the return // Wrap tracked deletions as the return
set := NewNodeSet(t.owner) set := NewNodeSet(t.owner)
t.tracer.markDeletions(set) t.tracer.markDeletions(set)
return emptyRoot, set, nil return emptyRoot, set
} }
// Derive the hash for all dirty nodes first. We hold the assumption // Derive the hash for all dirty nodes first. We hold the assumption
// in the following procedure that all nodes are hashed. // in the following procedure that all nodes are hashed.
@ -588,15 +588,12 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
// Replace the root node with the origin hash in order to // Replace the root node with the origin hash in order to
// ensure all resolved nodes are dropped after the commit. // ensure all resolved nodes are dropped after the commit.
t.root = hashedNode t.root = hashedNode
return rootHash, nil, nil return rootHash, nil
} }
h := newCommitter(t.owner, t.tracer, collectLeaf) h := newCommitter(t.owner, t.tracer, collectLeaf)
newRoot, nodes, err := h.Commit(t.root) newRoot, nodes := h.Commit(t.root)
if err != nil {
return common.Hash{}, nil, err
}
t.root = newRoot t.root = newRoot
return rootHash, nodes, nil return rootHash, nodes
} }
// hashRoot calculates the root hash of the given trie // hashRoot calculates the root hash of the given trie

View File

@ -83,7 +83,7 @@ func testMissingNode(t *testing.T, memonly bool) {
trie := NewEmpty(triedb) trie := NewEmpty(triedb)
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
if !memonly { if !memonly {
triedb.Commit(root, false) triedb.Commit(root, false)
@ -166,10 +166,7 @@ func TestInsert(t *testing.T) {
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
root, _, err := trie.Commit(false) root, _ = trie.Commit(false)
if err != nil {
t.Fatalf("commit error: %v", err)
}
if root != exp { if root != exp {
t.Errorf("case 2: exp %x got %x", exp, root) t.Errorf("case 2: exp %x got %x", exp, root)
} }
@ -194,7 +191,7 @@ func TestGet(t *testing.T) {
if i == 1 { if i == 1 {
return return
} }
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
trie, _ = New(TrieID(root), db) trie, _ = New(TrieID(root), db)
} }
@ -266,10 +263,7 @@ func TestReplication(t *testing.T) {
for _, val := range vals { for _, val := range vals {
updateString(trie, val.k, val.v) updateString(trie, val.k, val.v)
} }
exp, nodes, err := trie.Commit(false) exp, nodes := trie.Commit(false)
if err != nil {
t.Fatalf("commit error: %v", err)
}
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
// create a new trie on top of the database and check that lookups work. // create a new trie on top of the database and check that lookups work.
@ -282,10 +276,7 @@ func TestReplication(t *testing.T) {
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v) t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v)
} }
} }
hash, nodes, err := trie2.Commit(false) hash, nodes := trie2.Commit(false)
if err != nil {
t.Fatalf("commit error: %v", err)
}
if hash != exp { if hash != exp {
t.Errorf("root failure. expected %x got %x", exp, hash) t.Errorf("root failure. expected %x got %x", exp, hash)
} }
@ -454,11 +445,7 @@ func runRandTest(rt randTest) bool {
case opHash: case opHash:
tr.Hash() tr.Hash()
case opCommit: case opCommit:
root, nodes, err := tr.Commit(true) root, nodes := tr.Commit(true)
if err != nil {
rt[i].err = err
return false
}
// Validity the returned nodeset // Validity the returned nodeset
if nodes != nil { if nodes != nil {
for path, node := range nodes.updates.nodes { for path, node := range nodes.updates.nodes {
@ -710,7 +697,7 @@ func TestCommitAfterHash(t *testing.T) {
if exp != root { if exp != root {
t.Errorf("got %x, exp %x", root, exp) t.Errorf("got %x, exp %x", root, exp)
} }
root, _, _ = trie.Commit(false) root, _ = trie.Commit(false)
if exp != root { if exp != root {
t.Errorf("got %x, exp %x", root, exp) t.Errorf("got %x, exp %x", root, exp)
} }
@ -813,7 +800,7 @@ func TestCommitSequence(t *testing.T) {
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i])
} }
// Flush trie -> database // Flush trie -> database
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Commit(root, false) db.Commit(root, false)
@ -854,7 +841,7 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
trie.Update(key, val) trie.Update(key, val)
} }
// Flush trie -> database // Flush trie -> database
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Commit(root, false) db.Commit(root, false)
@ -893,7 +880,7 @@ func TestCommitSequenceStackTrie(t *testing.T) {
stTrie.TryUpdate(key, val) stTrie.TryUpdate(key, val)
} }
// Flush trie -> database // Flush trie -> database
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
db.Commit(root, false) db.Commit(root, false)
@ -941,7 +928,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
trie.TryUpdate(key, []byte{0x1}) trie.TryUpdate(key, []byte{0x1})
stTrie.TryUpdate(key, []byte{0x1}) stTrie.TryUpdate(key, []byte{0x1})
// Flush trie -> database // Flush trie -> database
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
// Flush memdb -> disk (sponge) // Flush memdb -> disk (sponge)
db.Update(NewWithNodeSet(nodes)) db.Update(NewWithNodeSet(nodes))
db.Commit(root, false) db.Commit(root, false)
@ -1114,7 +1101,7 @@ func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts []
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i])
} }
h := trie.Hash() h := trie.Hash()
_, nodes, _ := trie.Commit(false) _, nodes := trie.Commit(false)
triedb.Update(NewWithNodeSet(nodes)) triedb.Update(NewWithNodeSet(nodes))
b.StartTimer() b.StartTimer()
triedb.Dereference(h) triedb.Dereference(h)

View File

@ -69,7 +69,7 @@ func TestTrieTracer(t *testing.T) {
} }
// Commit the changes and re-create with new root // Commit the changes and re-create with new root
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
if err := db.Update(NewWithNodeSet(nodes)); err != nil { if err := db.Update(NewWithNodeSet(nodes)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -154,7 +154,7 @@ func TestTrieTracePrevValue(t *testing.T) {
} }
// Commit the changes and re-create with new root // Commit the changes and re-create with new root
root, nodes, _ := trie.Commit(false) root, nodes := trie.Commit(false)
if err := db.Update(NewWithNodeSet(nodes)); err != nil { if err := db.Update(NewWithNodeSet(nodes)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -261,10 +261,7 @@ func TestDeleteAll(t *testing.T) {
for _, val := range vals { for _, val := range vals {
trie.Update([]byte(val.k), []byte(val.v)) trie.Update([]byte(val.k), []byte(val.v))
} }
root, set, err := trie.Commit(false) root, set := trie.Commit(false)
if err != nil {
t.Fatal(err)
}
if err := db.Update(NewWithNodeSet(set)); err != nil { if err := db.Update(NewWithNodeSet(set)); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -288,10 +285,7 @@ func TestDeleteAll(t *testing.T) {
for _, val := range vals { for _, val := range vals {
trie.Delete([]byte(val.k)) trie.Delete([]byte(val.k))
} }
root, set, err = trie.Commit(false) root, set = trie.Commit(false)
if err != nil {
t.Fatalf("Failed to delete trie %v", err)
}
if root != emptyRoot { if root != emptyRoot {
t.Fatalf("Invalid trie root %v", root) t.Fatalf("Invalid trie root %v", root)
} }