ffiwrapper: Test unsealing

This commit is contained in:
Łukasz Magiera 2020-05-19 01:03:42 +02:00
parent 86f3c0916e
commit 78c0b8f985
3 changed files with 89 additions and 13 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/sector-storage/stores" "github.com/filecoin-project/sector-storage/stores"
"github.com/filecoin-project/sector-storage/storiface"
) )
type sectorFile struct { type sectorFile struct {
@ -63,13 +64,22 @@ func (b *Provider) AcquireSector(ctx context.Context, id abi.SectorID, existing
return stores.SectorPaths{}, nil, ctx.Err() return stores.SectorPaths{}, nil, ctx.Err()
} }
path := filepath.Join(b.Root, fileType.String(), stores.SectorName(id))
prevDone := done prevDone := done
done = func() { done = func() {
prevDone() prevDone()
<-ch <-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 return out, done, nil

View File

@ -136,7 +136,7 @@ func (cf closerFunc) Close() error {
return cf() 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() maxPieceSize := abi.PaddedPieceSize(sb.ssize).Unpadded()
// try finding existing // 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) 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() { go func() {
defer close(outWait) defer close(outWait)
defer os.Remove(outpath) 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() defer outpipe.Close()
_, perr = io.CopyN(out, outpipe, int64(size)) _, 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.Number,
sector.Miner, sector.Miner,
randomness, randomness,
cid, commd,
at, at,
piece.Len) piece.Len)
if err != nil { if err != nil {

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
ffi "github.com/filecoin-project/filecoin-ffi"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -17,18 +16,22 @@ import (
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"golang.org/x/xerrors" "golang.org/x/xerrors"
ffi "github.com/filecoin-project/filecoin-ffi"
paramfetch "github.com/filecoin-project/go-paramfetch" 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/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" "github.com/filecoin-project/specs-storage/storage"
) )
func init() { func init() {
logging.SetLogLevel("*", "INFO") //nolint: errcheck logging.SetLogLevel("*", "DEBUG") //nolint: errcheck
} }
var sectorSize = abi.SectorSize(2048) var sectorSize = abi.SectorSize(2048)
var sealProofType = abi.RegisteredProof_StackedDRG2KiBSeal 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 { type seal struct {
id abi.SectorID id abi.SectorID
@ -37,18 +40,22 @@ type seal struct {
ticket abi.SealRandomness 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()) { func (s *seal) precommit(t *testing.T, sb *Sealer, id abi.SectorID, done func()) {
defer done() defer done()
dlen := abi.PaddedPieceSize(sectorSize).Unpadded() dlen := abi.PaddedPieceSize(sectorSize).Unpadded()
var err error 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) s.pi, err = sb.AddPiece(context.TODO(), id, []abi.UnpaddedPieceSize{}, dlen, r)
if err != nil { if err != nil {
t.Fatalf("%+v", err) 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}) p1, err := sb.SealPreCommit1(context.TODO(), id, s.ticket, []abi.PieceInfo{s.pi})
if err != nil { 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 { 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} /*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) t.Fatalf("%+v", err)
} }
s.unseal(t, sb, sp, si, func() {})
fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String()) fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String())
fmt.Printf("Commit: %s\n", commit.Sub(precommit).String()) fmt.Printf("Commit: %s\n", commit.Sub(precommit).String())
fmt.Printf("GenCandidates: %s\n", genCandidiates.Sub(commit).String()) fmt.Printf("GenCandidates: %s\n", genCandidiates.Sub(commit).String())