lotus/lib/sectorbuilder/sectorbuilder_test.go

129 lines
2.9 KiB
Go
Raw Normal View History

2019-08-14 22:17:27 +00:00
package sectorbuilder_test
import (
"io"
"math/rand"
2019-11-07 16:39:27 +00:00
"os"
"testing"
"github.com/ipfs/go-datastore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-10-22 06:43:14 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/lib/sectorbuilder"
)
2019-10-27 11:58:15 +00:00
const sectorSize = 1024
func TestSealAndVerify(t *testing.T) {
2019-11-07 16:39:27 +00:00
//t.Skip("this is slow")
os.Setenv("BELLMAN_NO_GPU", "1")
2019-11-05 17:09:42 +00:00
2019-10-27 11:58:15 +00:00
build.SectorSizes = []uint64{sectorSize}
2019-10-22 06:43:14 +00:00
if err := build.GetParams(true); err != nil {
2019-11-07 16:39:27 +00:00
t.Fatalf("%+v", err)
2019-10-22 06:43:14 +00:00
}
sb, cleanup, err := sectorbuilder.TempSectorbuilder(sectorSize, datastore.NewMapDatastore())
if err != nil {
2019-11-07 16:39:27 +00:00
t.Fatalf("%+v", err)
}
defer cleanup()
2019-10-27 11:58:15 +00:00
dlen := sectorbuilder.UserBytesForSectorSize(sectorSize)
2019-11-07 16:39:27 +00:00
sid, err := sb.AcquireSectorId()
if err != nil {
t.Fatalf("%+v", err)
}
2019-10-27 11:58:15 +00:00
r := io.LimitReader(rand.New(rand.NewSource(42)), int64(dlen))
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
}
ticket := sectorbuilder.SealTicket{
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-07 16:39:27 +00:00
pco, err := sb.SealPreCommit(sid, ticket, []sectorbuilder.PublicPieceInfo{ppi})
if err != nil {
2019-11-07 16:39:27 +00:00
t.Fatalf("%+v", err)
}
seed := sectorbuilder.SealSeed{
BlockHeight: 15,
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-11-07 16:39:27 +00:00
proof, err := sb.SealCommit(sid, ticket, seed, []sectorbuilder.PublicPieceInfo{ppi}, []string{"foo"}, pco)
if err != nil {
2019-11-07 16:39:27 +00:00
t.Fatalf("%+v", err)
}
2019-11-07 16:39:27 +00:00
ok, err := sectorbuilder.VerifySeal(sectorSize, pco.CommR[:], pco.CommD[:], sb.Miner, ticket.TicketBytes[:], seed.TicketBytes[:], sid, proof)
if err != nil {
2019-11-07 16:39:27 +00:00
t.Fatalf("%+v", err)
}
if !ok {
t.Fatal("proof failed to validate")
}
2019-11-07 16:43:59 +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}
ssi := sectorbuilder.NewSortedSectorInfo([]sectorbuilder.SectorInfo{{
SectorID: sid,
CommR: pco.CommR,
}})
postProof, err := sb.GeneratePoSt(ssi, cSeed, []uint64{})
if err != nil {
t.Fatalf("%+v", err)
}
ok, err = sectorbuilder.VerifyPost(sb.SectorSize(), ssi, cSeed, postProof, []uint64{})
if err != nil {
t.Fatalf("%+v", err)
}
if !ok {
t.Fatal("bad post")
}
}
func TestAcquireID(t *testing.T) {
ds := datastore.NewMapDatastore()
sb, cleanup, err := sectorbuilder.TempSectorbuilder(sectorSize, ds)
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)
cleanup()
sb, cleanup, err = sectorbuilder.TempSectorbuilder(sectorSize, ds)
if err != nil {
t.Fatalf("%+v", err)
}
assertAcquire(4)
assertAcquire(5)
assertAcquire(6)
cleanup()
}