-
Notifications
You must be signed in to change notification settings - Fork 901
/
host.go
92 lines (78 loc) · 2.61 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package p2p
import (
"context"
"fmt"
"github.com/libp2p/go-libp2p"
p2pconfig "github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/crypto"
hst "github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/routing"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)
// routedHost constructs a wrapped Host that may fallback to address discovery,
// if any top-level operation on the Host is provided with PeerID(Hash(PbK)) only.
func routedHost(base HostBase, r routing.PeerRouting) hst.Host {
return routedhost.Wrap(base, r)
}
// host returns constructor for Host.
func host(params hostParams) (HostBase, error) {
opts := []libp2p.Option{
libp2p.NoListenAddrs, // do not listen automatically
libp2p.AddrsFactory(params.AddrF),
libp2p.Identity(params.Key),
libp2p.Peerstore(params.PStore),
libp2p.ConnectionManager(params.ConnMngr),
libp2p.ConnectionGater(params.ConnGater),
libp2p.UserAgent(fmt.Sprintf("celestia-%s", params.Net)),
libp2p.NATPortMap(), // enables upnp
libp2p.DisableRelay(),
libp2p.BandwidthReporter(params.Bandwidth),
libp2p.ResourceManager(params.ResourceManager),
// to clearly define what defaults we rely upon
libp2p.DefaultSecurity,
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
}
if params.Registry != nil {
opts = append(opts, libp2p.PrometheusRegisterer(params.Registry))
} else {
opts = append(opts, libp2p.DisableMetrics())
}
// All node types except light (bridge, full) will enable NATService
if params.Tp != node.Light {
opts = append(opts, libp2p.EnableNATService())
}
h, err := libp2p.NewWithoutDefaults(opts...)
if err != nil {
return nil, err
}
params.Lc.Append(fx.Hook{OnStop: func(context.Context) error {
return h.Close()
}})
return h, nil
}
type HostBase hst.Host
type hostParams struct {
fx.In
Net Network
Lc fx.Lifecycle
ID peer.ID
Key crypto.PrivKey
AddrF p2pconfig.AddrsFactory
PStore peerstore.Peerstore
ConnMngr connmgr.ConnManager
ConnGater *conngater.BasicConnectionGater
Bandwidth *metrics.BandwidthCounter
ResourceManager network.ResourceManager
Registry prometheus.Registerer `optional:"true"`
Tp node.Type
}