2019-09-23 15:27:30 +00:00
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-25 04:45:13 +00:00
|
|
|
|
2023-09-21 15:37:02 +00:00
|
|
|
"github.com/hashicorp/golang-lru/arc/v2"
|
2020-08-06 01:16:54 +00:00
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2021-04-05 17:56:53 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
2019-11-30 23:17:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
2020-08-06 01:16:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen/slashfilter"
|
2020-10-09 19:52:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/journal"
|
2019-09-23 15:27:30 +00:00
|
|
|
)
|
|
|
|
|
2020-07-28 16:16:46 +00:00
|
|
|
type MineReq struct {
|
|
|
|
InjectNulls abi.ChainEpoch
|
2020-08-27 00:51:16 +00:00
|
|
|
Done func(bool, abi.ChainEpoch, error)
|
2020-07-28 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 17:56:53 +00:00
|
|
|
func NewTestMiner(nextCh <-chan MineReq, addr address.Address) func(v1api.FullNode, gen.WinningPoStProver) *Miner {
|
|
|
|
return func(api v1api.FullNode, epp gen.WinningPoStProver) *Miner {
|
2023-09-21 15:37:02 +00:00
|
|
|
arc, err := arc.NewARC[abi.ChainEpoch, bool](10000)
|
2020-01-17 07:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:17:50 +00:00
|
|
|
m := &Miner{
|
2020-01-17 07:10:47 +00:00
|
|
|
api: api,
|
|
|
|
waitFunc: chanWaiter(nextCh),
|
|
|
|
epp: epp,
|
|
|
|
minedBlockHeights: arc,
|
2020-05-05 19:01:44 +00:00
|
|
|
address: addr,
|
2020-08-06 01:16:54 +00:00
|
|
|
sf: slashfilter.New(ds.NewMapDatastore()),
|
2020-10-09 19:52:04 +00:00
|
|
|
journal: journal.NilJournal(),
|
2019-09-23 15:27:30 +00:00
|
|
|
}
|
2019-11-30 23:17:50 +00:00
|
|
|
|
2020-05-05 19:01:44 +00:00
|
|
|
if err := m.Start(context.TODO()); err != nil {
|
2019-11-30 23:17:50 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return m
|
2019-09-23 15:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:51:16 +00:00
|
|
|
func chanWaiter(next <-chan MineReq) func(ctx context.Context, _ uint64) (func(bool, abi.ChainEpoch, error), abi.ChainEpoch, error) {
|
|
|
|
return func(ctx context.Context, _ uint64) (func(bool, abi.ChainEpoch, error), abi.ChainEpoch, error) {
|
2019-09-23 15:27:30 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-07-28 16:16:46 +00:00
|
|
|
return nil, 0, ctx.Err()
|
|
|
|
case req := <-next:
|
|
|
|
return req.Done, req.InjectNulls, nil
|
2019-09-23 15:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|