lotus/itests/kit/blockminer.go

125 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"
2021-05-21 12:39:09 +00:00
"sync"
2020-08-13 20:37:09 +00:00
"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
2021-05-21 12:39:09 +00:00
wg sync.WaitGroup
cancel context.CancelFunc
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,
2021-05-21 12:39:09 +00:00
cancel: func() {},
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
2021-05-21 12:39:09 +00:00
// wrap context in a cancellable context.
ctx, bm.cancel = context.WithCancel(ctx)
bm.wg.Add(1)
2020-08-13 20:37:09 +00:00
go func() {
2021-05-21 12:39:09 +00:00
defer bm.wg.Done()
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
2020-11-12 15:23:46 +00:00
}
2021-05-19 09:51:32 +00:00
nulls := atomic.SwapInt64(&bm.nextNulls, 0)
2021-05-21 12:39:09 +00:00
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) {},
2021-05-21 12:39:09 +00:00
})
switch {
case err == nil: // wrap around
case ctx.Err() != nil: // context fired.
return
default: // log error
2020-08-13 20:37:09 +00:00
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")
2021-05-21 12:39:09 +00:00
bm.cancel()
bm.wg.Wait()
2020-08-13 20:37:09 +00:00
}