zerocomm: 'Fix' the test

This commit is contained in:
Łukasz Magiera 2020-02-27 01:54:39 +01:00
parent 767392332e
commit 2aeab13aae
3 changed files with 72 additions and 26 deletions

10
lib/nullreader/reader.go Normal file
View File

@ -0,0 +1,10 @@
package nullreader
type Reader struct{}
func (Reader) Read(out []byte) (int, error) {
for i := range out {
out[i] = 0
}
return len(out), nil
}

View File

@ -3,44 +3,87 @@ package zerocomm
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"testing" "testing"
commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/filecoin-project/go-sectorbuilder" "github.com/filecoin-project/go-sectorbuilder"
abi "github.com/filecoin-project/specs-actors/actors/abi" abi "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/filecoin-project/lotus/lib/nullreader"
) )
func TestComms(t *testing.T) { func TestComms(t *testing.T) {
t.Skip("don't have enough ram") // no, but seriously, currently this needs like 3tb of /tmp
var expPieceComms [levels - skip]cid.Cid var expPieceComms [levels - skip]cid.Cid
{ {
l2, err := sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, bytes.NewReader(make([]byte, 127)), 127) l2, err := sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, bytes.NewReader(make([]byte, 127)), 127)
if err != nil { if err != nil {
return t.Fatal(err)
} }
expPieceComms[0] = l2 expPieceComms[0] = l2
} }
for i := 1; i < levels-2; i++ { for i := 1; i < levels-2; i++ {
var err error var err error
sectorbuilder.GenerateUnsealedCID() sz := abi.UnpaddedPieceSize(127 << i)
expPieceComms[i], err = sectorbuilder.GenerateDataCommitment(128<<i, []sectorbuilder.PublicPieceInfo{ fmt.Println(i, sz)
{ r := io.LimitReader(&nullreader.Reader{}, int64(sz))
Size: 127 << (i - 1),
CommP: expPieceComms[i-1], expPieceComms[i], err = sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, r, sz)
},
{
Size: 127 << (i - 1),
CommP: expPieceComms[i-1],
},
})
if err != nil { if err != nil {
panic(err) t.Fatal(err)
} }
} }
for i, comm := range expPieceComms { for i, comm := range expPieceComms {
if string(comm[:]) != string(pieceComms[i][:]) { c, err := commcid.CIDToPieceCommitmentV1(comm)
if err != nil {
t.Fatal(err)
}
if string(c) != string(pieceComms[i][:]) {
t.Errorf("zero commitment %d didn't match", i)
}
}
for _, comm := range expPieceComms { // Could do codegen, but this is good enough
fmt.Printf("%#v,\n", comm)
}
}
func TestCommsSmall(t *testing.T) {
var expPieceComms [8]cid.Cid
lvls := len(expPieceComms) + skip
{
l2, err := sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, bytes.NewReader(make([]byte, 127)), 127)
if err != nil {
t.Fatal(err)
}
expPieceComms[0] = l2
}
for i := 1; i < lvls-2; i++ {
var err error
sz := abi.UnpaddedPieceSize(127 << i)
fmt.Println(i, sz)
r := io.LimitReader(&nullreader.Reader{}, int64(sz))
expPieceComms[i], err = sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, r, sz)
if err != nil {
t.Fatal(err)
}
}
for i, comm := range expPieceComms {
c, err := commcid.CIDToPieceCommitmentV1(comm)
if err != nil {
t.Fatal(err)
}
if string(c) != string(pieceComms[i][:]) {
t.Errorf("zero commitment %d didn't match", i) t.Errorf("zero commitment %d didn't match", i)
} }
} }
@ -51,13 +94,13 @@ func TestComms(t *testing.T) {
} }
func TestForSise(t *testing.T) { func TestForSise(t *testing.T) {
exp, err := sectorbuilder.GeneratePieceCommitment(bytes.NewReader(make([]byte, 1016)), 1016) exp, err := sectorbuilder.GeneratePieceCIDFromFile(abi.RegisteredProof_StackedDRG2KiBPoSt, bytes.NewReader(make([]byte, 1016)), 1016)
if err != nil { if err != nil {
return return
} }
actual := ForSize(1016) actual := ForSize(1016)
if string(exp[:]) != string(actual[:]) { if !exp.Equals(actual) {
t.Errorf("zero commitment didn't match") t.Errorf("zero commitment didn't match")
} }
} }

View File

@ -7,19 +7,12 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/lotus/lib/nullreader"
) )
type nullReader struct{}
func (nullReader) Read(out []byte) (int, error) {
for i := range out {
out[i] = 0
}
return len(out), nil
}
func (m *Sealing) pledgeReader(size abi.UnpaddedPieceSize) io.Reader { func (m *Sealing) pledgeReader(size abi.UnpaddedPieceSize) io.Reader {
return io.LimitReader(&nullReader{}, int64(size)) return io.LimitReader(&nullreader.Reader{}, int64(size))
} }
func (m *Sealing) pledgeSector(ctx context.Context, sectorID abi.SectorNumber, existingPieceSizes []abi.UnpaddedPieceSize, sizes ...abi.UnpaddedPieceSize) ([]Piece, error) { func (m *Sealing) pledgeSector(ctx context.Context, sectorID abi.SectorNumber, existingPieceSizes []abi.UnpaddedPieceSize, sizes ...abi.UnpaddedPieceSize) ([]Piece, error) {