2020-01-15 20:49:11 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-18 16:02:13 +00:00
|
|
|
"errors"
|
2020-07-01 13:30:25 +00:00
|
|
|
"sync"
|
2021-01-18 20:59:34 +00:00
|
|
|
"time"
|
2020-01-15 20:49:11 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-16 02:55:12 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"golang.org/x/xerrors"
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-03-16 18:53:00 +00:00
|
|
|
"github.com/filecoin-project/go-bitfield"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2021-01-13 21:19:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/dline"
|
2020-12-02 20:47:45 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-04-06 18:07:26 +00:00
|
|
|
statemachine "github.com/filecoin-project/go-statemachine"
|
2020-12-02 20:47:45 +00:00
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-09-17 07:32:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2020-09-17 15:30:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2021-01-25 10:28:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-17 13:39:33 +00:00
|
|
|
sectorstorage "github.com/filecoin-project/lotus/extern/sector-storage"
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
|
2021-06-01 09:56:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
|
2021-06-08 13:43:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2020-01-15 20:49:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SectorStorePrefix = "/sectors"
|
|
|
|
|
2020-09-11 11:28:09 +00:00
|
|
|
var ErrTooManySectorsSealing = xerrors.New("too many sectors sealing")
|
|
|
|
|
2020-01-15 20:49:11 +00:00
|
|
|
var log = logging.Logger("sectors")
|
|
|
|
|
2020-07-15 14:51:02 +00:00
|
|
|
type SectorLocation struct {
|
2020-07-15 14:53:17 +00:00
|
|
|
Deadline uint64
|
2020-07-15 14:51:02 +00:00
|
|
|
Partition uint64
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:02:13 +00:00
|
|
|
var ErrSectorAllocated = errors.New("sectorNumber is allocated, but PreCommit info wasn't found on chain")
|
|
|
|
|
2021-08-18 10:43:44 +00:00
|
|
|
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/api.go -package=mocks . SealingAPI
|
|
|
|
|
2020-04-06 18:07:26 +00:00
|
|
|
type SealingAPI interface {
|
|
|
|
StateWaitMsg(context.Context, cid.Cid) (MsgLookup, error)
|
2020-08-18 16:02:13 +00:00
|
|
|
StateSearchMsg(context.Context, cid.Cid) (*MsgLookup, error)
|
2020-06-15 13:13:35 +00:00
|
|
|
StateComputeDataCommitment(ctx context.Context, maddr address.Address, sectorType abi.RegisteredSealProof, deals []abi.DealID, tok TipSetToken) (cid.Cid, error)
|
2020-08-18 16:02:13 +00:00
|
|
|
|
|
|
|
// Can return ErrSectorAllocated in case precommit info wasn't found, but the sector number is marked as allocated
|
2020-04-06 21:03:47 +00:00
|
|
|
StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*miner.SectorPreCommitOnChainInfo, error)
|
2020-05-28 00:10:50 +00:00
|
|
|
StateSectorGetInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*miner.SectorOnChainInfo, error)
|
2020-07-15 14:51:02 +00:00
|
|
|
StateSectorPartition(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*SectorLocation, error)
|
2021-01-25 10:28:39 +00:00
|
|
|
StateLookupID(context.Context, address.Address, TipSetToken) (address.Address, error)
|
2020-04-06 20:23:37 +00:00
|
|
|
StateMinerSectorSize(context.Context, address.Address, TipSetToken) (abi.SectorSize, error)
|
2020-04-09 17:34:07 +00:00
|
|
|
StateMinerWorkerAddress(ctx context.Context, maddr address.Address, tok TipSetToken) (address.Address, error)
|
2020-07-28 18:50:42 +00:00
|
|
|
StateMinerPreCommitDepositForPower(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
|
2020-06-26 15:58:29 +00:00
|
|
|
StateMinerInitialPledgeCollateral(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
|
2020-11-04 20:29:08 +00:00
|
|
|
StateMinerInfo(context.Context, address.Address, TipSetToken) (miner.MinerInfo, error)
|
2021-07-12 16:46:05 +00:00
|
|
|
StateMinerAvailableBalance(context.Context, address.Address, TipSetToken) (big.Int, error)
|
2020-10-13 19:35:29 +00:00
|
|
|
StateMinerSectorAllocated(context.Context, address.Address, abi.SectorNumber, TipSetToken) (bool, error)
|
2022-03-16 18:53:00 +00:00
|
|
|
StateMinerActiveSectors(context.Context, address.Address, TipSetToken) (bitfield.BitField, error)
|
2021-01-25 10:28:39 +00:00
|
|
|
StateMarketStorageDeal(context.Context, abi.DealID, TipSetToken) (*api.MarketDeal, error)
|
|
|
|
StateMarketStorageDealProposal(context.Context, abi.DealID, TipSetToken) (market.DealProposal, error)
|
2020-09-17 02:34:13 +00:00
|
|
|
StateNetworkVersion(ctx context.Context, tok TipSetToken) (network.Version, error)
|
2021-01-13 21:19:10 +00:00
|
|
|
StateMinerProvingDeadline(context.Context, address.Address, TipSetToken) (*dline.Info, error)
|
2021-01-14 16:14:26 +00:00
|
|
|
StateMinerPartitions(ctx context.Context, m address.Address, dlIdx uint64, tok TipSetToken) ([]api.Partition, error)
|
2020-08-12 17:47:00 +00:00
|
|
|
SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (cid.Cid, error)
|
2020-04-06 18:07:26 +00:00
|
|
|
ChainHead(ctx context.Context) (TipSetToken, abi.ChainEpoch, error)
|
2021-06-09 01:40:52 +00:00
|
|
|
ChainBaseFee(context.Context, TipSetToken) (abi.TokenAmount, error)
|
2021-01-25 10:28:39 +00:00
|
|
|
ChainGetMessage(ctx context.Context, mc cid.Cid) (*types.Message, error)
|
2021-09-12 02:24:53 +00:00
|
|
|
StateGetRandomnessFromBeacon(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte, tok TipSetToken) (abi.Randomness, error)
|
|
|
|
StateGetRandomnessFromTickets(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte, tok TipSetToken) (abi.Randomness, error)
|
2020-04-15 18:00:29 +00:00
|
|
|
ChainReadObj(context.Context, cid.Cid) ([]byte, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 12:22:35 +00:00
|
|
|
type SectorStateNotifee func(before, after SectorInfo)
|
|
|
|
|
2020-12-02 20:47:45 +00:00
|
|
|
type AddrSel func(ctx context.Context, mi miner.MinerInfo, use api.AddrUse, goodFunds, minFunds abi.TokenAmount) (address.Address, abi.TokenAmount, error)
|
|
|
|
|
2020-01-15 20:49:11 +00:00
|
|
|
type Sealing struct {
|
2021-08-18 10:43:44 +00:00
|
|
|
Api SealingAPI
|
|
|
|
DealInfo *CurrentDealInfoManager
|
|
|
|
|
2021-06-08 13:43:43 +00:00
|
|
|
feeCfg config.MinerFeeConfig
|
2020-04-06 18:07:26 +00:00
|
|
|
events Events
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2021-06-15 19:04:11 +00:00
|
|
|
startupWait sync.WaitGroup
|
|
|
|
|
2020-04-09 17:34:07 +00:00
|
|
|
maddr address.Address
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
sealer sectorstorage.SectorManager
|
2020-01-15 20:49:11 +00:00
|
|
|
sectors *statemachine.StateGroup
|
2020-03-17 20:19:52 +00:00
|
|
|
sc SectorIDCounter
|
2020-04-04 02:55:19 +00:00
|
|
|
verif ffiwrapper.Verifier
|
2021-01-18 13:26:03 +00:00
|
|
|
pcp PreCommitPolicy
|
2020-04-07 22:26:07 +00:00
|
|
|
|
2021-01-20 15:00:00 +00:00
|
|
|
inputLk sync.Mutex
|
|
|
|
openSectors map[abi.SectorID]*openSector
|
|
|
|
sectorTimers map[abi.SectorID]*time.Timer
|
|
|
|
pendingPieces map[cid.Cid]*pendingPiece
|
|
|
|
assignedPieces map[abi.SectorID][]cid.Cid
|
2021-05-30 16:30:38 +00:00
|
|
|
creating *abi.SectorNumber // used to prevent a race where we could create a new sector more than once
|
2020-07-01 13:30:25 +00:00
|
|
|
|
2022-03-16 16:33:05 +00:00
|
|
|
available map[abi.SectorID]struct{}
|
|
|
|
|
2020-08-11 12:22:35 +00:00
|
|
|
notifee SectorStateNotifee
|
2020-12-02 20:47:45 +00:00
|
|
|
addrSel AddrSel
|
2020-08-26 15:38:23 +00:00
|
|
|
|
2020-08-18 14:54:05 +00:00
|
|
|
stats SectorStats
|
|
|
|
|
2021-05-18 14:51:06 +00:00
|
|
|
terminator *TerminateBatcher
|
|
|
|
precommiter *PreCommitBatcher
|
|
|
|
commiter *CommitBatcher
|
2021-01-12 23:42:01 +00:00
|
|
|
|
2020-08-18 14:20:31 +00:00
|
|
|
getConfig GetSealingConfigFunc
|
2020-06-23 21:47:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:26:03 +00:00
|
|
|
type openSector struct {
|
2021-12-08 17:11:19 +00:00
|
|
|
used abi.UnpaddedPieceSize // change to bitfield/rle when AddPiece gains offset support to better fill sectors
|
|
|
|
number abi.SectorNumber
|
|
|
|
ccUpdate bool
|
2021-01-18 13:26:03 +00:00
|
|
|
|
2021-01-20 15:00:00 +00:00
|
|
|
maybeAccept func(cid.Cid) error // called with inputLk
|
2020-07-09 22:27:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 16:33:05 +00:00
|
|
|
func (o *openSector) dealFitsInLifetime(dealEnd abi.ChainEpoch, expF expFn) (bool, error) {
|
2021-12-08 17:11:19 +00:00
|
|
|
if !o.ccUpdate {
|
|
|
|
return true, nil
|
|
|
|
}
|
2022-03-16 16:33:05 +00:00
|
|
|
expiration, _, err := expF(o.number)
|
2021-12-08 17:11:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return expiration >= dealEnd, nil
|
|
|
|
}
|
|
|
|
|
2022-02-21 07:51:25 +00:00
|
|
|
type pieceAcceptResp struct {
|
|
|
|
sn abi.SectorNumber
|
|
|
|
offset abi.UnpaddedPieceSize
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:26:03 +00:00
|
|
|
type pendingPiece struct {
|
2022-02-21 09:51:43 +00:00
|
|
|
doneCh chan struct{}
|
2022-02-21 14:27:51 +00:00
|
|
|
resp *pieceAcceptResp
|
2022-02-21 07:51:25 +00:00
|
|
|
|
2021-01-18 13:26:03 +00:00
|
|
|
size abi.UnpaddedPieceSize
|
2021-05-19 11:05:07 +00:00
|
|
|
deal api.PieceDealInfo
|
2021-01-18 13:26:03 +00:00
|
|
|
|
|
|
|
data storage.Data
|
|
|
|
|
|
|
|
assigned bool // assigned to a sector?
|
|
|
|
accepted func(abi.SectorNumber, abi.UnpaddedPieceSize, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 09:43:06 +00:00
|
|
|
func New(mctx context.Context, api SealingAPI, fc config.MinerFeeConfig, events Events, maddr address.Address, ds datastore.Batching, sealer sectorstorage.SectorManager, sc SectorIDCounter, verif ffiwrapper.Verifier, prov ffiwrapper.Prover, pcp PreCommitPolicy, gc GetSealingConfigFunc, notifee SectorStateNotifee, as AddrSel) *Sealing {
|
2020-01-15 20:49:11 +00:00
|
|
|
s := &Sealing{
|
2021-08-18 10:43:44 +00:00
|
|
|
Api: api,
|
|
|
|
DealInfo: &CurrentDealInfoManager{api},
|
|
|
|
|
2020-08-12 17:47:00 +00:00
|
|
|
feeCfg: fc,
|
2020-01-15 20:49:11 +00:00
|
|
|
events: events,
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
maddr: maddr,
|
|
|
|
sealer: sealer,
|
|
|
|
sc: sc,
|
|
|
|
verif: verif,
|
|
|
|
pcp: pcp,
|
2020-07-01 14:33:59 +00:00
|
|
|
|
2021-01-20 15:00:00 +00:00
|
|
|
openSectors: map[abi.SectorID]*openSector{},
|
|
|
|
sectorTimers: map[abi.SectorID]*time.Timer{},
|
|
|
|
pendingPieces: map[cid.Cid]*pendingPiece{},
|
|
|
|
assignedPieces: map[abi.SectorID][]cid.Cid{},
|
2020-08-26 15:38:23 +00:00
|
|
|
|
2022-03-16 16:33:05 +00:00
|
|
|
available: map[abi.SectorID]struct{}{},
|
|
|
|
|
2020-08-26 15:38:23 +00:00
|
|
|
notifee: notifee,
|
2020-12-02 20:47:45 +00:00
|
|
|
addrSel: as,
|
2020-08-26 15:38:23 +00:00
|
|
|
|
2021-06-16 09:43:06 +00:00
|
|
|
terminator: NewTerminationBatcher(mctx, maddr, api, as, fc, gc),
|
|
|
|
precommiter: NewPreCommitBatcher(mctx, maddr, api, as, fc, gc),
|
|
|
|
commiter: NewCommitBatcher(mctx, maddr, api, as, fc, gc, prov),
|
2021-01-12 23:42:01 +00:00
|
|
|
|
2020-08-18 14:20:31 +00:00
|
|
|
getConfig: gc,
|
2020-08-18 16:02:13 +00:00
|
|
|
|
|
|
|
stats: SectorStats{
|
2021-10-19 16:52:54 +00:00
|
|
|
bySector: map[abi.SectorID]SectorState{},
|
|
|
|
byState: map[SectorState]int64{},
|
2020-08-18 16:02:13 +00:00
|
|
|
},
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
2021-06-15 19:04:11 +00:00
|
|
|
s.startupWait.Add(1)
|
2020-01-15 20:49:11 +00:00
|
|
|
|
|
|
|
s.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), s, SectorInfo{})
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) Run(ctx context.Context) error {
|
|
|
|
if err := m.restartSectors(ctx); err != nil {
|
|
|
|
log.Errorf("%+v", err)
|
|
|
|
return xerrors.Errorf("failed load sector states: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) Stop(ctx context.Context) error {
|
2021-01-12 23:42:01 +00:00
|
|
|
if err := m.terminator.Stop(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.sectors.Stop(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
2020-11-04 20:29:08 +00:00
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
func (m *Sealing) Remove(ctx context.Context, sid abi.SectorNumber) error {
|
2021-06-15 19:04:11 +00:00
|
|
|
m.startupWait.Wait()
|
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
return m.sectors.Send(uint64(sid), SectorRemove{})
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:42:01 +00:00
|
|
|
func (m *Sealing) Terminate(ctx context.Context, sid abi.SectorNumber) error {
|
2021-06-15 19:04:11 +00:00
|
|
|
m.startupWait.Wait()
|
|
|
|
|
2021-01-12 23:42:01 +00:00
|
|
|
return m.sectors.Send(uint64(sid), SectorTerminate{})
|
|
|
|
}
|
|
|
|
|
2021-01-13 22:32:04 +00:00
|
|
|
func (m *Sealing) TerminateFlush(ctx context.Context) (*cid.Cid, error) {
|
|
|
|
return m.terminator.Flush(ctx)
|
|
|
|
}
|
|
|
|
|
2021-01-14 11:37:23 +00:00
|
|
|
func (m *Sealing) TerminatePending(ctx context.Context) ([]abi.SectorID, error) {
|
|
|
|
return m.terminator.Pending(ctx)
|
|
|
|
}
|
|
|
|
|
2021-06-01 12:35:30 +00:00
|
|
|
func (m *Sealing) SectorPreCommitFlush(ctx context.Context) ([]sealiface.PreCommitBatchRes, error) {
|
2021-05-18 14:51:06 +00:00
|
|
|
return m.precommiter.Flush(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) SectorPreCommitPending(ctx context.Context) ([]abi.SectorID, error) {
|
|
|
|
return m.precommiter.Pending(ctx)
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:56:19 +00:00
|
|
|
func (m *Sealing) CommitFlush(ctx context.Context) ([]sealiface.CommitBatchRes, error) {
|
2021-03-10 15:16:44 +00:00
|
|
|
return m.commiter.Flush(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) CommitPending(ctx context.Context) ([]abi.SectorID, error) {
|
|
|
|
return m.commiter.Pending(ctx)
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:29:08 +00:00
|
|
|
func (m *Sealing) currentSealProof(ctx context.Context) (abi.RegisteredSealProof, error) {
|
2021-08-18 10:43:44 +00:00
|
|
|
mi, err := m.Api.StateMinerInfo(ctx, m.maddr, nil)
|
2020-11-04 20:29:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 10:43:44 +00:00
|
|
|
ver, err := m.Api.StateNetworkVersion(ctx, nil)
|
2021-01-20 02:06:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return miner.PreferredSealProofTypeFromWindowPoStType(ver, mi.WindowPoStProofType)
|
2020-11-04 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) minerSector(spt abi.RegisteredSealProof, num abi.SectorNumber) storage.SectorRef {
|
|
|
|
return storage.SectorRef{
|
|
|
|
ID: m.minerSectorID(num),
|
|
|
|
ProofType: spt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) minerSectorID(num abi.SectorNumber) abi.SectorID {
|
2020-03-17 20:19:52 +00:00
|
|
|
mid, err := address.IDFromAddress(m.maddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return abi.SectorID{
|
|
|
|
Number: num,
|
2020-03-18 01:08:11 +00:00
|
|
|
Miner: abi.ActorID(mid),
|
2020-03-17 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Sealing) Address() address.Address {
|
|
|
|
return m.maddr
|
|
|
|
}
|
2020-07-20 16:43:19 +00:00
|
|
|
|
2021-01-20 17:18:12 +00:00
|
|
|
func getDealPerSectorLimit(size abi.SectorSize) (int, error) {
|
2020-07-20 16:43:19 +00:00
|
|
|
if size < 64<<30 {
|
2021-01-20 14:20:44 +00:00
|
|
|
return 256, nil
|
2020-07-20 16:43:19 +00:00
|
|
|
}
|
2021-01-20 14:20:44 +00:00
|
|
|
return 512, nil
|
2020-07-20 16:43:19 +00:00
|
|
|
}
|