-
Notifications
You must be signed in to change notification settings - Fork 37
/
piece.go
50 lines (38 loc) · 1.07 KB
/
piece.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
package abi
import (
"math/bits"
cid "github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
// UnpaddedPieceSize is the size of a piece, in bytes
type UnpaddedPieceSize uint64
type PaddedPieceSize uint64
func (s UnpaddedPieceSize) Padded() PaddedPieceSize {
return PaddedPieceSize(s + (s / 127))
}
func (s UnpaddedPieceSize) Validate() error {
if s < 127 {
return xerrors.New("minimum piece size is 127 bytes")
}
// is 127 * 2^n
if uint64(s)>>bits.TrailingZeros64(uint64(s)) != 127 {
return xerrors.New("unpadded piece size must be a power of 2 multiple of 127")
}
return nil
}
func (s PaddedPieceSize) Unpadded() UnpaddedPieceSize {
return UnpaddedPieceSize(s - (s / 128))
}
func (s PaddedPieceSize) Validate() error {
if s < 128 {
return xerrors.New("minimum padded piece size is 128 bytes")
}
if bits.OnesCount64(uint64(s)) != 1 {
return xerrors.New("padded piece size must be a power of 2")
}
return nil
}
type PieceInfo struct {
Size PaddedPieceSize // Size in nodes. For BLS12-381 (capacity 254 bits), must be >= 16. (16 * 8 = 128)
PieceCID cid.Cid
}