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: read the correct number of bytes for the piece #17

Merged
merged 2 commits into from
Aug 5, 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
3 changes: 2 additions & 1 deletion commp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
// GeneratePieceCIDFromFile generates an a piece cid from an io.Reader
func GeneratePieceCIDFromFile(proofType abi.RegisteredSealProof, piece io.Reader, pieceSize abi.UnpaddedPieceSize) (cid.Cid, error) {
var cc commphh.Calc
if _, err := io.Copy(&cc, piece); err != nil {
expectedBytes := int64(pieceSize)
if _, err := io.CopyN(&cc, piece, expectedBytes); err != nil {
return cid.Undef, err
}
p, _, err := cc.Digest()
Expand Down
33 changes: 14 additions & 19 deletions commp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package commp_test

import (
"bytes"
"io"
"testing"

"github.com/filecoin-project/go-commp-utils/v2"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
)

func TestZeroPadding(t *testing.T) {
Expand All @@ -17,34 +19,27 @@ func TestZeroPadding(t *testing.T) {
copy(data, content)

pcid, err := commp.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1_1, bytes.NewReader(data), psize)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// value calculated using separate tooling
expCid, err := cid.Decode("baga6ea4seaqozp3abki6vgdf7ztbipcycmxfyt2o64cpuyvdkczsjxsg7bqmioi")
if err != nil {
t.Fatal(err)
}

if pcid != expCid {
t.Fatalf("expected %s, got %s", expCid, pcid)
}
require.NoError(t, err)
require.Equal(t, expCid, pcid)

padded, err := commp.ZeroPadPieceCommitment(pcid, psize, psize*4)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

data2 := make([]byte, psize*4)
copy(data2, content)

padExpCid, err := commp.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1_1, bytes.NewReader(data2), psize*4)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
require.Equal(t, padded, padExpCid)
}

if padded != padExpCid {
t.Fatalf("wrong padding, expected %s, got %s", padExpCid, padded)
}
func TestMismatchedPieceSize(t *testing.T) {
psize := abi.PaddedPieceSize(128).Unpadded()
data := []byte("i am the smallest cat") // less than psize
_, err := commp.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1_1, bytes.NewReader(data), psize)
require.ErrorIs(t, err, io.EOF)
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v2.0.0"
"version": "v2.0.1"
}
5 changes: 2 additions & 3 deletions writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"math/bits"
"runtime"

"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-padreader"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"

commp "github.com/filecoin-project/go-commp-utils/v2"
"github.com/filecoin-project/go-commp-utils/v2/zerocomm"
Expand Down
Loading