-
Notifications
You must be signed in to change notification settings - Fork 940
/
proof.go
58 lines (49 loc) · 1.38 KB
/
proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ipld
import (
"math"
"github.com/ipfs/go-cid"
)
// Proof contains information required for Leaves inclusion validation.
type Proof struct {
Nodes []cid.Cid
Start, End int
}
// proofCollector collects proof nodes' CIDs for the construction of a shares inclusion validation
// nmt.Proof.
type proofCollector struct {
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))) + 1
return &proofCollector{
left: make([]cid.Cid, height),
right: make([]cid.Cid, height),
}
}
func (c *proofCollector) addLeft(cid cid.Cid, depth int) {
c.left[depth] = cid
}
func (c *proofCollector) addRight(cid cid.Cid, depth int) {
c.right[depth] = cid
}
// Nodes returns nodes collected by proofCollector in the order that nmt.Proof validator will use
// to traverse the tree.
func (c *proofCollector) Nodes() []cid.Cid {
cids := make([]cid.Cid, 0, len(c.left)+len(c.right))
// left side will be traversed in bottom-up order
for _, cid := range c.left {
if cid.Defined() {
cids = append(cids, cid)
}
}
// right side of the tree will be traversed from top to bottom,
// so sort in reversed order
for i := len(c.right) - 1; i >= 0; i-- {
cid := c.right[i]
if cid.Defined() {
cids = append(cids, cid)
}
}
return cids
}