2022-08-24 19:27:27 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
2022-09-09 10:22:21 +00:00
|
|
|
"bytes"
|
2022-08-24 19:27:27 +00:00
|
|
|
"context"
|
2022-09-09 10:54:48 +00:00
|
|
|
"net/url"
|
2022-08-31 11:56:25 +00:00
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-08-24 19:27:27 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
2022-09-09 10:22:21 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
2022-08-24 19:27:27 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-09-09 10:22:21 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/go-state-types/proof"
|
2022-08-31 11:56:25 +00:00
|
|
|
"github.com/filecoin-project/go-statemachine"
|
2022-08-24 19:27:27 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-09-02 13:47:35 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2022-08-24 19:27:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Sealing) Receive(ctx context.Context, meta api.RemoteSectorMeta) error {
|
2022-09-09 10:22:21 +00:00
|
|
|
m.inputLk.Lock()
|
|
|
|
defer m.inputLk.Unlock()
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
si, err := m.checkSectorMeta(ctx, meta)
|
|
|
|
if err != nil {
|
2022-08-24 19:27:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
exists, err := m.sectors.Has(uint64(meta.Sector.Number))
|
|
|
|
if err != nil {
|
2022-08-30 19:45:36 +00:00
|
|
|
return xerrors.Errorf("checking if sector exists: %w", err)
|
2022-08-31 10:43:40 +00:00
|
|
|
}
|
|
|
|
if exists {
|
2022-08-30 19:45:36 +00:00
|
|
|
return xerrors.Errorf("sector %d state already exists", meta.Sector.Number)
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
err = m.sectors.Send(uint64(meta.Sector.Number), SectorReceive{
|
|
|
|
State: si,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("receiving sector: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
func (m *Sealing) checkSectorMeta(ctx context.Context, meta api.RemoteSectorMeta) (SectorInfo, error) {
|
2022-08-24 19:27:27 +00:00
|
|
|
{
|
|
|
|
mid, err := address.IDFromAddress(m.maddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Sector.Miner != abi.ActorID(mid) {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, xerrors.Errorf("sector for wrong actor - expected actor id %d, sector was for actor %d", mid, meta.Sector.Miner)
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// initial sanity check, doesn't prevent races
|
|
|
|
_, err := m.GetSectorInfo(meta.Sector.Number)
|
|
|
|
if err != nil && !xerrors.Is(err, datastore.ErrNotFound) {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, err
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, xerrors.Errorf("sector with ID %d already exists in the sealing pipeline", meta.Sector.Number)
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
spt, err := m.currentSealProof(ctx)
|
|
|
|
if err != nil {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, err
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Type != spt {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, xerrors.Errorf("sector seal proof type doesn't match current seal proof type (%d!=%d)", meta.Type, spt)
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
ts, err := m.Api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("getting chain head: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
var info SectorInfo
|
2022-09-09 10:22:21 +00:00
|
|
|
var validatePoRep bool
|
2022-08-30 19:45:36 +00:00
|
|
|
|
2022-08-24 19:27:27 +00:00
|
|
|
switch SectorState(meta.State) {
|
2022-08-30 19:45:36 +00:00
|
|
|
case Proving, Available:
|
2022-09-16 21:45:23 +00:00
|
|
|
if meta.CommitMessage != nil {
|
|
|
|
if err := checkMessagePrefix(*meta.CommitMessage); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("commit message prefix: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.CommitMessage = meta.CommitMessage
|
|
|
|
}
|
2022-08-24 19:27:27 +00:00
|
|
|
|
|
|
|
fallthrough
|
2022-08-30 19:45:36 +00:00
|
|
|
case SubmitCommit:
|
2022-09-05 13:28:36 +00:00
|
|
|
if meta.PreCommitDeposit == nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("sector PreCommitDeposit was null")
|
|
|
|
}
|
|
|
|
|
|
|
|
info.PreCommitDeposit = *meta.PreCommitDeposit
|
2022-08-30 19:45:36 +00:00
|
|
|
info.PreCommitTipSet = meta.PreCommitTipSet
|
2022-09-16 21:45:23 +00:00
|
|
|
if info.PreCommitMessage != nil {
|
|
|
|
if err := checkMessagePrefix(*meta.PreCommitMessage); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("commit message prefix: %w", err)
|
|
|
|
}
|
|
|
|
info.PreCommitMessage = meta.PreCommitMessage
|
|
|
|
}
|
2022-08-30 19:45:36 +00:00
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
// check provided seed
|
|
|
|
if len(meta.SeedValue) != abi.RandomnessLength {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("seed randomness had wrong length %d", len(meta.SeedValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
maddrBuf := new(bytes.Buffer)
|
|
|
|
if err := m.maddr.MarshalCBOR(maddrBuf); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("marshal miner address for seed check: %w", err)
|
|
|
|
}
|
2023-08-01 10:05:39 +00:00
|
|
|
rand, err := m.Api.StateGetRandomnessFromBeacon(ctx, crypto.DomainSeparationTag_InteractiveSealChallengeSeed, meta.SeedEpoch, maddrBuf.Bytes(), ts.Key())
|
2022-09-09 10:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("generating check seed: %w", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(rand, meta.SeedValue) {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("provided(%x) and generated(%x) seeds differ", meta.SeedValue, rand)
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
info.SeedValue = meta.SeedValue
|
|
|
|
info.SeedEpoch = meta.SeedEpoch
|
|
|
|
|
|
|
|
info.Proof = meta.CommitProof
|
2022-09-09 10:22:21 +00:00
|
|
|
validatePoRep = true
|
2022-08-24 19:27:27 +00:00
|
|
|
|
|
|
|
fallthrough
|
|
|
|
case PreCommitting:
|
2022-09-09 10:22:21 +00:00
|
|
|
// check provided ticket
|
|
|
|
if len(meta.TicketValue) != abi.RandomnessLength {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("ticket randomness had wrong length %d", len(meta.TicketValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
maddrBuf := new(bytes.Buffer)
|
|
|
|
if err := m.maddr.MarshalCBOR(maddrBuf); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("marshal miner address for ticket check: %w", err)
|
|
|
|
}
|
|
|
|
rand, err := m.Api.StateGetRandomnessFromTickets(ctx, crypto.DomainSeparationTag_SealRandomness, meta.TicketEpoch, maddrBuf.Bytes(), ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("generating check ticket: %w", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(rand, meta.TicketValue) {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("provided(%x) and generated(%x) tickets differ", meta.TicketValue, rand)
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
info.TicketValue = meta.TicketValue
|
|
|
|
info.TicketEpoch = meta.TicketEpoch
|
2022-08-24 19:27:27 +00:00
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
info.PreCommit1Out = meta.PreCommit1Out
|
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
// check CommD/R
|
|
|
|
if meta.CommD == nil || meta.CommR == nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("both CommR/CommD cids need to be set for sectors in PreCommitting and later states")
|
|
|
|
}
|
|
|
|
|
|
|
|
dp := meta.CommD.Prefix()
|
|
|
|
if dp.Version != 1 || dp.Codec != cid.FilCommitmentUnsealed || dp.MhType != multihash.SHA2_256_TRUNC254_PADDED || dp.MhLength != 32 {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("CommD cid has wrong prefix")
|
|
|
|
}
|
|
|
|
|
|
|
|
rp := meta.CommR.Prefix()
|
|
|
|
if rp.Version != 1 || rp.Codec != cid.FilCommitmentSealed || rp.MhType != multihash.POSEIDON_BLS12_381_A1_FC1 || rp.MhLength != 32 {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("CommR cid has wrong prefix")
|
|
|
|
}
|
|
|
|
|
|
|
|
info.CommD = meta.CommD
|
2022-08-30 19:45:36 +00:00
|
|
|
info.CommR = meta.CommR
|
2022-08-24 19:27:27 +00:00
|
|
|
|
2022-09-02 13:47:35 +00:00
|
|
|
if meta.DataSealed == nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("expected DataSealed to be set")
|
|
|
|
}
|
|
|
|
if meta.DataCache == nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("expected DataCache to be set")
|
|
|
|
}
|
2022-09-09 08:32:27 +00:00
|
|
|
info.RemoteDataSealed = meta.DataSealed // todo make head requests to check?
|
2022-09-02 13:47:35 +00:00
|
|
|
info.RemoteDataCache = meta.DataCache
|
2022-09-09 10:54:48 +00:00
|
|
|
|
|
|
|
if meta.RemoteCommit1Endpoint != "" {
|
|
|
|
// validate the url
|
|
|
|
if _, err := url.Parse(meta.RemoteCommit1Endpoint); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("parsing remote c1 endpoint url: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.RemoteCommit1Endpoint = meta.RemoteCommit1Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.RemoteCommit2Endpoint != "" {
|
|
|
|
// validate the url
|
|
|
|
if _, err := url.Parse(meta.RemoteCommit2Endpoint); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("parsing remote c2 endpoint url: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.RemoteCommit2Endpoint = meta.RemoteCommit2Endpoint
|
|
|
|
}
|
2022-09-02 13:47:35 +00:00
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
// If we get a sector after PC2, and remote C1 endpoint is set, assume that we're getting finalized sector data
|
|
|
|
if info.RemoteCommit1Endpoint != "" {
|
|
|
|
info.RemoteDataFinalized = true
|
|
|
|
}
|
2022-09-02 13:47:35 +00:00
|
|
|
|
2022-08-24 19:27:27 +00:00
|
|
|
fallthrough
|
2022-09-09 10:22:21 +00:00
|
|
|
case GetTicket, Packing:
|
|
|
|
info.Return = ReturnState(meta.State)
|
2022-08-31 10:43:40 +00:00
|
|
|
info.State = ReceiveSector
|
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
info.SectorNumber = meta.Sector.Number
|
|
|
|
info.Pieces = meta.Pieces
|
|
|
|
info.SectorType = meta.Type
|
|
|
|
|
2022-09-09 12:38:23 +00:00
|
|
|
if meta.RemoteSealingDoneEndpoint != "" {
|
|
|
|
// validate the url
|
|
|
|
if _, err := url.Parse(meta.RemoteSealingDoneEndpoint); err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("parsing remote sealing-done endpoint url: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.RemoteSealingDoneEndpoint = meta.RemoteSealingDoneEndpoint
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:45:36 +00:00
|
|
|
if err := checkPieces(ctx, m.maddr, meta.Sector.Number, meta.Pieces, m.Api, false); err != nil {
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, xerrors.Errorf("checking pieces: %w", err)
|
2022-08-30 19:45:36 +00:00
|
|
|
}
|
2022-08-24 19:27:27 +00:00
|
|
|
|
2022-09-02 13:47:35 +00:00
|
|
|
if meta.DataUnsealed == nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("expected DataUnsealed to be set")
|
|
|
|
}
|
|
|
|
info.RemoteDataUnsealed = meta.DataUnsealed
|
|
|
|
|
2022-09-09 10:22:21 +00:00
|
|
|
// some late checks which require previous checks
|
|
|
|
if validatePoRep {
|
|
|
|
ok, err := m.verif.VerifySeal(proof.SealVerifyInfo{
|
|
|
|
SealProof: meta.Type,
|
|
|
|
SectorID: meta.Sector,
|
|
|
|
DealIDs: nil,
|
|
|
|
Randomness: meta.TicketValue,
|
|
|
|
InteractiveRandomness: meta.SeedValue,
|
|
|
|
Proof: meta.CommitProof,
|
|
|
|
SealedCID: *meta.CommR,
|
|
|
|
UnsealedCID: *meta.CommD,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("validating seal proof: %w", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return SectorInfo{}, xerrors.Errorf("seal proof invalid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
return info, nil
|
2022-08-24 19:27:27 +00:00
|
|
|
default:
|
2022-08-31 10:43:40 +00:00
|
|
|
return SectorInfo{}, xerrors.Errorf("imported sector State in not supported")
|
2022-08-24 19:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 10:43:40 +00:00
|
|
|
|
|
|
|
func (m *Sealing) handleReceiveSector(ctx statemachine.Context, sector SectorInfo) error {
|
2022-09-16 21:45:23 +00:00
|
|
|
toFetch := map[storiface.SectorFileType]storiface.SectorLocation{}
|
2022-09-02 13:47:35 +00:00
|
|
|
|
2022-09-16 21:45:23 +00:00
|
|
|
for fileType, data := range map[storiface.SectorFileType]*storiface.SectorLocation{
|
2022-09-02 13:47:35 +00:00
|
|
|
storiface.FTUnsealed: sector.RemoteDataUnsealed,
|
|
|
|
storiface.FTSealed: sector.RemoteDataSealed,
|
|
|
|
storiface.FTCache: sector.RemoteDataCache,
|
|
|
|
} {
|
|
|
|
if data == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Local {
|
|
|
|
// todo check exists
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
toFetch[fileType] = *data
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toFetch) > 0 {
|
|
|
|
if err := m.sealer.DownloadSectorData(ctx.Context(), m.minerSector(sector.SectorType, sector.SectorNumber), sector.RemoteDataFinalized, toFetch); err != nil {
|
|
|
|
return xerrors.Errorf("downloading sector data: %w", err) // todo send err event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo data checks?
|
|
|
|
|
2022-08-31 10:43:40 +00:00
|
|
|
return ctx.Send(SectorReceived{})
|
|
|
|
}
|
2022-09-16 21:45:23 +00:00
|
|
|
|
|
|
|
func checkMessagePrefix(c cid.Cid) error {
|
|
|
|
p := c.Prefix()
|
|
|
|
if p.Version != 1 || p.MhLength != 32 || p.MhType != multihash.BLAKE2B_MIN+31 || p.Codec != cid.DagCBOR {
|
|
|
|
return xerrors.New("invalid message prefix")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|