2019-11-28 17:44:49 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-07 18:02:52 +00:00
|
|
|
"time"
|
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-04-13 21:25:38 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2020-03-06 05:30:47 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
2020-01-13 20:47:27 +00:00
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const StartConfidence = 4 // TODO: config
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
type WindowPoStScheduler struct {
|
2020-03-08 08:07:58 +00:00
|
|
|
api storageMinerApi
|
2020-04-07 19:55:34 +00:00
|
|
|
prover storage.Prover
|
2020-03-08 08:07:58 +00:00
|
|
|
proofType abi.RegisteredProof
|
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
|
2020-04-07 19:55:34 +00:00
|
|
|
activeDeadline *Deadline
|
2020-04-10 21:07:18 +00:00
|
|
|
abort context.CancelFunc
|
2019-12-08 19:48:17 +00:00
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
//failed abi.ChainEpoch // eps
|
|
|
|
//failLk sync.Mutex
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 21:25:38 +00:00
|
|
|
func NewWindowedPoStScheduler(api storageMinerApi, sb storage.Prover, actor address.Address, worker address.Address) (*WindowPoStScheduler, error) {
|
|
|
|
mss, err := api.StateMinerSectorSize(context.TODO(), actor, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spt, err := ffiwrapper.SealProofTypeFromSectorSize(mss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := spt.RegisteredWindowPoStProof()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &WindowPoStScheduler{api: api, prover: sb, actor: actor, worker: worker, proofType: rt}, nil
|
2020-04-07 19:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Deadline struct {
|
|
|
|
// ID
|
|
|
|
start abi.ChainEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Deadline) Equals(other Deadline) bool {
|
|
|
|
panic("maybe equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
type abiPartition uint64
|
|
|
|
|
|
|
|
func (s *WindowPoStScheduler) getCurrentDeadline(ts *types.TipSet) (Deadline, error) {
|
|
|
|
return Deadline{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowPoStScheduler) getDeadlinePartitions(ts *types.TipSet, d Deadline) ([]abiPartition, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WindowPoStScheduler) getPartitionSectors(ts *types.TipSet, partition []abiPartition) ([]abi.SectorInfo, error) {
|
|
|
|
// TODO: maybe make this per partition
|
|
|
|
return nil, nil
|
2020-02-03 20:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
func (s *WindowPoStScheduler) Run(ctx context.Context) {
|
2019-12-03 22:34:15 +00:00
|
|
|
defer s.abortActivePoSt()
|
|
|
|
|
2020-04-07 18:02:52 +00:00
|
|
|
var notifs <-chan []*store.HeadChange
|
|
|
|
var err error
|
|
|
|
var gotCur bool
|
|
|
|
|
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 {
|
|
|
|
log.Errorf("ChainNotify error: %+v")
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gotCur = false
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
select {
|
2019-12-03 22:34:15 +00:00
|
|
|
case changes, ok := <-notifs:
|
|
|
|
if !ok {
|
2020-04-07 17:41:41 +00:00
|
|
|
log.Warn("WindowPoStScheduler 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
|
|
|
|
}
|
|
|
|
if changes[0].Type != store.HCCurrent {
|
|
|
|
log.Errorf("expected first notif to tell current ts")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.update(ctx, changes[0].Val); err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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-04-07 17:41:41 +00:00
|
|
|
func (s *WindowPoStScheduler) 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
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
newDeadline, err := s.getCurrentDeadline(newLowest)
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
if !s.activeDeadline.Equals(newDeadline) {
|
2019-11-28 17:44:49 +00:00
|
|
|
s.abortActivePoSt()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
func (s *WindowPoStScheduler) update(ctx context.Context, new *types.TipSet) error {
|
2020-01-18 15:00:22 +00:00
|
|
|
if new == nil {
|
2020-04-07 17:41:41 +00:00
|
|
|
return xerrors.Errorf("no new tipset in WindowPoStScheduler.update")
|
2020-01-18 15:00:22 +00:00
|
|
|
}
|
2020-04-07 19:55:34 +00:00
|
|
|
shouldPost, newDeadline, err := s.shouldPost(ctx, new)
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-07 19:55:34 +00:00
|
|
|
if !shouldPost {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
/*s.failLk.Lock()
|
2019-12-08 19:48:17 +00:00
|
|
|
if s.failed > 0 {
|
|
|
|
s.failed = 0
|
|
|
|
s.activeEPS = 0
|
|
|
|
}
|
2020-04-07 19:55:34 +00:00
|
|
|
s.failLk.Unlock()*/
|
|
|
|
|
2019-12-03 21:59:08 +00:00
|
|
|
s.abortActivePoSt()
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
if newDeadline != nil {
|
|
|
|
s.doPost(ctx, newDeadline, new)
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:41:41 +00:00
|
|
|
func (s *WindowPoStScheduler) abortActivePoSt() {
|
2020-04-07 19:55:34 +00:00
|
|
|
if s.activeDeadline == nil {
|
2019-11-28 17:44:49 +00:00
|
|
|
return // noop
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.abort != nil {
|
|
|
|
s.abort()
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
log.Warnf("Aborting Fallback PoSt (Deadline: %+v)", s.activeDeadline)
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
s.activeDeadline = nil
|
2019-11-28 17:44:49 +00:00
|
|
|
s.abort = nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
func (s *WindowPoStScheduler) shouldPost(ctx context.Context, ts *types.TipSet) (bool, *Deadline, error) {
|
2020-04-16 17:17:56 +00:00
|
|
|
|
|
|
|
|
2020-04-07 19:55:34 +00:00
|
|
|
// call getCurrentDeadline, set activeDeadline if needed
|
|
|
|
panic("todo check actor state for post in the deadline")
|
|
|
|
return true, nil, nil
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|