Skip to content

Commit 54aeb8e

Browse files
lmarsfjl
authored andcommitted
p2p/simulations: various stability fixes (#15198)
p2p/simulations: introduce dialBan - Refactor simulations/network connection getters to support avoiding simultaneous dials between two peers If two peers dial simultaneously, the connection will be dropped to help avoid that, we essentially lock the connection object with a timestamp which serves as a ban on dialing for a period of time (dialBanTimeout). - The connection getter InitConn can be wrapped and passed to the nodes via adapters.NodeConfig#Reachable field and then used by the respective services when they initiate connections. This massively stablise the emerging connectivity when running with hundreds of nodes bootstrapping a network. p2p: add Inbound public method to p2p.Peer p2p/simulations: Add server id to logs to support debugging in-memory network simulations when multiple peers are logging. p2p: SetupConn now returns error. The dialer checks the error and only calls resolve if the actual TCP dial fails.
1 parent 73067fd commit 54aeb8e

11 files changed

Lines changed: 171 additions & 69 deletions

File tree

‎node/config.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ type Config struct {
135135
// *WARNING* Only set this if the node is running in a trusted network, exposing
136136
// private APIs to untrusted users is a major security risk.
137137
WSExposeAll bool `toml:",omitempty"`
138+
139+
// Logger is a custom logger to use with the p2p.Server.
140+
Logger log.Logger
138141
}
139142

140143
// IPCEndpoint resolves an IPC endpoint based on a configured value, taking into

‎node/node.go‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Node struct {
6969

7070
stop chan struct{} // Channel to wait for termination notifications
7171
lock sync.RWMutex
72+
73+
log log.Logger
7274
}
7375

7476
// New creates a new P2P node, ready for protocol registration.
@@ -101,6 +103,9 @@ func New(conf *Config) (*Node, error) {
101103
if err != nil {
102104
return nil, err
103105
}
106+
if conf.Logger == nil {
107+
conf.Logger = log.New()
108+
}
104109
// Note: any interaction with Config that would create/touch files
105110
// in the data directory or instance directory is delayed until Start.
106111
return &Node{
@@ -112,6 +117,7 @@ func New(conf *Config) (*Node, error) {
112117
httpEndpoint: conf.HTTPEndpoint(),
113118
wsEndpoint: conf.WSEndpoint(),
114119
eventmux: new(event.TypeMux),
120+
log: conf.Logger,
115121
}, nil
116122
}
117123

@@ -146,6 +152,7 @@ func (n *Node) Start() error {
146152
n.serverConfig = n.config.P2P
147153
n.serverConfig.PrivateKey = n.config.NodeKey()
148154
n.serverConfig.Name = n.config.NodeName()
155+
n.serverConfig.Logger = n.log
149156
if n.serverConfig.StaticNodes == nil {
150157
n.serverConfig.StaticNodes = n.config.StaticNodes()
151158
}
@@ -156,7 +163,7 @@ func (n *Node) Start() error {
156163
n.serverConfig.NodeDatabase = n.config.NodeDB()
157164
}
158165
running := &p2p.Server{Config: n.serverConfig}
159-
log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name)
166+
n.log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name)
160167

161168
// Otherwise copy and specialize the P2P configuration
162169
services := make(map[reflect.Type]Service)
@@ -280,7 +287,7 @@ func (n *Node) startInProc(apis []rpc.API) error {
280287
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
281288
return err
282289
}
283-
log.Debug(fmt.Sprintf("InProc registered %T under '%s'", api.Service, api.Namespace))
290+
n.log.Debug(fmt.Sprintf("InProc registered %T under '%s'", api.Service, api.Namespace))
284291
}
285292
n.inprocHandler = handler
286293
return nil
@@ -306,7 +313,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
306313
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
307314
return err
308315
}
309-
log.Debug(fmt.Sprintf("IPC registered %T under '%s'", api.Service, api.Namespace))
316+
n.log.Debug(fmt.Sprintf("IPC registered %T under '%s'", api.Service, api.Namespace))
310317
}
311318
// All APIs registered, start the IPC listener
312319
var (
@@ -317,7 +324,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
317324
return err
318325
}
319326
go func() {
320-
log.Info(fmt.Sprintf("IPC endpoint opened: %s", n.ipcEndpoint))
327+
n.log.Info(fmt.Sprintf("IPC endpoint opened: %s", n.ipcEndpoint))
321328

322329
for {
323330
conn, err := listener.Accept()
@@ -330,7 +337,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
330337
return
331338
}
332339
// Not closed, just some error; report and continue
333-
log.Error(fmt.Sprintf("IPC accept failed: %v", err))
340+
n.log.Error(fmt.Sprintf("IPC accept failed: %v", err))
334341
continue
335342
}
336343
go handler.ServeCodec(rpc.NewJSONCodec(conn), rpc.OptionMethodInvocation|rpc.OptionSubscriptions)
@@ -349,7 +356,7 @@ func (n *Node) stopIPC() {
349356
n.ipcListener.Close()
350357
n.ipcListener = nil
351358

352-
log.Info(fmt.Sprintf("IPC endpoint closed: %s", n.ipcEndpoint))
359+
n.log.Info(fmt.Sprintf("IPC endpoint closed: %s", n.ipcEndpoint))
353360
}
354361
if n.ipcHandler != nil {
355362
n.ipcHandler.Stop()
@@ -375,7 +382,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
375382
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
376383
return err
377384
}
378-
log.Debug(fmt.Sprintf("HTTP registered %T under '%s'", api.Service, api.Namespace))
385+
n.log.Debug(fmt.Sprintf("HTTP registered %T under '%s'", api.Service, api.Namespace))
379386
}
380387
}
381388
// All APIs registered, start the HTTP listener
@@ -387,7 +394,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
387394
return err
388395
}
389396
go rpc.NewHTTPServer(cors, handler).Serve(listener)
390-
log.Info(fmt.Sprintf("HTTP endpoint opened: http://%s", endpoint))
397+
n.log.Info(fmt.Sprintf("HTTP endpoint opened: http://%s", endpoint))
391398

392399
// All listeners booted successfully
393400
n.httpEndpoint = endpoint
@@ -403,7 +410,7 @@ func (n *Node) stopHTTP() {
403410
n.httpListener.Close()
404411
n.httpListener = nil
405412

406-
log.Info(fmt.Sprintf("HTTP endpoint closed: http://%s", n.httpEndpoint))
413+
n.log.Info(fmt.Sprintf("HTTP endpoint closed: http://%s", n.httpEndpoint))
407414
}
408415
if n.httpHandler != nil {
409416
n.httpHandler.Stop()
@@ -429,7 +436,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
429436
if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
430437
return err
431438
}
432-
log.Debug(fmt.Sprintf("WebSocket registered %T under '%s'", api.Service, api.Namespace))
439+
n.log.Debug(fmt.Sprintf("WebSocket registered %T under '%s'", api.Service, api.Namespace))
433440
}
434441
}
435442
// All APIs registered, start the HTTP listener
@@ -441,7 +448,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
441448
return err
442449
}
443450
go rpc.NewWSServer(wsOrigins, handler).Serve(listener)
444-
log.Info(fmt.Sprintf("WebSocket endpoint opened: ws://%s", listener.Addr()))
451+
n.log.Info(fmt.Sprintf("WebSocket endpoint opened: ws://%s", listener.Addr()))
445452

446453
// All listeners booted successfully
447454
n.wsEndpoint = endpoint
@@ -457,7 +464,7 @@ func (n *Node) stopWS() {
457464
n.wsListener.Close()
458465
n.wsListener = nil
459466

460-
log.Info(fmt.Sprintf("WebSocket endpoint closed: ws://%s", n.wsEndpoint))
467+
n.log.Info(fmt.Sprintf("WebSocket endpoint closed: ws://%s", n.wsEndpoint))
461468
}
462469
if n.wsHandler != nil {
463470
n.wsHandler.Stop()
@@ -496,7 +503,7 @@ func (n *Node) Stop() error {
496503
// Release instance directory lock.
497504
if n.instanceDirLock != nil {
498505
if err := n.instanceDirLock.Release(); err != nil {
499-
log.Error("Can't release datadir lock", "err", err)
506+
n.log.Error("Can't release datadir lock", "err", err)
500507
}
501508
n.instanceDirLock = nil
502509
}

‎p2p/dial.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,14 @@ func (t *dialTask) Do(srv *Server) {
291291
return
292292
}
293293
}
294-
success := t.dial(srv, t.dest)
295-
// Try resolving the ID of static nodes if dialing failed.
296-
if !success && t.flags&staticDialedConn != 0 {
297-
if t.resolve(srv) {
298-
t.dial(srv, t.dest)
294+
err := t.dial(srv, t.dest)
295+
if err != nil {
296+
log.Trace("Dial error", "task", t, "err", err)
297+
// Try resolving the ID of static nodes if dialing failed.
298+
if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 {
299+
if t.resolve(srv) {
300+
t.dial(srv, t.dest)
301+
}
299302
}
300303
}
301304
}
@@ -334,16 +337,18 @@ func (t *dialTask) resolve(srv *Server) bool {
334337
return true
335338
}
336339

340+
type dialError struct {
341+
error
342+
}
343+
337344
// dial performs the actual connection attempt.
338-
func (t *dialTask) dial(srv *Server, dest *discover.Node) bool {
345+
func (t *dialTask) dial(srv *Server, dest *discover.Node) error {
339346
fd, err := srv.Dialer.Dial(dest)
340347
if err != nil {
341-
log.Trace("Dial error", "task", t, "err", err)
342-
return false
348+
return &dialError{err}
343349
}
344350
mfd := newMeteredConn(fd, false)
345-
srv.SetupConn(mfd, t.flags, dest)
346-
return true
351+
return srv.SetupConn(mfd, t.flags, dest)
347352
}
348353

349354
func (t *dialTask) String() string {

‎p2p/peer.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func (p *Peer) String() string {
160160
return fmt.Sprintf("Peer %x %v", p.rw.id[:8], p.RemoteAddr())
161161
}
162162

163+
// Inbound returns true if the peer is an inbound connection
164+
func (p *Peer) Inbound() bool {
165+
return p.rw.flags&inboundConn != 0
166+
}
167+
163168
func newPeer(conn *conn, protocols []Protocol) *Peer {
164169
protomap := matchProtocols(protocols, conn.caps, conn)
165170
p := &Peer{

0 commit comments

Comments
 (0)