Skip to content

Commit 44a7069

Browse files
committed
trie: make commit collapse into hashnode, don't touch dirtyness
1 parent 51d9801 commit 44a7069

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

trie/committer.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,29 @@ func (c *committer) commitNeeded(n node) bool {
7979
return hash == nil || dirty
8080
}
8181

82+
// commit collapses a node down into a hash node and inserts it into the database
83+
func (c *committer) Commit(n node, db *Database) (hashNode, error) {
84+
if db == nil {
85+
return nil, errors.New("no db provided")
86+
}
87+
h, err := c.commit(n, db, true)
88+
if err != nil {
89+
return nil, err
90+
}
91+
hn, ok := h.(hashNode)
92+
if !ok {
93+
panic("commit did not hash down to a hashnode!")
94+
}
95+
return hn, nil
96+
}
97+
8298
// commit collapses a node down into a hash node and inserts it into the database
8399
func (c *committer) commit(n node, db *Database, force bool) (node, error) {
84100
// if this path is clean, use available cached data
85101
hash, dirty := n.cache()
86102
if hash != nil && !dirty {
87103
return hash, nil
88104
}
89-
if db == nil {
90-
return nil, errors.New("no db provided")
91-
}
92105
// Commit children, then parent, and remove remove the dirty flag.
93106
switch cn := n.(type) {
94107
case *shortNode:
@@ -105,7 +118,6 @@ func (c *committer) commit(n node, db *Database, force bool) (node, error) {
105118
collapsed.Key = hexToCompact(cn.Key)
106119
hashedNode := c.store(collapsed, db, force, true)
107120
if hn, ok := hashedNode.(hashNode); ok {
108-
cn.flags.dirty = false
109121
return hn, nil
110122
} else {
111123
return collapsed, nil
@@ -120,7 +132,6 @@ func (c *committer) commit(n node, db *Database, force bool) (node, error) {
120132

121133
hashedNode := c.store(collapsed, db, force, hasVnodes)
122134
if hn, ok := hashedNode.(hashNode); ok {
123-
cn.flags.dirty = false
124135
return hn, nil
125136
} else {
126137
return collapsed, nil

trie/trie.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
438438
h.commitLoop(t.db)
439439
}()
440440
}
441-
_, err = h.commit(t.root, t.db, true)
441+
var newRoot hashNode
442+
newRoot, err = h.Commit(t.root, t.db)
442443
if onleaf != nil {
443444
// The leafch is created in newCommitter if there was an onleaf callback
444445
// provided. The commitLoop only _reads_ from it, and the commit
@@ -450,6 +451,10 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
450451
if err != nil {
451452
return common.Hash{}, err
452453
}
454+
if common.BytesToHash(newRoot) != rootHash{
455+
panic(fmt.Sprintf("Committed root %x != roothash %x", newRoot, rootHash))
456+
}
457+
t.root = newRoot
453458
return rootHash, nil
454459
}
455460

0 commit comments

Comments
 (0)