2023-10-25 18:58:16 +00:00
|
|
|
package chainsched
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-25 19:13:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-10-25 18:58:16 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
var log = logging.Logger("curio/chainsched")
|
2023-10-25 18:58:16 +00:00
|
|
|
|
|
|
|
type NodeAPI interface {
|
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
ChainNotify(context.Context) (<-chan []*api.HeadChange, error)
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
type CurioChainSched struct {
|
2023-10-25 18:58:16 +00:00
|
|
|
api NodeAPI
|
|
|
|
|
|
|
|
callbacks []UpdateFunc
|
|
|
|
started bool
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func New(api NodeAPI) *CurioChainSched {
|
|
|
|
return &CurioChainSched{
|
2023-10-25 18:58:16 +00:00
|
|
|
api: api,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateFunc func(ctx context.Context, revert, apply *types.TipSet) error
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func (s *CurioChainSched) AddHandler(ch UpdateFunc) error {
|
2023-10-25 18:58:16 +00:00
|
|
|
if s.started {
|
|
|
|
return xerrors.Errorf("cannot add handler after start")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.callbacks = append(s.callbacks, ch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func (s *CurioChainSched) Run(ctx context.Context) {
|
2023-10-25 18:58:16 +00:00
|
|
|
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
|
2024-01-05 16:07:55 +00:00
|
|
|
log.Info("restarting chain scheduler")
|
2023-10-25 18:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case changes, ok := <-notifs:
|
|
|
|
if !ok {
|
2024-01-05 16:07:55 +00:00
|
|
|
log.Warn("chain notifs channel closed")
|
2023-10-25 18:58:16 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "CurioChainSched.headChange")
|
2023-10-25 18:58:16 +00:00
|
|
|
|
|
|
|
s.update(ctx, nil, chg.Val)
|
|
|
|
|
|
|
|
span.End()
|
|
|
|
gotCur = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "CurioChainSched.headChange")
|
2023-10-25 18:58:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
func (s *CurioChainSched) update(ctx context.Context, revert, apply *types.TipSet) {
|
2023-10-25 18:58:16 +00:00
|
|
|
if apply == nil {
|
2024-03-15 21:38:13 +00:00
|
|
|
log.Error("no new tipset in CurioChainSched.update")
|
2023-10-25 18:58:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range s.callbacks {
|
|
|
|
if err := ch(ctx, revert, apply); err != nil {
|
2024-03-15 21:38:13 +00:00
|
|
|
log.Errorf("handling head updates in curio chain sched: %+v", err)
|
2023-10-25 18:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|