Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 09347be

Browse files
authored
Merge pull request #758 from iotaledger/feat/autopeering-improvements
Autopeering improvements
2 parents 72a5a73 + ee4c2ae commit 09347be

File tree

27 files changed

+679
-499
lines changed

27 files changed

+679
-499
lines changed

components/dashboard/component.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/iotaledger/hive.go/ierrors"
1818
"github.com/iotaledger/iota-core/components/metricstracker"
1919
"github.com/iotaledger/iota-core/pkg/daemon"
20-
"github.com/iotaledger/iota-core/pkg/network/p2p"
20+
"github.com/iotaledger/iota-core/pkg/network"
2121
"github.com/iotaledger/iota-core/pkg/protocol"
2222
)
2323

@@ -49,7 +49,7 @@ type dependencies struct {
4949
Host host.Host
5050
Protocol *protocol.Protocol
5151
AppInfo *app.Info
52-
P2PManager *p2p.Manager
52+
NetworkManager network.Manager
5353
MetricsTracker *metricstracker.MetricsTracker
5454
}
5555

@@ -171,28 +171,20 @@ func currentNodeStatus() *nodestatus {
171171

172172
func neighborMetrics() []neighbormetric {
173173
var stats []neighbormetric
174-
if deps.P2PManager == nil {
174+
if deps.NetworkManager == nil {
175175
return stats
176176
}
177177

178178
// gossip plugin might be disabled
179-
neighbors := deps.P2PManager.AllNeighbors()
179+
neighbors := deps.NetworkManager.AllNeighbors()
180180
if neighbors == nil {
181181
return stats
182182
}
183183

184184
for _, neighbor := range neighbors {
185-
// origin := "Inbound"
186-
// for _, p := range deps.P2PManager.AllNeighbors() {
187-
// if neighbor.Peer == peer {
188-
// origin = "Outbound"
189-
// break
190-
// }
191-
// }
192-
193185
stats = append(stats, neighbormetric{
194-
ID: neighbor.Peer.ID.String(),
195-
Addresses: fmt.Sprintf("%s", neighbor.Peer.PeerAddresses),
186+
ID: neighbor.Peer().ID.String(),
187+
Addresses: fmt.Sprintf("%s", neighbor.Peer().PeerAddresses),
196188
PacketsRead: neighbor.PacketsRead(),
197189
PacketsWritten: neighbor.PacketsWritten(),
198190
})

components/p2p/component.go

+39-81
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package p2p
22

33
import (
44
"context"
5-
"fmt"
65
"path/filepath"
7-
"time"
86

97
"github.com/libp2p/go-libp2p"
108
"github.com/libp2p/go-libp2p/core/crypto"
@@ -21,11 +19,8 @@ import (
2119
"github.com/iotaledger/hive.go/ierrors"
2220
"github.com/iotaledger/hive.go/kvstore"
2321
hivedb "github.com/iotaledger/hive.go/kvstore/database"
24-
"github.com/iotaledger/hive.go/runtime/event"
2522
"github.com/iotaledger/iota-core/pkg/daemon"
2623
"github.com/iotaledger/iota-core/pkg/network"
27-
"github.com/iotaledger/iota-core/pkg/network/autopeering"
28-
"github.com/iotaledger/iota-core/pkg/network/manualpeering"
2924
"github.com/iotaledger/iota-core/pkg/network/p2p"
3025
"github.com/iotaledger/iota-core/pkg/protocol"
3126
)
@@ -51,9 +46,7 @@ type dependencies struct {
5146
dig.In
5247
PeeringConfig *configuration.Configuration `name:"peeringConfig"`
5348
PeeringConfigManager *p2p.ConfigManager
54-
ManualPeeringMgr *manualpeering.Manager
55-
AutoPeeringMgr *autopeering.Manager
56-
P2PManager *p2p.Manager
49+
NetworkManager network.Manager
5750
PeerDB *network.DB
5851
Protocol *protocol.Protocol
5952
PeerDBKVSTore kvstore.KVStore `name:"peerDBKVStore"`
@@ -79,49 +72,6 @@ func initConfigParams(c *dig.Container) error {
7972
}
8073

8174
func provide(c *dig.Container) error {
82-
type manualPeeringDeps struct {
83-
dig.In
84-
85-
P2PManager *p2p.Manager
86-
}
87-
88-
if err := c.Provide(func(deps manualPeeringDeps) *manualpeering.Manager {
89-
return manualpeering.NewManager(deps.P2PManager, Component.WorkerPool, Component.Logger)
90-
}); err != nil {
91-
return err
92-
}
93-
94-
type autoPeeringDeps struct {
95-
dig.In
96-
97-
Protocol *protocol.Protocol
98-
P2PManager *p2p.Manager
99-
Host host.Host
100-
PeerDB *network.DB
101-
}
102-
103-
if err := c.Provide(func(deps autoPeeringDeps) *autopeering.Manager {
104-
peersMultiAddresses, err := getMultiAddrsFromString(ParamsPeers.BootstrapPeers)
105-
if err != nil {
106-
Component.LogFatalf("Failed to parse bootstrapPeers param: %s", err)
107-
}
108-
109-
for _, multiAddr := range peersMultiAddresses {
110-
bootstrapPeer, err := network.NewPeerFromMultiAddr(multiAddr)
111-
if err != nil {
112-
Component.LogFatalf("Failed to parse bootstrap peer multiaddress: %s", err)
113-
}
114-
115-
if err := deps.PeerDB.UpdatePeer(bootstrapPeer); err != nil {
116-
Component.LogErrorf("Failed to update bootstrap peer: %s", err)
117-
}
118-
}
119-
120-
return autopeering.NewManager(deps.Protocol.LatestAPI().ProtocolParameters().NetworkName(), deps.P2PManager, deps.Host, deps.PeerDB, Component.Logger)
121-
}); err != nil {
122-
return err
123-
}
124-
12575
type peerDatabaseResult struct {
12676
dig.Out
12777

@@ -251,7 +201,7 @@ func provide(c *dig.Container) error {
251201
connManager, err := connmgr.NewConnManager(
252202
ParamsP2P.ConnectionManager.LowWatermark,
253203
ParamsP2P.ConnectionManager.HighWatermark,
254-
connmgr.WithGracePeriod(time.Minute),
204+
connmgr.WithEmergencyTrim(true),
255205
)
256206
if err != nil {
257207
Component.LogPanicf("unable to initialize connection manager: %s", err)
@@ -263,6 +213,7 @@ func provide(c *dig.Container) error {
263213
libp2p.Transport(tcp.NewTCPTransport),
264214
libp2p.ConnectionManager(connManager),
265215
libp2p.NATPortMap(),
216+
libp2p.DisableRelay(),
266217
// Define a custom address factory to inject external addresses to the DHT advertisements.
267218
libp2p.AddrsFactory(func() func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
268219
var externalMultiAddrs []multiaddr.Multiaddr
@@ -294,8 +245,31 @@ func provide(c *dig.Container) error {
294245
Component.LogPanic(err.Error())
295246
}
296247

297-
return c.Provide(func(host host.Host, peerDB *network.DB) *p2p.Manager {
298-
return p2p.NewManager(host, peerDB, Component.Logger)
248+
type p2pManagerDeps struct {
249+
dig.In
250+
Host host.Host
251+
PeerDB *network.DB
252+
}
253+
254+
return c.Provide(func(inDeps p2pManagerDeps) network.Manager {
255+
256+
peersMultiAddresses, err := getMultiAddrsFromString(ParamsPeers.BootstrapPeers)
257+
if err != nil {
258+
Component.LogFatalf("Failed to parse bootstrapPeers param: %s", err)
259+
}
260+
261+
for _, multiAddr := range peersMultiAddresses {
262+
bootstrapPeer, err := network.NewPeerFromMultiAddr(multiAddr)
263+
if err != nil {
264+
Component.LogFatalf("Failed to parse bootstrap peer multiaddress: %s", err)
265+
}
266+
267+
if err := inDeps.PeerDB.UpdatePeer(bootstrapPeer); err != nil {
268+
Component.LogErrorf("Failed to update bootstrap peer: %s", err)
269+
}
270+
}
271+
272+
return p2p.NewManager(inDeps.Host, inDeps.PeerDB, ParamsP2P.Autopeering.MaxPeers, Component.Logger)
299273
})
300274
}
301275

@@ -321,43 +295,27 @@ func configure() error {
321295
}
322296

323297
// log the p2p events
324-
deps.P2PManager.Events.NeighborAdded.Hook(func(neighbor *p2p.Neighbor) {
325-
Component.LogInfof("Neighbor added: %s / %s", neighbor.PeerAddresses, neighbor.ID)
326-
}, event.WithWorkerPool(Component.WorkerPool))
298+
deps.NetworkManager.OnNeighborAdded(func(neighbor network.Neighbor) {
299+
Component.LogInfof("neighbor added: %s / %s", neighbor.Peer().PeerAddresses, neighbor.Peer().ID)
300+
})
327301

328-
deps.P2PManager.Events.NeighborRemoved.Hook(func(neighbor *p2p.Neighbor) {
329-
Component.LogInfof("Neighbor removed: %s / %s", neighbor.PeerAddresses, neighbor.ID)
330-
}, event.WithWorkerPool(Component.WorkerPool))
302+
deps.NetworkManager.OnNeighborRemoved(func(neighbor network.Neighbor) {
303+
Component.LogInfof("neighbor removed: %s / %s", neighbor.Peer().PeerAddresses, neighbor.Peer().ID)
304+
})
331305

332306
return nil
333307
}
334308

335309
func run() error {
336310
if err := Component.Daemon().BackgroundWorker(Component.Name, func(ctx context.Context) {
337-
deps.ManualPeeringMgr.Start()
338-
if err := deps.AutoPeeringMgr.Start(ctx); err != nil {
339-
Component.LogFatalf("Failed to start autopeering manager: %s", err)
311+
defer deps.NetworkManager.Shutdown()
312+
313+
if err := deps.NetworkManager.Start(ctx, deps.Protocol.LatestAPI().ProtocolParameters().NetworkName()); err != nil {
314+
Component.LogFatalf("Failed to start p2p manager: %s", err)
340315
}
341316

342-
defer func() {
343-
if err := deps.ManualPeeringMgr.Stop(); err != nil {
344-
Component.LogErrorf("Failed to stop the manager", "err", err)
345-
}
346-
}()
347317
//nolint:contextcheck // false positive
348318
connectConfigKnownPeers()
349-
<-ctx.Done()
350-
}, daemon.PriorityManualPeering); err != nil {
351-
Component.LogFatalf("Failed to start as daemon: %s", err)
352-
}
353-
354-
if err := Component.Daemon().BackgroundWorker(fmt.Sprintf("%s-P2PManager", Component.Name), func(ctx context.Context) {
355-
defer deps.P2PManager.Shutdown()
356-
defer func() {
357-
if err := deps.P2PManager.P2PHost().Close(); err != nil {
358-
Component.LogWarnf("Failed to close libp2p host: %+v", err)
359-
}
360-
}()
361319

362320
<-ctx.Done()
363321
}, daemon.PriorityP2P); err != nil {
@@ -395,7 +353,7 @@ func connectConfigKnownPeers() {
395353
Component.LogPanicf("invalid peer address info: %s", err)
396354
}
397355

398-
if err := deps.ManualPeeringMgr.AddPeers(multiAddr); err != nil {
356+
if err := deps.NetworkManager.AddManualPeers(multiAddr); err != nil {
399357
Component.LogInfof("failed to add peer: %s, error: %s", multiAddr.String(), err)
400358
}
401359
}

components/p2p/params.go

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type ParametersP2P struct {
2828
// Defines the private key used to derive the node identity (optional).
2929
IdentityPrivateKey string `default:"" usage:"private key used to derive the node identity (optional)"`
3030

31+
Autopeering struct {
32+
MaxPeers int `default:"5" usage:"the max number of autopeer connections. Set to 0 to disable autopeering."`
33+
}
34+
3135
Database struct {
3236
// Defines the path to the p2p database.
3337
Path string `default:"testnet/p2pstore" usage:"the path to the p2p database"`

components/protocol/component.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/iotaledger/hive.go/runtime/workerpool"
1515
"github.com/iotaledger/iota-core/pkg/daemon"
1616
"github.com/iotaledger/iota-core/pkg/model"
17-
"github.com/iotaledger/iota-core/pkg/network/p2p"
17+
"github.com/iotaledger/iota-core/pkg/network"
1818
"github.com/iotaledger/iota-core/pkg/protocol"
1919
"github.com/iotaledger/iota-core/pkg/protocol/engine/attestation/slotattestation"
2020
"github.com/iotaledger/iota-core/pkg/protocol/engine/filter/presolidfilter/presolidblockfilter"
@@ -115,7 +115,7 @@ func provide(c *dig.Container) error {
115115

116116
DatabaseEngine hivedb.Engine `name:"databaseEngine"`
117117
ProtocolParameters []iotago.ProtocolParameters
118-
P2PManager *p2p.Manager
118+
NetworkManager network.Manager
119119
}
120120

121121
return c.Provide(func(deps protocolDeps) *protocol.Protocol {
@@ -132,7 +132,7 @@ func provide(c *dig.Container) error {
132132
return protocol.New(
133133
Component.Logger,
134134
workerpool.NewGroup("Protocol"),
135-
deps.P2PManager,
135+
deps.NetworkManager,
136136
protocol.WithBaseDirectory(ParamsDatabase.Path),
137137
protocol.WithStorageOptions(
138138
storage.WithDBEngine(deps.DatabaseEngine),

config_defaults.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
},
2929
"externalMultiAddresses": [],
3030
"identityPrivateKey": "",
31+
"autopeering": {
32+
"maxPeers": 5
33+
},
3134
"db": {
3235
"path": "testnet/p2pstore"
3336
}

documentation/configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Example:
9999
| [connectionManager](#p2p_connectionmanager) | Configuration for connectionManager | object | |
100100
| externalMultiAddresses | External reacheable multi addresses advertised to the network | array | |
101101
| identityPrivateKey | Private key used to derive the node identity (optional) | string | "" |
102+
| [autopeering](#p2p_autopeering) | Configuration for autopeering | object | |
102103
| [db](#p2p_db) | Configuration for db | object | |
103104

104105
### <a id="p2p_connectionmanager"></a> ConnectionManager
@@ -108,6 +109,12 @@ Example:
108109
| highWatermark | The threshold up on which connections count truncates to the lower watermark | int | 10 |
109110
| lowWatermark | The minimum connections count to hold after the high watermark was reached | int | 5 |
110111

112+
### <a id="p2p_autopeering"></a> Autopeering
113+
114+
| Name | Description | Type | Default value |
115+
| -------- | ------------------------------------------------------------------------ | ---- | ------------- |
116+
| maxPeers | The max number of autopeer connections. Set to 0 to disable autopeering. | int | 5 |
117+
111118
### <a id="p2p_db"></a> Db
112119

113120
| Name | Description | Type | Default value |
@@ -129,6 +136,9 @@ Example:
129136
},
130137
"externalMultiAddresses": [],
131138
"identityPrivateKey": "",
139+
"autopeering": {
140+
"maxPeers": 5
141+
},
132142
"db": {
133143
"path": "testnet/p2pstore"
134144
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ require (
144144
github.com/pmezard/go-difflib v1.0.0 // indirect
145145
github.com/pokt-network/smt v0.9.2 // indirect
146146
github.com/polydawn/refmt v0.89.0 // indirect
147-
github.com/prometheus/client_model v0.5.0 // indirect
147+
github.com/prometheus/client_model v0.6.0 // indirect
148148
github.com/prometheus/common v0.47.0 // indirect
149149
github.com/prometheus/procfs v0.12.0 // indirect
150150
github.com/quic-go/qpack v0.4.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
547547
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
548548
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
549549
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
550-
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
551-
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
550+
github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos=
551+
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
552552
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
553553
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
554554
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=

pkg/daemon/shutdown.go

-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ package daemon
55

66
const (
77
PriorityCloseDatabase = iota // no dependencies
8-
PriorityPeerDatabase
98
PriorityP2P
10-
PriorityManualPeering
119
PriorityProtocol
12-
PriorityBlockIssuer
13-
PriorityActivity // depends on BlockIssuer
1410
PriorityRestAPI
1511
PriorityINX
1612
PriorityDashboardMetrics

0 commit comments

Comments
 (0)