synchronize all miners to mine next block

This commit is contained in:
vyzo 2020-06-25 14:16:04 +03:00
parent 58923fe2ff
commit b476bc86ce
2 changed files with 13 additions and 5 deletions

View File

@ -67,26 +67,33 @@ func runBaselineMiner(t *TestEnvironment) error {
return err return err
} }
clients := t.IntParam("clients")
miners := t.IntParam("miners")
// mine / stop mining // mine / stop mining
mine := true mine := true
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer close(done) defer close(done)
for mine { for mine {
time.Sleep(100 * time.Millisecond)
//t.RecordMessage("mine one block") // synchronize all miners to mine the next block
t.RecordMessage("synchronizing all miners to mine next block")
t.SyncClient.MustSignalAndWait(context.Background(), stateMineNext, miners)
time.Sleep(time.Duration(rand.Intn(int(100 * time.Millisecond))))
// wait and synchronise // wait and synchronise
if err := miner.MineOne(context.TODO(), func(bool) { err := miner.MineOne(context.TODO(), func(bool) {
// after a block is mined // after a block is mined
}); err != nil { })
if err != nil {
panic(err) panic(err)
} }
} }
}() }()
// wait for a signal from all clients to stop mining // wait for a signal from all clients to stop mining
clients := t.IntParam("clients")
err = <-t.SyncClient.MustBarrier(context.Background(), stateStopMining, clients).C err = <-t.SyncClient.MustBarrier(context.Background(), stateStopMining, clients).C
if err != nil { if err != nil {
return err return err

View File

@ -73,6 +73,7 @@ var (
stateReady = sync.State("ready") stateReady = sync.State("ready")
stateDone = sync.State("done") stateDone = sync.State("done")
stateMineNext = sync.State("mine-next")
stateStopMining = sync.State("stop-mining") stateStopMining = sync.State("stop-mining")
) )