Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Fix tests mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Feb 28, 2020
1 parent 6d36323 commit 9311bc4
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 93 deletions.
50 changes: 50 additions & 0 deletions fs/basic.go
Original file line number Diff line number Diff line change
@@ -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{}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ 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
github.com/ipfs/go-cid v0.0.5
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
Expand Down
14 changes: 14 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sectorbuilder
import (
"context"
"errors"
"fmt"
"io"

"github.com/filecoin-project/specs-actors/actors/abi"
Expand All @@ -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("<unknown %d>", t)
}
}

type SectorPaths struct {
Id abi.SectorID

Expand Down
154 changes: 64 additions & 90 deletions sectorbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 := &sectorbuilder.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)
}
Expand All @@ -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 {
Expand All @@ -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 := &sectorbuilder.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)
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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 := &sectorbuilder.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)
Expand All @@ -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)
}
Expand All @@ -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)
}
}

0 comments on commit 9311bc4

Please sign in to comment.