2019-07-29 18:57:23 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-30 00:46:56 +00:00
|
|
|
"fmt"
|
2019-08-14 20:27:10 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/storage/sector"
|
2019-07-29 18:57:23 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
host "github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("storageminer")
|
|
|
|
|
|
|
|
type Miner struct {
|
|
|
|
api storageMinerApi
|
|
|
|
|
2019-08-14 20:27:10 +00:00
|
|
|
secst *sector.Store
|
2019-07-29 18:57:23 +00:00
|
|
|
|
|
|
|
maddr address.Address
|
|
|
|
|
|
|
|
worker address.Address
|
|
|
|
|
|
|
|
h host.Host
|
|
|
|
|
|
|
|
ds datastore.Batching
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-31 16:54:28 +00:00
|
|
|
ChainCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error)
|
2019-07-29 18:57:23 +00:00
|
|
|
|
|
|
|
MpoolPush(context.Context, *types.SignedMessage) error
|
|
|
|
MpoolGetNonce(context.Context, address.Address) (uint64, error)
|
|
|
|
|
|
|
|
ChainWaitMsg(context.Context, cid.Cid) (*api.MsgWait, error)
|
2019-07-30 00:46:56 +00:00
|
|
|
ChainNotify(context.Context) (<-chan *store.HeadChange, error)
|
2019-08-08 04:24:49 +00:00
|
|
|
|
|
|
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
|
|
|
WalletSign(context.Context, address.Address, []byte) (*types.Signature, 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-08-14 20:27:10 +00:00
|
|
|
func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datastore.Batching, secst *sector.Store) (*Miner, error) {
|
2019-07-29 18:57:23 +00:00
|
|
|
return &Miner{
|
|
|
|
api: api,
|
|
|
|
maddr: addr,
|
|
|
|
h: h,
|
|
|
|
ds: ds,
|
2019-08-14 20:27:10 +00:00
|
|
|
secst: secst,
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
go m.handlePostingSealedSectors(ctx)
|
|
|
|
go m.runPoSt(ctx)
|
|
|
|
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
|
|
|
|
log.Warning("sealed sector channel closed, aborting process")
|
|
|
|
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():
|
|
|
|
log.Warning("exiting seal posting routine")
|
|
|
|
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-08-08 01:16:58 +00:00
|
|
|
ok, err := sectorbuilder.VerifySeal(1024, sinfo.CommR[:], sinfo.CommD[:], sinfo.CommRStar[:], m.maddr, sinfo.SectorID, sinfo.Proof)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to verify seal we just created: ", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Error("seal we just created failed verification")
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:57:23 +00:00
|
|
|
params := &actors.CommitSectorParams{
|
2019-08-07 06:35:57 +00:00
|
|
|
SectorID: types.NewInt(sinfo.SectorID),
|
2019-07-30 00:46:56 +00:00
|
|
|
CommD: sinfo.CommD[:],
|
|
|
|
CommR: sinfo.CommR[:],
|
|
|
|
CommRStar: sinfo.CommRStar[:],
|
2019-07-29 18:57:23 +00:00
|
|
|
Proof: sinfo.Proof,
|
|
|
|
}
|
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-07-30 00:46:56 +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-08-07 23:22:35 +00:00
|
|
|
GasLimit: types.NewInt(1000 /* i dont know help */),
|
2019-07-29 18:57:23 +00:00
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, err := m.api.MpoolGetNonce(ctx, m.worker)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get nonce")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Nonce = nonce
|
|
|
|
|
|
|
|
data, err := msg.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "serializing commit sector message")
|
|
|
|
}
|
|
|
|
|
2019-08-08 04:24:49 +00:00
|
|
|
sig, err := m.api.WalletSign(ctx, m.worker, data)
|
2019-07-29 18:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "signing commit sector message")
|
|
|
|
}
|
|
|
|
|
|
|
|
smsg := &types.SignedMessage{
|
|
|
|
Message: msg,
|
2019-07-30 00:46:56 +00:00
|
|
|
Signature: *sig,
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.api.MpoolPush(ctx, smsg); err != nil {
|
|
|
|
return errors.Wrap(err, "pushing commit sector message to mpool")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.trackCommitSectorMessage(smsg)
|
2019-07-30 00:46:56 +00:00
|
|
|
return nil
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the message gets included in the chain successfully
|
|
|
|
func (m *Miner) trackCommitSectorMessage(smsg *types.SignedMessage) {
|
|
|
|
log.Warning("not currently tracking commit sector messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) runPoSt(ctx context.Context) {
|
|
|
|
log.Warning("dont care about posts yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) runPreflightChecks(ctx context.Context) error {
|
|
|
|
worker, err := m.getWorkerAddr(ctx)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Miner) getWorkerAddr(ctx context.Context) (address.Address, error) {
|
2019-07-30 00:46:56 +00:00
|
|
|
msg := &types.Message{
|
|
|
|
To: m.maddr,
|
|
|
|
From: m.maddr, // it doesnt like it if we dont give it a from... probably should fix that
|
|
|
|
Method: actors.MAMethods.GetWorkerAddr,
|
|
|
|
Params: actors.EmptyStructCBOR,
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:54:28 +00:00
|
|
|
recpt, err := m.api.ChainCall(ctx, msg, nil)
|
2019-07-29 18:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, errors.Wrapf(err, "calling getWorker(%s)", m.maddr)
|
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
if recpt.ExitCode != 0 {
|
|
|
|
return address.Undef, fmt.Errorf("failed to call getWorker(%s): return %d", m.maddr, recpt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.NewFromBytes(recpt.Return)
|
2019-07-29 18:57:23 +00:00
|
|
|
}
|