2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
nilrouting "github.com/ipfs/go-ipfs-routing/none"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2019-08-01 14:14:16 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
2019-07-01 09:09:48 +00:00
|
|
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
|
|
|
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
|
|
|
|
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2019-12-17 06:51:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2019-07-01 09:09:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type P2PHostIn struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
ID peer.ID
|
|
|
|
Peerstore peerstore.Peerstore
|
|
|
|
|
|
|
|
Opts [][]libp2p.Option `group:"libp2p"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ////////////////////////
|
|
|
|
|
|
|
|
type RawHost host.Host
|
|
|
|
|
|
|
|
func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (RawHost, error) {
|
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
|
|
|
pkey := params.Peerstore.PrivKey(params.ID)
|
|
|
|
if pkey == nil {
|
|
|
|
return nil, fmt.Errorf("missing private key for node ID: %s", params.ID.Pretty())
|
|
|
|
}
|
|
|
|
|
2019-12-18 19:33:13 +00:00
|
|
|
opts := []libp2p.Option{
|
|
|
|
libp2p.Identity(pkey),
|
|
|
|
libp2p.Peerstore(params.Peerstore),
|
|
|
|
libp2p.NoListenAddrs,
|
|
|
|
libp2p.Ping(true),
|
2020-06-01 18:43:51 +00:00
|
|
|
libp2p.UserAgent("lotus-" + build.UserVersion()),
|
2020-02-07 13:02:55 +00:00
|
|
|
}
|
2019-07-01 09:09:48 +00:00
|
|
|
for _, o := range params.Opts {
|
|
|
|
opts = append(opts, o...)
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := libp2p.New(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return h.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MockHost(mn mocknet.Mocknet, id peer.ID, ps peerstore.Peerstore) (RawHost, error) {
|
|
|
|
return mn.AddPeerWithPeerstore(id, ps)
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:52:46 +00:00
|
|
|
func DHTRouting(mode dht.ModeOpt) interface{} {
|
2020-05-14 01:10:49 +00:00
|
|
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host RawHost, dstore dtypes.MetadataDS, validator record.Validator, nn dtypes.NetworkName, bs dtypes.Bootstrapper) (BaseIpfsRouting, error) {
|
2019-07-01 09:09:48 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2020-05-14 01:10:49 +00:00
|
|
|
if bs {
|
|
|
|
mode = dht.ModeServer
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []dht.Option{dht.Mode(mode),
|
2020-04-21 22:52:46 +00:00
|
|
|
dht.Datastore(dstore),
|
|
|
|
dht.Validator(validator),
|
2020-05-12 17:38:04 +00:00
|
|
|
dht.ProtocolPrefix(build.DhtProtocolName(nn)),
|
2020-04-21 22:52:46 +00:00
|
|
|
dht.QueryFilter(dht.PublicQueryFilter),
|
|
|
|
dht.RoutingTableFilter(dht.PublicRoutingTableFilter),
|
2020-04-21 23:59:24 +00:00
|
|
|
dht.DisableProviders(),
|
2020-05-14 01:10:49 +00:00
|
|
|
dht.DisableValues()}
|
|
|
|
d, err := dht.New(
|
|
|
|
ctx, host, opts...,
|
2019-07-01 09:09:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return d.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NilRouting(mctx helpers.MetricsCtx) (BaseIpfsRouting, error) {
|
|
|
|
return nilrouting.ConstructNilRouting(mctx, nil, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RoutedHost(rh RawHost, r BaseIpfsRouting) host.Host {
|
|
|
|
return routedhost.Wrap(rh, r)
|
|
|
|
}
|