From d64b21936f2734b917db2753bda5482775ca9d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 14 Nov 2019 19:26:09 +0100 Subject: [PATCH] parallel sectorbuilder test --- lib/sectorbuilder/sectorbuilder_test.go | 194 ++++++++++++++++-------- 1 file changed, 131 insertions(+), 63 deletions(-) diff --git a/lib/sectorbuilder/sectorbuilder_test.go b/lib/sectorbuilder/sectorbuilder_test.go index a5e394b36..1964faa06 100644 --- a/lib/sectorbuilder/sectorbuilder_test.go +++ b/lib/sectorbuilder/sectorbuilder_test.go @@ -8,7 +8,9 @@ import ( "io/ioutil" "math/rand" "os" + "sync" "testing" + "time" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/lib/sectorbuilder" @@ -16,6 +18,89 @@ import ( const sectorSize = 1024 +type seal struct { + sid uint64 + + pco sectorbuilder.RawSealPreCommitOutput + ppi sectorbuilder.PublicPieceInfo + + ticket sectorbuilder.SealTicket +} + +func (s *seal) precommit(t *testing.T, sb *sectorbuilder.SectorBuilder, sid uint64, done func()) { + dlen := sectorbuilder.UserBytesForSectorSize(sectorSize) + + var err error + s.sid, err = sb.AcquireSectorId() + if err != nil { + t.Fatalf("%+v", err) + } + assert.Equal(t, sid, s.sid) + + r := io.LimitReader(rand.New(rand.NewSource(42 + int64(sid))), int64(dlen)) + s.ppi, err = sb.AddPiece(dlen, sid, r, []uint64{}) + if err != nil { + t.Fatalf("%+v", err) + } + + s.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}, + } + + s.pco, err = sb.SealPreCommit(sid, s.ticket, []sectorbuilder.PublicPieceInfo{s.ppi}) + if err != nil { + t.Fatalf("%+v", err) + } + + done() +} + +func (s *seal) commit(t *testing.T, sb *sectorbuilder.SectorBuilder, done func()) { + 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}, + } + + proof, err := sb.SealCommit(s.sid, s.ticket, seed, []sectorbuilder.PublicPieceInfo{s.ppi}, []string{"foo"}, s.pco) + if err != nil { + t.Fatalf("%+v", err) + } + + ok, err := sectorbuilder.VerifySeal(sectorSize, s.pco.CommR[:], s.pco.CommD[:], sb.Miner, s.ticket.TicketBytes[:], seed.TicketBytes[:], s.sid, proof) + if err != nil { + t.Fatalf("%+v", err) + } + + if !ok { + t.Fatal("proof failed to validate") + } + + done() +} + +func (s *seal) post(t *testing.T, sb *sectorbuilder.SectorBuilder) { + 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: s.sid, + CommR: s.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 TestSealAndVerify(t *testing.T) { //t.Skip("this is slow") os.Setenv("BELLMAN_NO_GPU", "1") @@ -46,67 +131,13 @@ func TestSealAndVerify(t *testing.T) { } defer cleanup() - dlen := sectorbuilder.UserBytesForSectorSize(sectorSize) + s := seal{} - sid, err := sb.AcquireSectorId() - if err != nil { - t.Fatalf("%+v", err) - } + s.precommit(t, sb, 1, func() {}) - r := io.LimitReader(rand.New(rand.NewSource(42)), int64(dlen)) - ppi, err := sb.AddPiece(dlen, sid, r, []uint64{}) - if err != nil { - t.Fatalf("%+v", err) - } + s.commit(t, sb, func() {}) - 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}, - } - - pco, err := sb.SealPreCommit(sid, ticket, []sectorbuilder.PublicPieceInfo{ppi}) - if err != nil { - 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}, - } - - proof, err := sb.SealCommit(sid, ticket, seed, []sectorbuilder.PublicPieceInfo{ppi}, []string{"foo"}, pco) - if err != nil { - t.Fatalf("%+v", err) - } - - ok, err := sectorbuilder.VerifySeal(sectorSize, pco.CommR[:], pco.CommD[:], sb.Miner, ticket.TicketBytes[:], seed.TicketBytes[:], sid, proof) - if err != nil { - t.Fatalf("%+v", err) - } - - if !ok { - t.Fatal("proof failed to validate") - } - - 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") - } + s.post(t, sb) // Restart sectorbuilder, re-run post sb.Destroy() @@ -115,18 +146,55 @@ func TestSealAndVerify(t *testing.T) { t.Fatalf("%+v", err) } - postProof, err = sb.GeneratePoSt(ssi, cSeed, []uint64{}) - if err != nil { + s.post(t, sb) +} + +func TestSealAndVerify2(t *testing.T) { + //t.Skip("this is slow") + os.Setenv("BELLMAN_NO_GPU", "1") + os.Setenv("RUST_LOG", "info") + + build.SectorSizes = []uint64{sectorSize} + + if err := build.GetParams(true); err != nil { t.Fatalf("%+v", err) } - ok, err = sectorbuilder.VerifyPost(sb.SectorSize(), ssi, cSeed, postProof, []uint64{}) + 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) } - if !ok { - t.Fatal("bad post") + cleanup := func() { + sb.Destroy() + if err := os.RemoveAll(dir); err != nil { + t.Error(err) + } } + + defer cleanup() + + var wg sync.WaitGroup + + s1 := seal{} + s2 := seal{} + + 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() } func TestAcquireID(t *testing.T) {