lotus/node/modules/lp2p/host.go

124 lines
2.9 KiB
Go
Raw Normal View History

2019-07-02 12:40:25 +00:00
package lp2p
import (
"context"
"fmt"
chore: migrate to boxo This migrates everything except the `go-car` librairy: https://github.com/ipfs/boxo/issues/218#issuecomment-1529922103 I didn't migrated everything in the previous release because all the boxo code wasn't compatible with the go-ipld-prime one due to a an in flight (/ aftermath) revert of github.com/ipfs/go-block-format. go-block-format has been unmigrated since slight bellow absolutely everything depends on it that would have required everything to be moved on boxo or everything to optin into using boxo which were all deal breakers for different groups. This worked fine because lotus's codebase could live hapely on the first multirepo setup however boost is now trying to use boxo's code with lotus's (still on multirepo) setup: https://filecoinproject.slack.com/archives/C03AQ3QAUG1/p1685022344779649 The alternative would be for boost to write shim types which just forward calls and return with the different interface definitions. Btw why is that an issue in the first place is because unlike what go's duck typing model suggest interfaces are not transparent https://github.com/golang/go/issues/58112, interfaces are strongly typed but they have implicit narrowing. The issue is if you return an interface from an interface Go does not have a function definition to insert the implicit conversion thus instead the type checker complains you are not returning the right type. Stubbing types were reverted https://github.com/ipfs/boxo/issues/218#issuecomment-1478650351 Last time I only migrated `go-bitswap` to `boxo/bitswap` because of the security issues and because we never had the interface return an interface problem (we had concrete wrappers where the implicit conversion took place).
2023-05-25 14:31:53 +00:00
nilrouting "github.com/ipfs/boxo/routing/none"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
record "github.com/libp2p/go-libp2p-record"
2022-08-25 18:20:41 +00:00
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
2022-11-24 16:32:27 +00:00
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"go.uber.org/fx"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/helpers"
)
type P2PHostIn struct {
fx.In
ID peer.ID
Peerstore peerstore.Peerstore
Opts [][]libp2p.Option `group:"libp2p"`
}
// ////////////////////////
type RawHost host.Host
func Peerstore() (peerstore.Peerstore, error) {
return pstoremem.NewPeerstore()
}
func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (RawHost, error) {
pkey := params.Peerstore.PrivKey(params.ID)
if pkey == nil {
return nil, fmt.Errorf("missing private key for node ID: %s", params.ID)
}
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()),
}
for _, o := range params.Opts {
opts = append(opts, o...)
}
2021-12-11 21:03:00 +00:00
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return h.Close()
},
})
return h, nil
}
2023-01-31 12:46:24 +00:00
func UserAgentOption(agent string) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
opts.Opts = append(opts.Opts, libp2p.UserAgent(agent))
return
}
}
func MockHost(mn mocknet.Mocknet, id peer.ID, ps peerstore.Peerstore) (RawHost, error) {
return mn.AddPeerWithPeerstore(id, ps)
}
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) {
ctx := helpers.LifecycleCtx(mctx, lc)
2020-05-14 01:10:49 +00:00
if bs {
mode = dht.ModeServer
}
opts := []dht.Option{dht.Mode(mode),
dht.Datastore(dstore),
dht.Validator(validator),
2020-05-12 17:38:04 +00:00
dht.ProtocolPrefix(build.DhtProtocolName(nn)),
dht.QueryFilter(dht.PublicQueryFilter),
dht.RoutingTableFilter(dht.PublicRoutingTableFilter),
dht.DisableProviders(),
2020-05-14 01:10:49 +00:00
dht.DisableValues()}
d, err := dht.New(
ctx, host, opts...,
)
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)
}