-
Notifications
You must be signed in to change notification settings - Fork 209
/
host.go
97 lines (86 loc) · 2.96 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
93
94
95
96
97
package p2p
import (
"context"
"fmt"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/security/noise"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
p2pmetrics "github.com/spacemeshos/go-spacemesh/p2p/metrics"
)
// DefaultConfig config.
func DefaultConfig() Config {
return Config{
Listen: "/ip4/0.0.0.0/tcp/7513",
Flood: true,
MinPeers: 6,
LowPeers: 40,
HighPeers: 100,
GracePeersShutdown: 30 * time.Second,
MaxMessageSize: 2 << 20,
}
}
// Config for all things related to p2p layer.
type Config struct {
DataDir string
LogLevel log.Level
GracePeersShutdown time.Duration
MaxMessageSize int
DisableNatPort bool `mapstructure:"disable-natport"`
Flood bool `mapstructure:"flood"`
Listen string `mapstructure:"listen"`
Bootnodes []string `mapstructure:"bootnodes"`
MinPeers int `mapstructure:"min-peers"`
LowPeers int `mapstructure:"low-peers"`
HighPeers int `mapstructure:"high-peers"`
AdvertiseAddress string `mapstructure:"advertise-address"`
}
// New initializes libp2p host configured for spacemesh.
func New(_ context.Context, logger log.Log, cfg Config, genesisID types.Hash20, opts ...Opt) (*Host, error) {
logger.Info("starting libp2p host with config %+v", cfg)
key, err := EnsureIdentity(cfg.DataDir)
if err != nil {
return nil, err
}
cm, err := connmgr.NewConnManager(cfg.LowPeers, cfg.HighPeers, connmgr.WithGracePeriod(cfg.GracePeersShutdown))
if err != nil {
return nil, fmt.Errorf("p2p create conn mgr: %w", err)
}
streamer := *yamux.DefaultTransport
ps, err := pstoremem.NewPeerstore()
if err != nil {
return nil, fmt.Errorf("can't create peer store: %w", err)
}
lopts := []libp2p.Option{
libp2p.Identity(key),
libp2p.ListenAddrStrings(cfg.Listen),
libp2p.UserAgent("go-spacemesh"),
libp2p.DisableRelay(),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Security(noise.ID, noise.New),
libp2p.Muxer("/yamux/1.0.0", &streamer),
libp2p.ConnectionManager(cm),
libp2p.Peerstore(ps),
libp2p.BandwidthReporter(p2pmetrics.NewBandwidthCollector()),
}
if !cfg.DisableNatPort {
lopts = append(lopts, libp2p.NATPortMap())
}
h, err := libp2p.New(lopts...)
if err != nil {
return nil, fmt.Errorf("failed to initialize libp2p host: %w", err)
}
h.Network().Notify(p2pmetrics.NewConnectionsMeeter())
logger.With().Info("local node identity",
log.String("identity", h.ID().String()),
)
// TODO(dshulyak) this is small mess. refactor to avoid this patching
// both New and Upgrade should use options.
opts = append(opts, WithConfig(cfg), WithLog(logger))
return Upgrade(h, genesisID, opts...)
}