From 9311bc405ba16dd9dd24dc16fe8cba7cc18847b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 28 Feb 2020 04:39:37 +0100 Subject: [PATCH] Fix tests mostly --- fs/basic.go | 50 ++++++++++++++ go.mod | 5 +- interface.go | 14 ++++ sectorbuilder_test.go | 154 ++++++++++++++++++------------------------ 4 files changed, 130 insertions(+), 93 deletions(-) create mode 100644 fs/basic.go diff --git a/fs/basic.go b/fs/basic.go new file mode 100644 index 0000000..c609444 --- /dev/null +++ b/fs/basic.go @@ -0,0 +1,50 @@ +package fs + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/filecoin-project/go-address" + "github.com/filecoin-project/specs-actors/actors/abi" + + "github.com/filecoin-project/go-sectorbuilder" +) + +type Basic struct { + Miner address.Address + NextID abi.SectorNumber + Root string +} + +func (b Basic) AcquireSectorNumber() (abi.SectorNumber, error) { + b.NextID++ + return b.NextID, nil +} + +func (b *Basic) FinalizeSector(abi.SectorNumber) error { + return nil +} + +func (b *Basic) AcquireSector(id abi.SectorNumber, existing sectorbuilder.SectorFileType, allocate sectorbuilder.SectorFileType, sealing bool) (sectorbuilder.SectorPaths, func(), error) { + mid, err := address.IDFromAddress(b.Miner) + if err != nil { + return sectorbuilder.SectorPaths{}, nil, err + } + + os.Mkdir(filepath.Join(b.Root, sectorbuilder.FTUnsealed.String()), 0755) + os.Mkdir(filepath.Join(b.Root, sectorbuilder.FTSealed.String()), 0755) + os.Mkdir(filepath.Join(b.Root, sectorbuilder.FTCache.String()), 0755) + + return sectorbuilder.SectorPaths{ + Id: abi.SectorID{ + Miner: abi.ActorID(mid), + Number: id, + }, + Unsealed: filepath.Join(b.Root, sectorbuilder.FTUnsealed.String(), fmt.Sprintf("s-%s-%d", b.Miner.String(), id)), + Sealed: filepath.Join(b.Root, sectorbuilder.FTSealed.String(), fmt.Sprintf("s-%s-%d", b.Miner.String(), id)), + Cache: filepath.Join(b.Root, sectorbuilder.FTCache.String(), fmt.Sprintf("s-%s-%d", b.Miner.String(), id)), + }, func() {}, nil +} + +var _ sectorbuilder.SectorProvider = &Basic{} diff --git a/go.mod b/go.mod index b874bb0..2e9c979 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.13 require ( github.com/filecoin-project/filecoin-ffi v0.0.0-20200226205820-4da0bccccefb github.com/filecoin-project/go-address v0.0.2-0.20200218010043-eb9bb40ed5be - github.com/filecoin-project/go-fil-commcid v0.0.0-20200208005934-2b8bd03caca5 github.com/filecoin-project/go-paramfetch v0.0.2-0.20200218225740-47c639bab663 github.com/filecoin-project/specs-actors v0.0.0-20200226200336-94c9b92b2775 github.com/gopherjs/gopherjs v0.0.0-20190812055157-5d271430af9f // indirect @@ -13,9 +12,9 @@ require ( github.com/ipfs/go-datastore v0.1.1 github.com/ipfs/go-ipld-format v0.0.2 // indirect github.com/ipfs/go-log v1.0.0 - github.com/ipfs/go-log/v2 v2.0.2 + github.com/ipfs/go-log/v2 v2.0.2 // indirect github.com/mattn/go-isatty v0.0.9 // indirect - github.com/otiai10/copy v1.0.2 + github.com/otiai10/copy v1.0.2 // indirect github.com/pkg/errors v0.9.1 github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a // indirect github.com/smartystreets/assertions v1.0.1 // indirect diff --git a/interface.go b/interface.go index 0d891ca..e533af3 100644 --- a/interface.go +++ b/interface.go @@ -3,6 +3,7 @@ package sectorbuilder import ( "context" "errors" + "fmt" "io" "github.com/filecoin-project/specs-actors/actors/abi" @@ -18,6 +19,19 @@ const ( FTCache ) +func (t SectorFileType) String() string { + switch t { + case FTUnsealed: + return "unsealed" + case FTSealed: + return "sealed" + case FTCache: + return "cache" + default: + return fmt.Sprintf("", t) + } +} + type SectorPaths struct { Id abi.SectorID diff --git a/sectorbuilder_test.go b/sectorbuilder_test.go index 10e465b..6b2fbe7 100644 --- a/sectorbuilder_test.go +++ b/sectorbuilder_test.go @@ -17,15 +17,11 @@ import ( paramfetch "github.com/filecoin-project/go-paramfetch" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/ipfs/go-cid" - "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" "github.com/pkg/errors" "github.com/filecoin-project/go-sectorbuilder" "github.com/filecoin-project/go-sectorbuilder/fs" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func init() { @@ -45,6 +41,7 @@ type seal struct { } func (s *seal) precommit(t *testing.T, sb *sectorbuilder.SectorBuilder, num abi.SectorNumber, done func()) { + defer done() dlen := abi.PaddedPieceSize(sectorSize).Unpadded() var err error @@ -56,20 +53,27 @@ func (s *seal) precommit(t *testing.T, sb *sectorbuilder.SectorBuilder, num abi. 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} - sealedCID, unsealedCID, err := sb.SealPreCommit(context.TODO(), num, s.ticket, []abi.PieceInfo{s.pi}) + p1, err := sb.SealPreCommit1(context.TODO(), num, s.ticket, []abi.PieceInfo{s.pi}) + if err != nil { + t.Fatalf("%+v", err) + } + sealedCID, unsealedCID, err := sb.SealPreCommit2(context.TODO(), num, p1) if err != nil { t.Fatalf("%+v", err) } s.sealedCID = sealedCID s.unsealedCID = unsealedCID - - done() } func (s *seal) commit(t *testing.T, sb *sectorbuilder.SectorBuilder, done func()) { + 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} - proof, err := sb.SealCommit(context.TODO(), s.num, s.ticket, seed, []abi.PieceInfo{s.pi}, s.sealedCID, s.unsealedCID) + pc1, err := sb.SealCommit1(context.TODO(), s.num, s.ticket, seed, []abi.PieceInfo{s.pi}, s.sealedCID, s.unsealedCID) + if err != nil { + t.Fatalf("%+v", err) + } + proof, err := sb.SealCommit2(context.TODO(), s.num, pc1) if err != nil { t.Fatalf("%+v", err) } @@ -101,8 +105,6 @@ func (s *seal) commit(t *testing.T, sb *sectorbuilder.SectorBuilder, done func() if !ok { t.Fatal("proof failed to validate") } - - done() } func post(t *testing.T, sb *sectorbuilder.SectorBuilder, seals ...seal) time.Time { @@ -193,49 +195,42 @@ func TestSealAndVerify(t *testing.T) { getGrothParamFileAndVerifyingKeys(sectorSize) - ds := datastore.NewMapDatastore() - cdir, err := ioutil.TempDir("", "sbtest-c-") if err != nil { t.Fatal(err) } - sdir, err := ioutil.TempDir("", "sbtest-s-") + addr, err := address.NewFromString("t0123") if err != nil { t.Fatal(err) } - paths := []fs.PathConfig{ - { - Path: cdir, - Cache: true, - Weight: 1, - }, - { - Path: sdir, - Cache: false, - Weight: 1, - }, + cfg := §orbuilder.Config{ + SealProofType: sealProofType, + PoStProofType: postProofType, + Miner: addr, } - sb, err := sectorbuilder.TempSectorbuilderDir(paths, sealProofType, postProofType, ds) + sp := &fs.Basic{ + Miner: addr, + NextID: 0, + Root: cdir, + } + sb, err := sectorbuilder.New(sp, cfg) if err != nil { t.Fatalf("%+v", err) } cleanup := func() { if t.Failed() { - fmt.Printf("not removing %s, %s\n", cdir, sdir) + fmt.Printf("not removing %s\n", cdir) return } if err := os.RemoveAll(cdir); err != nil { t.Error(err) } - if err := os.RemoveAll(sdir); err != nil { - t.Error(err) - } } defer cleanup() - si, err := sb.AcquireSectorNumber() + si, err := sp.AcquireSectorNumber() if err != nil { t.Fatalf("%+v", err) } @@ -256,12 +251,6 @@ func TestSealAndVerify(t *testing.T) { epost := time.Now() - // Restart sectorbuilder, re-run post - sb, err = sectorbuilder.TempSectorbuilderDir(paths, sealProofType, postProofType, ds) - if err != nil { - t.Fatalf("%+v", err) - } - post(t, sb, s) if err := sb.FinalizeSector(context.TODO(), 1); err != nil { @@ -282,17 +271,31 @@ func TestSealPoStNoCommit(t *testing.T) { getGrothParamFileAndVerifyingKeys(sectorSize) - ds := datastore.NewMapDatastore() - dir, err := ioutil.TempDir("", "sbtest") if err != nil { t.Fatal(err) } - sb, err := sectorbuilder.TempSectorbuilderDir(sectorbuilder.SimplePath(dir), sealProofType, postProofType, ds) + addr, err := address.NewFromString("t0123") + if err != nil { + t.Fatal(err) + } + + cfg := §orbuilder.Config{ + SealProofType: sealProofType, + PoStProofType: postProofType, + Miner: addr, + } + sp := &fs.Basic{ + Miner: addr, + NextID: 0, + Root: dir, + } + sb, err := sectorbuilder.New(sp, cfg) if err != nil { t.Fatalf("%+v", err) } + cleanup := func() { if t.Failed() { fmt.Printf("not removing %s\n", dir) @@ -304,7 +307,7 @@ func TestSealPoStNoCommit(t *testing.T) { } defer cleanup() - si, err := sb.AcquireSectorNumber() + si, err := sp.AcquireSectorNumber() if err != nil { t.Fatalf("%+v", err) } @@ -317,13 +320,7 @@ func TestSealPoStNoCommit(t *testing.T) { precommit := time.Now() - // Restart sectorbuilder, re-run post - sb, err = sectorbuilder.TempSectorbuilderDir(sectorbuilder.SimplePath(dir), sealProofType, postProofType, ds) - if err != nil { - t.Fatalf("%+v", err) - } - - if err := sb.TrimCache(context.TODO(), 1); err != nil { + if err := sb.FinalizeSector(context.TODO(), 1); err != nil { t.Fatal(err) } @@ -340,21 +337,35 @@ func TestSealAndVerify2(t *testing.T) { if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware t.Skip("this is slow") } - _ = os.Setenv("RUST_LOG", "info") + _ = os.Setenv("RUST_LOG", "trace") getGrothParamFileAndVerifyingKeys(sectorSize) - ds := datastore.NewMapDatastore() - dir, err := ioutil.TempDir("", "sbtest") if err != nil { t.Fatal(err) } - sb, err := sectorbuilder.TempSectorbuilderDir(sectorbuilder.SimplePath(dir), sealProofType, postProofType, ds) + addr, err := address.NewFromString("t0123") + if err != nil { + t.Fatal(err) + } + + cfg := §orbuilder.Config{ + SealProofType: sealProofType, + PoStProofType: postProofType, + Miner: addr, + } + sp := &fs.Basic{ + Miner: addr, + NextID: 0, + Root: dir, + } + sb, err := sectorbuilder.New(sp, cfg) if err != nil { t.Fatalf("%+v", err) } + cleanup := func() { if err := os.RemoveAll(dir); err != nil { t.Error(err) @@ -365,11 +376,11 @@ func TestSealAndVerify2(t *testing.T) { var wg sync.WaitGroup - si1, err := sb.AcquireSectorNumber() + si1, err := sp.AcquireSectorNumber() if err != nil { t.Fatalf("%+v", err) } - si2, err := sb.AcquireSectorNumber() + si2, err := sp.AcquireSectorNumber() if err != nil { t.Fatalf("%+v", err) } @@ -390,40 +401,3 @@ func TestSealAndVerify2(t *testing.T) { post(t, sb, s1, s2) } - -func TestAcquireID(t *testing.T) { - ds := datastore.NewMapDatastore() - - dir, err := ioutil.TempDir("", "sbtest") - if err != nil { - t.Fatal(err) - } - - sb, err := sectorbuilder.TempSectorbuilderDir(sectorbuilder.SimplePath(dir), sealProofType, postProofType, ds) - if err != nil { - t.Fatalf("%+v", err) - } - - assertAcquire := func(expect abi.SectorNumber) { - num, err := sb.AcquireSectorNumber() - require.NoError(t, err) - assert.Equal(t, expect, num) - } - - assertAcquire(1) - assertAcquire(2) - assertAcquire(3) - - sb, err = sectorbuilder.TempSectorbuilderDir(sectorbuilder.SimplePath(dir), sealProofType, postProofType, ds) - if err != nil { - t.Fatalf("%+v", err) - } - - assertAcquire(4) - assertAcquire(5) - assertAcquire(6) - - if err := os.RemoveAll(dir); err != nil { - t.Error(err) - } -}