2019-07-29 18:57:23 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-26 23:07:40 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
logging "github.com/ipfs/go-log"
|
2019-09-18 13:32:21 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-07-29 18:57:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-10-24 14:24:31 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-29 18:57:23 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/sectorbuilder"
|
|
|
|
"github.com/filecoin-project/lotus/storage/commitment"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sector"
|
2019-07-29 18:57:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("storageminer")
|
|
|
|
|
2019-10-17 00:42:32 +00:00
|
|
|
const PoStConfidence = 3
|
2019-09-18 13:32:21 +00:00
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
type Miner struct {
|
2019-09-18 11:45:52 +00:00
|
|
|
api storageMinerApi
|
|
|
|
events *events.Events
|
2019-07-29 18:57:23 +00:00
|
|
|
|
2019-08-14 20:27:10 +00:00
|
|
|
secst *sector.Store
|
2019-09-16 16:40:26 +00:00
|
|
|
commt *commitment.Tracker
|
2019-07-29 18:57:23 +00:00
|
|
|
|
|
|
|
maddr address.Address
|
|
|
|
|
|
|
|
worker address.Address
|
|
|
|
|
|
|
|
h host.Host
|
|
|
|
|
|
|
|
ds datastore.Batching
|
2019-09-19 16:17:49 +00:00
|
|
|
|
|
|
|
schedLk sync.Mutex
|
2019-09-26 20:57:20 +00:00
|
|
|
schedPost uint64
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageMinerApi interface {
|
|
|
|
// I think I want this... but this is tricky
|
|
|
|
//ReadState(ctx context.Context, addr address.Address) (????, error)
|
|
|
|
|
|
|
|
// Call a read only method on actors (no interaction with the chain required)
|
2019-09-06 06:26:02 +00:00
|
|
|
StateCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error)
|
2019-09-18 02:50:03 +00:00
|
|
|
StateMinerWorker(context.Context, address.Address, *types.TipSet) (address.Address, error)
|
|
|
|
StateMinerProvingPeriodEnd(context.Context, address.Address, *types.TipSet) (uint64, error)
|
|
|
|
StateMinerProvingSet(context.Context, address.Address, *types.TipSet) ([]*api.SectorInfo, error)
|
2019-10-16 07:07:16 +00:00
|
|
|
StateMinerSectorSize(context.Context, address.Address, *types.TipSet) (uint64, error)
|
2019-10-08 05:51:34 +00:00
|
|
|
StateWaitMsg(context.Context, cid.Cid) (*api.MsgWait, error)
|
2019-07-29 18:57:23 +00:00
|
|
|
|
2019-09-19 16:17:49 +00:00
|
|
|
MpoolPushMessage(context.Context, *types.Message) (*types.SignedMessage, error)
|
2019-07-29 18:57:23 +00:00
|
|
|
|
2019-09-18 13:32:21 +00:00
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
2019-09-18 11:01:52 +00:00
|
|
|
ChainNotify(context.Context) (<-chan []*store.HeadChange, error)
|
2019-09-18 02:50:03 +00:00
|
|
|
ChainGetRandomness(context.Context, *types.TipSet, []*types.Ticket, int) ([]byte, error)
|
2019-09-18 00:08:49 +00:00
|
|
|
ChainGetTipSetByHeight(context.Context, uint64, *types.TipSet) (*types.TipSet, error)
|
2019-09-18 11:45:52 +00:00
|
|
|
ChainGetBlockMessages(context.Context, cid.Cid) (*api.BlockMessages, error)
|
2019-08-08 04:24:49 +00:00
|
|
|
|
|
|
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
2019-08-08 17:29:23 +00:00
|
|
|
WalletHas(context.Context, address.Address) (bool, error)
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:40:26 +00:00
|
|
|
func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datastore.Batching, secst *sector.Store, commt *commitment.Tracker) (*Miner, error) {
|
2019-07-29 18:57:23 +00:00
|
|
|
return &Miner{
|
2019-09-18 18:07:39 +00:00
|
|
|
api: api,
|
2019-09-18 11:45:52 +00:00
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
maddr: addr,
|
|
|
|
h: h,
|
|
|
|
ds: ds,
|
2019-08-14 20:27:10 +00:00
|
|
|
secst: secst,
|
2019-09-16 16:40:26 +00:00
|
|
|
commt: commt,
|
2019-07-29 18:57:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) Run(ctx context.Context) error {
|
|
|
|
if err := m.runPreflightChecks(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "miner preflight checks failed")
|
|
|
|
}
|
|
|
|
|
2019-09-18 18:07:39 +00:00
|
|
|
m.events = events.NewEvents(ctx, m.api)
|
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
go m.handlePostingSealedSectors(ctx)
|
2019-09-26 20:57:20 +00:00
|
|
|
go m.beginPosting(ctx)
|
2019-07-29 18:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) handlePostingSealedSectors(ctx context.Context) {
|
2019-08-14 20:27:10 +00:00
|
|
|
incoming := m.secst.Incoming()
|
|
|
|
defer m.secst.CloseIncoming(incoming)
|
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-08-14 20:27:10 +00:00
|
|
|
case sinfo, ok := <-incoming:
|
2019-07-29 18:57:23 +00:00
|
|
|
if !ok {
|
|
|
|
// TODO: set some state variable so that this state can be
|
|
|
|
// visible via some status command
|
2019-10-09 20:23:04 +00:00
|
|
|
log.Warn("sealed sector channel closed, aborting process")
|
2019-07-29 18:57:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
if err := m.commitSector(ctx, sinfo); err != nil {
|
|
|
|
log.Errorf("failed to commit sector: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
case <-ctx.Done():
|
2019-10-09 20:23:04 +00:00
|
|
|
log.Warn("exiting seal posting routine")
|
2019-07-29 18:57:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 00:46:56 +00:00
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSealingStatus) error {
|
2019-08-12 21:48:18 +00:00
|
|
|
log.Info("committing sector")
|
|
|
|
|
2019-10-16 07:07:16 +00:00
|
|
|
ssize, err := m.SectorSize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to check out own sector size: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:58:41 +00:00
|
|
|
ok, err := sectorbuilder.VerifySeal(ssize, sinfo.CommR[:], sinfo.CommD[:], m.maddr, sinfo.Ticket.TicketBytes[:], sinfo.SectorID, sinfo.Proof)
|
2019-08-08 01:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to verify seal we just created: ", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Error("seal we just created failed verification")
|
|
|
|
}
|
|
|
|
|
2019-10-24 14:24:31 +00:00
|
|
|
deals, err := m.secst.DealsForCommit(sinfo.SectorID)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting sector deals failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 17:34:59 +00:00
|
|
|
params := &actors.OnChainSealVerifyInfo{
|
2019-10-21 11:58:41 +00:00
|
|
|
CommD: sinfo.CommD[:],
|
|
|
|
CommR: sinfo.CommR[:],
|
|
|
|
Proof: sinfo.Proof,
|
2019-10-24 14:24:31 +00:00
|
|
|
|
|
|
|
DealIDs: deals,
|
2019-10-22 17:34:59 +00:00
|
|
|
SectorNumber: sinfo.SectorID,
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
2019-07-30 00:46:56 +00:00
|
|
|
enc, aerr := actors.SerializeParams(params)
|
|
|
|
if aerr != nil {
|
|
|
|
return errors.Wrap(aerr, "could not serialize commit sector parameters")
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 16:17:49 +00:00
|
|
|
msg := &types.Message{
|
2019-07-29 18:57:23 +00:00
|
|
|
To: m.maddr,
|
|
|
|
From: m.worker,
|
|
|
|
Method: actors.MAMethods.CommitSector,
|
2019-07-30 00:46:56 +00:00
|
|
|
Params: enc,
|
2019-07-29 18:57:23 +00:00
|
|
|
Value: types.NewInt(0), // TODO: need to ensure sufficient collateral
|
2019-10-18 04:08:36 +00:00
|
|
|
GasLimit: types.NewInt(1000000 /* i dont know help */),
|
2019-07-29 18:57:23 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:17:49 +00:00
|
|
|
smsg, err := m.api.MpoolPushMessage(ctx, msg)
|
2019-07-29 18:57:23 +00:00
|
|
|
if err != nil {
|
2019-09-19 16:17:49 +00:00
|
|
|
return errors.Wrap(err, "pushing message to mpool")
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 18:05:22 +00:00
|
|
|
if err := m.commt.TrackCommitSectorMsg(m.maddr, sinfo.SectorID, smsg.Cid()); err != nil {
|
2019-09-16 16:40:26 +00:00
|
|
|
return errors.Wrap(err, "tracking sector commitment")
|
|
|
|
}
|
2019-07-29 18:57:23 +00:00
|
|
|
|
2019-09-19 16:17:49 +00:00
|
|
|
go func() {
|
2019-10-08 05:51:34 +00:00
|
|
|
_, err := m.api.StateWaitMsg(ctx, smsg.Cid())
|
2019-09-19 16:17:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:57:20 +00:00
|
|
|
m.beginPosting(ctx)
|
2019-09-19 16:17:49 +00:00
|
|
|
}()
|
|
|
|
|
2019-09-16 16:40:26 +00:00
|
|
|
return nil
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) runPreflightChecks(ctx context.Context) error {
|
2019-09-18 00:08:49 +00:00
|
|
|
worker, err := m.api.StateMinerWorker(ctx, m.maddr, nil)
|
2019-07-29 18:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.worker = worker
|
|
|
|
|
2019-08-08 17:29:23 +00:00
|
|
|
has, err := m.api.WalletHas(ctx, worker)
|
2019-07-30 00:46:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to check wallet for worker key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
return errors.New("key for worker not found in local wallet")
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
log.Infof("starting up miner %s, worker addr %s", m.maddr, m.worker)
|
2019-07-29 18:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-16 07:07:16 +00:00
|
|
|
|
|
|
|
func (m *Miner) SectorSize(ctx context.Context) (uint64, error) {
|
|
|
|
// TODO: cache this
|
|
|
|
return m.api.StateMinerSectorSize(ctx, m.maddr, nil)
|
|
|
|
}
|