Skip to content

Commit 04200c1

Browse files
committed
peerconn: fix make lint issues
1 parent 90a0849 commit 04200c1

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

peerconn/conn_manager.go

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"errors"
77
"fmt"
88
"math/big"
9+
prand "math/rand"
910
"net"
1011
"sync"
1112
"sync/atomic"
1213
"time"
1314

14-
prand "math/rand"
15-
1615
"github.com/btcsuite/btcd/btcec/v2"
1716
"github.com/btcsuite/btcd/connmgr"
1817
"github.com/btcsuite/btcd/wire"
@@ -139,7 +138,7 @@ type PeerConnManagerConfig struct {
139138
// regardless of the flag's value
140139
StaggerInitialReconnect bool
141140

142-
//The timeout value for network connections.
141+
// The timeout value for network connections.
143142
ConnectionTimeout time.Duration
144143

145144
// Listeners is a list of addresses that's specified during the startup
@@ -253,12 +252,13 @@ func (p *PeerConnManager) Start() error {
253252
RetryDuration: connRetryDuration,
254253
TargetOutbound: outboundNum,
255254
Dial: noiseDial(
256-
p.IdentityECDH, p.Config.Net, p.Config.ConnectionTimeout,
255+
p.IdentityECDH, p.Config.Net,
256+
p.Config.ConnectionTimeout,
257257
),
258258
OnConnection: p.OutboundPeerConnected,
259259
})
260260
if err != nil {
261-
return fmt.Errorf("Creating conn manager failed: %w", err)
261+
return fmt.Errorf("creating conn manager failed: %w", err)
262262
}
263263
p.connMgr = cmgr
264264

@@ -267,8 +267,10 @@ func (p *PeerConnManager) Start() error {
267267

268268
p.connMgr.Start()
269269

270-
if err := p.PeerNotifier.Start(); err != nil {
270+
err = p.PeerNotifier.Start()
271+
if err != nil {
271272
err = fmt.Errorf("PeerNotifier failed to start %w", err)
273+
return
272274
}
273275

274276
atomic.StoreInt32(&p.active, 1)
@@ -302,8 +304,10 @@ func (p *PeerConnManager) Stop() error {
302304
close(p.quit)
303305
p.wg.Wait()
304306

305-
if err := p.PeerNotifier.Stop(); err != nil {
307+
err = p.PeerNotifier.Stop()
308+
if err != nil {
306309
err = fmt.Errorf("PeerNotifier failed to stop: %w", err)
310+
return
307311
}
308312
})
309313

@@ -351,6 +355,8 @@ func NewPeerConnManager(nodeKey keychain.SingleKeyECDH,
351355

352356
// UpdatePersistentPeerAddrs subscribes to topology changes and stores
353357
// advertised addresses for any NodeAnnouncements from our persisted peers.
358+
//
359+
//nolint:lll
354360
func (p *PeerConnManager) UpdatePersistentPeerAddrs() error {
355361
graphSub, err := p.Config.SubscribeTopology()
356362
if err != nil {
@@ -524,19 +530,19 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
524530
//
525531
// TODO(roasbeef): add reverse policy too?
526532

527-
if epochAttempts > 0 &&
528-
atomic.LoadUint32(&epochErrors) >= epochAttempts {
529-
533+
numErr := atomic.LoadUint32(&epochErrors)
534+
if epochAttempts > 0 && numErr >= epochAttempts {
530535
sampleTicker.Stop()
531536

532537
backOff *= 2
533538
if backOff > bootstrapBackOffCeiling {
534539
backOff = bootstrapBackOffCeiling
535540
}
536541

537-
connLog.Debugf("Backing off peer bootstrapper to "+
538-
"%v", backOff)
542+
connLog.Debugf("Backing off peer "+
543+
"bootstrapper to %v", backOff)
539544
sampleTicker = time.NewTicker(backOff)
545+
540546
continue
541547
}
542548

@@ -589,7 +595,9 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
589595
connLog.Errorf("Unable to "+
590596
"connect to %v: %v",
591597
a, err)
592-
atomic.AddUint32(&epochErrors, 1)
598+
atomic.AddUint32(
599+
&epochErrors, 1,
600+
)
593601
case <-p.quit:
594602
}
595603
}(addr)
@@ -603,8 +611,8 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
603611
// initialPeerBootstrap attempts to continuously connect to peers on startup
604612
// until the target number of peers has been reached. This ensures that nodes
605613
// 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,
608616
bootstrappers []discovery.NetworkPeerBootstrapper) {
609617

610618
connLog.Debugf("Init bootstrap with targetPeers=%v, bootstrappers=%v, "+
@@ -761,7 +769,8 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
761769

762770
// If the remote party has announced the channel to us, but we
763771
// 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.
765774
if policy == nil {
766775
connLog.Warnf("No channel policy found for "+
767776
"ChannelPoint(%v): ", chanInfo.ChannelPoint)
@@ -809,7 +818,8 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
809818
// addresses if Tor outbound support is enabled.
810819
case *tor.OnionAddr:
811820
if p.Config.TorActive {
812-
addrSet[lnAddress.String()] = lnAddress
821+
addrSet[lnAddress.String()] =
822+
lnAddress
813823
}
814824
}
815825
}
@@ -830,6 +840,7 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
830840
}
831841

832842
nodeAddrsMap[pubStr] = n
843+
833844
return nil
834845
})
835846
if err != nil && !errors.Is(err, channeldb.ErrGraphNoEdgesFound) {
@@ -969,6 +980,9 @@ func (p *PeerConnManager) BroadcastMessage(skips map[route.Vertex]struct{},
969980
defer p.wg.Done()
970981
defer wg.Done()
971982

983+
//nolint: errcheck
984+
//
985+
// TODO(yy): check the error returned?
972986
peer.SendMessageLazy(false, msgs...)
973987
}(sPeer)
974988
}
@@ -1011,6 +1025,7 @@ func (p *PeerConnManager) NotifyWhenOnline(peerKey [33]byte,
10111025
p.peerConnectedListeners[pubStr], peerChan,
10121026
)
10131027
p.mu.Unlock()
1028+
10141029
return
10151030
}
10161031

@@ -1095,7 +1110,9 @@ func (p *PeerConnManager) FindPeerByPubStr(
10951110

10961111
// findPeerByPubStr is an internal method that retrieves the specified peer from
10971112
// 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+
10991116
peer, ok := p.peersByPub[pubStr]
11001117
if !ok {
11011118
return nil, ErrPeerNotConnected
@@ -1198,6 +1215,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
11981215
p, conn.LocalAddr(), conn.RemoteAddr())
11991216

12001217
conn.Close()
1218+
12011219
return
12021220
}
12031221

@@ -1208,6 +1226,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
12081226
connLog.Debugf("Ignoring connection from %v, peer %v already "+
12091227
"scheduled", conn.RemoteAddr(), p)
12101228
conn.Close()
1229+
12111230
return
12121231
}
12131232

@@ -1240,6 +1259,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
12401259
"peer %v, but already have outbound "+
12411260
"connection, dropping conn", connectedPeer)
12421261
conn.Close()
1262+
12431263
return
12441264
}
12451265

@@ -1297,12 +1317,14 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
12971317
p.connMgr.Remove(connReq.ID())
12981318
}
12991319
conn.Close()
1320+
13001321
return
13011322
}
13021323
if _, ok := p.persistentConnReqs[pubStr]; !ok && connReq != nil {
13031324
connLog.Debugf("Ignoring canceled outbound connection")
13041325
p.connMgr.Remove(connReq.ID())
13051326
conn.Close()
1327+
13061328
return
13071329
}
13081330

@@ -1315,8 +1337,8 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
13151337
if connReq != nil {
13161338
p.connMgr.Remove(connReq.ID())
13171339
}
1318-
13191340
conn.Close()
1341+
13201342
return
13211343
}
13221344

@@ -1363,6 +1385,7 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
13631385
p.connMgr.Remove(connReq.ID())
13641386
}
13651387
conn.Close()
1388+
13661389
return
13671390
}
13681391

@@ -1386,7 +1409,7 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
13861409
// UnassignedConnID is the default connection ID that a request can have before
13871410
// it actually is submitted to the connmgr.
13881411
// TODO(conner): move into connmgr package, or better, add connmgr method for
1389-
// generating atomic IDs
1412+
// generating atomic IDs.
13901413
const UnassignedConnID uint64 = 0
13911414

13921415
// cancelConnReqs stops all persistent connection requests for a given pubkey.
@@ -1700,6 +1723,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
17001723
delete(p.scheduledPeerConnection, pubStr)
17011724
connCallback()
17021725
}
1726+
17031727
return
17041728
}
17051729

@@ -1747,6 +1771,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
17471771
connLog.Debugf("Ignoring reconnection attempt "+
17481772
"to inbound peer %v without "+
17491773
"advertised address", peer)
1774+
17501775
return
17511776

17521777
// We came across an error retrieving an advertised
@@ -2018,7 +2043,8 @@ func (p *PeerConnManager) ConnectToPeer(addr *lnwire.NetAddress,
20182043
// zero.
20192044
p.persistentPeers[targetPub] = true
20202045
if _, ok := p.persistentPeersBackoff[targetPub]; !ok {
2021-
p.persistentPeersBackoff[targetPub] = p.Config.MinBackoff
2046+
p.persistentPeersBackoff[targetPub] =
2047+
p.Config.MinBackoff
20222048
}
20232049
p.persistentConnReqs[targetPub] = append(
20242050
p.persistentConnReqs[targetPub], connReq,
@@ -2061,6 +2087,7 @@ func (p *PeerConnManager) connectToPeer(addr *lnwire.NetAddress,
20612087
case errChan <- err:
20622088
case <-p.quit:
20632089
}
2090+
20642091
return
20652092
}
20662093

@@ -2180,7 +2207,7 @@ func computeNextBackoff(currBackoff, maxBackoff time.Duration) time.Duration {
21802207

21812208
var wiggle big.Int
21822209
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
21842211
// Randomizing is not mission critical, so we'll just return the
21852212
// current backoff.
21862213
return nextBackoff
@@ -2222,12 +2249,13 @@ func (p *PeerConnManager) fetchNodeAdvertisedAddrs(
22222249
// noiseDial is a factory function which creates a connmgr compliant dialing
22232250
// function by returning a closure which includes the server's identity key.
22242251
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) {
22262254

22272255
return func(a net.Addr) (net.Conn, error) {
22282256
lnAddr, ok := a.(*lnwire.NetAddress)
22292257
if !ok {
2230-
return nil, fmt.Errorf("Unexpected network address "+
2258+
return nil, fmt.Errorf("unexpected network address "+
22312259
"type %v", a)
22322260
}
22332261

peerconn/log.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,3 @@ func DisableLog() {
2222
func UseLogger(logger btclog.Logger) {
2323
connLog = logger
2424
}
25-
26-
// logClosure is used to provide a closure over expensive logging operations
27-
// so they aren't performed when the logging level doesn't warrant it.
28-
type logClosure func() string
29-
30-
// String invokes the underlying function and returns the result.
31-
func (c logClosure) String() string {
32-
return c()
33-
}
34-
35-
// newLogClosure returns a new closure over a function that returns a string
36-
// which itself provides a Stringer interface so that it can be used with the
37-
// logging system.
38-
func newLogClosure(c func() string) logClosure {
39-
return logClosure(c)
40-
}

server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2636,7 +2636,8 @@ func (s *server) createPartialPeerConfig() peer.Config {
26362636
//
26372637
// NOTE: This function is safe for concurrent access.
26382638
func (s *server) OpenChannel(
2639-
req *funding.InitFundingMsg) (chan *lnrpc.OpenStatusUpdate, chan error) {
2639+
req *funding.InitFundingMsg) (chan *lnrpc.OpenStatusUpdate,
2640+
chan error) {
26402641

26412642
// The updateChan will have a buffer of 2, since we expect a ChanPending
26422643
// + a ChanOpen update, and we want to make sure the funding process is

0 commit comments

Comments
 (0)