2019-11-28 17:44:49 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-28 18:08:10 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-12 22:12:11 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
2019-11-28 18:08:10 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (s *FPoStScheduler) failPost(eps abi.ChainEpoch) {
|
2019-12-08 19:48:17 +00:00
|
|
|
s.failLk.Lock()
|
|
|
|
if eps > s.failed {
|
|
|
|
s.failed = eps
|
|
|
|
}
|
|
|
|
s.failLk.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (s *FPoStScheduler) doPost(ctx context.Context, eps abi.ChainEpoch, ts *types.TipSet) {
|
2019-11-28 17:44:49 +00:00
|
|
|
ctx, abort := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
s.abort = abort
|
|
|
|
s.activeEPS = eps
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer abort()
|
|
|
|
|
2020-02-03 20:09:21 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "FPoStScheduler.doPost")
|
2019-11-28 17:44:49 +00:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
proof, err := s.runPost(ctx, eps, ts)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("runPost failed: %+v", err)
|
2019-12-08 19:48:17 +00:00
|
|
|
s.failPost(eps)
|
2019-11-28 17:44:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.submitPost(ctx, proof); err != nil {
|
|
|
|
log.Errorf("submitPost failed: %+v", err)
|
2019-12-08 19:48:17 +00:00
|
|
|
s.failPost(eps)
|
2019-11-28 17:44:49 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-08 19:48:17 +00:00
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:12:11 +00:00
|
|
|
func (s *FPoStScheduler) declareFaults(ctx context.Context, fc uint64, params *miner.DeclareTemporaryFaultsParams) error {
|
2020-01-30 00:50:58 +00:00
|
|
|
log.Warnf("DECLARING %d FAULTS", fc)
|
|
|
|
|
|
|
|
enc, aerr := actors.SerializeParams(params)
|
|
|
|
if aerr != nil {
|
|
|
|
return xerrors.Errorf("could not serialize declare faults parameters: %w", aerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: s.actor,
|
|
|
|
From: s.worker,
|
2020-02-12 22:12:11 +00:00
|
|
|
Method: builtin.MethodsMiner.DeclareTemporaryFaults,
|
2020-01-30 00:50:58 +00:00
|
|
|
Params: enc,
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasLimit: types.NewInt(10000000), // i dont know help
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := s.api.MpoolPushMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("pushing faults message to mpool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec, err := s.api.StateWaitMsg(ctx, sm.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("waiting for declare faults: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.Receipt.ExitCode != 0 {
|
|
|
|
return xerrors.Errorf("declare faults exit %d", rec.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Faults declared successfully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
func (s *FPoStScheduler) checkFaults(ctx context.Context, ssi []abi.SectorNumber) ([]abi.SectorNumber, error) {
|
2020-03-03 22:19:22 +00:00
|
|
|
//faults := s.sb.Scrub(ssi)
|
|
|
|
log.Warnf("Stub checkFaults")
|
2020-03-05 19:21:06 +00:00
|
|
|
var faults []struct {
|
|
|
|
SectorNum abi.SectorNumber
|
|
|
|
Err error
|
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
declaredFaults := map[abi.SectorNumber]struct{}{}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-01-30 00:50:58 +00:00
|
|
|
{
|
2020-02-11 23:29:45 +00:00
|
|
|
chainFaults, err := s.api.StateMinerFaults(ctx, s.actor, types.EmptyTSK)
|
2020-01-30 00:50:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("checking on-chain faults: %w", err)
|
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-01-30 00:50:58 +00:00
|
|
|
for _, fault := range chainFaults {
|
|
|
|
declaredFaults[fault] = struct{}{}
|
2019-12-17 22:23:43 +00:00
|
|
|
}
|
2020-01-30 00:50:58 +00:00
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-02-12 22:12:11 +00:00
|
|
|
var faultIDs []abi.SectorNumber
|
2020-01-30 00:50:58 +00:00
|
|
|
if len(faults) > 0 {
|
2020-02-14 14:14:39 +00:00
|
|
|
params := &miner.DeclareTemporaryFaultsParams{
|
2020-02-14 21:38:30 +00:00
|
|
|
Duration: 900, // TODO: duration is annoying
|
|
|
|
SectorNumbers: abi.NewBitField(),
|
2020-02-14 14:14:39 +00:00
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-01-30 00:50:58 +00:00
|
|
|
for _, fault := range faults {
|
2020-02-12 22:12:11 +00:00
|
|
|
if _, ok := declaredFaults[(fault.SectorNum)]; ok {
|
2020-01-30 00:50:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
log.Warnf("new fault detected: sector %d: %s", fault.SectorNum, fault.Err)
|
|
|
|
declaredFaults[fault.SectorNum] = struct{}{}
|
2019-12-17 22:23:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 22:12:11 +00:00
|
|
|
faultIDs = make([]abi.SectorNumber, 0, len(declaredFaults))
|
|
|
|
for fault := range declaredFaults {
|
2020-02-14 00:24:24 +00:00
|
|
|
faultIDs = append(faultIDs, fault)
|
2020-02-14 14:14:39 +00:00
|
|
|
params.SectorNumbers.Set(uint64(fault))
|
2019-12-17 22:23:43 +00:00
|
|
|
}
|
2020-02-12 22:12:11 +00:00
|
|
|
|
|
|
|
if len(faultIDs) > 0 {
|
|
|
|
if err := s.declareFaults(ctx, uint64(len(faultIDs)), params); err != nil {
|
2020-01-30 00:50:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
}
|
2020-01-30 00:50:58 +00:00
|
|
|
}
|
2019-12-17 22:23:43 +00:00
|
|
|
|
|
|
|
return faultIDs, nil
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
func (s *FPoStScheduler) runPost(ctx context.Context, eps abi.ChainEpoch, ts *types.TipSet) (*abi.OnChainPoStVerifyInfo, error) {
|
2019-11-28 17:44:49 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "storage.runPost")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-02-23 20:00:47 +00:00
|
|
|
challengeRound := eps + build.FallbackPoStDelay
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-02-23 20:00:47 +00:00
|
|
|
rand, err := s.api.ChainGetRandomness(ctx, ts.Key(), crypto.DomainSeparationTag_WindowedPoStChallengeSeed, challengeRound, s.actor.Bytes())
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get chain randomness for fpost (ts=%d; eps=%d): %w", ts.Height(), eps, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ssi, err := s.sortedSectorInfo(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting sorted sector info: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:01:42 +00:00
|
|
|
log.Infow("running fPoSt",
|
|
|
|
"chain-random", rand,
|
|
|
|
"eps", eps,
|
|
|
|
"height", ts.Height())
|
2019-11-28 17:44:49 +00:00
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
var snums []abi.SectorNumber
|
|
|
|
for _, si := range ssi {
|
|
|
|
snums = append(snums, si.SectorNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
faults, err := s.checkFaults(ctx, snums)
|
2019-12-17 22:23:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to declare faults: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
tsStart := time.Now()
|
|
|
|
|
2020-01-21 15:01:42 +00:00
|
|
|
log.Infow("generating fPoSt",
|
2020-02-27 21:45:31 +00:00
|
|
|
"sectors", len(ssi),
|
2020-01-21 15:01:42 +00:00
|
|
|
"faults", len(faults))
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
scandidates, proof, err := s.sb.GenerateFallbackPoSt(ssi, abi.PoStRandomness(rand), faults)
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("running post failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(tsStart)
|
|
|
|
log.Infow("submitting PoSt", "pLen", len(proof), "elapsed", elapsed)
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
candidates := make([]abi.PoStCandidate, len(scandidates))
|
2019-11-28 17:44:49 +00:00
|
|
|
for i, sc := range scandidates {
|
2019-12-06 08:32:57 +00:00
|
|
|
part := make([]byte, 32)
|
2020-02-27 21:45:31 +00:00
|
|
|
copy(part, sc.Candidate.PartialTicket[:])
|
2020-02-14 14:14:39 +00:00
|
|
|
candidates[i] = abi.PoStCandidate{
|
|
|
|
RegisteredProof: abi.RegisteredProof_StackedDRG32GiBPoSt, // TODO: build setting
|
2020-02-14 21:38:30 +00:00
|
|
|
PartialTicket: part,
|
2020-02-27 21:45:31 +00:00
|
|
|
SectorID: sc.Candidate.SectorID,
|
|
|
|
ChallengeIndex: sc.Candidate.ChallengeIndex,
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
return &abi.OnChainPoStVerifyInfo{
|
2020-02-27 21:45:31 +00:00
|
|
|
Proofs: proof,
|
2019-11-28 17:44:49 +00:00
|
|
|
Candidates: candidates,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
func (s *FPoStScheduler) sortedSectorInfo(ctx context.Context, ts *types.TipSet) ([]abi.SectorInfo, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
sset, err := s.api.StateMinerProvingSet(ctx, s.actor, ts.Key())
|
2019-11-28 17:44:49 +00:00
|
|
|
if err != nil {
|
2020-02-27 21:45:31 +00:00
|
|
|
return nil, xerrors.Errorf("failed to get proving set for miner (tsH: %d): %w", ts.Height(), err)
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
if len(sset) == 0 {
|
|
|
|
log.Warn("empty proving set! (ts.H: %d)", ts.Height())
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
sbsi := make([]abi.SectorInfo, len(sset))
|
2019-11-28 17:44:49 +00:00
|
|
|
for k, sector := range sset {
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
sbsi[k] = abi.SectorInfo{
|
|
|
|
SectorNumber: sector.Info.Info.SectorNumber,
|
|
|
|
SealedCID: sector.Info.Info.SealedCID,
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
return sbsi, nil
|
2019-11-28 17:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:14:39 +00:00
|
|
|
func (s *FPoStScheduler) submitPost(ctx context.Context, proof *abi.OnChainPoStVerifyInfo) error {
|
2019-11-28 17:44:49 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "storage.commitPost")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
enc, aerr := actors.SerializeParams(proof)
|
|
|
|
if aerr != nil {
|
|
|
|
return xerrors.Errorf("could not serialize submit post parameters: %w", aerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: s.actor,
|
|
|
|
From: s.worker,
|
2020-02-14 14:14:39 +00:00
|
|
|
Method: builtin.MethodsMiner.SubmitWindowedPoSt,
|
2019-11-28 17:44:49 +00:00
|
|
|
Params: enc,
|
|
|
|
Value: types.NewInt(1000), // currently hard-coded late fee in actor, returned if not late
|
|
|
|
GasLimit: types.NewInt(10000000), // i dont know help
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: consider maybe caring about the output
|
|
|
|
sm, err := s.api.MpoolPushMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("pushing message to mpool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Submitted fallback post: %s", sm.Cid())
|
|
|
|
|
2020-01-29 22:47:28 +00:00
|
|
|
go func() {
|
|
|
|
rec, err := s.api.StateWaitMsg(context.TODO(), sm.Cid())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.Receipt.ExitCode == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("Submitting fallback post %s failed: exit %d", sm.Cid(), rec.Receipt.ExitCode)
|
|
|
|
}()
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
return nil
|
|
|
|
}
|