2020-01-23 15:38:01 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-01-23 17:34:04 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
"github.com/filecoin-project/go-statemachine"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
2020-01-23 15:38:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const minRetryTime = 1 * time.Minute
|
|
|
|
|
|
|
|
func failedCooldown(ctx statemachine.Context, sector SectorInfo) error {
|
2020-04-03 17:45:48 +00:00
|
|
|
// TODO: Exponential backoff when we see consecutive failures
|
|
|
|
|
2020-01-23 15:38:01 +00:00
|
|
|
retryStart := time.Unix(int64(sector.Log[len(sector.Log)-1].Timestamp), 0).Add(minRetryTime)
|
|
|
|
if len(sector.Log) > 0 && !time.Now().After(retryStart) {
|
2020-04-06 22:31:33 +00:00
|
|
|
log.Infof("%s(%d), waiting %s before retrying", sector.State, sector.SectorNumber, time.Until(retryStart))
|
2020-01-23 15:38:01 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(time.Until(retryStart)):
|
|
|
|
case <-ctx.Context().Done():
|
|
|
|
return ctx.Context().Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 07:44:20 +00:00
|
|
|
func (m *Sealing) checkPreCommitted(ctx statemachine.Context, sector SectorInfo) (*miner.SectorPreCommitOnChainInfo, bool) {
|
2020-04-06 18:07:26 +00:00
|
|
|
tok, _, err := m.api.ChainHead(ctx.Context())
|
2020-01-23 15:38:01 +00:00
|
|
|
if err != nil {
|
2020-06-04 15:29:31 +00:00
|
|
|
log.Errorf("handleSealPrecommit1Failed(%d): temp error: %+v", sector.SectorNumber, err)
|
2020-01-23 17:34:04 +00:00
|
|
|
return nil, true
|
2020-01-23 15:38:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:31:33 +00:00
|
|
|
info, err := m.api.StateSectorPreCommitInfo(ctx.Context(), m.maddr, sector.SectorNumber, tok)
|
2020-01-23 15:38:01 +00:00
|
|
|
if err != nil {
|
2020-06-04 15:29:31 +00:00
|
|
|
log.Errorf("handleSealPrecommit1Failed(%d): temp error: %+v", sector.SectorNumber, err)
|
2020-01-23 17:34:04 +00:00
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
return info, false
|
2020-01-23 17:34:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:29:31 +00:00
|
|
|
func (m *Sealing) handleSealPrecommit1Failed(ctx statemachine.Context, sector SectorInfo) error {
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
2020-01-23 15:38:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorRetrySealPreCommit1{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) handleSealPrecommit2Failed(ctx statemachine.Context, sector SectorInfo) error {
|
2020-01-23 15:38:01 +00:00
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-04 15:29:31 +00:00
|
|
|
if sector.PreCommit2Fails > 1 {
|
|
|
|
return ctx.Send(SectorRetrySealPreCommit1{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetrySealPreCommit2{})
|
2020-01-23 15:38:01 +00:00
|
|
|
}
|
2020-01-23 17:34:04 +00:00
|
|
|
|
|
|
|
func (m *Sealing) handlePreCommitFailed(ctx statemachine.Context, sector SectorInfo) error {
|
2020-04-09 17:34:07 +00:00
|
|
|
tok, height, err := m.api.ChainHead(ctx.Context())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("handlePreCommitFailed: api error, not proceeding: %+v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkPrecommit(ctx.Context(), m.Address(), sector, tok, height, m.api); err != nil {
|
2020-01-23 17:34:04 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case *ErrApi:
|
|
|
|
log.Errorf("handlePreCommitFailed: api error, not proceeding: %+v", err)
|
|
|
|
return nil
|
2020-04-03 16:54:01 +00:00
|
|
|
case *ErrBadCommD: // TODO: Should this just back to packing? (not really needed since handlePreCommit1 will do that too)
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("bad CommD error: %w", err)})
|
2020-01-23 17:34:04 +00:00
|
|
|
case *ErrExpiredTicket:
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("ticket expired error: %w", err)})
|
2020-06-02 21:45:28 +00:00
|
|
|
case *ErrBadTicket:
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("bad expired: %w", err)})
|
2020-08-05 01:30:58 +00:00
|
|
|
case *ErrNoPrecommit:
|
|
|
|
return ctx.Send(SectorRetryPreCommit{})
|
2020-06-02 21:45:28 +00:00
|
|
|
case *ErrPrecommitOnChain:
|
|
|
|
// noop
|
2020-01-23 17:34:04 +00:00
|
|
|
default:
|
2020-04-03 17:45:48 +00:00
|
|
|
return xerrors.Errorf("checkPrecommit sanity check error: %w", err)
|
2020-01-23 17:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pci, is := m.checkPreCommitted(ctx, sector); is && pci != nil {
|
|
|
|
if sector.PreCommitMessage != nil {
|
2020-04-06 22:31:33 +00:00
|
|
|
log.Warn("sector %d is precommitted on chain, but we don't have precommit message", sector.SectorNumber)
|
2020-06-02 21:45:28 +00:00
|
|
|
return ctx.Send(SectorPreCommitLanded{TipSet: tok})
|
2020-01-23 17:34:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
if pci.Info.SealedCID != *sector.CommR {
|
2020-04-06 22:31:33 +00:00
|
|
|
log.Warn("sector %d is precommitted on chain, with different CommR: %x != %x", sector.SectorNumber, pci.Info.SealedCID, sector.CommR)
|
2020-01-23 17:34:04 +00:00
|
|
|
return nil // TODO: remove when the actor allows re-precommit
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we could compare more things, but I don't think we really need to
|
|
|
|
// CommR tells us that CommD (and CommPs), and the ticket are all matching
|
|
|
|
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetryWaitSeed{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if sector.PreCommitMessage != nil {
|
|
|
|
log.Warn("retrying precommit even though the message failed to apply")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetryPreCommit{})
|
|
|
|
}
|
2020-04-03 17:45:48 +00:00
|
|
|
|
|
|
|
func (m *Sealing) handleComputeProofFailed(ctx statemachine.Context, sector SectorInfo) error {
|
|
|
|
// TODO: Check sector files
|
|
|
|
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:30:40 +00:00
|
|
|
if sector.InvalidProofs > 1 {
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("consecutive compute fails")})
|
2020-06-02 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:45:48 +00:00
|
|
|
return ctx.Send(SectorRetryComputeProof{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) handleCommitFailed(ctx statemachine.Context, sector SectorInfo) error {
|
2020-04-09 17:34:07 +00:00
|
|
|
tok, height, err := m.api.ChainHead(ctx.Context())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("handleCommitting: api error, not proceeding: %+v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkPrecommit(ctx.Context(), m.maddr, sector, tok, height, m.api); err != nil {
|
2020-04-03 17:45:48 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case *ErrApi:
|
|
|
|
log.Errorf("handleCommitFailed: api error, not proceeding: %+v", err)
|
|
|
|
return nil
|
|
|
|
case *ErrBadCommD:
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("bad CommD error: %w", err)})
|
2020-04-03 17:45:48 +00:00
|
|
|
case *ErrExpiredTicket:
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("ticket expired error: %w", err)})
|
2020-06-02 21:45:28 +00:00
|
|
|
case *ErrBadTicket:
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("bad expired: %w", err)})
|
2020-08-05 01:30:58 +00:00
|
|
|
case nil:
|
|
|
|
return ctx.Send(SectorChainPreCommitFailed{xerrors.Errorf("no precommit: %w", err)})
|
2020-06-02 21:45:28 +00:00
|
|
|
case *ErrPrecommitOnChain:
|
|
|
|
// noop, this is expected
|
2020-04-03 17:45:48 +00:00
|
|
|
default:
|
2020-08-05 01:30:58 +00:00
|
|
|
return xerrors.Errorf("checkPrecommit sanity check error (%T): %w", err, err)
|
2020-04-03 17:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:34:07 +00:00
|
|
|
if err := m.checkCommit(ctx.Context(), sector, sector.Proof, tok); err != nil {
|
2020-04-03 17:45:48 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case *ErrApi:
|
|
|
|
log.Errorf("handleCommitFailed: api error, not proceeding: %+v", err)
|
|
|
|
return nil
|
|
|
|
case *ErrBadSeed:
|
|
|
|
log.Errorf("seed changed, will retry: %+v", err)
|
|
|
|
return ctx.Send(SectorRetryWaitSeed{})
|
2020-04-04 01:50:05 +00:00
|
|
|
case *ErrInvalidProof:
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sector.InvalidProofs > 0 {
|
2020-06-04 15:29:31 +00:00
|
|
|
return ctx.Send(SectorSealPreCommit1Failed{xerrors.Errorf("consecutive invalid proofs")})
|
2020-04-04 01:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetryInvalidProof{})
|
2020-06-17 15:19:36 +00:00
|
|
|
case *ErrPrecommitOnChain:
|
|
|
|
log.Errorf("no precommit on chain, will retry: %+v", err)
|
|
|
|
return ctx.Send(SectorRetryPreCommitWait{})
|
2020-08-05 01:30:58 +00:00
|
|
|
case *ErrNoPrecommit:
|
|
|
|
return ctx.Send(SectorRetryPreCommit{})
|
2020-04-03 17:45:48 +00:00
|
|
|
default:
|
2020-08-05 01:30:58 +00:00
|
|
|
return xerrors.Errorf("checkCommit sanity check error (%T): %w", err, err)
|
2020-04-03 17:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check sector files
|
|
|
|
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetryComputeProof{})
|
|
|
|
}
|
2020-06-03 21:42:13 +00:00
|
|
|
|
|
|
|
func (m *Sealing) handleFinalizeFailed(ctx statemachine.Context, sector SectorInfo) error {
|
|
|
|
// TODO: Check sector files
|
|
|
|
|
|
|
|
if err := failedCooldown(ctx, sector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Send(SectorRetryFinalize{})
|
|
|
|
}
|