2019-11-28 17:44:49 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-08 19:48:17 +00:00
|
|
|
"sync"
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-01-13 20:47:27 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Inactive = 0
|
|
|
|
|
|
|
|
const StartConfidence = 4 // TODO: config
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
type FPoStScheduler struct {
|
2019-11-28 17:44:49 +00:00
|
|
|
api storageMinerApi
|
2020-01-13 20:47:27 +00:00
|
|
|
sb sectorbuilder.Interface
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
actor address.Address
|
|
|
|
worker address.Address
|
|
|
|
|
|
|
|
cur *types.TipSet
|
|
|
|
|
|
|
|
// if a post is in progress, this indicates for which ElectionPeriodStart
|
|
|
|
activeEPS uint64
|
|
|
|
abort context.CancelFunc
|
2019-12-08 19:48:17 +00:00
|
|
|
|
|
|
|
failed uint64 // eps
|
|
|
|
failLk sync.Mutex
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
func NewFPoStScheduler(api storageMinerApi, sb sectorbuilder.Interface, actor address.Address, worker address.Address) *FPoStScheduler {
|
|
|
|
return &FPoStScheduler{api: api, sb: sb, actor: actor, worker: worker}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FPoStScheduler) Run(ctx context.Context) {
|
2019-11-28 17:44:49 +00:00
|
|
|
notifs, err := s.api.ChainNotify(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
current := <-notifs
|
|
|
|
if len(current) != 1 {
|
|
|
|
panic("expected first notif to have len = 1")
|
|
|
|
}
|
2019-11-28 18:08:10 +00:00
|
|
|
if current[0].Type != store.HCCurrent {
|
2019-11-28 17:44:49 +00:00
|
|
|
panic("expected first notif to tell current ts")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.update(ctx, current[0].Val); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:34:15 +00:00
|
|
|
defer s.abortActivePoSt()
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
// not fine to panic after this point
|
|
|
|
for {
|
|
|
|
select {
|
2019-12-03 22:34:15 +00:00
|
|
|
case changes, ok := <-notifs:
|
|
|
|
if !ok {
|
2020-02-03 20:09:21 +00:00
|
|
|
log.Warn("FPoStScheduler notifs channel closed")
|
2019-12-03 22:34:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "FPoStScheduler.headChange")
|
2019-11-28 17:44:49 +00:00
|
|
|
|
|
|
|
var lowest, highest *types.TipSet = s.cur, nil
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.revert(ctx, lowest); err != nil {
|
|
|
|
log.Error("handling head reverts in fallbackPost sched: %+v", err)
|
|
|
|
}
|
|
|
|
if err := s.update(ctx, highest); err != nil {
|
|
|
|
log.Error("handling head updates in fallbackPost sched: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
span.End()
|
2019-12-03 22:34:15 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
func (s *FPoStScheduler) revert(ctx context.Context, newLowest *types.TipSet) error {
|
2019-11-28 17:44:49 +00:00
|
|
|
if s.cur == newLowest {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.cur = newLowest
|
|
|
|
|
|
|
|
newEPS, _, err := s.shouldFallbackPost(ctx, newLowest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if newEPS != s.activeEPS {
|
|
|
|
s.abortActivePoSt()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
func (s *FPoStScheduler) update(ctx context.Context, new *types.TipSet) error {
|
2020-01-18 15:00:22 +00:00
|
|
|
if new == nil {
|
2020-02-03 20:09:21 +00:00
|
|
|
return xerrors.Errorf("no new tipset in FPoStScheduler.update")
|
2020-01-18 15:00:22 +00:00
|
|
|
}
|
2019-11-28 17:44:49 +00:00
|
|
|
newEPS, start, err := s.shouldFallbackPost(ctx, new)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 19:48:17 +00:00
|
|
|
s.failLk.Lock()
|
|
|
|
if s.failed > 0 {
|
|
|
|
s.failed = 0
|
|
|
|
s.activeEPS = 0
|
|
|
|
}
|
|
|
|
s.failLk.Unlock()
|
|
|
|
|
2019-12-03 21:59:08 +00:00
|
|
|
if newEPS == s.activeEPS {
|
|
|
|
return nil
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 21:59:08 +00:00
|
|
|
s.abortActivePoSt()
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
if newEPS != Inactive && start {
|
|
|
|
s.doPost(ctx, newEPS, new)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
func (s *FPoStScheduler) abortActivePoSt() {
|
2019-11-28 17:44:49 +00:00
|
|
|
if s.activeEPS == Inactive {
|
|
|
|
return // noop
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.abort != nil {
|
|
|
|
s.abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("Aborting Fallback PoSt (EPS: %d)", s.activeEPS)
|
|
|
|
|
2019-12-03 21:59:08 +00:00
|
|
|
s.activeEPS = Inactive
|
2019-11-28 17:44:49 +00:00
|
|
|
s.abort = nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
func (s *FPoStScheduler) shouldFallbackPost(ctx context.Context, ts *types.TipSet) (uint64, bool, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
eps, err := s.api.StateMinerElectionPeriodStart(ctx, s.actor, ts.Key())
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, false, xerrors.Errorf("getting ElectionPeriodStart: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts.Height() >= eps+build.FallbackPoStDelay {
|
|
|
|
return eps, ts.Height() >= eps+build.FallbackPoStDelay+StartConfidence, nil
|
|
|
|
}
|
|
|
|
return 0, false, nil
|
|
|
|
}
|