Skip to content

Commit

Permalink
use cid instead of node hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Nov 28, 2022
1 parent 5a2816d commit b56ef92
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
7 changes: 6 additions & 1 deletion share/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,16 @@ func TestGetSharesWithProofsByNamespace(t *testing.T) {
leaves = append(leaves, append(sh[:NamespaceSize], sh...))
}

proofNodes := make([][]byte, 0, len(proof.Nodes))
for _, n := range proof.Nodes {
proofNodes = append(proofNodes, ipld.NamespacedSha256FromCID(n))
}

// construct new proof
inclusionProof := nmt.NewInclusionProof(
proof.Start,
proof.End,
proof.Nodes,
proofNodes,
ipld.NMTIgnoreMaxNamespace)

// verify inclusion
Expand Down
4 changes: 2 additions & 2 deletions share/ipld/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ func GetLeavesByNamespace(
// proof is on the right side, if the nID is less than min namespace of jobNid
if nID.Less(nmt.MinNamespace(jobNid, nID.Size())) {
if collectProofs {
proofs.addRight(jobNid)
proofs.addRight(lnk.Cid)
}
continue
}

// proof is on the left side, if the nID is bigger than max namespace of jobNid
if !nID.LessOrEqual(nmt.MaxNamespace(jobNid, nID.Size())) {
if collectProofs {
proofs.addLeft(jobNid)
proofs.addLeft(lnk.Cid)
}
continue
}
Expand Down
30 changes: 16 additions & 14 deletions share/ipld/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,50 @@ package ipld

import (
"math"

"github.com/ipfs/go-cid"
)

// Proof contains information required for Leaves inclusion validation.
type Proof struct {
Nodes [][]byte
Nodes []cid.Cid
Start, End int
}

// proofCollector collects proof nodes with corresponding indexes and provides Nodes array that
// proofCollector collects proof nodes CIDs provides CIDs array that
// could be used to construct nmt.Proof for shares inclusion validation.
type proofCollector struct {
leftProofs, rightProofs [][]byte
left, right []cid.Cid
}

func newProofCollector(maxShares int) *proofCollector {
// maximum possible amount of required proofs from each side is equal to tree height.
height := int(math.Log2(float64(maxShares)))
return &proofCollector{
leftProofs: make([][]byte, 0, height),
rightProofs: make([][]byte, 0, height),
left: make([]cid.Cid, 0, height),
right: make([]cid.Cid, 0, height),
}
}

func (c *proofCollector) addLeft(node []byte) {
c.leftProofs = append(c.leftProofs, node)
func (c *proofCollector) addLeft(node cid.Cid) {
c.left = append(c.left, node)
}

func (c *proofCollector) addRight(node []byte) {
c.rightProofs = append(c.rightProofs, node)
func (c *proofCollector) addRight(node cid.Cid) {
c.right = append(c.right, node)
}

// Nodes returns nodes collected by proofCollector in the order that nmt.Proof validator will use
// to traverse the tree.
func (c *proofCollector) Nodes() [][]byte {
nodes := make([][]byte, 0, len(c.leftProofs)+len(c.leftProofs))
func (c *proofCollector) Nodes() []cid.Cid {
nodes := make([]cid.Cid, 0, len(c.left)+len(c.left))
// left side will be traversed in bottom-up order
nodes = append(nodes, c.leftProofs...)
nodes = append(nodes, c.left...)

// right side of the tree will be traversed from top to bottom,
// so sort in reversed order
for i := len(c.rightProofs) - 1; i >= 0; i-- {
nodes = append(nodes, c.rightProofs[i])
for i := len(c.right) - 1; i >= 0; i-- {
nodes = append(nodes, c.right[i])
}
return nodes
}

0 comments on commit b56ef92

Please sign in to comment.