lotus/itests/kit/blockminer.go

121 lines
2.5 KiB
Go
Raw Normal View History

2021-05-18 21:01:10 +00:00
package kit
2020-08-13 20:37:09 +00:00
import (
"context"
"sync/atomic"
"testing"
"time"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-08-13 20:37:09 +00:00
"github.com/filecoin-project/lotus/miner"
"github.com/stretchr/testify/require"
2020-08-13 20:37:09 +00:00
)
2021-05-19 09:51:32 +00:00
// BlockMiner is a utility that makes a test miner Mine blocks on a timer.
2020-08-13 20:37:09 +00:00
type BlockMiner struct {
2021-05-19 09:51:32 +00:00
t *testing.T
miner TestMiner
2021-05-19 09:51:32 +00:00
nextNulls int64
stopCh chan chan struct{}
2020-08-13 20:37:09 +00:00
}
func NewBlockMiner(t *testing.T, miner TestMiner) *BlockMiner {
2020-08-13 20:37:09 +00:00
return &BlockMiner{
2021-05-19 09:51:32 +00:00
t: t,
miner: miner,
stopCh: make(chan chan struct{}),
2020-08-13 20:37:09 +00:00
}
}
2021-05-19 09:51:32 +00:00
func (bm *BlockMiner) MineBlocks(ctx context.Context, blocktime time.Duration) {
2020-08-13 20:37:09 +00:00
time.Sleep(time.Second)
2021-05-19 09:51:32 +00:00
2020-08-13 20:37:09 +00:00
go func() {
2021-05-19 09:51:32 +00:00
for {
2020-11-12 15:23:46 +00:00
select {
2021-05-19 09:51:32 +00:00
case <-time.After(blocktime):
case <-ctx.Done():
return
case ch := <-bm.stopCh:
close(ch)
close(bm.stopCh)
2020-11-12 15:23:46 +00:00
return
}
2021-05-19 09:51:32 +00:00
nulls := atomic.SwapInt64(&bm.nextNulls, 0)
if err := bm.miner.MineOne(ctx, miner.MineReq{
2020-08-13 20:37:09 +00:00
InjectNulls: abi.ChainEpoch(nulls),
Done: func(bool, abi.ChainEpoch, error) {},
2020-08-13 20:37:09 +00:00
}); err != nil {
bm.t.Error(err)
}
}
}()
}
2021-05-19 09:51:32 +00:00
// InjectNulls injects the specified amount of null rounds in the next
// mining rounds.
func (bm *BlockMiner) InjectNulls(rounds abi.ChainEpoch) {
atomic.AddInt64(&bm.nextNulls, int64(rounds))
}
func (bm *BlockMiner) MineUntilBlock(ctx context.Context, fn TestFullNode, cb func(abi.ChainEpoch)) {
for i := 0; i < 1000; i++ {
var (
success bool
err error
epoch abi.ChainEpoch
wait = make(chan struct{})
)
doneFn := func(win bool, ep abi.ChainEpoch, e error) {
success = win
err = e
epoch = ep
wait <- struct{}{}
}
mineErr := bm.miner.MineOne(ctx, miner.MineReq{Done: doneFn})
require.NoError(bm.t, mineErr)
<-wait
require.NoError(bm.t, err)
if success {
// Wait until it shows up on the given full nodes ChainHead
nloops := 50
for i := 0; i < nloops; i++ {
ts, err := fn.ChainHead(ctx)
require.NoError(bm.t, err)
if ts.Height() == epoch {
break
}
require.NotEqual(bm.t, i, nloops-1, "block never managed to sync to node")
time.Sleep(time.Millisecond * 10)
}
if cb != nil {
cb(epoch)
}
return
}
bm.t.Log("did not Mine block, trying again", i)
}
bm.t.Fatal("failed to Mine 1000 times in a row...")
}
2021-05-19 09:51:32 +00:00
// Stop stops the block miner.
2020-08-13 20:37:09 +00:00
func (bm *BlockMiner) Stop() {
2021-05-19 09:51:32 +00:00
bm.t.Log("shutting down mining")
if _, ok := <-bm.stopCh; ok {
// already stopped
return
}
ch := make(chan struct{})
bm.stopCh <- ch
<-ch
2020-08-13 20:37:09 +00:00
}