2019-10-21 18:12:11 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
2019-10-24 14:24:31 +00:00
|
|
|
"bytes"
|
2019-10-21 18:12:11 +00:00
|
|
|
"context"
|
|
|
|
|
2019-10-28 23:51:50 +00:00
|
|
|
ipldfree "github.com/ipld/go-ipld-prime/impl/free"
|
|
|
|
"github.com/ipld/go-ipld-prime/traversal/selector"
|
|
|
|
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
unixfile "github.com/ipfs/go-unixfs/file"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-11-06 19:00:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/padreader"
|
2019-10-21 18:12:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
type providerHandlerFunc func(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error)
|
|
|
|
|
|
|
|
func (p *Provider) handle(ctx context.Context, deal MinerDeal, cb providerHandlerFunc, next api.DealState) {
|
|
|
|
go func() {
|
|
|
|
mut, err := cb(ctx, deal)
|
|
|
|
|
|
|
|
if err == nil && next == api.DealNoUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p.updated <- minerDealUpdate{
|
|
|
|
newState: next,
|
|
|
|
id: deal.ProposalCid,
|
|
|
|
err: err,
|
|
|
|
mut: mut,
|
|
|
|
}:
|
|
|
|
case <-p.stop:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACCEPTED
|
|
|
|
func (p *Provider) accept(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
|
|
|
|
2019-10-23 17:39:14 +00:00
|
|
|
head, err := p.full.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if head.Height() >= deal.Proposal.ProposalExpiration {
|
|
|
|
return nil, xerrors.Errorf("deal proposal already expired")
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:44:34 +00:00
|
|
|
// TODO: check StorageCollateral
|
|
|
|
|
2019-10-29 12:02:24 +00:00
|
|
|
minPrice := types.BigDiv(types.BigMul(p.ask.Ask.Price, types.NewInt(deal.Proposal.PieceSize)), types.NewInt(1<<30))
|
2019-10-29 10:01:18 +00:00
|
|
|
if deal.Proposal.StoragePricePerEpoch.LessThan(minPrice) {
|
|
|
|
return nil, xerrors.Errorf("storage price per epoch less than asking price: %s < %s", deal.Proposal.StoragePricePerEpoch, minPrice)
|
2019-10-25 12:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if deal.Proposal.PieceSize < p.ask.Ask.MinPieceSize {
|
|
|
|
return nil, xerrors.Errorf("piece size less than minimum required size: %d < %d", deal.Proposal.PieceSize, p.ask.Ask.MinPieceSize)
|
|
|
|
}
|
2019-10-21 18:12:11 +00:00
|
|
|
|
|
|
|
// check market funds
|
2019-10-22 10:09:36 +00:00
|
|
|
clientMarketBalance, err := p.full.StateMarketBalance(ctx, deal.Proposal.Client, nil)
|
2019-10-21 18:12:11 +00:00
|
|
|
if err != nil {
|
2019-10-23 12:59:57 +00:00
|
|
|
return nil, xerrors.Errorf("getting client market balance failed: %w", err)
|
2019-10-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This doesn't guarantee that the client won't withdraw / lock those funds
|
|
|
|
// but it's a decent first filter
|
2019-10-29 10:01:18 +00:00
|
|
|
if clientMarketBalance.Available.LessThan(deal.Proposal.TotalStoragePrice()) {
|
2019-10-21 18:12:11 +00:00
|
|
|
return nil, xerrors.New("clientMarketBalance.Available too small")
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:39:14 +00:00
|
|
|
waddr, err := p.full.StateMinerWorker(ctx, deal.Proposal.Provider, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:15:38 +00:00
|
|
|
// TODO: check StorageCollateral (may be too large (or too small))
|
|
|
|
if err := p.full.MarketEnsureAvailable(ctx, waddr, deal.Proposal.StorageCollateral); err != nil {
|
|
|
|
return nil, err
|
2019-10-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("publishing deal")
|
|
|
|
|
2019-10-23 17:39:14 +00:00
|
|
|
params, err := actors.SerializeParams(&actors.PublishStorageDealsParams{
|
2019-12-07 14:12:10 +00:00
|
|
|
Deals: []actors.StorageDealProposal{deal.Proposal},
|
2019-10-23 17:39:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing PublishStorageDeals params failed: ", err)
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
// TODO: We may want this to happen after fetching data
|
|
|
|
smsg, err := p.full.MpoolPushMessage(ctx, &types.Message{
|
|
|
|
To: actors.StorageMarketAddress,
|
2019-10-23 17:39:14 +00:00
|
|
|
From: waddr,
|
2019-10-21 18:12:11 +00:00
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
GasLimit: types.NewInt(1000000),
|
|
|
|
Method: actors.SMAMethods.PublishStorageDeals,
|
2019-10-23 17:39:14 +00:00
|
|
|
Params: params,
|
2019-10-21 18:12:11 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r, err := p.full.StateWaitMsg(ctx, smsg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Receipt.ExitCode != 0 {
|
|
|
|
return nil, xerrors.Errorf("publishing deal failed: exit %d", r.Receipt.ExitCode)
|
|
|
|
}
|
2019-10-24 14:24:31 +00:00
|
|
|
var resp actors.PublishStorageDealResponse
|
|
|
|
if err := resp.UnmarshalCBOR(bytes.NewReader(r.Receipt.Return)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(resp.DealIDs) != 1 {
|
2019-12-01 20:07:42 +00:00
|
|
|
return nil, xerrors.Errorf("got unexpected number of DealIDs from SMA")
|
2019-10-24 14:24:31 +00:00
|
|
|
}
|
2019-10-21 18:12:11 +00:00
|
|
|
|
2019-12-01 20:07:42 +00:00
|
|
|
log.Infof("fetching data for a deal %d", resp.DealIDs[0])
|
2019-10-23 12:59:57 +00:00
|
|
|
err = p.sendSignedResponse(&Response{
|
2019-11-07 12:57:00 +00:00
|
|
|
State: api.DealAccepted,
|
|
|
|
|
2019-12-07 14:12:10 +00:00
|
|
|
Proposal: deal.ProposalCid,
|
|
|
|
StorageDealSubmission: smsg,
|
2019-10-21 18:12:11 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:57:00 +00:00
|
|
|
if err := p.disconnect(deal); err != nil {
|
|
|
|
log.Warnf("closing client connection: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-28 23:51:50 +00:00
|
|
|
ssb := builder.NewSelectorSpecBuilder(ipldfree.NodeBuilder())
|
|
|
|
|
|
|
|
// this is the selector for "get the whole DAG"
|
2019-10-29 00:57:12 +00:00
|
|
|
// TODO: support storage deals with custom payload selectors
|
2019-10-28 23:51:50 +00:00
|
|
|
allSelector := ssb.ExploreRecursive(selector.RecursionLimitNone(),
|
|
|
|
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()
|
|
|
|
|
2019-10-29 00:57:12 +00:00
|
|
|
// initiate a pull data transfer. This will complete asynchronously and the
|
|
|
|
// completion of the data transfer will trigger a change in deal state
|
|
|
|
// (see onDataTransferEvent)
|
2019-10-31 03:00:02 +00:00
|
|
|
_, err = p.dataTransfer.OpenPullDataChannel(ctx,
|
|
|
|
deal.Client,
|
2019-12-01 20:07:42 +00:00
|
|
|
&StorageDataTransferVoucher{Proposal: deal.ProposalCid, DealID: resp.DealIDs[0]},
|
2019-10-28 23:51:50 +00:00
|
|
|
deal.Ref,
|
|
|
|
allSelector,
|
|
|
|
)
|
2019-12-05 05:14:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to open pull data channel: %w", err)
|
|
|
|
}
|
2019-10-28 23:51:50 +00:00
|
|
|
|
2019-12-01 20:07:42 +00:00
|
|
|
return nil, nil
|
2019-10-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// STAGED
|
|
|
|
|
|
|
|
func (p *Provider) staged(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
|
|
|
root, err := p.dag.Get(ctx, deal.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get file root for deal: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: abstract this away into ReadSizeCloser + implement different modes
|
|
|
|
n, err := unixfile.NewUnixfsFile(ctx, p.dag, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("cannot open unixfs file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uf, ok := n.(sectorblocks.UnixfsReader)
|
|
|
|
if !ok {
|
|
|
|
// we probably got directory, unsupported for now
|
|
|
|
return nil, xerrors.Errorf("unsupported unixfs file type")
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:04:07 +00:00
|
|
|
// TODO: uf.Size() is user input, not trusted
|
|
|
|
// This won't be useful / here after we migrate to putting CARs into sectors
|
|
|
|
size, err := uf.Size()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting unixfs file size: %w", err)
|
|
|
|
}
|
2019-11-06 19:00:04 +00:00
|
|
|
if padreader.PaddedSize(uint64(size)) != deal.Proposal.PieceSize {
|
|
|
|
return nil, xerrors.Errorf("deal.Proposal.PieceSize didn't match padded unixfs file size")
|
2019-10-23 18:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:58:31 +00:00
|
|
|
sectorID, err := p.secb.AddUnixfsPiece(ctx, uf, deal.DealID)
|
2019-11-05 18:40:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("AddPiece failed: %s", err)
|
|
|
|
}
|
2019-12-01 20:07:42 +00:00
|
|
|
log.Warnf("New Sector: %d (deal %d)", sectorID, deal.DealID)
|
2019-11-05 18:40:51 +00:00
|
|
|
|
|
|
|
return func(deal *MinerDeal) {
|
|
|
|
deal.SectorID = sectorID
|
|
|
|
}, nil
|
2019-10-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SEALING
|
|
|
|
|
|
|
|
func (p *Provider) sealing(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
2019-11-07 18:22:59 +00:00
|
|
|
// TODO: consider waiting for seal to happen
|
2019-10-21 18:12:11 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) complete(ctx context.Context, deal MinerDeal) (func(*MinerDeal), error) {
|
2019-11-07 14:09:11 +00:00
|
|
|
// TODO: observe sector lifecycle, status, expiration..
|
2019-10-21 18:12:11 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|