2019-08-01 17:12:41 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-06 22:04:21 +00:00
|
|
|
"math"
|
2019-08-01 17:12:41 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2019-08-02 16:25:10 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-08-01 17:12:41 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-08-02 14:09:54 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-08-07 19:48:53 +00:00
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
2019-08-01 17:12:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-09-13 21:00:36 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-09-13 18:19:49 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-08-26 13:45:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
2019-08-01 17:12:41 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-09-13 21:00:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/stmgr"
|
2019-08-01 17:12:41 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-08-02 16:25:10 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/wallet"
|
2019-09-13 21:00:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/cborrpc"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
|
2019-08-26 13:45:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/retrieval/discovery"
|
2019-08-01 17:12:41 +00:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(ClientDeal{})
|
2019-09-13 18:19:49 +00:00
|
|
|
cbor.RegisterCborType(actors.PieceInclVoucherData{}) // TODO: USE CBORGEN!
|
|
|
|
cbor.RegisterCborType(types.SignedVoucher{})
|
|
|
|
cbor.RegisterCborType(types.ModVerifyParams{})
|
|
|
|
cbor.RegisterCborType(types.Signature{})
|
|
|
|
cbor.RegisterCborType(actors.PaymentInfo{})
|
|
|
|
cbor.RegisterCborType(actors.InclusionProof{})
|
2019-08-06 23:08:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 17:12:41 +00:00
|
|
|
var log = logging.Logger("deals")
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
type ClientDeal struct {
|
|
|
|
ProposalCid cid.Cid
|
2019-09-10 12:35:43 +00:00
|
|
|
Proposal StorageDealProposal
|
2019-09-10 14:13:24 +00:00
|
|
|
State api.DealState
|
2019-08-06 22:04:21 +00:00
|
|
|
Miner peer.ID
|
2019-09-10 12:35:43 +00:00
|
|
|
|
|
|
|
s inet.Stream
|
2019-08-01 17:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2019-09-13 21:00:36 +00:00
|
|
|
sm *stmgr.StateManager
|
2019-08-26 13:45:36 +00:00
|
|
|
h host.Host
|
|
|
|
w *wallet.Wallet
|
|
|
|
dag dtypes.ClientDAG
|
|
|
|
discovery *discovery.Local
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-09-13 19:19:13 +00:00
|
|
|
deals ClientStateStore
|
2019-09-10 12:35:43 +00:00
|
|
|
conns map[cid.Cid]inet.Stream
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
incoming chan ClientDeal
|
2019-09-10 12:35:43 +00:00
|
|
|
updated chan clientDealUpdate
|
2019-08-01 17:12:41 +00:00
|
|
|
|
|
|
|
stop chan struct{}
|
|
|
|
stopped chan struct{}
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
type clientDealUpdate struct {
|
2019-09-10 14:13:24 +00:00
|
|
|
newState api.DealState
|
2019-09-10 12:35:43 +00:00
|
|
|
id cid.Cid
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2019-09-13 21:00:36 +00:00
|
|
|
func NewClient(sm *stmgr.StateManager, h host.Host, w *wallet.Wallet, ds dtypes.MetadataDS, dag dtypes.ClientDAG, discovery *discovery.Local) *Client {
|
2019-08-01 17:12:41 +00:00
|
|
|
c := &Client{
|
2019-09-13 21:00:36 +00:00
|
|
|
sm: sm,
|
2019-08-26 13:45:36 +00:00
|
|
|
h: h,
|
|
|
|
w: w,
|
|
|
|
dag: dag,
|
|
|
|
discovery: discovery,
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-09-13 19:19:13 +00:00
|
|
|
deals: ClientStateStore{StateStore{ds: namespace.Wrap(ds, datastore.NewKey("/deals/client"))}},
|
2019-09-10 12:35:43 +00:00
|
|
|
conns: map[cid.Cid]inet.Stream{},
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
incoming: make(chan ClientDeal, 16),
|
2019-09-10 12:35:43 +00:00
|
|
|
updated: make(chan clientDealUpdate, 16),
|
2019-08-01 17:12:41 +00:00
|
|
|
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
func (c *Client) Run(ctx context.Context) {
|
2019-08-01 17:12:41 +00:00
|
|
|
go func() {
|
|
|
|
defer close(c.stopped)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case deal := <-c.incoming:
|
2019-09-10 12:35:43 +00:00
|
|
|
c.onIncoming(deal)
|
|
|
|
case update := <-c.updated:
|
|
|
|
c.onUpdated(ctx, update)
|
2019-08-01 17:12:41 +00:00
|
|
|
case <-c.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
func (c *Client) onIncoming(deal ClientDeal) {
|
|
|
|
log.Info("incoming deal")
|
2019-08-07 19:48:53 +00:00
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
if _, ok := c.conns[deal.ProposalCid]; ok {
|
|
|
|
log.Errorf("tracking deal connection: already tracking connection for deal %s", deal.ProposalCid)
|
|
|
|
return
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-09-10 12:35:43 +00:00
|
|
|
c.conns[deal.ProposalCid] = deal.s
|
2019-08-07 19:48:53 +00:00
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
if err := c.deals.Begin(deal.ProposalCid, deal); err != nil {
|
|
|
|
// We may have re-sent the proposal
|
|
|
|
log.Errorf("deal tracking failed: %s", err)
|
|
|
|
c.failDeal(deal.ProposalCid, err)
|
|
|
|
return
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-07 19:48:53 +00:00
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
go func() {
|
|
|
|
c.updated <- clientDealUpdate{
|
2019-09-10 14:13:24 +00:00
|
|
|
newState: api.DealUnknown,
|
2019-09-10 12:35:43 +00:00
|
|
|
id: deal.ProposalCid,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}()
|
2019-08-07 19:48:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
func (c *Client) onUpdated(ctx context.Context, update clientDealUpdate) {
|
|
|
|
log.Infof("Deal %s updated state to %d", update.id, update.newState)
|
|
|
|
var deal ClientDeal
|
|
|
|
err := c.deals.MutateClient(update.id, func(d *ClientDeal) error {
|
|
|
|
d.State = update.newState
|
|
|
|
deal = *d
|
|
|
|
return nil
|
|
|
|
})
|
2019-09-13 19:43:33 +00:00
|
|
|
if update.err != nil {
|
|
|
|
log.Errorf("deal %s failed: %s", update.id, update.err)
|
|
|
|
c.failDeal(update.id, update.err)
|
|
|
|
return
|
|
|
|
}
|
2019-08-02 14:09:54 +00:00
|
|
|
if err != nil {
|
2019-09-10 12:35:43 +00:00
|
|
|
c.failDeal(update.id, err)
|
|
|
|
return
|
2019-08-07 19:48:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
switch update.newState {
|
2019-09-10 14:13:24 +00:00
|
|
|
case api.DealUnknown: // new
|
|
|
|
c.handle(ctx, deal, c.new, api.DealAccepted)
|
|
|
|
case api.DealAccepted:
|
|
|
|
c.handle(ctx, deal, c.accepted, api.DealStaged)
|
|
|
|
case api.DealStaged:
|
|
|
|
c.handle(ctx, deal, c.staged, api.DealSealing)
|
|
|
|
case api.DealSealing:
|
|
|
|
c.handle(ctx, deal, c.sealing, api.DealComplete)
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-07 19:48:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:28:52 +00:00
|
|
|
type ClientDealProposal struct {
|
|
|
|
Data cid.Cid
|
|
|
|
|
|
|
|
TotalPrice types.BigInt
|
|
|
|
Duration uint64
|
|
|
|
|
|
|
|
Payment actors.PaymentInfo
|
|
|
|
|
|
|
|
MinerAddress address.Address
|
|
|
|
ClientAddress address.Address
|
|
|
|
MinerID peer.ID
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
func (c *Client) VerifyParams(ctx context.Context, data cid.Cid) (*actors.PieceInclVoucherData, error) {
|
2019-08-15 14:28:11 +00:00
|
|
|
commP, size, err := c.commP(ctx, data)
|
2019-08-07 19:48:53 +00:00
|
|
|
if err != nil {
|
2019-08-15 14:28:11 +00:00
|
|
|
return nil, err
|
2019-08-01 17:12:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
return &actors.PieceInclVoucherData{
|
2019-08-15 14:28:11 +00:00
|
|
|
CommP: commP,
|
|
|
|
PieceSize: types.NewInt(uint64(size)),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
func (c *Client) Start(ctx context.Context, p ClientDealProposal, vd *actors.PieceInclVoucherData) (cid.Cid, error) {
|
2019-08-02 14:09:54 +00:00
|
|
|
proposal := StorageDealProposal{
|
2019-08-26 08:02:26 +00:00
|
|
|
PieceRef: p.Data,
|
2019-08-06 23:08:34 +00:00
|
|
|
SerializationMode: SerializationUnixFs,
|
2019-08-15 14:28:11 +00:00
|
|
|
CommP: vd.CommP[:],
|
|
|
|
Size: vd.PieceSize.Uint64(),
|
2019-08-15 00:28:52 +00:00
|
|
|
TotalPrice: p.TotalPrice,
|
|
|
|
Duration: p.Duration,
|
|
|
|
Payment: p.Payment,
|
|
|
|
MinerAddress: p.MinerAddress,
|
|
|
|
ClientAddress: p.ClientAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := c.h.NewStream(ctx, p.MinerID, ProtocolID)
|
2019-08-01 17:12:41 +00:00
|
|
|
if err != nil {
|
2019-08-06 22:04:21 +00:00
|
|
|
return cid.Undef, err
|
2019-08-01 17:12:41 +00:00
|
|
|
}
|
2019-08-02 16:25:10 +00:00
|
|
|
|
2019-08-15 00:28:52 +00:00
|
|
|
if err := c.sendProposal(s, proposal, p.ClientAddress); err != nil {
|
2019-08-06 22:04:21 +00:00
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
proposalNd, err := cbor.WrapObject(proposal, math.MaxUint64, -1)
|
2019-08-06 22:04:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 12:35:43 +00:00
|
|
|
deal := ClientDeal{
|
|
|
|
ProposalCid: proposalNd.Cid(),
|
|
|
|
Proposal: proposal,
|
2019-09-10 14:13:24 +00:00
|
|
|
State: api.DealUnknown,
|
2019-09-10 12:35:43 +00:00
|
|
|
Miner: p.MinerID,
|
|
|
|
|
|
|
|
s: s,
|
|
|
|
}
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-08-07 19:48:53 +00:00
|
|
|
// TODO: actually care about what happens with the deal after it was accepted
|
2019-09-10 12:35:43 +00:00
|
|
|
c.incoming <- deal
|
2019-08-26 13:45:36 +00:00
|
|
|
|
|
|
|
// TODO: start tracking after the deal is sealed
|
|
|
|
return deal.ProposalCid, c.discovery.AddPeer(p.Data, discovery.RetrievalPeer{
|
|
|
|
Address: proposal.MinerAddress,
|
|
|
|
ID: deal.Miner,
|
|
|
|
})
|
2019-08-01 17:12:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:00:36 +00:00
|
|
|
func (c *Client) QueryAsk(ctx context.Context, p peer.ID, a address.Address) (*types.SignedStorageAsk, error) {
|
|
|
|
s, err := c.h.NewStream(ctx, p, AskProtocolID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &AskRequest{
|
|
|
|
Miner: a,
|
|
|
|
}
|
|
|
|
if err := cborrpc.WriteCborRPC(s, req); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to send ask request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out AskResponse
|
|
|
|
if err := cborrpc.ReadCborRPC(s, &out); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to read ask response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Ask == nil {
|
|
|
|
return nil, xerrors.Errorf("got no ask back")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Ask.Ask.Miner != a {
|
|
|
|
return nil, xerrors.Errorf("got back ask for wrong miner")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.checkAskSignature(out.Ask); err != nil {
|
|
|
|
return nil, xerrors.Errorf("ask was not properly signed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Ask, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:13:24 +00:00
|
|
|
func (c *Client) List() ([]ClientDeal, error) {
|
|
|
|
return c.deals.ListClient()
|
|
|
|
}
|
|
|
|
|
2019-08-01 17:12:41 +00:00
|
|
|
func (c *Client) Stop() {
|
|
|
|
close(c.stop)
|
|
|
|
<-c.stopped
|
|
|
|
}
|