From b0c3963506cb680886657fb033e9477958475ab2 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 25 Jun 2020 18:29:01 +0300 Subject: [PATCH] extract common roles for bootstrapper and miner --- lotus-soup/baseline.go | 65 ++---------------------------------- lotus-soup/common_roles.go | 68 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 62 deletions(-) create mode 100644 lotus-soup/common_roles.go diff --git a/lotus-soup/baseline.go b/lotus-soup/baseline.go index 3a3c04cf5..502d22cb8 100644 --- a/lotus-soup/baseline.go +++ b/lotus-soup/baseline.go @@ -14,6 +14,7 @@ import ( "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/types" + "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" ipld "github.com/ipfs/go-ipld-format" @@ -42,71 +43,11 @@ import ( // The we create a genesis block that allocates some funds to each node and collects // the presealed sectors. var baselineRoles = map[string]func(*TestEnvironment) error{ - "bootstrapper": runBaselineBootstrapper, - "miner": runBaselineMiner, + "bootstrapper": runBootstrapper, + "miner": runMiner, "client": runBaselineClient, } -func runBaselineBootstrapper(t *TestEnvironment) error { - t.RecordMessage("running bootstrapper") - _, err := prepareBootstrapper(t) - if err != nil { - return err - } - - ctx := context.Background() - t.SyncClient.MustSignalAndWait(ctx, stateDone, t.TestInstanceCount) - return nil -} - -func runBaselineMiner(t *TestEnvironment) error { - t.RecordMessage("running miner") - miner, err := prepareMiner(t) - if err != nil { - return err - } - - ctx := context.Background() - - clients := t.IntParam("clients") - miners := t.IntParam("miners") - - // mine / stop mining - mine := true - done := make(chan struct{}) - go func() { - defer close(done) - for mine { - - // synchronize all miners to mine the next block - t.RecordMessage("synchronizing all miners to mine next block") - t.SyncClient.MustSignalAndWait(ctx, stateMineNext, miners) - - time.Sleep(time.Duration(100 + rand.Intn(int(100*time.Millisecond)))) - - err := miner.MineOne(ctx, func(bool) { - // after a block is mined - }) - if err != nil { - panic(err) - } - } - }() - - // wait for a signal from all clients to stop mining - err = <-t.SyncClient.MustBarrier(ctx, stateStopMining, clients).C - if err != nil { - return err - } - - mine = false - t.RecordMessage("shutting down mining") - <-done - - t.SyncClient.MustSignalAndWait(ctx, stateDone, t.TestInstanceCount) - return nil -} - func runBaselineClient(t *TestEnvironment) error { t.RecordMessage("running client") cl, err := prepareClient(t) diff --git a/lotus-soup/common_roles.go b/lotus-soup/common_roles.go new file mode 100644 index 000000000..140318807 --- /dev/null +++ b/lotus-soup/common_roles.go @@ -0,0 +1,68 @@ +package main + +import ( + "context" + "math/rand" + "time" +) + +func runBootstrapper(t *TestEnvironment) error { + t.RecordMessage("running bootstrapper") + _, err := prepareBootstrapper(t) + if err != nil { + return err + } + + ctx := context.Background() + t.SyncClient.MustSignalAndWait(ctx, stateDone, t.TestInstanceCount) + return nil +} + +func runMiner(t *TestEnvironment) error { + t.RecordMessage("running miner") + miner, err := prepareMiner(t) + if err != nil { + return err + } + + ctx := context.Background() + + clients := t.IntParam("clients") + miners := t.IntParam("miners") + + // mine / stop mining + mine := true + done := make(chan struct{}) + go func() { + defer close(done) + for mine { + + // synchronize all miners to mine the next block + t.RecordMessage("synchronizing all miners to mine next block") + t.SyncClient.MustSignalAndWait(ctx, stateMineNext, miners) + + // add some random delay to encourage a different miner winning each round + time.Sleep(time.Duration(100 + rand.Intn(int(100*time.Millisecond)))) + + err := miner.MineOne(ctx, func(bool) { + // after a block is mined + }) + if err != nil { + panic(err) + } + } + }() + + // wait for a signal from all clients to stop mining + err = <-t.SyncClient.MustBarrier(ctx, stateStopMining, clients).C + if err != nil { + return err + } + + mine = false + t.RecordMessage("shutting down mining") + <-done + + t.SyncClient.MustSignalAndWait(ctx, stateDone, t.TestInstanceCount) + return nil +}