Skip to content

Commit 22edaac

Browse files
gzliudanDarioush Jalali
andauthored
trie: update comments + err check for preimages ethereum#25672 (#1117)
This PR includes minor updates to comments in trie/committer that reference insertion to the db, and adds an err != nil check for the return value of preimages.commit. Co-authored-by: Darioush Jalali <[email protected]>
1 parent 79460e4 commit 22edaac

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

trie/committer.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func newCommitter(owner common.Hash, collectLeaf bool) *committer {
4444
}
4545
}
4646

47-
// Commit collapses a node down into a hash node and inserts it into the database
47+
// Commit collapses a node down into a hash node and returns it along with
48+
// the modified nodeset.
4849
func (c *committer) Commit(n node) (hashNode, *NodeSet, error) {
4950
h, err := c.commit(nil, n)
5051
if err != nil {
@@ -53,7 +54,7 @@ func (c *committer) Commit(n node) (hashNode, *NodeSet, error) {
5354
return h.(hashNode), c.nodes, nil
5455
}
5556

56-
// commit collapses a node down into a hash node and inserts it into the database
57+
// commit collapses a node down into a hash node and returns it.
5758
func (c *committer) commit(path []byte, n node) (node, error) {
5859
// if this path is clean, use available cached data
5960
hash, dirty := n.cache()
@@ -75,7 +76,8 @@ func (c *committer) commit(path []byte, n node) (node, error) {
7576
}
7677
collapsed.Val = childV
7778
}
78-
// The key needs to be copied, since we're delivering it to database
79+
// The key needs to be copied, since we're adding it to the
80+
// modified nodeset.
7981
collapsed.Key = hexToCompact(cn.Key)
8082
hashedNode := c.store(path, collapsed)
8183
if hn, ok := hashedNode.(hashNode); ok {
@@ -134,17 +136,16 @@ func (c *committer) commitChildren(path []byte, n *fullNode) ([17]node, error) {
134136
return children, nil
135137
}
136138

137-
// store hashes the node n and if we have a storage layer specified, it writes
138-
// the key/value pair to it and tracks any node->child references as well as any
139-
// node->external trie references.
139+
// store hashes the node n and adds it to the modified nodeset. If leaf collection
140+
// is enabled, leaf nodes will be tracked in the modified nodeset as well.
140141
func (c *committer) store(path []byte, n node) node {
141142
// Larger nodes are replaced by their hash and stored in the database.
142143
var hash, _ = n.cache()
143144

144145
// This was not generated - must be a small node stored in the parent.
145146
// In theory, we should check if the node is leaf here (embedded node
146-
// usually is leaf node). But small value(less than 32bytes) is not
147-
// our target(leaves in account trie only).
147+
// usually is leaf node). But small value (less than 32bytes) is not
148+
// our target (leaves in account trie only).
148149
if hash == nil {
149150
return n
150151
}

trie/database.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ func (db *Database) Cap(limit common.StorageSize) error {
577577
// If the Preimage Cache got large enough, push to disk. If it's still small
578578
// leave for later to deduplicate writes.
579579
if db.preimages != nil {
580-
db.preimages.commit(false)
580+
if err := db.preimages.commit(false); err != nil {
581+
return err
582+
}
581583
}
582584
// Keep committing nodes from the flush-list until we're below allowance
583585
oldest := db.oldest
@@ -655,7 +657,9 @@ func (db *Database) Commit(node common.Hash, report bool) error {
655657

656658
// Move all of the accumulated preimages into a write batch
657659
if db.preimages != nil {
658-
db.preimages.commit(true)
660+
if err := db.preimages.commit(true); err != nil {
661+
return err
662+
}
659663
}
660664
// Move the trie itself into the batch, flushing if enough data is accumulated
661665
nodes, storage := len(db.dirties), db.dirtiesSize

trie/secure_trie.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
199199
return t.preimages.preimage(common.BytesToHash(shaKey))
200200
}
201201

202-
// Commit collects all dirty nodes in the trie and replace them with the
203-
// corresponding node hash. All collected nodes(including dirty leaves if
202+
// Commit collects all dirty nodes in the trie and replaces them with the
203+
// corresponding node hash. All collected nodes (including dirty leaves if
204204
// collectLeaf is true) will be encapsulated into a nodeset for return.
205-
// The returned nodeset can be nil if the trie is clean(nothing to commit).
205+
// The returned nodeset can be nil if the trie is clean (nothing to commit).
206206
// All cached preimages will be also flushed if preimages recording is enabled.
207207
// Once the trie is committed, it's not usable anymore. A new trie must
208208
// be created with new root and updated trie database for following usage
@@ -218,7 +218,7 @@ func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
218218
}
219219
t.secKeyCache = make(map[string][]byte)
220220
}
221-
// Commit the trie to its intermediate node database
221+
// Commit the trie and return its modified nodeset.
222222
return t.trie.Commit(collectLeaf)
223223
}
224224

trie/trie.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,10 @@ func (t *Trie) Hash() common.Hash {
743743
return common.BytesToHash(hash.(hashNode))
744744
}
745745

746-
// Commit collects all dirty nodes in the trie and replace them with the
747-
// corresponding node hash. All collected nodes(including dirty leaves if
746+
// Commit collects all dirty nodes in the trie and replaces them with the
747+
// corresponding node hash. All collected nodes (including dirty leaves if
748748
// collectLeaf is true) will be encapsulated into a nodeset for return.
749-
// The returned nodeset can be nil if the trie is clean(nothing to commit).
749+
// The returned nodeset can be nil if the trie is clean (nothing to commit).
750750
// Once the trie is committed, it's not usable anymore. A new trie must
751751
// be created with new root and updated trie database for following usage
752752
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {

0 commit comments

Comments
 (0)