Skip to content

Commit

Permalink
refactor: don't return an error from Node._hash
Browse files Browse the repository at this point in the history
There's no need because the hashing can never fail.
  • Loading branch information
elias-orijtech committed May 24, 2023
1 parent 4a49004 commit 89e2880
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
4 changes: 1 addition & 3 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func newImporter(tree *MutableTree, version int64) (*Importer, error) {

// writeNode writes the node content to the storage.
func (i *Importer) writeNode(node *Node) error {
if _, err := node._hash(node.nodeKey.version); err != nil {
return err
}
node._hash(node.nodeKey.version)
if err := node.validate(); err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,10 +1025,7 @@ func (tree *MutableTree) saveNewNodes(version int64) error {
}
}

_, err = node._hash(version)
if err != nil {
return nil, err
}
node._hash(version)
return node.nodeKey.GetKey(), nil
}

Expand Down
13 changes: 5 additions & 8 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ func MakeNode(nk, buf []byte) (*Node, error) {
}
node.value = val
// ensure take the hash for the leaf node
if _, err := node._hash(node.nodeKey.version); err != nil {
return nil, fmt.Errorf("calculating hash error: %v", err)
}

node._hash(node.nodeKey.version)
} else { // Read children.
node.hash, n, err = encoding.DecodeBytes(buf)
if err != nil {
Expand Down Expand Up @@ -417,18 +414,18 @@ func (node *Node) getByIndex(t *ImmutableTree, index int64) (key []byte, value [

// Computes the hash of the node without computing its descendants. Must be
// called on nodes which have descendant node hashes already computed.
func (node *Node) _hash(version int64) ([]byte, error) {
func (node *Node) _hash(version int64) []byte {
if node.hash != nil {
return node.hash, nil
return node.hash
}

h := sha256.New()
if err := node.writeHashBytes(h, version); err != nil {
return nil, err
return nil
}
node.hash = h.Sum(nil)

return node.hash, nil
return node.hash
}

// Hash the node and its descendants recursively. This usually mutates all
Expand Down
5 changes: 1 addition & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ func printNode(ndb *nodeDB, node *Node, indent int) error {
printNode(ndb, rightNode, indent+1) //nolint:errcheck
}

hash, err := node._hash(node.nodeKey.version)
if err != nil {
return err
}
hash := node._hash(node.nodeKey.version)

fmt.Printf("%sh:%X\n", indentPrefix, hash)
if node.isLeaf() {
Expand Down

0 comments on commit 89e2880

Please sign in to comment.