2020-03-18 00:25:12 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
2020-11-24 22:32:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2020-03-18 00:25:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2020-11-24 22:32:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2020-04-07 02:17:02 +00:00
|
|
|
"github.com/ipfs/go-graphsync"
|
|
|
|
graphsyncimpl "github.com/ipfs/go-graphsync/impl"
|
2020-03-18 00:25:12 +00:00
|
|
|
gsnet "github.com/ipfs/go-graphsync/network"
|
|
|
|
"github.com/ipfs/go-graphsync/storeutil"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2020-08-20 04:49:10 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-03-18 00:25:12 +00:00
|
|
|
"go.uber.org/fx"
|
2020-11-24 22:32:30 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-18 00:25:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Graphsync creates a graphsync instance from the given loader and storer
|
2020-11-24 22:32:30 +00:00
|
|
|
func Graphsync(mctx helpers.MetricsCtx, lc fx.Lifecycle, r repo.LockedRepo, clientBs dtypes.ClientBlockstore, chainBs dtypes.ChainBlockstore, h host.Host) (dtypes.Graphsync, error) {
|
2020-03-18 00:25:12 +00:00
|
|
|
graphsyncNetwork := gsnet.NewFromLibp2pHost(h)
|
2020-04-07 02:17:02 +00:00
|
|
|
loader := storeutil.LoaderForBlockstore(clientBs)
|
|
|
|
storer := storeutil.StorerForBlockstore(clientBs)
|
2020-11-24 22:32:30 +00:00
|
|
|
raw, err := r.Config()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, ok := raw.(*config.FullNode)
|
|
|
|
if !ok {
|
|
|
|
return nil, xerrors.New("expected address of config.FullNode")
|
|
|
|
}
|
|
|
|
|
|
|
|
gs := graphsyncimpl.New(helpers.LifecycleCtx(mctx, lc), graphsyncNetwork, loader, storer, graphsyncimpl.RejectAllRequestsByDefault(), graphsyncimpl.MaxInProgressRequests(cfg.Client.SimultaneousTransfers))
|
2020-04-07 02:17:02 +00:00
|
|
|
chainLoader := storeutil.LoaderForBlockstore(chainBs)
|
|
|
|
chainStorer := storeutil.StorerForBlockstore(chainBs)
|
2020-11-24 22:32:30 +00:00
|
|
|
err = gs.RegisterPersistenceOption("chainstore", chainLoader, chainStorer)
|
2020-04-07 02:17:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gs.RegisterIncomingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
|
|
|
|
_, has := requestData.Extension("chainsync")
|
|
|
|
if has {
|
|
|
|
// TODO: we should confirm the selector is a reasonable one before we validate
|
|
|
|
// TODO: this code will get more complicated and should probably not live here eventually
|
|
|
|
hookActions.ValidateRequest()
|
|
|
|
hookActions.UsePersistenceOption("chainstore")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
gs.RegisterOutgoingRequestHook(func(p peer.ID, requestData graphsync.RequestData, hookActions graphsync.OutgoingRequestHookActions) {
|
|
|
|
_, has := requestData.Extension("chainsync")
|
|
|
|
if has {
|
|
|
|
hookActions.UsePersistenceOption("chainstore")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return gs, nil
|
2020-03-18 00:25:12 +00:00
|
|
|
}
|