lotus/extern/storage-sealing/checks.go

195 lines
6.6 KiB
Go
Raw Normal View History

2020-01-20 22:03:50 +00:00
package sealing
import (
2020-04-20 18:21:11 +00:00
"bytes"
2020-01-20 22:03:50 +00:00
"context"
2020-09-07 06:08:53 +00:00
v0proof "github.com/filecoin-project/specs-actors/actors/runtime/proof"
2020-01-20 22:03:50 +00:00
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/sector-storage/zerocomm"
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
2020-01-20 22:03:50 +00:00
)
// TODO: For now we handle this by halting state execution, when we get jsonrpc reconnecting
// We should implement some wait-for-api logic
type ErrApi struct{ error }
2020-01-23 12:17:45 +00:00
type ErrInvalidDeals struct{ error }
2020-02-23 00:47:47 +00:00
type ErrInvalidPiece struct{ error }
type ErrExpiredDeals struct{ error }
2020-01-23 12:17:45 +00:00
type ErrBadCommD struct{ error }
type ErrExpiredTicket struct{ error }
2020-06-02 21:45:28 +00:00
type ErrBadTicket struct{ error }
type ErrPrecommitOnChain struct{ error }
type ErrSectorNumberAllocated struct{ error }
2020-01-23 14:05:44 +00:00
type ErrBadSeed struct{ error }
2020-04-04 01:50:05 +00:00
type ErrInvalidProof struct{ error }
2020-06-17 15:19:36 +00:00
type ErrNoPrecommit struct{ error }
type ErrCommitWaitFailed struct{ error }
func checkPieces(ctx context.Context, maddr address.Address, si SectorInfo, api SealingAPI) error {
tok, height, err := api.ChainHead(ctx)
2020-01-23 12:17:45 +00:00
if err != nil {
return &ErrApi{xerrors.Errorf("getting chain head: %w", err)}
2020-01-23 12:17:45 +00:00
}
for i, p := range si.Pieces {
// if no deal is associated with the piece, ensure that we added it as
// filler (i.e. ensure that it has a zero PieceCID)
if p.DealInfo == nil {
exp := zerocomm.ZeroPieceCommitment(p.Piece.Size.Unpadded())
if !p.Piece.PieceCID.Equals(exp) {
return &ErrInvalidPiece{xerrors.Errorf("sector %d piece %d had non-zero PieceCID %+v", si.SectorNumber, i, p.Piece.PieceCID)}
2020-02-23 00:47:47 +00:00
}
continue
}
proposal, err := api.StateMarketStorageDeal(ctx, p.DealInfo.DealID, tok)
2020-01-20 22:03:50 +00:00
if err != nil {
2020-08-05 01:30:58 +00:00
return &ErrInvalidDeals{xerrors.Errorf("getting deal %d for piece %d: %w", p.DealInfo.DealID, i, err)}
2020-01-20 22:03:50 +00:00
}
if proposal.Provider != maddr {
return &ErrInvalidDeals{xerrors.Errorf("piece %d (of %d) of sector %d refers deal %d with wrong provider: %s != %s", i, len(si.Pieces), si.SectorNumber, p.DealInfo.DealID, proposal.Provider, maddr)}
}
if proposal.PieceCID != p.Piece.PieceCID {
return &ErrInvalidDeals{xerrors.Errorf("piece %d (of %d) of sector %d refers deal %d with wrong PieceCID: %x != %x", i, len(si.Pieces), si.SectorNumber, p.DealInfo.DealID, p.Piece.PieceCID, proposal.PieceCID)}
2020-01-20 22:03:50 +00:00
}
if p.Piece.Size != proposal.PieceSize {
return &ErrInvalidDeals{xerrors.Errorf("piece %d (of %d) of sector %d refers deal %d with different size: %d != %d", i, len(si.Pieces), si.SectorNumber, p.DealInfo.DealID, p.Piece.Size, proposal.PieceSize)}
2020-01-23 12:17:45 +00:00
}
if height >= proposal.StartEpoch {
return &ErrExpiredDeals{xerrors.Errorf("piece %d (of %d) of sector %d refers expired deal %d - should start at %d, head %d", i, len(si.Pieces), si.SectorNumber, p.DealInfo.DealID, proposal.StartEpoch, height)}
2020-01-20 22:03:50 +00:00
}
}
return nil
}
// checkPrecommit checks that data commitment generated in the sealing process
2020-01-24 20:15:02 +00:00
// matches pieces, and that the seal ticket isn't expired
func checkPrecommit(ctx context.Context, maddr address.Address, si SectorInfo, tok TipSetToken, height abi.ChainEpoch, api SealingAPI) (err error) {
if err := checkPieces(ctx, maddr, si, api); err != nil {
return err
}
commD, err := api.StateComputeDataCommitment(ctx, maddr, si.SectorType, si.dealIDs(), tok)
2020-01-20 22:03:50 +00:00
if err != nil {
return &ErrApi{xerrors.Errorf("calling StateComputeDataCommitment: %w", err)}
2020-02-23 00:47:47 +00:00
}
if si.CommD == nil || !commD.Equals(*si.CommD) {
return &ErrBadCommD{xerrors.Errorf("on chain CommD differs from sector: %s != %s", commD, si.CommD)}
2020-01-23 14:05:44 +00:00
}
if height-(si.TicketEpoch+SealRandomnessLookback) > SealRandomnessLookbackLimit(si.SectorType) {
return &ErrExpiredTicket{xerrors.Errorf("ticket expired: seal height: %d, head: %d", si.TicketEpoch+SealRandomnessLookback, height)}
2020-01-20 22:03:50 +00:00
}
2020-06-02 21:45:28 +00:00
pci, err := api.StateSectorPreCommitInfo(ctx, maddr, si.SectorNumber, tok)
if err != nil {
if err == ErrSectorAllocated {
return &ErrSectorNumberAllocated{err}
}
2020-06-02 21:45:28 +00:00
return &ErrApi{xerrors.Errorf("getting precommit info: %w", err)}
}
if pci != nil {
if pci.Info.SealRandEpoch != si.TicketEpoch {
2020-08-05 01:30:58 +00:00
return &ErrBadTicket{xerrors.Errorf("bad ticket epoch: %d != %d", pci.Info.SealRandEpoch, si.TicketEpoch)}
2020-06-02 21:45:28 +00:00
}
2020-08-05 01:30:58 +00:00
return &ErrPrecommitOnChain{xerrors.Errorf("precommit already on chain")}
2020-06-02 21:45:28 +00:00
}
2020-01-20 22:03:50 +00:00
return nil
}
func (m *Sealing) checkCommit(ctx context.Context, si SectorInfo, proof []byte, tok TipSetToken) (err error) {
2020-04-06 20:23:37 +00:00
if si.SeedEpoch == 0 {
return &ErrBadSeed{xerrors.Errorf("seed epoch was not set")}
}
2020-04-06 22:31:33 +00:00
pci, err := m.api.StateSectorPreCommitInfo(ctx, m.maddr, si.SectorNumber, tok)
if err == ErrSectorAllocated {
// not much more we can check here, basically try to wait for commit,
// and hope that this will work
if si.CommitMessage != nil {
return &ErrCommitWaitFailed{err}
}
return err
}
if err != nil {
return xerrors.Errorf("getting precommit info: %w", err)
}
2020-06-17 15:19:36 +00:00
if pci == nil {
return &ErrNoPrecommit{xerrors.Errorf("precommit info not found on-chain")}
}
2020-04-06 20:23:37 +00:00
if pci.PreCommitEpoch+miner.PreCommitChallengeDelay != si.SeedEpoch {
return &ErrBadSeed{xerrors.Errorf("seed epoch doesn't match on chain info: %d != %d", pci.PreCommitEpoch+miner.PreCommitChallengeDelay, si.SeedEpoch)}
}
2020-04-20 18:21:11 +00:00
buf := new(bytes.Buffer)
if err := m.maddr.MarshalCBOR(buf); err != nil {
return err
}
2020-08-11 23:58:35 +00:00
seed, err := m.api.ChainGetRandomnessFromBeacon(ctx, tok, crypto.DomainSeparationTag_InteractiveSealChallengeSeed, si.SeedEpoch, buf.Bytes())
if err != nil {
return &ErrApi{xerrors.Errorf("failed to get randomness for computing seal proof: %w", err)}
}
2020-04-06 20:23:37 +00:00
if string(seed) != string(si.SeedValue) {
return &ErrBadSeed{xerrors.Errorf("seed has changed")}
}
2020-04-06 20:23:37 +00:00
ss, err := m.api.StateMinerSectorSize(ctx, m.maddr, tok)
2020-04-04 01:50:05 +00:00
if err != nil {
return &ErrApi{err}
}
2020-04-10 21:16:48 +00:00
spt, err := ffiwrapper.SealProofTypeFromSectorSize(ss)
2020-04-04 01:50:05 +00:00
if err != nil {
return err
}
if *si.CommR != pci.Info.SealedCID {
log.Warn("on-chain sealed CID doesn't match!")
}
ok, err := m.verif.VerifySeal(v0proof.SealVerifyInfo{
2020-05-22 01:03:34 +00:00
SectorID: m.minerSector(si.SectorNumber),
SealedCID: pci.Info.SealedCID,
2020-06-15 13:13:35 +00:00
SealProof: spt,
2020-05-22 01:03:34 +00:00
Proof: proof,
2020-04-06 20:23:37 +00:00
Randomness: si.TicketValue,
InteractiveRandomness: si.SeedValue,
2020-04-04 01:50:05 +00:00
UnsealedCID: *si.CommD,
})
if err != nil {
return &ErrInvalidProof{xerrors.Errorf("verify seal: %w", err)}
2020-04-04 01:50:05 +00:00
}
if !ok {
return &ErrInvalidProof{xerrors.New("invalid proof (compute error?)")}
}
if err := checkPieces(ctx, m.maddr, si, m.api); err != nil {
return err
}
return nil
2020-01-20 22:03:50 +00:00
}