2019-08-14 22:17:27 +00:00
|
|
|
package sectorbuilder_test
|
2019-08-08 01:16:58 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-25 23:31:34 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-08-08 01:16:58 +00:00
|
|
|
"io"
|
2019-11-08 18:55:23 +00:00
|
|
|
"io/ioutil"
|
2019-08-08 01:16:58 +00:00
|
|
|
"math/rand"
|
2019-11-07 16:39:27 +00:00
|
|
|
"os"
|
2019-11-14 22:39:00 +00:00
|
|
|
"runtime"
|
2019-11-14 18:26:09 +00:00
|
|
|
"sync"
|
2019-08-08 01:16:58 +00:00
|
|
|
"testing"
|
2019-11-14 18:26:09 +00:00
|
|
|
"time"
|
2019-08-08 01:16:58 +00:00
|
|
|
|
2019-11-13 06:44:29 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
2019-12-06 00:27:32 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-11-13 06:44:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-11-27 17:10:34 +00:00
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
2019-10-22 06:43:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sectorbuilder"
|
2019-08-08 01:16:58 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
func init() {
|
|
|
|
logging.SetLogLevel("*", "INFO")
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:58:15 +00:00
|
|
|
const sectorSize = 1024
|
2019-11-26 15:15:32 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
type seal struct {
|
|
|
|
sid uint64
|
2019-11-05 17:09:42 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
pco sectorbuilder.RawSealPreCommitOutput
|
|
|
|
ppi sectorbuilder.PublicPieceInfo
|
2019-10-27 11:58:15 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
ticket sectorbuilder.SealTicket
|
|
|
|
}
|
2019-08-08 01:16:58 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
func (s *seal) precommit(t *testing.T, sb *sectorbuilder.SectorBuilder, sid uint64, done func()) {
|
2019-10-27 11:58:15 +00:00
|
|
|
dlen := sectorbuilder.UserBytesForSectorSize(sectorSize)
|
2019-08-08 01:16:58 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
var err error
|
2019-11-15 01:01:53 +00:00
|
|
|
r := io.LimitReader(rand.New(rand.NewSource(42+int64(sid))), int64(dlen))
|
2019-11-14 18:26:09 +00:00
|
|
|
s.ppi, err = sb.AddPiece(dlen, sid, r, []uint64{})
|
2019-10-27 11:58:15 +00:00
|
|
|
if err != nil {
|
2019-11-07 16:39:27 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-10-27 11:58:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
s.ticket = sectorbuilder.SealTicket{
|
2019-10-31 16:55:35 +00:00
|
|
|
BlockHeight: 5,
|
|
|
|
TicketBytes: [32]byte{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},
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
s.pco, err = sb.SealPreCommit(sid, s.ticket, []sectorbuilder.PublicPieceInfo{s.ppi})
|
2019-10-31 16:55:35 +00:00
|
|
|
if err != nil {
|
2019-11-07 16:39:27 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-08-08 01:16:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *seal) commit(t *testing.T, sb *sectorbuilder.SectorBuilder, done func()) {
|
2019-10-31 16:55:35 +00:00
|
|
|
seed := sectorbuilder.SealSeed{
|
|
|
|
BlockHeight: 15,
|
2019-11-04 16:47:08 +00:00
|
|
|
TicketBytes: [32]byte{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},
|
2019-10-31 16:55:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 15:15:32 +00:00
|
|
|
proof, err := sb.SealCommit(s.sid, s.ticket, seed, []sectorbuilder.PublicPieceInfo{s.ppi}, s.pco)
|
2019-10-31 16:55:35 +00:00
|
|
|
if err != nil {
|
2019-11-07 16:39:27 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-10-31 16:55:35 +00:00
|
|
|
}
|
2019-08-08 01:16:58 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
ok, err := sectorbuilder.VerifySeal(sectorSize, s.pco.CommR[:], s.pco.CommD[:], sb.Miner, s.ticket.TicketBytes[:], seed.TicketBytes[:], s.sid, proof)
|
2019-08-08 01:16:58 +00:00
|
|
|
if err != nil {
|
2019-11-07 16:39:27 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-08-08 01:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("proof failed to validate")
|
|
|
|
}
|
2019-11-07 16:43:59 +00:00
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:52:28 +00:00
|
|
|
func post(t *testing.T, sb *sectorbuilder.SectorBuilder, seals ...seal) time.Time {
|
2019-11-25 23:31:34 +00:00
|
|
|
cSeed := [32]byte{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}
|
|
|
|
|
2019-11-28 18:52:28 +00:00
|
|
|
ppi := make([]ffi.PublicSectorInfo, len(seals))
|
|
|
|
for i, s := range seals {
|
|
|
|
ppi[i] = ffi.PublicSectorInfo{
|
|
|
|
SectorID: s.sid,
|
|
|
|
CommR: s.pco.CommR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssi := sectorbuilder.NewSortedPublicSectorInfo(ppi)
|
2019-11-25 23:31:34 +00:00
|
|
|
|
|
|
|
candndates, err := sb.GenerateEPostCandidates(ssi, cSeed, []uint64{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-26 12:29:42 +00:00
|
|
|
genCandidates := time.Now()
|
|
|
|
|
|
|
|
if len(candndates) != 1 {
|
|
|
|
t.Fatal("expected 1 candidate")
|
|
|
|
}
|
|
|
|
|
2019-11-25 23:31:34 +00:00
|
|
|
postProof, err := sb.ComputeElectionPoSt(ssi, cSeed[:], candndates)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:46:56 +00:00
|
|
|
ok, err := sectorbuilder.VerifyElectionPost(context.TODO(), sb.SectorSize(), ssi, cSeed[:], postProof, candndates, sb.Miner)
|
2019-11-25 23:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("bad post")
|
|
|
|
}
|
2019-11-26 12:29:42 +00:00
|
|
|
|
|
|
|
return genCandidates
|
2019-11-14 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSealAndVerify(t *testing.T) {
|
2019-12-09 15:49:46 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode")
|
|
|
|
}
|
2019-11-14 22:39:00 +00:00
|
|
|
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
|
|
|
|
t.Skip("this is slow")
|
|
|
|
}
|
2019-12-02 12:51:16 +00:00
|
|
|
_ = os.Setenv("RUST_LOG", "info")
|
2019-11-14 18:26:09 +00:00
|
|
|
|
|
|
|
build.SectorSizes = []uint64{sectorSize}
|
|
|
|
|
2019-12-04 19:44:15 +00:00
|
|
|
if err := build.GetParams(sectorSize); err != nil {
|
2019-11-14 18:26:09 +00:00
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "sbtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
cleanup := func() {
|
2019-11-25 23:31:34 +00:00
|
|
|
if t.Failed() {
|
|
|
|
fmt.Printf("not removing %s\n", dir)
|
|
|
|
return
|
|
|
|
}
|
2019-11-14 18:26:09 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-11-14 21:18:22 +00:00
|
|
|
si, err := sb.AcquireSectorId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := seal{sid: si}
|
2019-11-14 18:26:09 +00:00
|
|
|
|
2019-11-26 12:29:42 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
s.precommit(t, sb, 1, func() {})
|
|
|
|
|
2019-11-26 12:29:42 +00:00
|
|
|
precommit := time.Now()
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
s.commit(t, sb, func() {})
|
|
|
|
|
2019-11-26 12:29:42 +00:00
|
|
|
commit := time.Now()
|
|
|
|
|
2019-11-28 18:52:28 +00:00
|
|
|
genCandidiates := post(t, sb, s)
|
2019-11-26 12:29:42 +00:00
|
|
|
|
|
|
|
epost := time.Now()
|
2019-11-09 10:42:36 +00:00
|
|
|
|
|
|
|
// Restart sectorbuilder, re-run post
|
|
|
|
sb, err = sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:52:28 +00:00
|
|
|
post(t, sb, s)
|
2019-11-26 12:29:42 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
fmt.Printf("EPoSt: %s\n", epost.Sub(genCandidiates).String())
|
2019-11-14 18:26:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 15:15:32 +00:00
|
|
|
func TestSealPoStNoCommit(t *testing.T) {
|
2019-12-09 15:49:46 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode")
|
|
|
|
}
|
2019-11-26 15:15:32 +00:00
|
|
|
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
|
|
|
|
t.Skip("this is slow")
|
|
|
|
}
|
2019-12-02 12:51:16 +00:00
|
|
|
_ = os.Setenv("RUST_LOG", "info")
|
2019-11-26 15:15:32 +00:00
|
|
|
|
|
|
|
build.SectorSizes = []uint64{sectorSize}
|
|
|
|
|
2019-12-04 19:44:15 +00:00
|
|
|
if err := build.GetParams(sectorSize); err != nil {
|
2019-11-26 15:15:32 +00:00
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "sbtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
|
|
|
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()
|
|
|
|
|
|
|
|
si, err := sb.AcquireSectorId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := seal{sid: si}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
s.precommit(t, sb, 1, func() {})
|
|
|
|
|
|
|
|
precommit := time.Now()
|
|
|
|
|
|
|
|
// Restart sectorbuilder, re-run post
|
|
|
|
sb, err = sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
2019-12-06 21:15:06 +00:00
|
|
|
|
|
|
|
if err := sb.TrimCache(1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-26 15:15:32 +00:00
|
|
|
|
2019-11-28 18:52:28 +00:00
|
|
|
genCandidiates := post(t, sb, s)
|
2019-11-26 15:15:32 +00:00
|
|
|
|
|
|
|
epost := time.Now()
|
|
|
|
|
|
|
|
fmt.Printf("PreCommit: %s\n", precommit.Sub(start).String())
|
|
|
|
fmt.Printf("GenCandidates: %s\n", genCandidiates.Sub(precommit).String())
|
|
|
|
fmt.Printf("EPoSt: %s\n", epost.Sub(genCandidiates).String())
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
func TestSealAndVerify2(t *testing.T) {
|
2019-12-09 15:49:46 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode")
|
|
|
|
}
|
2019-11-14 22:39:00 +00:00
|
|
|
if runtime.NumCPU() < 10 && os.Getenv("CI") == "" { // don't bother on slow hardware
|
|
|
|
t.Skip("this is slow")
|
|
|
|
}
|
2019-12-02 12:51:16 +00:00
|
|
|
_ = os.Setenv("RUST_LOG", "info")
|
2019-11-14 18:26:09 +00:00
|
|
|
|
|
|
|
build.SectorSizes = []uint64{sectorSize}
|
|
|
|
|
2019-12-04 19:44:15 +00:00
|
|
|
if err := build.GetParams(sectorSize); err != nil {
|
2019-11-09 10:42:36 +00:00
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:26:09 +00:00
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "sbtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
2019-11-09 10:42:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
2019-11-14 18:26:09 +00:00
|
|
|
cleanup := func() {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-11-09 10:42:36 +00:00
|
|
|
}
|
2019-11-14 18:26:09 +00:00
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2019-11-14 21:18:22 +00:00
|
|
|
si1, err := sb.AcquireSectorId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
si2, err := sb.AcquireSectorId()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := seal{sid: si1}
|
|
|
|
s2 := seal{sid: si2}
|
2019-11-14 18:26:09 +00:00
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
go s1.precommit(t, sb, 1, wg.Done)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
go s2.precommit(t, sb, 2, wg.Done)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
go s1.commit(t, sb, wg.Done)
|
|
|
|
go s2.commit(t, sb, wg.Done)
|
|
|
|
wg.Wait()
|
2019-11-28 18:52:28 +00:00
|
|
|
|
|
|
|
post(t, sb, s1, s2)
|
2019-08-08 01:16:58 +00:00
|
|
|
}
|
2019-11-08 18:49:36 +00:00
|
|
|
|
|
|
|
func TestAcquireID(t *testing.T) {
|
|
|
|
ds := datastore.NewMapDatastore()
|
|
|
|
|
2019-11-08 18:55:23 +00:00
|
|
|
dir, err := ioutil.TempDir("", "sbtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
2019-11-08 18:49:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertAcquire := func(expect uint64) {
|
|
|
|
id, err := sb.AcquireSectorId()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expect, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertAcquire(1)
|
|
|
|
assertAcquire(2)
|
|
|
|
assertAcquire(3)
|
|
|
|
|
2019-11-08 18:55:23 +00:00
|
|
|
sb, err = sectorbuilder.TempSectorbuilderDir(dir, sectorSize, ds)
|
2019-11-08 18:49:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertAcquire(4)
|
|
|
|
assertAcquire(5)
|
|
|
|
assertAcquire(6)
|
|
|
|
|
2019-11-08 18:55:23 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-11-08 18:49:36 +00:00
|
|
|
}
|