@@ -6,13 +6,12 @@ import (
6
6
"errors"
7
7
"fmt"
8
8
"math/big"
9
+ prand "math/rand"
9
10
"net"
10
11
"sync"
11
12
"sync/atomic"
12
13
"time"
13
14
14
- prand "math/rand"
15
-
16
15
"github.com/btcsuite/btcd/btcec/v2"
17
16
"github.com/btcsuite/btcd/connmgr"
18
17
"github.com/btcsuite/btcd/wire"
@@ -139,7 +138,7 @@ type PeerConnManagerConfig struct {
139
138
// regardless of the flag's value
140
139
StaggerInitialReconnect bool
141
140
142
- //The timeout value for network connections.
141
+ // The timeout value for network connections.
143
142
ConnectionTimeout time.Duration
144
143
145
144
// Listeners is a list of addresses that's specified during the startup
@@ -253,12 +252,13 @@ func (p *PeerConnManager) Start() error {
253
252
RetryDuration : connRetryDuration ,
254
253
TargetOutbound : outboundNum ,
255
254
Dial : noiseDial (
256
- p .IdentityECDH , p .Config .Net , p .Config .ConnectionTimeout ,
255
+ p .IdentityECDH , p .Config .Net ,
256
+ p .Config .ConnectionTimeout ,
257
257
),
258
258
OnConnection : p .OutboundPeerConnected ,
259
259
})
260
260
if err != nil {
261
- return fmt .Errorf ("Creating conn manager failed: %w" , err )
261
+ return fmt .Errorf ("creating conn manager failed: %w" , err )
262
262
}
263
263
p .connMgr = cmgr
264
264
@@ -267,8 +267,10 @@ func (p *PeerConnManager) Start() error {
267
267
268
268
p .connMgr .Start ()
269
269
270
- if err := p .PeerNotifier .Start (); err != nil {
270
+ err = p .PeerNotifier .Start ()
271
+ if err != nil {
271
272
err = fmt .Errorf ("PeerNotifier failed to start %w" , err )
273
+ return
272
274
}
273
275
274
276
atomic .StoreInt32 (& p .active , 1 )
@@ -302,8 +304,10 @@ func (p *PeerConnManager) Stop() error {
302
304
close (p .quit )
303
305
p .wg .Wait ()
304
306
305
- if err := p .PeerNotifier .Stop (); err != nil {
307
+ err = p .PeerNotifier .Stop ()
308
+ if err != nil {
306
309
err = fmt .Errorf ("PeerNotifier failed to stop: %w" , err )
310
+ return
307
311
}
308
312
})
309
313
@@ -351,6 +355,8 @@ func NewPeerConnManager(nodeKey keychain.SingleKeyECDH,
351
355
352
356
// UpdatePersistentPeerAddrs subscribes to topology changes and stores
353
357
// advertised addresses for any NodeAnnouncements from our persisted peers.
358
+ //
359
+ //nolint:lll
354
360
func (p * PeerConnManager ) UpdatePersistentPeerAddrs () error {
355
361
graphSub , err := p .Config .SubscribeTopology ()
356
362
if err != nil {
@@ -524,19 +530,19 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
524
530
//
525
531
// TODO(roasbeef): add reverse policy too?
526
532
527
- if epochAttempts > 0 &&
528
- atomic .LoadUint32 (& epochErrors ) >= epochAttempts {
529
-
533
+ numErr := atomic .LoadUint32 (& epochErrors )
534
+ if epochAttempts > 0 && numErr >= epochAttempts {
530
535
sampleTicker .Stop ()
531
536
532
537
backOff *= 2
533
538
if backOff > bootstrapBackOffCeiling {
534
539
backOff = bootstrapBackOffCeiling
535
540
}
536
541
537
- connLog .Debugf ("Backing off peer bootstrapper to " +
538
- "%v" , backOff )
542
+ connLog .Debugf ("Backing off peer " +
543
+ "bootstrapper to %v" , backOff )
539
544
sampleTicker = time .NewTicker (backOff )
545
+
540
546
continue
541
547
}
542
548
@@ -589,7 +595,9 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
589
595
connLog .Errorf ("Unable to " +
590
596
"connect to %v: %v" ,
591
597
a , err )
592
- atomic .AddUint32 (& epochErrors , 1 )
598
+ atomic .AddUint32 (
599
+ & epochErrors , 1 ,
600
+ )
593
601
case <- p .quit :
594
602
}
595
603
}(addr )
@@ -603,8 +611,8 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
603
611
// initialPeerBootstrap attempts to continuously connect to peers on startup
604
612
// until the target number of peers has been reached. This ensures that nodes
605
613
// receive an up to date network view as soon as possible.
606
- func (p * PeerConnManager ) initialPeerBootstrap (ignore map [autopilot. NodeID ] struct {},
607
- numTargetPeers uint32 ,
614
+ func (p * PeerConnManager ) initialPeerBootstrap (
615
+ ignore map [autopilot. NodeID ] struct {}, numTargetPeers uint32 ,
608
616
bootstrappers []discovery.NetworkPeerBootstrapper ) {
609
617
610
618
connLog .Debugf ("Init bootstrap with targetPeers=%v, bootstrappers=%v, " +
@@ -761,7 +769,8 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
761
769
762
770
// If the remote party has announced the channel to us, but we
763
771
// haven't yet, then we won't have a policy. However, we don't
764
- // need this to connect to the peer, so we'll log it and move on.
772
+ // need this to connect to the peer, so we'll log it and move
773
+ // on.
765
774
if policy == nil {
766
775
connLog .Warnf ("No channel policy found for " +
767
776
"ChannelPoint(%v): " , chanInfo .ChannelPoint )
@@ -809,7 +818,8 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
809
818
// addresses if Tor outbound support is enabled.
810
819
case * tor.OnionAddr :
811
820
if p .Config .TorActive {
812
- addrSet [lnAddress .String ()] = lnAddress
821
+ addrSet [lnAddress .String ()] =
822
+ lnAddress
813
823
}
814
824
}
815
825
}
@@ -830,6 +840,7 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
830
840
}
831
841
832
842
nodeAddrsMap [pubStr ] = n
843
+
833
844
return nil
834
845
})
835
846
if err != nil && ! errors .Is (err , channeldb .ErrGraphNoEdgesFound ) {
@@ -969,6 +980,9 @@ func (p *PeerConnManager) BroadcastMessage(skips map[route.Vertex]struct{},
969
980
defer p .wg .Done ()
970
981
defer wg .Done ()
971
982
983
+ //nolint: errcheck
984
+ //
985
+ // TODO(yy): check the error returned?
972
986
peer .SendMessageLazy (false , msgs ... )
973
987
}(sPeer )
974
988
}
@@ -1011,6 +1025,7 @@ func (p *PeerConnManager) NotifyWhenOnline(peerKey [33]byte,
1011
1025
p .peerConnectedListeners [pubStr ], peerChan ,
1012
1026
)
1013
1027
p .mu .Unlock ()
1028
+
1014
1029
return
1015
1030
}
1016
1031
@@ -1095,7 +1110,9 @@ func (p *PeerConnManager) FindPeerByPubStr(
1095
1110
1096
1111
// findPeerByPubStr is an internal method that retrieves the specified peer from
1097
1112
// the server's internal state using.
1098
- func (p * PeerConnManager ) findPeerByPubStr (pubStr string ) (* peer.Brontide , error ) {
1113
+ func (p * PeerConnManager ) findPeerByPubStr (
1114
+ pubStr string ) (* peer.Brontide , error ) {
1115
+
1099
1116
peer , ok := p .peersByPub [pubStr ]
1100
1117
if ! ok {
1101
1118
return nil , ErrPeerNotConnected
@@ -1198,6 +1215,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
1198
1215
p , conn .LocalAddr (), conn .RemoteAddr ())
1199
1216
1200
1217
conn .Close ()
1218
+
1201
1219
return
1202
1220
}
1203
1221
@@ -1208,6 +1226,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
1208
1226
connLog .Debugf ("Ignoring connection from %v, peer %v already " +
1209
1227
"scheduled" , conn .RemoteAddr (), p )
1210
1228
conn .Close ()
1229
+
1211
1230
return
1212
1231
}
1213
1232
@@ -1240,6 +1259,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
1240
1259
"peer %v, but already have outbound " +
1241
1260
"connection, dropping conn" , connectedPeer )
1242
1261
conn .Close ()
1262
+
1243
1263
return
1244
1264
}
1245
1265
@@ -1297,12 +1317,14 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
1297
1317
p .connMgr .Remove (connReq .ID ())
1298
1318
}
1299
1319
conn .Close ()
1320
+
1300
1321
return
1301
1322
}
1302
1323
if _ , ok := p .persistentConnReqs [pubStr ]; ! ok && connReq != nil {
1303
1324
connLog .Debugf ("Ignoring canceled outbound connection" )
1304
1325
p .connMgr .Remove (connReq .ID ())
1305
1326
conn .Close ()
1327
+
1306
1328
return
1307
1329
}
1308
1330
@@ -1315,8 +1337,8 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
1315
1337
if connReq != nil {
1316
1338
p .connMgr .Remove (connReq .ID ())
1317
1339
}
1318
-
1319
1340
conn .Close ()
1341
+
1320
1342
return
1321
1343
}
1322
1344
@@ -1363,6 +1385,7 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
1363
1385
p .connMgr .Remove (connReq .ID ())
1364
1386
}
1365
1387
conn .Close ()
1388
+
1366
1389
return
1367
1390
}
1368
1391
@@ -1386,7 +1409,7 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
1386
1409
// UnassignedConnID is the default connection ID that a request can have before
1387
1410
// it actually is submitted to the connmgr.
1388
1411
// TODO(conner): move into connmgr package, or better, add connmgr method for
1389
- // generating atomic IDs
1412
+ // generating atomic IDs.
1390
1413
const UnassignedConnID uint64 = 0
1391
1414
1392
1415
// cancelConnReqs stops all persistent connection requests for a given pubkey.
@@ -1700,6 +1723,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
1700
1723
delete (p .scheduledPeerConnection , pubStr )
1701
1724
connCallback ()
1702
1725
}
1726
+
1703
1727
return
1704
1728
}
1705
1729
@@ -1747,6 +1771,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
1747
1771
connLog .Debugf ("Ignoring reconnection attempt " +
1748
1772
"to inbound peer %v without " +
1749
1773
"advertised address" , peer )
1774
+
1750
1775
return
1751
1776
1752
1777
// We came across an error retrieving an advertised
@@ -2018,7 +2043,8 @@ func (p *PeerConnManager) ConnectToPeer(addr *lnwire.NetAddress,
2018
2043
// zero.
2019
2044
p .persistentPeers [targetPub ] = true
2020
2045
if _ , ok := p .persistentPeersBackoff [targetPub ]; ! ok {
2021
- p .persistentPeersBackoff [targetPub ] = p .Config .MinBackoff
2046
+ p .persistentPeersBackoff [targetPub ] =
2047
+ p .Config .MinBackoff
2022
2048
}
2023
2049
p .persistentConnReqs [targetPub ] = append (
2024
2050
p .persistentConnReqs [targetPub ], connReq ,
@@ -2061,6 +2087,7 @@ func (p *PeerConnManager) connectToPeer(addr *lnwire.NetAddress,
2061
2087
case errChan <- err :
2062
2088
case <- p .quit :
2063
2089
}
2090
+
2064
2091
return
2065
2092
}
2066
2093
@@ -2180,7 +2207,7 @@ func computeNextBackoff(currBackoff, maxBackoff time.Duration) time.Duration {
2180
2207
2181
2208
var wiggle big.Int
2182
2209
wiggle .SetUint64 (uint64 (margin ))
2183
- if _ , err := rand .Int (rand .Reader , & wiggle ); err != nil {
2210
+ if _ , err := rand .Int (rand .Reader , & wiggle ); err != nil { //nolint:gosec
2184
2211
// Randomizing is not mission critical, so we'll just return the
2185
2212
// current backoff.
2186
2213
return nextBackoff
@@ -2222,12 +2249,13 @@ func (p *PeerConnManager) fetchNodeAdvertisedAddrs(
2222
2249
// noiseDial is a factory function which creates a connmgr compliant dialing
2223
2250
// function by returning a closure which includes the server's identity key.
2224
2251
func noiseDial (idKey keychain.SingleKeyECDH ,
2225
- netCfg tor.Net , timeout time.Duration ) func (net.Addr ) (net.Conn , error ) {
2252
+ netCfg tor.Net ,
2253
+ timeout time.Duration ) func (net.Addr ) (net.Conn , error ) {
2226
2254
2227
2255
return func (a net.Addr ) (net.Conn , error ) {
2228
2256
lnAddr , ok := a .(* lnwire.NetAddress )
2229
2257
if ! ok {
2230
- return nil , fmt .Errorf ("Unexpected network address " +
2258
+ return nil , fmt .Errorf ("unexpected network address " +
2231
2259
"type %v" , a )
2232
2260
}
2233
2261
0 commit comments