81ba6ab6f0
* feat: lp mig - first few steps * lp mig: default tasks * code comments * docs * lp-mig-progress * shared * comments and todos * fix: curio: rename lotus-provider to curio (#11645) * rename provider to curio * install gotext * fix lint errors, mod tidy * fix typo * fix API_INFO and add gotext to circleCI * add back gotext * add gotext after remerge * lp: channels doc * finish easy-migration TODOs * out generate * merging and more renames * avoid make-all * minor doc stuff * cu: make gen * make gen fix * make gen * tryfix * go mod tidy * minor ez migration fixes * ez setup - ui cleanups * better error message * guided setup colors * better path to saveconfigtolayer * loadconfigwithupgrades fix * readMiner oops * guided - homedir * err if miner is running * prompt error should exit * process already running, miner_id sectors in migration * dont prompt for language a second time * check miner stopped * unlock repo * render and sql oops * curio easyMig - some fixes * easyMigration runs successfully * lint * review fixes * fix backup path * fixes1 * fixes2 * fixes 3 --------- Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Co-authored-by: LexLuthr <lexluthr@protocol.ai>
137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package chainsched
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
"go.opencensus.io/trace"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/build"
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
)
|
|
|
|
var log = logging.Logger("curio/chainsched")
|
|
|
|
type NodeAPI interface {
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
ChainNotify(context.Context) (<-chan []*api.HeadChange, error)
|
|
}
|
|
|
|
type CurioChainSched struct {
|
|
api NodeAPI
|
|
|
|
callbacks []UpdateFunc
|
|
started bool
|
|
}
|
|
|
|
func New(api NodeAPI) *CurioChainSched {
|
|
return &CurioChainSched{
|
|
api: api,
|
|
}
|
|
}
|
|
|
|
type UpdateFunc func(ctx context.Context, revert, apply *types.TipSet) error
|
|
|
|
func (s *CurioChainSched) AddHandler(ch UpdateFunc) error {
|
|
if s.started {
|
|
return xerrors.Errorf("cannot add handler after start")
|
|
}
|
|
|
|
s.callbacks = append(s.callbacks, ch)
|
|
return nil
|
|
}
|
|
|
|
func (s *CurioChainSched) Run(ctx context.Context) {
|
|
s.started = true
|
|
|
|
var (
|
|
notifs <-chan []*api.HeadChange
|
|
err error
|
|
gotCur bool
|
|
)
|
|
|
|
// not fine to panic after this point
|
|
for {
|
|
if notifs == nil {
|
|
notifs, err = s.api.ChainNotify(ctx)
|
|
if err != nil {
|
|
log.Errorf("ChainNotify error: %+v", err)
|
|
|
|
build.Clock.Sleep(10 * time.Second)
|
|
continue
|
|
}
|
|
|
|
gotCur = false
|
|
log.Info("restarting chain scheduler")
|
|
}
|
|
|
|
select {
|
|
case changes, ok := <-notifs:
|
|
if !ok {
|
|
log.Warn("chain notifs channel closed")
|
|
notifs = nil
|
|
continue
|
|
}
|
|
|
|
if !gotCur {
|
|
if len(changes) != 1 {
|
|
log.Errorf("expected first notif to have len = 1")
|
|
continue
|
|
}
|
|
chg := changes[0]
|
|
if chg.Type != store.HCCurrent {
|
|
log.Errorf("expected first notif to tell current ts")
|
|
continue
|
|
}
|
|
|
|
ctx, span := trace.StartSpan(ctx, "CurioChainSched.headChange")
|
|
|
|
s.update(ctx, nil, chg.Val)
|
|
|
|
span.End()
|
|
gotCur = true
|
|
continue
|
|
}
|
|
|
|
ctx, span := trace.StartSpan(ctx, "CurioChainSched.headChange")
|
|
|
|
var lowest, highest *types.TipSet = nil, nil
|
|
|
|
for _, change := range changes {
|
|
if change.Val == nil {
|
|
log.Errorf("change.Val was nil")
|
|
}
|
|
switch change.Type {
|
|
case store.HCRevert:
|
|
lowest = change.Val
|
|
case store.HCApply:
|
|
highest = change.Val
|
|
}
|
|
}
|
|
|
|
s.update(ctx, lowest, highest)
|
|
|
|
span.End()
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *CurioChainSched) update(ctx context.Context, revert, apply *types.TipSet) {
|
|
if apply == nil {
|
|
log.Error("no new tipset in CurioChainSched.update")
|
|
return
|
|
}
|
|
|
|
for _, ch := range s.callbacks {
|
|
if err := ch(ctx, revert, apply); err != nil {
|
|
log.Errorf("handling head updates in curio chain sched: %+v", err)
|
|
}
|
|
}
|
|
}
|