Skip to content

Commit

Permalink
trie: update comments + err check for preimages (ethereum#25672)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
darioush authored and blakehhuynh committed Oct 3, 2022
1 parent ff365ef commit cd9c139
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
17 changes: 9 additions & 8 deletions trie/committer.go
Expand Up @@ -44,7 +44,8 @@ func newCommitter(owner common.Hash, collectLeaf bool) *committer {
}
}

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

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

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

// This was not generated - must be a small node stored in the parent.
// In theory, we should check if the node is leaf here (embedded node
// usually is leaf node). But small value(less than 32bytes) is not
// our target(leaves in account trie only).
// usually is leaf node). But small value (less than 32bytes) is not
// our target (leaves in account trie only).
if hash == nil {
return n
}
Expand Down
8 changes: 6 additions & 2 deletions trie/database.go
Expand Up @@ -562,7 +562,9 @@ func (db *Database) Cap(limit common.StorageSize) error {
// If the preimage cache got large enough, push to disk. If it's still small
// leave for later to deduplicate writes.
if db.preimages != nil {
db.preimages.commit(false)
if err := db.preimages.commit(false); err != nil {
return err
}
}
// Keep committing nodes from the flush-list until we're below allowance
oldest := db.oldest
Expand Down Expand Up @@ -640,7 +642,9 @@ func (db *Database) Commit(node common.Hash, report bool, callback func(common.H

// Move all of the accumulated preimages into a write batch
if db.preimages != nil {
db.preimages.commit(true)
if err := db.preimages.commit(true); err != nil {
return err
}
}
// Move the trie itself into the batch, flushing if enough data is accumulated
nodes, storage := len(db.dirties), db.dirtiesSize
Expand Down
8 changes: 4 additions & 4 deletions trie/secure_trie.go
Expand Up @@ -199,10 +199,10 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
return t.preimages.preimage(common.BytesToHash(shaKey))
}

// Commit collects all dirty nodes in the trie and replace them with the
// corresponding node hash. All collected nodes(including dirty leaves if
// Commit collects all dirty nodes in the trie and replaces them with the
// corresponding node hash. All collected nodes (including dirty leaves if
// collectLeaf is true) will be encapsulated into a nodeset for return.
// 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).
// 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
// be created with new root and updated trie database for following usage
Expand All @@ -218,7 +218,7 @@ func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
}
t.secKeyCache = make(map[string][]byte)
}
// Commit the trie to its intermediate node database
// Commit the trie and return its modified nodeset.
return t.trie.Commit(collectLeaf)
}

Expand Down
6 changes: 3 additions & 3 deletions trie/trie.go
Expand Up @@ -582,10 +582,10 @@ func (t *Trie) Hash() common.Hash {
return common.BytesToHash(hash.(hashNode))
}

// Commit collects all dirty nodes in the trie and replace them with the
// corresponding node hash. All collected nodes(including dirty leaves if
// Commit collects all dirty nodes in the trie and replaces them with the
// corresponding node hash. All collected nodes (including dirty leaves if
// collectLeaf is true) will be encapsulated into a nodeset for return.
// 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
// be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
Expand Down

0 comments on commit cd9c139

Please sign in to comment.