From 78c0b8f9858eeadde16e3614df6541811220c378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 19 May 2020 01:03:42 +0200 Subject: [PATCH] ffiwrapper: Test unsealing --- ffiwrapper/basicfs/fs.go | 12 ++++++- ffiwrapper/sealer_cgo.go | 15 ++++---- ffiwrapper/sealer_test.go | 75 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 13 deletions(-) diff --git a/ffiwrapper/basicfs/fs.go b/ffiwrapper/basicfs/fs.go index e7e755a77..41ec8d4b4 100644 --- a/ffiwrapper/basicfs/fs.go +++ b/ffiwrapper/basicfs/fs.go @@ -9,6 +9,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/sector-storage/stores" + "github.com/filecoin-project/sector-storage/storiface" ) type sectorFile struct { @@ -63,13 +64,22 @@ func (b *Provider) AcquireSector(ctx context.Context, id abi.SectorID, existing return stores.SectorPaths{}, nil, ctx.Err() } + path := filepath.Join(b.Root, fileType.String(), stores.SectorName(id)) + prevDone := done done = func() { prevDone() <-ch } - stores.SetPathByType(&out, fileType, filepath.Join(b.Root, fileType.String(), stores.SectorName(id))) + if !allocate.Has(fileType) { + if _, err := os.Stat(path); os.IsNotExist(err) { + done() + return stores.SectorPaths{}, nil, storiface.ErrSectorNotFound + } + } + + stores.SetPathByType(&out, fileType, path) } return out, done, nil diff --git a/ffiwrapper/sealer_cgo.go b/ffiwrapper/sealer_cgo.go index 0ed665f2d..879b04afb 100644 --- a/ffiwrapper/sealer_cgo.go +++ b/ffiwrapper/sealer_cgo.go @@ -136,7 +136,7 @@ func (cf closerFunc) Close() error { return cf() } -func (sb *Sealer) UnsealPiece(ctx context.Context, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize, randomness abi.SealRandomness, cid cid.Cid) error { +func (sb *Sealer) UnsealPiece(ctx context.Context, sector abi.SectorID, offset UnpaddedByteIndex, size abi.UnpaddedPieceSize, randomness abi.SealRandomness, commd cid.Cid) error { maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded() // try finding existing @@ -230,14 +230,15 @@ func (sb *Sealer) UnsealPiece(ctx context.Context, sector abi.SectorID, offset U return xerrors.Errorf("mk temp fifo: %w", err) } - outpipe, err = os.OpenFile(outpath, os.O_RDONLY, 0600) - if err != nil { - return xerrors.Errorf("open temp pipe: %w", err) - } - go func() { defer close(outWait) defer os.Remove(outpath) + + outpipe, err = os.OpenFile(outpath, os.O_RDONLY, 0600) + if err != nil { + perr = xerrors.Errorf("open temp pipe: %w", err) + return + } defer outpipe.Close() _, perr = io.CopyN(out, outpipe, int64(size)) @@ -253,7 +254,7 @@ func (sb *Sealer) UnsealPiece(ctx context.Context, sector abi.SectorID, offset U sector.Number, sector.Miner, randomness, - cid, + commd, at, piece.Len) if err != nil { diff --git a/ffiwrapper/sealer_test.go b/ffiwrapper/sealer_test.go index 2fd1a5f32..c39d0ade0 100644 --- a/ffiwrapper/sealer_test.go +++ b/ffiwrapper/sealer_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - ffi "github.com/filecoin-project/filecoin-ffi" "io" "io/ioutil" "math/rand" @@ -17,18 +16,22 @@ import ( logging "github.com/ipfs/go-log" "golang.org/x/xerrors" + ffi "github.com/filecoin-project/filecoin-ffi" paramfetch "github.com/filecoin-project/go-paramfetch" - "github.com/filecoin-project/sector-storage/ffiwrapper/basicfs" "github.com/filecoin-project/specs-actors/actors/abi" + + "github.com/filecoin-project/sector-storage/ffiwrapper/basicfs" + "github.com/filecoin-project/sector-storage/stores" "github.com/filecoin-project/specs-storage/storage" ) func init() { - logging.SetLogLevel("*", "INFO") //nolint: errcheck + logging.SetLogLevel("*", "DEBUG") //nolint: errcheck } var sectorSize = abi.SectorSize(2048) var sealProofType = abi.RegisteredProof_StackedDRG2KiBSeal +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} type seal struct { id abi.SectorID @@ -37,18 +40,22 @@ type seal struct { ticket abi.SealRandomness } +func data(sn abi.SectorNumber, dlen abi.UnpaddedPieceSize) io.Reader { + return io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(dlen)) +} + func (s *seal) precommit(t *testing.T, sb *Sealer, id abi.SectorID, done func()) { defer done() dlen := abi.PaddedPieceSize(sectorSize).Unpadded() var err error - r := io.LimitReader(rand.New(rand.NewSource(42+int64(id.Number))), int64(dlen)) + r := data(id.Number, dlen) s.pi, err = sb.AddPiece(context.TODO(), id, []abi.UnpaddedPieceSize{}, dlen, r) if err != nil { t.Fatalf("%+v", err) } - s.ticket = 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} + s.ticket = sealRand p1, err := sb.SealPreCommit1(context.TODO(), id, s.ticket, []abi.PieceInfo{s.pi}) if err != nil { @@ -95,6 +102,62 @@ func (s *seal) commit(t *testing.T, sb *Sealer, done func()) { } } +func (s *seal) unseal(t *testing.T, sb *Sealer, sp *basicfs.Provider, si abi.SectorID, done func()) { + defer done() + + var b bytes.Buffer + err := sb.ReadPiece(context.TODO(), &b, si, 0, 1016) + if err != nil { + t.Fatal(err) + } + + expect, _ := ioutil.ReadAll(data(si.Number, 1016)) + if !bytes.Equal(b.Bytes(), expect) { + t.Fatal("read wrong bytes") + } + + p, sd, err := sp.AcquireSector(context.TODO(), si, stores.FTUnsealed, stores.FTNone, false) + if err != nil { + t.Fatal(err) + } + if err := os.Remove(p.Unsealed); err != nil { + t.Fatal(err) + } + sd() + + err = sb.ReadPiece(context.TODO(), &b, si, 0, 1016) + 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() + err = sb.ReadPiece(context.TODO(), &b, si, 0, 1016) + if err != nil { + t.Fatal(err) + } + + expect, _ = ioutil.ReadAll(data(si.Number, 1016)) + if !bytes.Equal(b.Bytes(), expect) { + t.Fatal("read wrong bytes") + } + + b.Reset() + err = sb.ReadPiece(context.TODO(), &b, si, 0, 2032) + if err != nil { + t.Fatal(err) + } + + expect = append(expect, bytes.Repeat([]byte{0}, 1016)...) + if !bytes.Equal(b.Bytes(), expect) { + t.Fatal("read wrong bytes") + } +} + func post(t *testing.T, sealer *Sealer, seals ...seal) time.Time { /*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} @@ -232,6 +295,8 @@ func TestSealAndVerify(t *testing.T) { t.Fatalf("%+v", err) } + s.unseal(t, sb, sp, si, func() {}) + fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String()) fmt.Printf("Commit: %s\n", commit.Sub(precommit).String()) fmt.Printf("GenCandidates: %s\n", genCandidiates.Sub(commit).String())