Skip to content

Commit

Permalink
verify that in the case that dst.root != nil that the root node hash …
Browse files Browse the repository at this point in the history
…matches the provided hash and check dst.root != nil first
  • Loading branch information
Manav-Aggarwal committed Oct 28, 2022
1 parent 5a62ae1 commit 55965ae
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions deepsubtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func (dst *DeepSubTree) addPath(pl PathToLeaf, leaf *Node) error {
// and links them together using the populated left and right
// hashes and sets the root to be the node with the given rootHash
func (dst *DeepSubTree) BuildTree(rootHash []byte) error {
if dst.root == nil {
rootNode, rootErr := dst.ndb.GetNode(rootHash)
if rootErr != nil {
return fmt.Errorf("could not set root of deep subtree: %w", rootErr)
}
dst.root = rootNode
} else if !bytes.Equal(dst.root.hash, rootHash) {
return fmt.Errorf(
"deep Subtree rootHash: %s does not match expected rootHash: %s",
dst.root.hash,
rootHash,
)
}
nodes, traverseErr := dst.ndb.nodes()
if traverseErr != nil {
return fmt.Errorf("could not traverse nodedb: %w", traverseErr)
Expand All @@ -118,13 +131,6 @@ func (dst *DeepSubTree) BuildTree(rootHash []byte) error {
pnode.key = pnode.rightNode.getLowestKey()
}
}
if dst.root == nil {
rootNode, rootErr := dst.ndb.GetNode(rootHash)
if rootErr != nil {
return fmt.Errorf("could not set root of deep subtree: %w", rootErr)
}
dst.root = rootNode
}

return nil
}
Expand Down

0 comments on commit 55965ae

Please sign in to comment.