lotus/extern/sector-storage/ffiwrapper/sealer_test.go

623 lines
14 KiB
Go
Raw Normal View History

2020-03-26 02:50:56 +00:00
package ffiwrapper
import (
2020-05-14 15:35:38 +00:00
"bytes"
2020-03-26 02:50:56 +00:00
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
2020-06-09 10:24:03 +00:00
"path/filepath"
2020-03-26 02:50:56 +00:00
"runtime"
2020-06-09 10:24:03 +00:00
"strings"
2020-03-26 02:50:56 +00:00
"sync"
"testing"
"time"
2020-10-08 01:09:33 +00:00
proof2 "github.com/filecoin-project/specs-actors/v2/actors/runtime/proof"
2020-09-07 06:08:53 +00:00
2020-08-16 10:40:35 +00:00
"github.com/ipfs/go-cid"
2020-03-26 02:50:56 +00:00
logging "github.com/ipfs/go-log"
2020-05-29 15:21:10 +00:00
"github.com/stretchr/testify/require"
2020-03-26 02:50:56 +00:00
"golang.org/x/xerrors"
paramfetch "github.com/filecoin-project/go-paramfetch"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-05-28 17:15:15 +00:00
"github.com/filecoin-project/specs-storage/storage"
2020-05-18 23:03:42 +00:00
2020-08-16 10:40:35 +00:00
ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper/basicfs"
2020-09-14 18:28:47 +00:00
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
2020-03-26 02:50:56 +00:00
)
func init() {
2020-05-18 23:03:42 +00:00
logging.SetLogLevel("*", "DEBUG") //nolint: errcheck
2020-03-26 02:50:56 +00:00
}
2020-06-15 12:32:17 +00:00
var sealProofType = abi.RegisteredSealProof_StackedDrg2KiBV1
2020-05-29 15:21:10 +00:00
var sectorSize, _ = sealProofType.SectorSize()
2020-05-18 23:03:42 +00:00
var sealRand = abi.SealRandomness{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2}
2020-03-26 02:50:56 +00:00
type seal struct {
2020-11-05 06:34:24 +00:00
ref storage.SectorRef
2020-03-26 02:50:56 +00:00
cids storage.SectorCids
pi abi.PieceInfo
ticket abi.SealRandomness
}
2020-05-18 23:03:42 +00:00
func data(sn abi.SectorNumber, dlen abi.UnpaddedPieceSize) io.Reader {
return io.MultiReader(
io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(123)),
io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(dlen-123)),
)
2020-05-18 23:03:42 +00:00
}
2020-11-05 06:34:24 +00:00
func (s *seal) precommit(t *testing.T, sb *Sealer, id storage.SectorRef, done func()) {
2020-03-26 02:50:56 +00:00
defer done()
dlen := abi.PaddedPieceSize(sectorSize).Unpadded()
var err error
2020-11-05 06:34:24 +00:00
r := data(id.ID.Number, dlen)
2020-03-26 02:50:56 +00:00
s.pi, err = sb.AddPiece(context.TODO(), id, []abi.UnpaddedPieceSize{}, dlen, r)
if err != nil {
t.Fatalf("%+v", err)
}
2020-05-18 23:03:42 +00:00
s.ticket = sealRand
2020-03-26 02:50:56 +00:00
p1, err := sb.SealPreCommit1(context.TODO(), id, s.ticket, []abi.PieceInfo{s.pi})
if err != nil {
t.Fatalf("%+v", err)
}
cids, err := sb.SealPreCommit2(context.TODO(), id, p1)
if err != nil {
t.Fatalf("%+v", err)
}
s.cids = cids
}
2020-03-26 19:34:38 +00:00
func (s *seal) commit(t *testing.T, sb *Sealer, done func()) {
2020-03-26 02:50:56 +00:00
defer done()
seed := abi.InteractiveSealRandomness{0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 45, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9}
2020-11-05 06:34:24 +00:00
pc1, err := sb.SealCommit1(context.TODO(), s.ref, s.ticket, seed, []abi.PieceInfo{s.pi}, s.cids)
2020-03-26 02:50:56 +00:00
if err != nil {
t.Fatalf("%+v", err)
}
2020-11-05 06:34:24 +00:00
proof, err := sb.SealCommit2(context.TODO(), s.ref, pc1)
2020-03-26 02:50:56 +00:00
if err != nil {
t.Fatalf("%+v", err)
}
2020-10-08 01:09:33 +00:00
ok, err := ProofVerifier.VerifySeal(proof2.SealVerifyInfo{
2020-11-05 06:34:24 +00:00
SectorID: s.ref.ID,
2020-05-22 01:19:46 +00:00
SealedCID: s.cids.Sealed,
2020-11-05 06:34:24 +00:00
SealProof: s.ref.ProofType,
2020-05-22 01:19:46 +00:00
Proof: proof,
2020-03-26 02:50:56 +00:00
Randomness: s.ticket,
InteractiveRandomness: seed,
UnsealedCID: s.cids.Unsealed,
})
if err != nil {
t.Fatalf("%+v", err)
}
if !ok {
t.Fatal("proof failed to validate")
}
}
2020-11-05 06:34:24 +00:00
func (s *seal) unseal(t *testing.T, sb *Sealer, sp *basicfs.Provider, si storage.SectorRef, done func()) {
2020-05-18 23:03:42 +00:00
defer done()
var b bytes.Buffer
2020-07-30 20:38:05 +00:00
_, err := sb.ReadPiece(context.TODO(), &b, si, 0, 1016)
2020-05-18 23:03:42 +00:00
if err != nil {
t.Fatal(err)
}
2020-11-05 06:34:24 +00:00
expect, _ := ioutil.ReadAll(data(si.ID.Number, 1016))
2020-05-18 23:03:42 +00:00
if !bytes.Equal(b.Bytes(), expect) {
t.Fatal("read wrong bytes")
}
2020-09-06 16:54:00 +00:00
p, sd, err := sp.AcquireSector(context.TODO(), si, storiface.FTUnsealed, storiface.FTNone, storiface.PathStorage)
2020-05-18 23:03:42 +00:00
if err != nil {
t.Fatal(err)
}
if err := os.Remove(p.Unsealed); err != nil {
t.Fatal(err)
}
sd()
2020-07-30 20:38:05 +00:00
_, err = sb.ReadPiece(context.TODO(), &b, si, 0, 1016)
2020-05-18 23:03:42 +00:00
if err == nil {
t.Fatal("HOW?!")
}
log.Info("this is what we expect: ", err)
if err := sb.UnsealPiece(context.TODO(), si, 0, 1016, sealRand, s.cids.Unsealed); err != nil {
t.Fatal(err)
}
b.Reset()
2020-07-30 20:38:05 +00:00
_, err = sb.ReadPiece(context.TODO(), &b, si, 0, 1016)
2020-05-18 23:03:42 +00:00
if err != nil {
t.Fatal(err)
}
2020-11-05 06:34:24 +00:00
expect, _ = ioutil.ReadAll(data(si.ID.Number, 1016))
2020-05-29 15:21:10 +00:00
require.Equal(t, expect, b.Bytes())
2020-05-18 23:03:42 +00:00
b.Reset()
2020-07-30 20:38:05 +00:00
have, err := sb.ReadPiece(context.TODO(), &b, si, 0, 2032)
2020-05-18 23:03:42 +00:00
if err != nil {
t.Fatal(err)
}
2020-07-30 20:38:05 +00:00
if have {
t.Errorf("didn't expect to read things")
}
if b.Len() != 0 {
t.Fatal("read bytes")
2020-05-18 23:03:42 +00:00
}
}
func post(t *testing.T, sealer *Sealer, skipped []abi.SectorID, seals ...seal) {
randomness := abi.PoStRandomness{0, 9, 2, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 45, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 7}
2020-03-26 02:50:56 +00:00
2020-10-08 01:09:33 +00:00
sis := make([]proof2.SectorInfo, len(seals))
2020-03-26 02:50:56 +00:00
for i, s := range seals {
2020-10-08 01:09:33 +00:00
sis[i] = proof2.SectorInfo{
2020-11-05 06:34:24 +00:00
SealProof: s.ref.ProofType,
SectorNumber: s.ref.ID.Number,
2020-09-10 20:07:20 +00:00
SealedCID: s.cids.Sealed,
2020-03-26 02:50:56 +00:00
}
}
2020-11-05 06:34:24 +00:00
proofs, skp, err := sealer.GenerateWindowPoSt(context.TODO(), seals[0].ref.ID.Miner, sis, randomness)
if len(skipped) > 0 {
require.Error(t, err)
require.EqualValues(t, skipped, skp)
return
2020-03-26 02:50:56 +00:00
}
if err != nil {
t.Fatalf("%+v", err)
}
2020-10-08 01:09:33 +00:00
ok, err := ProofVerifier.VerifyWindowPoSt(context.TODO(), proof2.WindowPoStVerifyInfo{
2020-09-10 20:07:20 +00:00
Randomness: randomness,
Proofs: proofs,
ChallengedSectors: sis,
2020-11-05 06:34:24 +00:00
Prover: seals[0].ref.ID.Miner,
2020-03-26 02:50:56 +00:00
})
if err != nil {
t.Fatalf("%+v", err)
}
if !ok {
t.Fatal("bad post")
}
}
2020-11-05 06:34:24 +00:00
func corrupt(t *testing.T, sealer *Sealer, id storage.SectorRef) {
2020-09-14 18:28:47 +00:00
paths, done, err := sealer.sectors.AcquireSector(context.Background(), id, storiface.FTSealed, 0, storiface.PathStorage)
require.NoError(t, err)
defer done()
log.Infof("corrupt %s", paths.Sealed)
f, err := os.OpenFile(paths.Sealed, os.O_RDWR, 0664)
require.NoError(t, err)
_, err = f.WriteAt(bytes.Repeat([]byte{'d'}, 2048), 0)
require.NoError(t, err)
require.NoError(t, f.Close())
2020-03-26 02:50:56 +00:00
}
func getGrothParamFileAndVerifyingKeys(s abi.SectorSize) {
2020-08-16 11:28:39 +00:00
dat, err := ioutil.ReadFile("../../../build/proof-params/parameters.json")
2020-05-01 16:18:00 +00:00
if err != nil {
panic(err)
}
2020-03-26 02:50:56 +00:00
2020-08-16 10:14:24 +00:00
err = paramfetch.GetParams(context.TODO(), dat, uint64(s))
2020-03-26 02:50:56 +00:00
if err != nil {
panic(xerrors.Errorf("failed to acquire Groth parameters for 2KiB sectors: %w", err))
}
}
// TestDownloadParams exists only so that developers and CI can pre-download
// Groth parameters and verifying keys before running the tests which rely on
// those parameters and keys. To do this, run the following command:
//
// go test -run=^TestDownloadParams
//
func TestDownloadParams(t *testing.T) {
2020-06-09 10:24:03 +00:00
defer requireFDsClosed(t, openFDs(t))
2020-03-26 02:50:56 +00:00
getGrothParamFileAndVerifyingKeys(sectorSize)
}
func TestSealAndVerify(t *testing.T) {
2020-09-17 10:22:56 +00:00
if testing.Short() {
t.Skip("skipping test in short mode")
}
2020-06-09 10:24:03 +00:00
defer requireFDsClosed(t, openFDs(t))
2020-03-26 02:50:56 +00:00
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
t.Skip("this is slow")
}
_ = os.Setenv("RUST_LOG", "info")
getGrothParamFileAndVerifyingKeys(sectorSize)
cdir, err := ioutil.TempDir("", "sbtest-c-")
if err != nil {
t.Fatal(err)
}
miner := abi.ActorID(123)
sp := &basicfs.Provider{
Root: cdir,
}
2020-11-05 06:34:24 +00:00
sb, err := New(sp)
2020-03-26 02:50:56 +00:00
if err != nil {
t.Fatalf("%+v", err)
}
cleanup := func() {
if t.Failed() {
fmt.Printf("not removing %s\n", cdir)
return
}
if err := os.RemoveAll(cdir); err != nil {
t.Error(err)
}
}
defer cleanup()
2020-11-05 06:34:24 +00:00
si := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1},
ProofType: sealProofType,
}
2020-03-26 02:50:56 +00:00
2020-11-05 06:34:24 +00:00
s := seal{ref: si}
2020-03-26 02:50:56 +00:00
start := time.Now()
s.precommit(t, sb, si, func() {})
precommit := time.Now()
s.commit(t, sb, func() {})
commit := time.Now()
post(t, sb, nil, s)
2020-03-26 02:50:56 +00:00
epost := time.Now()
post(t, sb, nil, s)
2020-03-26 02:50:56 +00:00
if err := sb.FinalizeSector(context.TODO(), si, nil); err != nil {
2020-03-26 02:50:56 +00:00
t.Fatalf("%+v", err)
}
2020-05-18 23:03:42 +00:00
s.unseal(t, sb, sp, si, func() {})
2020-03-26 02:50:56 +00:00
fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String())
fmt.Printf("Commit: %s\n", commit.Sub(precommit).String())
fmt.Printf("EPoSt: %s\n", epost.Sub(commit).String())
2020-03-26 02:50:56 +00:00
}
func TestSealPoStNoCommit(t *testing.T) {
2020-09-17 10:22:56 +00:00
if testing.Short() {
t.Skip("skipping test in short mode")
}
2020-06-09 10:24:03 +00:00
defer requireFDsClosed(t, openFDs(t))
2020-03-26 02:50:56 +00:00
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
t.Skip("this is slow")
}
_ = os.Setenv("RUST_LOG", "info")
getGrothParamFileAndVerifyingKeys(sectorSize)
dir, err := ioutil.TempDir("", "sbtest")
if err != nil {
t.Fatal(err)
}
miner := abi.ActorID(123)
sp := &basicfs.Provider{
Root: dir,
}
2020-11-05 06:34:24 +00:00
sb, err := New(sp)
2020-03-26 02:50:56 +00:00
if err != nil {
t.Fatalf("%+v", err)
}
cleanup := func() {
if t.Failed() {
fmt.Printf("not removing %s\n", dir)
return
}
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}
defer cleanup()
2020-11-05 06:34:24 +00:00
si := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1},
ProofType: sealProofType,
}
2020-03-26 02:50:56 +00:00
2020-11-05 06:34:24 +00:00
s := seal{ref: si}
2020-03-26 02:50:56 +00:00
start := time.Now()
s.precommit(t, sb, si, func() {})
precommit := time.Now()
if err := sb.FinalizeSector(context.TODO(), si, nil); err != nil {
2020-03-26 02:50:56 +00:00
t.Fatal(err)
}
post(t, sb, nil, s)
2020-03-26 02:50:56 +00:00
epost := time.Now()
fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String())
fmt.Printf("EPoSt: %s\n", epost.Sub(precommit).String())
2020-03-26 02:50:56 +00:00
}
func TestSealAndVerify3(t *testing.T) {
2020-09-17 10:22:56 +00:00
if testing.Short() {
t.Skip("skipping test in short mode")
}
2020-06-09 10:24:03 +00:00
defer requireFDsClosed(t, openFDs(t))
2020-03-26 02:50:56 +00:00
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
t.Skip("this is slow")
}
_ = os.Setenv("RUST_LOG", "trace")
getGrothParamFileAndVerifyingKeys(sectorSize)
dir, err := ioutil.TempDir("", "sbtest")
if err != nil {
t.Fatal(err)
}
miner := abi.ActorID(123)
sp := &basicfs.Provider{
Root: dir,
}
2020-11-05 06:34:24 +00:00
sb, err := New(sp)
2020-03-26 02:50:56 +00:00
if err != nil {
t.Fatalf("%+v", err)
}
cleanup := func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
}
defer cleanup()
var wg sync.WaitGroup
2020-11-05 06:34:24 +00:00
si1 := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 1},
ProofType: sealProofType,
}
si2 := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 2},
ProofType: sealProofType,
}
si3 := storage.SectorRef{
ID: abi.SectorID{Miner: miner, Number: 3},
ProofType: sealProofType,
}
2020-03-26 02:50:56 +00:00
2020-11-05 06:34:24 +00:00
s1 := seal{ref: si1}
s2 := seal{ref: si2}
s3 := seal{ref: si3}
2020-03-26 02:50:56 +00:00
wg.Add(3)
2020-03-26 02:50:56 +00:00
go s1.precommit(t, sb, si1, wg.Done) //nolint: staticcheck
time.Sleep(100 * time.Millisecond)
go s2.precommit(t, sb, si2, wg.Done) //nolint: staticcheck
time.Sleep(100 * time.Millisecond)
go s3.precommit(t, sb, si3, wg.Done) //nolint: staticcheck
2020-03-26 02:50:56 +00:00
wg.Wait()
wg.Add(3)
2020-03-26 02:50:56 +00:00
go s1.commit(t, sb, wg.Done) //nolint: staticcheck
go s2.commit(t, sb, wg.Done) //nolint: staticcheck
go s3.commit(t, sb, wg.Done) //nolint: staticcheck
2020-03-26 02:50:56 +00:00
wg.Wait()
post(t, sb, nil, s1, s2, s3)
corrupt(t, sb, si1)
corrupt(t, sb, si2)
2020-11-05 06:34:24 +00:00
post(t, sb, []abi.SectorID{si1.ID, si2.ID}, s1, s2, s3)
2020-03-26 02:50:56 +00:00
}
2020-05-28 17:15:15 +00:00
func BenchmarkWriteWithAlignment(b *testing.B) {
bt := abi.UnpaddedPieceSize(2 * 127 * 1024 * 1024)
b.SetBytes(int64(bt))
for i := 0; i < b.N; i++ {
b.StopTimer()
rf, w, _ := ToReadableFile(bytes.NewReader(bytes.Repeat([]byte{0xff, 0}, int(bt/2))), int64(bt))
tf, _ := ioutil.TempFile("/tmp/", "scrb-")
b.StartTimer()
2020-08-16 10:40:35 +00:00
ffi.WriteWithAlignment(abi.RegisteredSealProof_StackedDrg2KiBV1, rf, bt, tf, nil) // nolint:errcheck
_ = w()
2020-05-28 17:15:15 +00:00
}
}
2020-06-09 10:24:03 +00:00
func openFDs(t *testing.T) int {
dent, err := ioutil.ReadDir("/proc/self/fd")
require.NoError(t, err)
var skip int
for _, info := range dent {
l, err := os.Readlink(filepath.Join("/proc/self/fd", info.Name()))
if err != nil {
continue
}
if strings.HasPrefix(l, "/dev/nvidia") {
skip++
}
2020-06-30 17:38:41 +00:00
if strings.HasPrefix(l, "/var/tmp/filecoin-proof-parameters/") {
skip++
}
2020-06-09 10:24:03 +00:00
}
return len(dent) - skip
}
func requireFDsClosed(t *testing.T, start int) {
openNow := openFDs(t)
if start != openNow {
dent, err := ioutil.ReadDir("/proc/self/fd")
require.NoError(t, err)
for _, info := range dent {
l, err := os.Readlink(filepath.Join("/proc/self/fd", info.Name()))
if err != nil {
fmt.Printf("FD err %s\n", err)
continue
}
fmt.Printf("FD %s -> %s\n", info.Name(), l)
}
}
log.Infow("open FDs", "start", start, "now", openNow)
require.Equal(t, start, openNow, "FDs shouldn't leak")
}
func TestGenerateUnsealedCID(t *testing.T) {
pt := abi.RegisteredSealProof_StackedDrg2KiBV1
ups := int(abi.PaddedPieceSize(2048).Unpadded())
commP := func(b []byte) cid.Cid {
pf, werr, err := ToReadableFile(bytes.NewReader(b), int64(len(b)))
require.NoError(t, err)
c, err := ffi.GeneratePieceCIDFromFile(pt, pf, abi.UnpaddedPieceSize(len(b)))
require.NoError(t, err)
require.NoError(t, werr())
return c
}
testCommEq := func(name string, in [][]byte, expect [][]byte) {
t.Run(name, func(t *testing.T) {
upi := make([]abi.PieceInfo, len(in))
for i, b := range in {
upi[i] = abi.PieceInfo{
Size: abi.UnpaddedPieceSize(len(b)).Padded(),
PieceCID: commP(b),
}
}
sectorPi := []abi.PieceInfo{
{
Size: 2048,
PieceCID: commP(bytes.Join(expect, nil)),
},
}
expectCid, err := GenerateUnsealedCID(pt, sectorPi)
require.NoError(t, err)
actualCid, err := GenerateUnsealedCID(pt, upi)
require.NoError(t, err)
require.Equal(t, expectCid, actualCid)
})
}
barr := func(b byte, den int) []byte {
return bytes.Repeat([]byte{b}, ups/den)
}
// 0000
testCommEq("zero",
nil,
[][]byte{barr(0, 1)},
)
// 1111
testCommEq("one",
[][]byte{barr(1, 1)},
[][]byte{barr(1, 1)},
)
// 11 00
testCommEq("one|2",
[][]byte{barr(1, 2)},
[][]byte{barr(1, 2), barr(0, 2)},
)
// 1 0 00
testCommEq("one|4",
[][]byte{barr(1, 4)},
[][]byte{barr(1, 4), barr(0, 4), barr(0, 2)},
)
// 11 2 0
testCommEq("one|2-two|4",
[][]byte{barr(1, 2), barr(2, 4)},
[][]byte{barr(1, 2), barr(2, 4), barr(0, 4)},
)
// 1 0 22
testCommEq("one|4-two|2",
[][]byte{barr(1, 4), barr(2, 2)},
[][]byte{barr(1, 4), barr(0, 4), barr(2, 2)},
)
// 1 0 22 0000
testCommEq("one|8-two|4",
[][]byte{barr(1, 8), barr(2, 4)},
[][]byte{barr(1, 8), barr(0, 8), barr(2, 4), barr(0, 2)},
)
// 11 2 0 0000
testCommEq("one|4-two|8",
[][]byte{barr(1, 4), barr(2, 8)},
[][]byte{barr(1, 4), barr(2, 8), barr(0, 8), barr(0, 2)},
)
// 1 0 22 3 0 00 4444 5 0 00
testCommEq("one|16-two|8-three|16-four|4-five|16",
[][]byte{barr(1, 16), barr(2, 8), barr(3, 16), barr(4, 4), barr(5, 16)},
[][]byte{barr(1, 16), barr(0, 16), barr(2, 8), barr(3, 16), barr(0, 16), barr(0, 8), barr(4, 4), barr(5, 16), barr(0, 16), barr(0, 8)},
)
}