package dms3p2p import ( "context" "gitlab.dms3.io/p2p/go-p2p" host "gitlab.dms3.io/p2p/go-p2p-core/host" peer "gitlab.dms3.io/p2p/go-p2p-core/peer" peerstore "gitlab.dms3.io/p2p/go-p2p-core/peerstore" routing "gitlab.dms3.io/p2p/go-p2p-core/routing" record "gitlab.dms3.io/p2p/go-p2p-record" routedhost "gitlab.dms3.io/p2p/go-p2p/p2p/host/routed" "gitlab.dms3.io/dms3/go-dms3/core/node/helpers" "gitlab.dms3.io/dms3/go-dms3/repo" "go.uber.org/fx" ) type P2PHostIn struct { fx.In Repo repo.Repo Validator record.Validator HostOption HostOption RoutingOption RoutingOption ID peer.ID Peerstore peerstore.Peerstore Opts [][]p2p.Option `group:"p2p"` } type P2PHostOut struct { fx.Out Host host.Host Routing BaseDms3Routing } func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut, err error) { opts := []p2p.Option{p2p.NoListenAddrs} for _, o := range params.Opts { opts = append(opts, o...) } ctx := helpers.LifecycleCtx(mctx, lc) cfg, err := params.Repo.Config() if err != nil { return out, err } bootstrappers, err := cfg.BootstrapPeers() if err != nil { return out, err } opts = append(opts, p2p.Routing(func(h host.Host) (routing.PeerRouting, error) { r, err := params.RoutingOption( ctx, h, params.Repo.Datastore(), params.Validator, bootstrappers..., ) out.Routing = r return r, err })) out.Host, err = params.HostOption(ctx, params.ID, params.Peerstore, opts...) if err != nil { return P2PHostOut{}, err } // this code is necessary just for tests: mock network constructions // ignore the p2p constructor options that actually construct the routing! if out.Routing == nil { r, err := params.RoutingOption(ctx, out.Host, params.Repo.Datastore(), params.Validator, bootstrappers...) if err != nil { return P2PHostOut{}, err } out.Routing = r out.Host = routedhost.Wrap(out.Host, out.Routing) } lc.Append(fx.Hook{ OnStop: func(ctx context.Context) error { return out.Host.Close() }, }) return out, err }