Skip to content

Commit

Permalink
tree: extend method to also return value
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Oct 13, 2023
1 parent 400b253 commit b366843
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func groupKeys(keys keylist, depth byte) []keylist {
return groups
}

func (n *InternalNode) LoadKeyForProof(key []byte, resolver NodeResolverFn) error {
func (n *InternalNode) GetAndLoadForProof(key []byte, resolver NodeResolverFn) ([]byte, error) {
// Each internal node that is part of the proof needs to load all it's
// children since it's needed for proof openings.
childrenKey := make([]byte, n.depth+1)
Expand All @@ -821,21 +821,23 @@ func (n *InternalNode) LoadKeyForProof(key []byte, resolver NodeResolverFn) erro
childrenKey[n.depth] = byte(i)
serialized, err := resolver(childrenKey)
if err != nil {
return err
return nil, fmt.Errorf("resolving node: %s", err)
}
c, err := ParseNode(serialized, n.depth+1)
if err != nil {
return err
return nil, fmt.Errorf("parsing resolved node: %s", err)
}
n.children[i] = c
}
if child, ok := n.children[i].(*InternalNode); ok {
if err := child.LoadKeyForProof(childrenKey, resolver); err != nil {
return err
}
}
}
return nil
switch child := n.children[key[n.depth+1]].(type) {
case *InternalNode: // If next node is an internal node, recurse.
return child.GetAndLoadForProof(childrenKey, resolver)
case *LeafNode: // If next node is a leaf node, return the value.
return child.Get(key, nil)
default:
panic("invalid node type")
}
}

func (n *InternalNode) GetProofItems(keys keylist, resolver NodeResolverFn) (*ProofElements, []byte, [][]byte, error) {
Expand Down

0 comments on commit b366843

Please sign in to comment.