2020-01-15 20:49:11 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2020-07-01 13:30:25 +00:00
|
|
|
"sync"
|
2020-07-06 17:04:11 +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"
|
|
|
|
padreader "github.com/filecoin-project/go-padreader"
|
|
|
|
statemachine "github.com/filecoin-project/go-statemachine"
|
|
|
|
sectorstorage "github.com/filecoin-project/sector-storage"
|
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2020-02-21 17:43:44 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-04-06 18:07:26 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
2020-02-21 17:43:44 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
2020-02-23 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2020-01-15 20:49:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SectorStorePrefix = "/sectors"
|
|
|
|
|
|
|
|
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-04-06 18:07:26 +00:00
|
|
|
type SealingAPI interface {
|
|
|
|
StateWaitMsg(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-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)
|
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-15 14:51:02 +00:00
|
|
|
StateMinerDeadlines(ctx context.Context, maddr address.Address, tok TipSetToken) ([]*miner.Deadline, 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-05-15 16:31:32 +00:00
|
|
|
StateMarketStorageDeal(context.Context, abi.DealID, TipSetToken) (market.DealProposal, error)
|
2020-04-06 18:07:26 +00:00
|
|
|
SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, gasPrice big.Int, gasLimit int64, params []byte) (cid.Cid, error)
|
|
|
|
ChainHead(ctx context.Context) (TipSetToken, abi.ChainEpoch, error)
|
2020-08-11 23:58:35 +00:00
|
|
|
ChainGetRandomnessFromBeacon(ctx context.Context, tok TipSetToken, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error)
|
|
|
|
ChainGetRandomnessFromTickets(ctx context.Context, tok TipSetToken, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (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
|
|
|
}
|
|
|
|
|
|
|
|
type Sealing struct {
|
2020-04-06 18:07:26 +00:00
|
|
|
api SealingAPI
|
|
|
|
events Events
|
2020-01-15 20:49:11 +00:00
|
|
|
|
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
|
2020-04-07 22:26:07 +00:00
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
pcp PreCommitPolicy
|
|
|
|
unsealedInfoMap UnsealedSectorMap
|
2020-07-01 13:30:25 +00:00
|
|
|
|
|
|
|
upgradeLk sync.Mutex
|
|
|
|
toUpgrade map[abi.SectorNumber]struct{}
|
2020-07-06 17:04:11 +00:00
|
|
|
|
|
|
|
getSealDelay GetSealingDelayFunc
|
2020-06-23 21:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
type UnsealedSectorMap struct {
|
|
|
|
infos map[abi.SectorNumber]UnsealedSectorInfo
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:47:53 +00:00
|
|
|
type UnsealedSectorInfo struct {
|
2020-07-20 16:43:19 +00:00
|
|
|
numDeals uint64
|
2020-07-09 22:27:19 +00:00
|
|
|
// stored should always equal sum of pieceSizes.Padded()
|
2020-07-30 12:22:05 +00:00
|
|
|
stored abi.PaddedPieceSize
|
2020-06-23 21:47:53 +00:00
|
|
|
pieceSizes []abi.UnpaddedPieceSize
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:04:11 +00:00
|
|
|
func New(api SealingAPI, events Events, maddr address.Address, ds datastore.Batching, sealer sectorstorage.SectorManager, sc SectorIDCounter, verif ffiwrapper.Verifier, pcp PreCommitPolicy, gsd GetSealingDelayFunc) *Sealing {
|
2020-01-15 20:49:11 +00:00
|
|
|
s := &Sealing{
|
2020-01-16 02:54:57 +00:00
|
|
|
api: api,
|
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,
|
|
|
|
unsealedInfoMap: UnsealedSectorMap{
|
|
|
|
infos: make(map[abi.SectorNumber]UnsealedSectorInfo),
|
|
|
|
mux: sync.Mutex{},
|
|
|
|
},
|
2020-07-01 14:33:59 +00:00
|
|
|
|
2020-07-06 17:04:11 +00:00
|
|
|
toUpgrade: map[abi.SectorNumber]struct{}{},
|
|
|
|
getSealDelay: gsd,
|
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 {
|
|
|
|
return m.sectors.Stop(ctx)
|
|
|
|
}
|
2020-07-30 12:22:05 +00:00
|
|
|
func (m *Sealing) AddPieceToAnySector(ctx context.Context, size abi.UnpaddedPieceSize, r io.Reader, d DealInfo) (abi.SectorNumber, abi.PaddedPieceSize, error) {
|
2020-06-23 21:31:51 +00:00
|
|
|
log.Infof("Adding piece for deal %d", d.DealID)
|
2020-02-11 01:10:50 +00:00
|
|
|
if (padreader.PaddedSize(uint64(size))) != size {
|
2020-01-15 20:49:11 +00:00
|
|
|
return 0, 0, xerrors.Errorf("cannot allocate unpadded piece")
|
|
|
|
}
|
2020-06-23 19:32:22 +00:00
|
|
|
|
2020-07-30 12:22:05 +00:00
|
|
|
if size > abi.PaddedPieceSize(m.sealer.SectorSize()).Unpadded() {
|
2020-06-18 18:19:30 +00:00
|
|
|
return 0, 0, xerrors.Errorf("piece cannot fit into a sector")
|
|
|
|
}
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Lock()
|
|
|
|
|
|
|
|
sid, pads, err := m.getSectorAndPadding(size)
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
2020-07-20 16:43:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Unlock()
|
2020-07-09 22:27:19 +00:00
|
|
|
return 0, 0, xerrors.Errorf("getting available sector: %w", err)
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
for _, p := range pads {
|
|
|
|
err = m.addPiece(ctx, sid, p.Unpadded(), m.pledgeReader(p.Unpadded()), nil)
|
|
|
|
if err != nil {
|
2020-07-20 16:43:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Unlock()
|
2020-07-09 22:27:19 +00:00
|
|
|
return 0, 0, xerrors.Errorf("writing pads: %w", err)
|
|
|
|
}
|
2020-06-23 20:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
offset := m.unsealedInfoMap.infos[sid].stored
|
|
|
|
err = m.addPiece(ctx, sid, size, r, &d)
|
2020-06-23 20:51:35 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-20 16:43:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Unlock()
|
2020-06-23 21:31:51 +00:00
|
|
|
return 0, 0, xerrors.Errorf("adding piece to sector: %w", err)
|
2020-06-23 20:51:35 +00:00
|
|
|
}
|
2020-06-23 21:31:51 +00:00
|
|
|
|
2020-07-20 16:43:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Unlock()
|
|
|
|
if m.unsealedInfoMap.infos[sid].numDeals == getDealPerSectorLimit(m.sealer.SectorSize()) {
|
2020-07-30 12:22:05 +00:00
|
|
|
if err := m.StartPacking(sid); err != nil {
|
|
|
|
return 0, 0, xerrors.Errorf("start packing: %w", err)
|
|
|
|
}
|
2020-07-20 16:43:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:21:50 +00:00
|
|
|
return sid, offset, nil
|
2020-06-23 20:51:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
// Caller should hold m.unsealedInfoMap.mux
|
|
|
|
func (m *Sealing) addPiece(ctx context.Context, sectorID abi.SectorNumber, size abi.UnpaddedPieceSize, r io.Reader, di *DealInfo) error {
|
2020-06-23 20:51:35 +00:00
|
|
|
log.Infof("Adding piece to sector %d", sectorID)
|
2020-07-09 22:27:19 +00:00
|
|
|
ppi, err := m.sealer.AddPiece(sectorstorage.WithPriority(ctx, DealSectorPriority), m.minerSector(sectorID), m.unsealedInfoMap.infos[sectorID].pieceSizes, size, r)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("writing piece: %w", err)
|
|
|
|
}
|
|
|
|
piece := Piece{
|
|
|
|
Piece: ppi,
|
|
|
|
DealInfo: di,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sectors.Send(uint64(sectorID), SectorAddPiece{NewPiece: piece})
|
2020-06-23 21:47:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
ui := m.unsealedInfoMap.infos[sectorID]
|
2020-07-20 16:43:19 +00:00
|
|
|
num := m.unsealedInfoMap.infos[sectorID].numDeals
|
|
|
|
if di != nil {
|
|
|
|
num = num + 1
|
|
|
|
}
|
2020-07-09 22:27:19 +00:00
|
|
|
m.unsealedInfoMap.infos[sectorID] = UnsealedSectorInfo{
|
2020-07-20 16:43:19 +00:00
|
|
|
numDeals: num,
|
2020-07-30 12:22:05 +00:00
|
|
|
stored: ui.stored + piece.Piece.Size,
|
2020-06-23 21:47:53 +00:00
|
|
|
pieceSizes: append(ui.pieceSizes, piece.Piece.Size.Unpadded()),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
func (m *Sealing) Remove(ctx context.Context, sid abi.SectorNumber) error {
|
|
|
|
return m.sectors.Send(uint64(sid), SectorRemove{})
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
// Caller should NOT hold m.unsealedInfoMap.mux
|
2020-06-23 19:59:57 +00:00
|
|
|
func (m *Sealing) StartPacking(sectorID abi.SectorNumber) error {
|
|
|
|
log.Infof("Starting packing sector %d", sectorID)
|
2020-06-23 21:47:53 +00:00
|
|
|
err := m.sectors.Send(uint64(sectorID), SectorStartPacking{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
m.unsealedInfoMap.mux.Lock()
|
|
|
|
delete(m.unsealedInfoMap.infos, sectorID)
|
|
|
|
m.unsealedInfoMap.mux.Unlock()
|
2020-06-23 21:47:53 +00:00
|
|
|
|
|
|
|
return nil
|
2020-06-23 19:59:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
// Caller should hold m.unsealedInfoMap.mux
|
|
|
|
func (m *Sealing) getSectorAndPadding(size abi.UnpaddedPieceSize) (abi.SectorNumber, []abi.PaddedPieceSize, error) {
|
2020-07-30 12:22:05 +00:00
|
|
|
ss := abi.PaddedPieceSize(m.sealer.SectorSize())
|
2020-07-09 22:27:19 +00:00
|
|
|
for k, v := range m.unsealedInfoMap.infos {
|
2020-07-30 12:22:05 +00:00
|
|
|
pads, padLength := ffiwrapper.GetRequiredPadding(v.stored, size.Padded())
|
|
|
|
if v.stored+size.Padded()+padLength <= ss {
|
2020-07-09 22:27:19 +00:00
|
|
|
return k, pads, nil
|
2020-06-23 23:21:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:19 +00:00
|
|
|
ns, err := m.newSector()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.unsealedInfoMap.infos[ns] = UnsealedSectorInfo{
|
2020-07-20 16:43:19 +00:00
|
|
|
numDeals: 0,
|
2020-07-09 22:27:19 +00:00
|
|
|
stored: 0,
|
|
|
|
pieceSizes: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns, nil, nil
|
2020-06-23 23:21:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:51:35 +00:00
|
|
|
// newSector creates a new sector for deal storage
|
2020-06-23 23:19:16 +00:00
|
|
|
func (m *Sealing) newSector() (abi.SectorNumber, error) {
|
|
|
|
sid, err := m.sc.Next()
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("getting sector number: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sealer.NewSector(context.TODO(), m.minerSector(sid))
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("initializing sector: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:07:54 +00:00
|
|
|
rt, err := ffiwrapper.SealProofTypeFromSectorSize(m.sealer.SectorSize())
|
|
|
|
if err != nil {
|
2020-06-23 23:19:16 +00:00
|
|
|
return 0, xerrors.Errorf("bad sector size: %w", err)
|
2020-06-23 20:07:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
log.Infof("Creating sector %d", sid)
|
2020-06-23 21:47:53 +00:00
|
|
|
err = m.sectors.Send(uint64(sid), SectorStart{
|
2020-03-22 20:44:27 +00:00
|
|
|
ID: sid,
|
|
|
|
SectorType: rt,
|
2020-01-15 20:49:11 +00:00
|
|
|
})
|
2020-06-23 21:47:53 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-06-23 23:19:16 +00:00
|
|
|
return 0, xerrors.Errorf("starting the sector fsm: %w", err)
|
2020-06-23 21:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:04:11 +00:00
|
|
|
sd, err := m.getSealDelay()
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("getting the sealing delay: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sd > 0 {
|
|
|
|
timer := time.NewTimer(sd)
|
|
|
|
go func() {
|
|
|
|
<-timer.C
|
|
|
|
m.StartPacking(sid)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:19:16 +00:00
|
|
|
return sid, nil
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
2020-03-17 20:19:52 +00:00
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
// newSectorCC accepts a slice of pieces with no deal (junk data)
|
2020-06-23 20:07:54 +00:00
|
|
|
func (m *Sealing) newSectorCC(sid abi.SectorNumber, pieces []Piece) error {
|
|
|
|
rt, err := ffiwrapper.SealProofTypeFromSectorSize(m.sealer.SectorSize())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("bad sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:32:22 +00:00
|
|
|
log.Infof("Creating CC sector %d", sid)
|
|
|
|
return m.sectors.Send(uint64(sid), SectorStartCC{
|
|
|
|
ID: sid,
|
|
|
|
Pieces: pieces,
|
|
|
|
SectorType: rt,
|
|
|
|
})
|
2020-06-22 16:42:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 20:19:52 +00:00
|
|
|
func (m *Sealing) minerSector(num abi.SectorNumber) abi.SectorID {
|
|
|
|
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
|
|
|
|
|
|
|
func getDealPerSectorLimit(size abi.SectorSize) uint64 {
|
|
|
|
if size < 64<<30 {
|
|
|
|
return 256
|
|
|
|
} else {
|
|
|
|
return 512
|
|
|
|
}
|
|
|
|
}
|