2020-08-13 20:37:09 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlockMiner struct {
|
|
|
|
ctx context.Context
|
|
|
|
t *testing.T
|
|
|
|
miner TestStorageNode
|
|
|
|
blocktime time.Duration
|
|
|
|
mine int64
|
|
|
|
nulls int64
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockMiner(ctx context.Context, t *testing.T, miner TestStorageNode, blocktime time.Duration) *BlockMiner {
|
|
|
|
return &BlockMiner{
|
|
|
|
ctx: ctx,
|
|
|
|
t: t,
|
|
|
|
miner: miner,
|
|
|
|
blocktime: blocktime,
|
|
|
|
mine: int64(1),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bm *BlockMiner) MineBlocks() {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
go func() {
|
|
|
|
defer close(bm.done)
|
|
|
|
for atomic.LoadInt64(&bm.mine) == 1 {
|
2020-11-12 15:23:46 +00:00
|
|
|
select {
|
|
|
|
case <-bm.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(bm.blocktime):
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:37:09 +00:00
|
|
|
nulls := atomic.SwapInt64(&bm.nulls, 0)
|
|
|
|
if err := bm.miner.MineOne(bm.ctx, miner.MineReq{
|
|
|
|
InjectNulls: abi.ChainEpoch(nulls),
|
2020-08-27 00:51:16 +00:00
|
|
|
Done: func(bool, abi.ChainEpoch, error) {},
|
2020-08-13 20:37:09 +00:00
|
|
|
}); err != nil {
|
|
|
|
bm.t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bm *BlockMiner) Stop() {
|
|
|
|
atomic.AddInt64(&bm.mine, -1)
|
|
|
|
fmt.Println("shutting down mining")
|
|
|
|
<-bm.done
|
|
|
|
}
|