Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: augment PieceAggregateCommP to return the resulting aggregate size #18

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions commd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type stackFrame struct {
// This function makes no assumptions, other than maximum size, about the pieces that you include.
// To create a correctly formed UnsealedCID (CommD) for a sector using this method, you should first
// ensure that the pieces add up to the required size.
func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.PieceInfo) (cid.Cid, error) {
func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.PieceInfo) (cid.Cid, abi.PaddedPieceSize, error) {
spi, found := abi.SealProofInfos[proofType]
if !found {
return cid.Undef, fmt.Errorf("unknown seal proof type %d", proofType)
return cid.Undef, 0, fmt.Errorf("unknown seal proof type %d", proofType)
}
if len(pieceInfos) == 0 {
return cid.Undef, errors.New("no pieces provided")
return cid.Undef, 0, errors.New("no pieces provided")
}

maxSize := uint64(spi.SectorSize)
Expand All @@ -41,18 +41,18 @@ func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.Pie
// sancheck everything
for i, p := range pieceInfos {
if p.Size < 128 {
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is too small", i, p.Size)
return cid.Undef, 0, fmt.Errorf("invalid Size of PieceInfo %d: value %d is too small", i, p.Size)
}
if uint64(p.Size) > maxSize {
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is larger than sector size of SealProofType %d", i, p.Size, proofType)
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)
}
if bits.OnesCount64(uint64(p.Size)) != 1 {
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is not a power of 2", i, p.Size)
return cid.Undef, 0, fmt.Errorf("invalid Size of PieceInfo %d: value %d is not a power of 2", i, p.Size)
}

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

if stack[0].size > maxSize {
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)
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)
}

return commcid.PieceCommitmentV1ToCID(stack[0].commP)
fincid, err := commcid.PieceCommitmentV1ToCID(stack[0].commP)

return fincid, abi.PaddedPieceSize(stack[0].size), err
}

var s256pool = sync.Pool{New: func() any { return sha256simd.New() }}
Expand Down
6 changes: 5 additions & 1 deletion commd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGenerateUnsealedCID(t *testing.T) {

expCommD := cidMustParse("baga6ea4seaqiw3gbmstmexb7sqwkc5r23o3i7zcyx5kr76pfobpykes3af62kca")

commD, _ := commp.PieceAggregateCommP(
commD, sizeD, _ := commp.PieceAggregateCommP(
abi.RegisteredSealProof_StackedDrg32GiBV1_1, // 32G sector SP
[]abi.PieceInfo{
{PieceCID: cidMustParse("baga6ea4seaqknzm22isnhsxt2s4dnw45kfywmhenngqq3nc7jvecakoca6ksyhy"), Size: 256 << 20}, // https://filfox.info/en/deal/3755444
Expand All @@ -49,6 +49,10 @@ func TestGenerateUnsealedCID(t *testing.T) {
if commD != expCommD {
t.Fatalf("calculated commd for sector f01392893:139074 as %s, expected %s", commD, expCommD)
}

if sizeD != 32<<30 {
t.Fatalf("calculated size for sector f01392893:139074 as %d, expected 32GiB", sizeD)
}
}

// Delete below when this ships in a stable release https://github.com/ipfs/go-cid/pull/139
Expand Down
4 changes: 2 additions & 2 deletions writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ func (w *Writer) Sum() (DataCIDSize, error) {
}
}

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

return DataCIDSize{
PayloadSize: rawLen,
PieceSize: abi.PaddedPieceSize(len(leaves)) * commPBufPad,
PieceSize: sz,
PieceCID: p,
}, nil
}