2019-08-01 14:19:53 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-07 12:35:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/bufbstore"
|
2020-07-08 10:52:37 +00:00
|
|
|
"time"
|
2019-11-11 20:25:19 +00:00
|
|
|
|
2020-03-18 19:43:06 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2020-07-08 08:35:50 +00:00
|
|
|
dtimpl "github.com/filecoin-project/go-data-transfer/impl"
|
|
|
|
dtnet "github.com/filecoin-project/go-data-transfer/network"
|
|
|
|
dtgstransport "github.com/filecoin-project/go-data-transfer/transport/graphsync"
|
2020-01-10 17:13:12 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
2019-12-17 10:46:39 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket/discovery"
|
2020-01-10 17:13:12 +00:00
|
|
|
retrievalimpl "github.com/filecoin-project/go-fil-markets/retrievalmarket/impl"
|
2020-01-24 20:19:52 +00:00
|
|
|
rmnet "github.com/filecoin-project/go-fil-markets/retrievalmarket/network"
|
2019-12-17 10:46:39 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
2020-01-14 01:24:04 +00:00
|
|
|
storageimpl "github.com/filecoin-project/go-fil-markets/storagemarket/impl"
|
2020-03-18 18:57:22 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation"
|
2020-02-06 02:43:37 +00:00
|
|
|
smnet "github.com/filecoin-project/go-fil-markets/storagemarket/network"
|
2020-01-10 19:55:58 +00:00
|
|
|
"github.com/filecoin-project/go-statestore"
|
2020-04-28 17:13:46 +00:00
|
|
|
"github.com/filecoin-project/go-storedcounter"
|
2019-08-01 14:19:53 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
|
2020-01-14 01:24:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/markets/retrievaladapter"
|
2020-03-18 18:57:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
2019-12-10 04:19:59 +00:00
|
|
|
payapi "github.com/filecoin-project/lotus/node/impl/paych"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2020-07-06 23:39:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo/importmgr"
|
2020-02-13 00:28:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/paychmgr"
|
2019-08-01 14:19:53 +00:00
|
|
|
)
|
|
|
|
|
2020-07-06 23:39:30 +00:00
|
|
|
func ClientMultiDatastore(lc fx.Lifecycle, r repo.LockedRepo) (dtypes.ClientMultiDstore, error) {
|
|
|
|
mds, err := importmgr.NewMultiDstore(r, "/client")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return mds.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return mds, nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
func ClientImportMgr(mds dtypes.ClientMultiDstore, ds dtypes.MetadataDS) dtypes.ClientImportMgr {
|
|
|
|
return importmgr.New(mds, namespace.Wrap(ds, datastore.NewKey("/client")))
|
2019-08-01 14:19:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
func ClientBlockstore(imgr dtypes.ClientImportMgr) dtypes.ClientBlockstore {
|
2020-07-07 12:35:02 +00:00
|
|
|
// TODO: This isn't.. the best
|
|
|
|
// - If it's easy to pass per-retrieval blockstores with markets we don't need this
|
|
|
|
// - If it's not easy, we need to store this in a separate datastore on disk
|
|
|
|
defaultWrite := blockstore.NewBlockstore(datastore.NewMapDatastore())
|
|
|
|
|
|
|
|
return blockstore.NewIdStore(bufbstore.NewTieredBstore(imgr.Blockstore, defaultWrite))
|
2019-08-26 13:45:36 +00:00
|
|
|
}
|
2019-08-06 22:04:21 +00:00
|
|
|
|
2019-11-11 20:25:19 +00:00
|
|
|
// RegisterClientValidator is an initialization hook that registers the client
|
|
|
|
// request validator with the data transfer module as the validator for
|
|
|
|
// StorageDataTransferVoucher types
|
2020-05-20 22:46:44 +00:00
|
|
|
func RegisterClientValidator(crv dtypes.ClientRequestValidator, dtm dtypes.ClientDataTransfer) {
|
|
|
|
if err := dtm.RegisterVoucherType(&requestvalidation.StorageDataTransferVoucher{}, (*requestvalidation.UnifiedRequestValidator)(crv)); err != nil {
|
2019-12-05 05:02:14 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-11 20:25:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 00:25:12 +00:00
|
|
|
// NewClientGraphsyncDataTransfer returns a data transfer manager that just
|
2019-11-11 20:51:28 +00:00
|
|
|
// uses the clients's Client DAG service for transfers
|
2020-07-08 08:35:50 +00:00
|
|
|
func NewClientGraphsyncDataTransfer(lc fx.Lifecycle, h host.Host, gs dtypes.Graphsync, ds dtypes.MetadataDS) (dtypes.ClientDataTransfer, error) {
|
2020-05-05 01:31:56 +00:00
|
|
|
sc := storedcounter.New(ds, datastore.NewKey("/datatransfer/client/counter"))
|
2020-07-08 08:35:50 +00:00
|
|
|
net := dtnet.NewFromLibp2pHost(h)
|
|
|
|
|
|
|
|
dtDs := namespace.Wrap(ds, datastore.NewKey("/datatransfer/client/transfers"))
|
|
|
|
transport := dtgstransport.NewTransport(h.ID(), gs)
|
|
|
|
dt, err := dtimpl.NewDataTransfer(dtDs, net, transport, sc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
return dt.Start(ctx)
|
|
|
|
},
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
return dt.Stop()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return dt, nil
|
2019-11-11 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientDealStore creates a statestore for the client to store its deals
|
2020-03-18 18:57:22 +00:00
|
|
|
func NewClientDealStore(ds dtypes.ClientDatastore) dtypes.ClientDealStore {
|
|
|
|
return statestore.New(ds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientDatastore creates a datastore for the client to store its deals
|
|
|
|
func NewClientDatastore(ds dtypes.MetadataDS) dtypes.ClientDatastore {
|
|
|
|
return namespace.Wrap(ds, datastore.NewKey("/deals/client"))
|
2019-11-11 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:46:44 +00:00
|
|
|
func NewClientRequestValidator(deals dtypes.ClientDealStore) dtypes.ClientRequestValidator {
|
2020-05-19 23:24:59 +00:00
|
|
|
return requestvalidation.NewUnifiedRequestValidator(nil, deals)
|
2019-12-17 10:46:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:11:40 +00:00
|
|
|
func StorageClient(lc fx.Lifecycle, h host.Host, ibs dtypes.ClientBlockstore, r repo.LockedRepo, dataTransfer dtypes.ClientDataTransfer, discovery *discovery.Local, deals dtypes.ClientDatastore, scn storagemarket.StorageClientNode) (storagemarket.StorageClient, error) {
|
2020-02-06 02:43:37 +00:00
|
|
|
net := smnet.NewFromLibp2pHost(h)
|
2020-07-08 10:52:37 +00:00
|
|
|
c, err := storageimpl.NewClient(net, ibs, dataTransfer, discovery, deals, scn, storageimpl.DealPollingInterval(time.Second))
|
2020-05-08 21:11:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
2020-06-15 22:43:47 +00:00
|
|
|
return c.Start(ctx)
|
2020-05-08 21:11:40 +00:00
|
|
|
},
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
c.Stop()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return c, nil
|
2019-12-17 10:46:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
// RetrievalClient creates a new retrieval client attached to the client blockstore
|
2020-03-18 18:57:22 +00:00
|
|
|
func RetrievalClient(h host.Host, bs dtypes.ClientBlockstore, pmgr *paychmgr.Manager, payapi payapi.PaychAPI, resolver retrievalmarket.PeerResolver, ds dtypes.MetadataDS, chainapi full.ChainAPI) (retrievalmarket.RetrievalClient, error) {
|
|
|
|
adapter := retrievaladapter.NewRetrievalClientNode(pmgr, payapi, chainapi)
|
2020-01-24 20:19:52 +00:00
|
|
|
network := rmnet.NewFromLibp2pHost(h)
|
2020-02-27 21:45:31 +00:00
|
|
|
sc := storedcounter.New(ds, datastore.NewKey("/retr"))
|
2020-05-20 22:46:44 +00:00
|
|
|
return retrievalimpl.NewClient(network, bs, adapter, resolver, namespace.Wrap(ds, datastore.NewKey("/retrievals/client")), sc)
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|