|
| 1 | +package blob |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/celestiaorg/go-square/merkle" |
| 8 | + "github.com/celestiaorg/go-square/v3/inclusion" |
| 9 | + libshare "github.com/celestiaorg/go-square/v3/share" |
| 10 | +) |
| 11 | + |
| 12 | +// parser helps to collect shares and transform them into a blob. |
| 13 | +// It can handle only one blob at a time. |
| 14 | +type parser struct { |
| 15 | + // index is a position of the blob inside the EDS. |
| 16 | + index int |
| 17 | + // length is an amount of the shares needed to build the blob. |
| 18 | + length int |
| 19 | + // shares is a set of shares to build the blob. |
| 20 | + shares []libshare.Share |
| 21 | + verifyFn func(blob *Blob) bool |
| 22 | +} |
| 23 | + |
| 24 | +// set tries to find the first blob's share by skipping padding shares and |
| 25 | +// sets the metadata of the blob(index and length) |
| 26 | +func (p *parser) set(index int, shrs []libshare.Share) ([]libshare.Share, error) { |
| 27 | + if len(shrs) == 0 { |
| 28 | + return nil, errEmptyShares |
| 29 | + } |
| 30 | + |
| 31 | + shrs, err := p.skipPadding(shrs) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + if len(shrs) == 0 { |
| 37 | + return nil, errEmptyShares |
| 38 | + } |
| 39 | + |
| 40 | + // `+=` as index could be updated in `skipPadding` |
| 41 | + p.index += index |
| 42 | + length := shrs[0].SequenceLen() |
| 43 | + containsSigner := shrs[0].Version() == libshare.ShareVersionOne |
| 44 | + p.length = libshare.SparseSharesNeeded(length, containsSigner) |
| 45 | + return shrs, nil |
| 46 | +} |
| 47 | + |
| 48 | +// addShares sets shares until the blob is completed and extra remaining shares back. |
| 49 | +// It assumes that the remaining shares required for blob completeness are correct and |
| 50 | +// do not include padding shares. |
| 51 | +func (p *parser) addShares(shares []libshare.Share) (shrs []libshare.Share, isComplete bool) { |
| 52 | + index := -1 |
| 53 | + for i, sh := range shares { |
| 54 | + p.shares = append(p.shares, sh) |
| 55 | + if len(p.shares) == p.length { |
| 56 | + index = i |
| 57 | + isComplete = true |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if index == -1 { |
| 63 | + return shrs, isComplete |
| 64 | + } |
| 65 | + |
| 66 | + if index+1 >= len(shares) { |
| 67 | + return shrs, true |
| 68 | + } |
| 69 | + return shares[index+1:], true |
| 70 | +} |
| 71 | + |
| 72 | +// parse ensures that correct amount of shares was collected and create a blob from the existing |
| 73 | +// shares. |
| 74 | +func (p *parser) parse() (*Blob, error) { |
| 75 | + if p.length != len(p.shares) { |
| 76 | + return nil, fmt.Errorf("invalid shares amount. want:%d, have:%d", p.length, len(p.shares)) |
| 77 | + } |
| 78 | + |
| 79 | + blobs, err := libshare.ParseBlobs(p.shares) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + if len(blobs) != 1 { |
| 85 | + return nil, errors.New("unexpected amount of blobs during parsing") |
| 86 | + } |
| 87 | + |
| 88 | + com, err := inclusion.CreateCommitment(blobs[0], merkle.HashFromByteSlices, subtreeRootThreshold) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + blob := &Blob{Blob: blobs[0], Commitment: com, index: p.index} |
| 94 | + return blob, nil |
| 95 | +} |
| 96 | + |
| 97 | +// skipPadding iterates through the shares until non-padding share will be found. It guarantees that |
| 98 | +// the returned set of shares will start with non-padding share(or empty set of shares). |
| 99 | +func (p *parser) skipPadding(shares []libshare.Share) ([]libshare.Share, error) { |
| 100 | + if len(shares) == 0 { |
| 101 | + return nil, errEmptyShares |
| 102 | + } |
| 103 | + |
| 104 | + offset := 0 |
| 105 | + for _, sh := range shares { |
| 106 | + if !sh.IsPadding() { |
| 107 | + break |
| 108 | + } |
| 109 | + offset++ |
| 110 | + } |
| 111 | + // set start index |
| 112 | + p.index = offset |
| 113 | + if len(shares) > offset { |
| 114 | + return shares[offset:], nil |
| 115 | + } |
| 116 | + return nil, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (p *parser) verify(blob *Blob) bool { |
| 120 | + if p.verifyFn == nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + return p.verifyFn(blob) |
| 124 | +} |
| 125 | + |
| 126 | +func (p *parser) isEmpty() bool { |
| 127 | + return p.index == 0 && p.length == 0 && len(p.shares) == 0 |
| 128 | +} |
| 129 | + |
| 130 | +// reset cleans up parser, so it can be re-used within the same verify functionality. |
| 131 | +func (p *parser) reset() { |
| 132 | + p.index = 0 |
| 133 | + p.length = 0 |
| 134 | + p.shares = nil |
| 135 | +} |
0 commit comments