2022-06-14 17:27:04 +00:00
|
|
|
package wdpost
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-07 18:02:52 +00:00
|
|
|
"time"
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2022-06-14 17:27:04 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-06-14 15:00:51 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-11-28 17:44:49 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-06-14 17:27:04 +00:00
|
|
|
"github.com/filecoin-project/go-bitfield"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 17:27:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v8/miner"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/dline"
|
2022-06-14 17:27:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-01-13 20:47:27 +00:00
|
|
|
|
2020-04-23 22:15:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-07-10 14:43:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-09-06 15:47:30 +00:00
|
|
|
lminer "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2019-11-28 17:44:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-07-20 13:45:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/journal"
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2022-06-14 17:32:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/ctladdr"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer"
|
2022-06-17 11:31:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2019-11-28 17:44:49 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 17:27:04 +00:00
|
|
|
var log = logging.Logger("wdpost")
|
|
|
|
|
|
|
|
type NodeAPI interface {
|
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
ChainNotify(context.Context) (<-chan []*api.HeadChange, error)
|
|
|
|
|
|
|
|
StateMinerInfo(context.Context, address.Address, types.TipSetKey) (api.MinerInfo, error)
|
|
|
|
StateMinerProvingDeadline(context.Context, address.Address, types.TipSetKey) (*dline.Info, error)
|
|
|
|
StateMinerSectors(context.Context, address.Address, *bitfield.BitField, types.TipSetKey) ([]*miner.SectorOnChainInfo, error)
|
|
|
|
StateNetworkVersion(context.Context, types.TipSetKey) (network.Version, error)
|
|
|
|
StateGetRandomnessFromTickets(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte, tsk types.TipSetKey) (abi.Randomness, error)
|
|
|
|
StateGetRandomnessFromBeacon(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte, tsk types.TipSetKey) (abi.Randomness, error)
|
|
|
|
StateWaitMsg(ctx context.Context, cid cid.Cid, confidence uint64, limit abi.ChainEpoch, allowReplaced bool) (*api.MsgLookup, error)
|
|
|
|
StateMinerPartitions(context.Context, address.Address, uint64, types.TipSetKey) ([]api.Partition, error)
|
|
|
|
StateLookupID(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
|
|
|
StateAccountKey(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
2022-09-06 15:47:30 +00:00
|
|
|
StateSectorPartition(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok types.TipSetKey) (*lminer.SectorLocation, error)
|
2022-06-14 17:27:04 +00:00
|
|
|
|
|
|
|
MpoolPushMessage(context.Context, *types.Message, *api.MessageSendSpec) (*types.SignedMessage, error)
|
|
|
|
|
|
|
|
GasEstimateMessageGas(context.Context, *types.Message, *api.MessageSendSpec, types.TipSetKey) (*types.Message, error)
|
|
|
|
GasEstimateFeeCap(context.Context, *types.Message, int64, types.TipSetKey) (types.BigInt, error)
|
|
|
|
GasEstimateGasPremium(_ context.Context, nblocksincl uint64, sender address.Address, gaslimit int64, tsk types.TipSetKey) (types.BigInt, error)
|
|
|
|
|
|
|
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
|
|
|
WalletHas(context.Context, address.Address) (bool, error)
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:45:47 +00:00
|
|
|
// WindowPoStScheduler is the coordinator for WindowPoSt submissions, fault
|
|
|
|
// declaration, and recovery declarations. It watches the chain for reverts and
|
|
|
|
// applies, and schedules/run those processes as partition deadlines arrive.
|
|
|
|
//
|
|
|
|
// WindowPoStScheduler watches the chain though the changeHandler, which in turn
|
|
|
|
// turn calls the scheduler when the time arrives to do work.
|
2020-04-07 17:41:41 +00:00
|
|
|
type WindowPoStScheduler struct {
|
2022-07-07 14:52:22 +00:00
|
|
|
api NodeAPI
|
|
|
|
feeCfg config.MinerFeeConfig
|
|
|
|
addrSel *ctladdr.AddressSelector
|
|
|
|
prover storiface.ProverPoSt
|
|
|
|
verifier storiface.Verifier
|
|
|
|
faultTracker sealer.FaultTracker
|
|
|
|
proofType abi.RegisteredPoStProof
|
|
|
|
partitionSectors uint64
|
|
|
|
disablePreChecks bool
|
|
|
|
maxPartitionsPerPostMessage int
|
|
|
|
maxPartitionsPerRecoveryMessage int
|
|
|
|
ch *changeHandler
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-10-21 03:35:18 +00:00
|
|
|
actor address.Address
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-09-02 18:45:25 +00:00
|
|
|
evtTypes [4]journal.EventType
|
2020-10-09 19:52:04 +00:00
|
|
|
journal journal.Journal
|
2019-12-08 19:48:17 +00:00
|
|
|
|
2020-07-20 13:45:17 +00:00
|
|
|
// failed abi.ChainEpoch // eps
|
|
|
|
// failLk sync.Mutex
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:45:47 +00:00
|
|
|
// NewWindowedPoStScheduler creates a new WindowPoStScheduler scheduler.
|
2022-06-14 17:27:04 +00:00
|
|
|
func NewWindowedPoStScheduler(api NodeAPI,
|
2021-05-14 18:45:47 +00:00
|
|
|
cfg config.MinerFeeConfig,
|
2022-07-01 20:20:05 +00:00
|
|
|
pcfg config.ProvingConfig,
|
2022-06-14 17:32:29 +00:00
|
|
|
as *ctladdr.AddressSelector,
|
2022-06-17 11:52:19 +00:00
|
|
|
sp storiface.ProverPoSt,
|
|
|
|
verif storiface.Verifier,
|
2022-06-14 18:03:38 +00:00
|
|
|
ft sealer.FaultTracker,
|
2021-05-14 18:45:47 +00:00
|
|
|
j journal.Journal,
|
|
|
|
actor address.Address) (*WindowPoStScheduler, error) {
|
2020-04-16 17:36:36 +00:00
|
|
|
mi, err := api.StateMinerInfo(context.TODO(), actor, types.EmptyTSK)
|
2020-04-13 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:06:05 +00:00
|
|
|
return &WindowPoStScheduler{
|
2022-07-07 14:52:22 +00:00
|
|
|
api: api,
|
|
|
|
feeCfg: cfg,
|
|
|
|
addrSel: as,
|
|
|
|
prover: sp,
|
|
|
|
verifier: verif,
|
|
|
|
faultTracker: ft,
|
|
|
|
proofType: mi.WindowPoStProofType,
|
|
|
|
partitionSectors: mi.WindowPoStPartitionSectors,
|
|
|
|
disablePreChecks: pcfg.DisableWDPoStPreChecks,
|
|
|
|
maxPartitionsPerPostMessage: pcfg.MaxPartitionsPerPoStMessage,
|
|
|
|
maxPartitionsPerRecoveryMessage: pcfg.MaxPartitionsPerRecoveryMessage,
|
2022-07-07 14:52:22 +00:00
|
|
|
actor: actor,
|
2020-09-02 18:45:25 +00:00
|
|
|
evtTypes: [...]journal.EventType{
|
2020-10-09 19:52:04 +00:00
|
|
|
evtTypeWdPoStScheduler: j.RegisterEventType("wdpost", "scheduler"),
|
|
|
|
evtTypeWdPoStProofs: j.RegisterEventType("wdpost", "proofs_processed"),
|
|
|
|
evtTypeWdPoStRecoveries: j.RegisterEventType("wdpost", "recoveries_processed"),
|
|
|
|
evtTypeWdPoStFaults: j.RegisterEventType("wdpost", "faults_processed"),
|
2020-09-02 18:45:25 +00:00
|
|
|
},
|
2020-10-09 19:52:04 +00:00
|
|
|
journal: j,
|
2020-04-29 18:06:05 +00:00
|
|
|
}, nil
|
2020-04-07 19:55:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
func (s *WindowPoStScheduler) Run(ctx context.Context) {
|
2021-05-14 19:08:15 +00:00
|
|
|
// Initialize change handler.
|
|
|
|
|
|
|
|
// callbacks is a union of the fullNodeFilteredAPI and ourselves.
|
|
|
|
callbacks := struct {
|
2022-06-14 17:27:04 +00:00
|
|
|
NodeAPI
|
2021-05-14 19:08:15 +00:00
|
|
|
*WindowPoStScheduler
|
|
|
|
}{s.api, s}
|
|
|
|
|
|
|
|
s.ch = newChangeHandler(callbacks, s.actor)
|
2020-09-18 16:03:59 +00:00
|
|
|
defer s.ch.shutdown()
|
|
|
|
s.ch.start()
|
2019-12-03 22:34:15 +00:00
|
|
|
|
2021-05-14 19:08:15 +00:00
|
|
|
var (
|
|
|
|
notifs <-chan []*api.HeadChange
|
|
|
|
err error
|
|
|
|
gotCur bool
|
|
|
|
)
|
2020-04-07 18:02:52 +00:00
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
// not fine to panic after this point
|
|
|
|
for {
|
2020-04-07 18:02:52 +00:00
|
|
|
if notifs == nil {
|
|
|
|
notifs, err = s.api.ChainNotify(ctx)
|
|
|
|
if err != nil {
|
2020-08-18 19:08:20 +00:00
|
|
|
log.Errorf("ChainNotify error: %+v", err)
|
2020-04-07 18:02:52 +00:00
|
|
|
|
2020-07-10 14:43:14 +00:00
|
|
|
build.Clock.Sleep(10 * time.Second)
|
2020-04-07 18:02:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gotCur = false
|
2021-11-10 06:47:42 +00:00
|
|
|
log.Info("restarting window post scheduler")
|
2020-04-07 18:02:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
select {
|
2019-12-03 22:34:15 +00:00
|
|
|
case changes, ok := <-notifs:
|
|
|
|
if !ok {
|
2020-09-16 01:19:27 +00:00
|
|
|
log.Warn("window post scheduler notifs channel closed")
|
2020-04-07 18:02:52 +00:00
|
|
|
notifs = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gotCur {
|
|
|
|
if len(changes) != 1 {
|
|
|
|
log.Errorf("expected first notif to have len = 1")
|
|
|
|
continue
|
|
|
|
}
|
2020-08-11 13:28:00 +00:00
|
|
|
chg := changes[0]
|
|
|
|
if chg.Type != store.HCCurrent {
|
2020-04-07 18:02:52 +00:00
|
|
|
log.Errorf("expected first notif to tell current ts")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:31:07 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "WindowPoStScheduler.headChange")
|
|
|
|
|
2020-09-18 16:03:59 +00:00
|
|
|
s.update(ctx, nil, chg.Val)
|
2020-04-07 18:02:52 +00:00
|
|
|
|
2020-09-25 12:31:07 +00:00
|
|
|
span.End()
|
2020-04-07 18:02:52 +00:00
|
|
|
gotCur = true
|
|
|
|
continue
|
2019-12-03 22:34:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "WindowPoStScheduler.headChange")
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-09-18 16:03:59 +00:00
|
|
|
var lowest, highest *types.TipSet = nil, nil
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
for _, change := range changes {
|
2020-01-18 15:00:22 +00:00
|
|
|
if change.Val == nil {
|
|
|
|
log.Errorf("change.Val was nil")
|
|
|
|
}
|
2019-11-28 17:44:49 +00:00
|
|
|
switch change.Type {
|
|
|
|
case store.HCRevert:
|
|
|
|
lowest = change.Val
|
|
|
|
case store.HCApply:
|
|
|
|
highest = change.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:03:59 +00:00
|
|
|
s.update(ctx, lowest, highest)
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
span.End()
|
2019-12-03 22:34:15 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:03:59 +00:00
|
|
|
func (s *WindowPoStScheduler) update(ctx context.Context, revert, apply *types.TipSet) {
|
|
|
|
if apply == nil {
|
|
|
|
log.Error("no new tipset in window post WindowPoStScheduler.update")
|
|
|
|
return
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
2020-09-18 16:03:59 +00:00
|
|
|
err := s.ch.update(ctx, revert, apply)
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
2020-09-18 16:03:59 +00:00
|
|
|
log.Errorf("handling head updates in window post sched: %+v", err)
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:03:59 +00:00
|
|
|
// onAbort is called when generating proofs or submitting proofs is aborted
|
|
|
|
func (s *WindowPoStScheduler) onAbort(ts *types.TipSet, deadline *dline.Info) {
|
2020-10-09 19:52:04 +00:00
|
|
|
s.journal.RecordEvent(s.evtTypes[evtTypeWdPoStScheduler], func() interface{} {
|
2020-09-18 16:03:59 +00:00
|
|
|
c := evtCommon{}
|
|
|
|
if ts != nil {
|
|
|
|
c.Deadline = deadline
|
|
|
|
c.Height = ts.Height()
|
|
|
|
c.TipSet = ts.Cids()
|
|
|
|
}
|
|
|
|
return WdPoStSchedulerEvt{
|
|
|
|
evtCommon: c,
|
|
|
|
State: SchedulerStateAborted,
|
|
|
|
}
|
|
|
|
})
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
2020-08-11 13:28:00 +00:00
|
|
|
|
2020-09-02 18:45:25 +00:00
|
|
|
func (s *WindowPoStScheduler) getEvtCommon(err error) evtCommon {
|
|
|
|
c := evtCommon{Error: err}
|
2020-09-18 16:03:59 +00:00
|
|
|
currentTS, currentDeadline := s.ch.currentTSDI()
|
|
|
|
if currentTS != nil {
|
|
|
|
c.Deadline = currentDeadline
|
|
|
|
c.Height = currentTS.Height()
|
|
|
|
c.TipSet = currentTS.Cids()
|
2020-08-11 13:28:00 +00:00
|
|
|
}
|
2020-09-02 18:45:25 +00:00
|
|
|
return c
|
2020-08-11 13:28:00 +00:00
|
|
|
}
|