2020-01-15 20:49:11 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-27 23:00:21 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2020-01-15 20:49:11 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"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-02-11 01:10:50 +00:00
|
|
|
"github.com/filecoin-project/go-padreader"
|
2020-03-06 18:59:08 +00:00
|
|
|
"github.com/filecoin-project/go-statemachine"
|
2020-02-21 17:43:44 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"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-02-09 06:06:32 +00:00
|
|
|
|
2020-01-15 20:49:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-03-27 23:00:21 +00:00
|
|
|
"github.com/filecoin-project/sector-storage"
|
2020-01-15 20:49:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SectorStorePrefix = "/sectors"
|
|
|
|
|
|
|
|
var log = logging.Logger("sectors")
|
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
type TicketFn func(context.Context) (*api.SealTicket, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-03-17 20:19:52 +00:00
|
|
|
type SectorIDCounter interface {
|
|
|
|
Next() (abi.SectorNumber, error)
|
|
|
|
}
|
|
|
|
|
2020-01-15 20:53:14 +00:00
|
|
|
type sealingApi interface { // TODO: trim down
|
2020-01-15 20:49:11 +00:00
|
|
|
// Call a read only method on actors (no interaction with the chain required)
|
2020-03-03 23:32:17 +00:00
|
|
|
StateCall(context.Context, *types.Message, types.TipSetKey) (*api.InvocResult, error)
|
2020-02-11 23:29:45 +00:00
|
|
|
StateMinerWorker(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
2020-02-24 17:32:02 +00:00
|
|
|
StateMinerPostState(ctx context.Context, actor address.Address, ts types.TipSetKey) (*miner.PoStState, error)
|
2020-02-11 23:29:45 +00:00
|
|
|
StateMinerSectors(context.Context, address.Address, types.TipSetKey) ([]*api.ChainSectorInfo, error)
|
|
|
|
StateMinerProvingSet(context.Context, address.Address, types.TipSetKey) ([]*api.ChainSectorInfo, error)
|
2020-02-24 17:32:02 +00:00
|
|
|
StateMinerSectorSize(context.Context, address.Address, types.TipSetKey) (abi.SectorSize, error)
|
2020-03-18 23:06:53 +00:00
|
|
|
StateWaitMsg(context.Context, cid.Cid) (*api.MsgLookup, error) // TODO: removeme eventually
|
2020-02-24 17:32:02 +00:00
|
|
|
StateGetActor(ctx context.Context, actor address.Address, ts types.TipSetKey) (*types.Actor, error)
|
2020-02-11 23:29:45 +00:00
|
|
|
StateGetReceipt(context.Context, cid.Cid, types.TipSetKey) (*types.MessageReceipt, error)
|
2020-02-24 17:32:02 +00:00
|
|
|
StateMarketStorageDeal(context.Context, abi.DealID, types.TipSetKey) (*api.MarketDeal, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
|
|
|
|
MpoolPushMessage(context.Context, *types.Message) (*types.SignedMessage, error)
|
|
|
|
|
2020-02-24 17:45:25 +00:00
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
ChainNotify(context.Context) (<-chan []*store.HeadChange, error)
|
2020-02-23 20:00:47 +00:00
|
|
|
ChainGetRandomness(ctx context.Context, tsk types.TipSetKey, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error)
|
2020-02-24 17:32:02 +00:00
|
|
|
ChainGetTipSetByHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
ChainGetBlockMessages(context.Context, cid.Cid) (*api.BlockMessages, error)
|
2020-01-23 17:34:04 +00:00
|
|
|
ChainReadObj(context.Context, cid.Cid) ([]byte, error)
|
2020-02-12 07:44:20 +00:00
|
|
|
ChainHasObj(context.Context, cid.Cid) (bool, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
|
2020-02-25 21:09:22 +00:00
|
|
|
WalletSign(context.Context, address.Address, []byte) (*crypto.Signature, error)
|
2020-01-15 20:49:11 +00:00
|
|
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
|
|
|
WalletHas(context.Context, address.Address) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Sealing struct {
|
2020-01-15 20:53:14 +00:00
|
|
|
api sealingApi
|
2020-01-15 20:49:11 +00:00
|
|
|
events *events.Events
|
|
|
|
|
|
|
|
maddr address.Address
|
|
|
|
worker address.Address
|
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
sealer sectorstorage.SectorManager
|
2020-01-15 20:49:11 +00:00
|
|
|
sectors *statemachine.StateGroup
|
|
|
|
tktFn TicketFn
|
2020-03-17 20:19:52 +00:00
|
|
|
sc SectorIDCounter
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
func New(api sealingApi, events *events.Events, maddr address.Address, worker address.Address, ds datastore.Batching, sealer sectorstorage.SectorManager, sc SectorIDCounter, tktFn TicketFn) *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-01-16 02:54:57 +00:00
|
|
|
maddr: maddr,
|
|
|
|
worker: worker,
|
2020-03-03 22:19:22 +00:00
|
|
|
sealer: sealer,
|
2020-01-16 02:54:57 +00:00
|
|
|
tktFn: tktFn,
|
2020-03-18 01:08:11 +00:00
|
|
|
sc: sc,
|
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-02-11 01:10:50 +00:00
|
|
|
func (m *Sealing) AllocatePiece(size abi.UnpaddedPieceSize) (sectorID abi.SectorNumber, offset uint64, err error) {
|
|
|
|
if (padreader.PaddedSize(uint64(size))) != size {
|
2020-01-15 20:49:11 +00:00
|
|
|
return 0, 0, xerrors.Errorf("cannot allocate unpadded piece")
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:19:52 +00:00
|
|
|
sid, err := m.sc.Next()
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
2020-03-17 20:19:52 +00:00
|
|
|
return 0, 0, xerrors.Errorf("getting sector number: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sealer.NewSector(context.TODO(), m.minerSector(sid)) // TODO: Put more than one thing in a sector
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, xerrors.Errorf("initializing sector: %w", err)
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// offset hard-coded to 0 since we only put one thing in a sector for now
|
|
|
|
return sid, 0, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (m *Sealing) SealPiece(ctx context.Context, size abi.UnpaddedPieceSize, r io.Reader, sectorID abi.SectorNumber, dealID abi.DealID) error {
|
2020-01-15 20:49:11 +00:00
|
|
|
log.Infof("Seal piece for deal %d", dealID)
|
|
|
|
|
2020-03-17 20:19:52 +00:00
|
|
|
ppi, err := m.sealer.AddPiece(ctx, m.minerSector(sectorID), []abi.UnpaddedPieceSize{}, size, r)
|
2020-01-15 20:49:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("adding piece to sector: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
_, rt, err := ffiwrapper.ProofTypeFromSectorSize(m.sealer.SectorSize())
|
2020-02-28 18:01:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("bad sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.newSector(sectorID, rt, []Piece{
|
2020-02-23 00:47:47 +00:00
|
|
|
{
|
|
|
|
DealID: &dealID,
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
Size: ppi.Size.Unpadded(),
|
|
|
|
CommP: ppi.PieceCID,
|
2020-02-23 00:47:47 +00:00
|
|
|
},
|
2020-02-23 15:50:36 +00:00
|
|
|
})
|
2020-01-15 20:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:01:43 +00:00
|
|
|
func (m *Sealing) newSector(sid abi.SectorNumber, rt abi.RegisteredProof, pieces []Piece) error {
|
2020-01-31 19:22:31 +00:00
|
|
|
log.Infof("Start sealing %d", sid)
|
2020-02-23 00:47:47 +00:00
|
|
|
return m.sectors.Send(uint64(sid), SectorStart{
|
2020-03-22 20:44:27 +00:00
|
|
|
ID: sid,
|
|
|
|
Pieces: pieces,
|
|
|
|
SectorType: rt,
|
2020-01-15 20:49:11 +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
|
|
|
|
}
|