Skip to content

Commit 6119fcb

Browse files
authored
fix: augment PieceAggregateCommP to return the resulting aggregate size (#18)
1 parent 11777d3 commit 6119fcb

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Diff for: commd.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ type stackFrame struct {
2525
// This function makes no assumptions, other than maximum size, about the pieces that you include.
2626
// To create a correctly formed UnsealedCID (CommD) for a sector using this method, you should first
2727
// ensure that the pieces add up to the required size.
28-
func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.PieceInfo) (cid.Cid, error) {
28+
func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.PieceInfo) (cid.Cid, abi.PaddedPieceSize, error) {
2929
spi, found := abi.SealProofInfos[proofType]
3030
if !found {
31-
return cid.Undef, fmt.Errorf("unknown seal proof type %d", proofType)
31+
return cid.Undef, 0, fmt.Errorf("unknown seal proof type %d", proofType)
3232
}
3333
if len(pieceInfos) == 0 {
34-
return cid.Undef, errors.New("no pieces provided")
34+
return cid.Undef, 0, errors.New("no pieces provided")
3535
}
3636

3737
maxSize := uint64(spi.SectorSize)
@@ -41,18 +41,18 @@ func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.Pie
4141
// sancheck everything
4242
for i, p := range pieceInfos {
4343
if p.Size < 128 {
44-
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is too small", i, p.Size)
44+
return cid.Undef, 0, fmt.Errorf("invalid Size of PieceInfo %d: value %d is too small", i, p.Size)
4545
}
4646
if uint64(p.Size) > maxSize {
47-
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is larger than sector size of SealProofType %d", i, p.Size, proofType)
47+
return cid.Undef, 0, fmt.Errorf("invalid Size of PieceInfo %d: value %d is larger than sector size of SealProofType %d", i, p.Size, proofType)
4848
}
4949
if bits.OnesCount64(uint64(p.Size)) != 1 {
50-
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is not a power of 2", i, p.Size)
50+
return cid.Undef, 0, fmt.Errorf("invalid Size of PieceInfo %d: value %d is not a power of 2", i, p.Size)
5151
}
5252

5353
cp, err := commcid.CIDToPieceCommitmentV1(p.PieceCID)
5454
if err != nil {
55-
return cid.Undef, fmt.Errorf("invalid PieceCid for PieceInfo %d: %w", i, err)
55+
return cid.Undef, 0, fmt.Errorf("invalid PieceCid for PieceInfo %d: %w", i, err)
5656
}
5757
todo[i] = stackFrame{size: uint64(p.Size), commP: cp}
5858
}
@@ -106,10 +106,12 @@ func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.Pie
106106
}
107107

108108
if stack[0].size > maxSize {
109-
return cid.Undef, fmt.Errorf("provided pieces sum up to %d bytes, which is larger than sector size of SealProofType %d", stack[0].size, proofType)
109+
return cid.Undef, 0, fmt.Errorf("provided pieces sum up to %d bytes, which is larger than sector size of SealProofType %d", stack[0].size, proofType)
110110
}
111111

112-
return commcid.PieceCommitmentV1ToCID(stack[0].commP)
112+
fincid, err := commcid.PieceCommitmentV1ToCID(stack[0].commP)
113+
114+
return fincid, abi.PaddedPieceSize(stack[0].size), err
113115
}
114116

115117
var s256pool = sync.Pool{New: func() any { return sha256simd.New() }}

Diff for: commd_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestGenerateUnsealedCID(t *testing.T) {
2626

2727
expCommD := cidMustParse("baga6ea4seaqiw3gbmstmexb7sqwkc5r23o3i7zcyx5kr76pfobpykes3af62kca")
2828

29-
commD, _ := commp.PieceAggregateCommP(
29+
commD, sizeD, _ := commp.PieceAggregateCommP(
3030
abi.RegisteredSealProof_StackedDrg32GiBV1_1, // 32G sector SP
3131
[]abi.PieceInfo{
3232
{PieceCID: cidMustParse("baga6ea4seaqknzm22isnhsxt2s4dnw45kfywmhenngqq3nc7jvecakoca6ksyhy"), Size: 256 << 20}, // https://filfox.info/en/deal/3755444
@@ -49,6 +49,10 @@ func TestGenerateUnsealedCID(t *testing.T) {
4949
if commD != expCommD {
5050
t.Fatalf("calculated commd for sector f01392893:139074 as %s, expected %s", commD, expCommD)
5151
}
52+
53+
if sizeD != 32<<30 {
54+
t.Fatalf("calculated size for sector f01392893:139074 as %d, expected 32GiB", sizeD)
55+
}
5256
}
5357

5458
// Delete below when this ships in a stable release https://github.com/ipfs/go-cid/pull/139

Diff for: writer/writer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ func (w *Writer) Sum() (DataCIDSize, error) {
143143
}
144144
}
145145

146-
p, err := commp.PieceAggregateCommP(abi.RegisteredSealProof_StackedDrg64GiBV1, pieces)
146+
p, sz, err := commp.PieceAggregateCommP(abi.RegisteredSealProof_StackedDrg64GiBV1, pieces)
147147
if err != nil {
148148
return DataCIDSize{}, xerrors.Errorf("generating unsealed CID: %w", err)
149149
}
150150

151151
return DataCIDSize{
152152
PayloadSize: rawLen,
153-
PieceSize: abi.PaddedPieceSize(len(leaves)) * commPBufPad,
153+
PieceSize: sz,
154154
PieceCID: p,
155155
}, nil
156156
}

0 commit comments

Comments
 (0)