github.com/sunrise-zone/sunrise-node@v0.13.1-sr2/share/ipld/proof_collector.go (about) 1 package ipld 2 3 import ( 4 "math" 5 6 "github.com/ipfs/go-cid" 7 ) 8 9 // proofCollector collects proof nodes' CIDs for the construction of a shares inclusion validation 10 // nmt.Proof. 11 type proofCollector struct { 12 left, right []cid.Cid 13 } 14 15 func newProofCollector(maxShares int) *proofCollector { 16 // maximum possible amount of required proofs from each side is equal to tree height. 17 height := int(math.Log2(float64(maxShares))) + 1 18 return &proofCollector{ 19 left: make([]cid.Cid, height), 20 right: make([]cid.Cid, height), 21 } 22 } 23 24 func (c *proofCollector) addLeft(cid cid.Cid, depth int) { 25 c.left[depth] = cid 26 } 27 28 func (c *proofCollector) addRight(cid cid.Cid, depth int) { 29 c.right[depth] = cid 30 } 31 32 // Nodes returns nodes collected by proofCollector in the order that nmt.Proof validator will use 33 // to traverse the tree. 34 func (c *proofCollector) Nodes() []cid.Cid { 35 cids := make([]cid.Cid, 0, len(c.left)+len(c.right)) 36 // left side will be traversed in bottom-up order 37 for _, cid := range c.left { 38 if cid.Defined() { 39 cids = append(cids, cid) 40 } 41 } 42 43 // right side of the tree will be traversed from top to bottom, 44 // so sort in reversed order 45 for i := len(c.right) - 1; i >= 0; i-- { 46 cid := c.right[i] 47 if cid.Defined() { 48 cids = append(cids, cid) 49 } 50 } 51 return cids 52 }