lotus/storage/wdpost_run.go

233 lines
5.9 KiB
Go
Raw Normal View History

package storage
import (
"bytes"
"context"
2020-03-17 20:19:52 +00:00
"github.com/filecoin-project/go-address"
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"
2019-11-28 18:08:10 +00:00
"go.opencensus.io/trace"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
)
2020-04-07 19:55:34 +00:00
func (s *WindowPoStScheduler) failPost(deadline *Deadline) {
log.Errorf("TODO")
/*s.failLk.Lock()
2019-12-08 19:48:17 +00:00
if eps > s.failed {
s.failed = eps
}
2020-04-07 19:55:34 +00:00
s.failLk.Unlock()*/
2019-12-08 19:48:17 +00:00
}
2020-04-07 19:55:34 +00:00
func (s *WindowPoStScheduler) doPost(ctx context.Context, deadline *Deadline, ts *types.TipSet) {
ctx, abort := context.WithCancel(ctx)
s.abort = abort
2020-04-07 19:55:34 +00:00
s.activeDeadline = deadline
go func() {
defer abort()
2020-04-07 17:41:41 +00:00
ctx, span := trace.StartSpan(ctx, "WindowPoStScheduler.doPost")
defer span.End()
2020-04-10 21:07:18 +00:00
proof, err := s.runPost(ctx, *deadline, ts)
if err != nil {
log.Errorf("runPost failed: %+v", err)
2020-04-07 19:55:34 +00:00
s.failPost(deadline)
return
}
if err := s.submitPost(ctx, proof); err != nil {
log.Errorf("submitPost failed: %+v", err)
2020-04-07 19:55:34 +00:00
s.failPost(deadline)
return
}
2019-12-08 19:48:17 +00:00
}()
}
2020-04-07 17:41:41 +00:00
func (s *WindowPoStScheduler) checkFaults(ctx context.Context, ssi []abi.SectorNumber) ([]abi.SectorNumber, error) {
2020-04-07 19:55:34 +00:00
//faults := s.prover.Scrub(ssi)
2020-03-03 22:19:22 +00:00
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
{
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 {
panic("Aaaaaaaaaaaaaaaaaaaa")
2020-01-30 00:50:58 +00:00
}
2019-12-17 22:23:43 +00:00
return faultIDs, nil
}
func (s *WindowPoStScheduler) runPost(ctx context.Context, deadline Deadline, ts *types.TipSet) (*abi.WindowPoStVerifyInfo, error) {
ctx, span := trace.StartSpan(ctx, "storage.runPost")
defer span.End()
2020-04-07 19:55:34 +00:00
challengeRound := deadline.start // TODO: check with spec
buf := new(bytes.Buffer)
if err := s.actor.MarshalCBOR(buf); err != nil {
return nil, xerrors.Errorf("failed to marshal address to cbor: %w", err)
}
rand, err := s.api.ChainGetRandomness(ctx, ts.Key(), crypto.DomainSeparationTag_WindowedPoStChallengeSeed, challengeRound, buf.Bytes())
if err != nil {
2020-04-07 19:55:34 +00:00
return nil, xerrors.Errorf("failed to get chain randomness for windowPost (ts=%d; deadline=%d): %w", ts.Height(), deadline, err)
}
2020-04-07 19:55:34 +00:00
partitions, err := s.getDeadlinePartitions(ts, deadline)
if err != nil {
return nil, err
}
ssi, err := s.sortedSectorInfo(ctx, partitions, ts) // TODO: Optimization: Only get challenged sectors
if err != nil {
return nil, xerrors.Errorf("getting sorted sector info: %w", err)
}
2020-03-07 05:26:00 +00:00
if len(ssi) == 0 {
2020-04-07 17:41:41 +00:00
log.Warn("attempted to run windowPost without any sectors...")
return nil, xerrors.Errorf("no sectors to run windowPost on")
2020-03-07 05:26:00 +00:00
}
2020-04-07 17:41:41 +00:00
log.Infow("running windowPost",
2020-01-21 15:01:42 +00:00
"chain-random", rand,
2020-04-07 19:55:34 +00:00
"deadline", deadline,
2020-01-21 15:01:42 +00:00
"height", ts.Height())
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)
}
tsStart := time.Now()
2020-04-07 17:41:41 +00:00
log.Infow("generating windowPost",
2020-02-27 21:45:31 +00:00
"sectors", len(ssi),
2020-01-21 15:01:42 +00:00
"faults", len(faults))
2020-03-17 20:19:52 +00:00
mid, err := address.IDFromAddress(s.actor)
if err != nil {
return nil, err
}
ci, err := s.prover.GenerateWinningPoStSectorChallenge(ctx, s.proofType, abi.ActorID(mid), abi.PoStRandomness(rand), uint64(len(ssi)))
if err != nil {
return nil, xerrors.Errorf("generating window post challenge: %w", err)
}
cssi := make([]abi.SectorInfo, len(ci))
for i, u := range ci {
cssi[i] = ssi[u]
}
2020-04-10 21:07:18 +00:00
// TODO: Faults!
postOut, err := s.prover.GenerateWindowPoSt(ctx, abi.ActorID(mid), cssi, abi.PoStRandomness(rand))
if err != nil {
return nil, xerrors.Errorf("running post failed: %w", err)
}
2020-04-10 21:07:18 +00:00
if len(postOut) == 0 {
return nil, xerrors.Errorf("received proofs back from generate window post")
}
elapsed := time.Since(tsStart)
2020-04-10 21:07:18 +00:00
log.Infow("submitting PoSt", "elapsed", elapsed)
return &abi.WindowPoStVerifyInfo{
Randomness: abi.PoStRandomness(rand),
Proofs: postOut,
ChallengedSectors: cssi,
Prover: abi.ActorID(mid),
}, nil
}
2020-04-07 19:55:34 +00:00
func (s *WindowPoStScheduler) sortedSectorInfo(ctx context.Context, partitions []abiPartition, ts *types.TipSet) ([]abi.SectorInfo, error) {
sset, err := s.getPartitionSectors(ts, partitions)
if err != nil {
2020-04-07 19:55:34 +00:00
return nil, err
}
2020-02-27 21:45:31 +00:00
sbsi := make([]abi.SectorInfo, len(sset))
for k, sector := range sset {
2020-02-27 21:45:31 +00:00
sbsi[k] = abi.SectorInfo{
2020-04-07 19:55:34 +00:00
SectorNumber: sector.SectorNumber,
SealedCID: sector.SealedCID,
RegisteredProof: sector.RegisteredProof,
}
}
2020-02-27 21:45:31 +00:00
return sbsi, nil
}
func (s *WindowPoStScheduler) submitPost(ctx context.Context, proof *abi.WindowPoStVerifyInfo) error {
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,
Params: enc,
2020-03-18 20:45:37 +00:00
Value: types.NewInt(1000), // currently hard-coded late fee in actor, returned if not late
GasLimit: 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)
}()
return nil
}