Skip to content

Commit

Permalink
peerconn: fix make lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Oct 13, 2023
1 parent 90a0849 commit 04200c1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
76 changes: 52 additions & 24 deletions peerconn/conn_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"errors"
"fmt"
"math/big"
prand "math/rand"
"net"
"sync"
"sync/atomic"
"time"

prand "math/rand"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/connmgr"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -139,7 +138,7 @@ type PeerConnManagerConfig struct {
// regardless of the flag's value
StaggerInitialReconnect bool

//The timeout value for network connections.
// The timeout value for network connections.
ConnectionTimeout time.Duration

// Listeners is a list of addresses that's specified during the startup
Expand Down Expand Up @@ -253,12 +252,13 @@ func (p *PeerConnManager) Start() error {
RetryDuration: connRetryDuration,
TargetOutbound: outboundNum,
Dial: noiseDial(
p.IdentityECDH, p.Config.Net, p.Config.ConnectionTimeout,
p.IdentityECDH, p.Config.Net,
p.Config.ConnectionTimeout,
),
OnConnection: p.OutboundPeerConnected,
})
if err != nil {
return fmt.Errorf("Creating conn manager failed: %w", err)
return fmt.Errorf("creating conn manager failed: %w", err)
}
p.connMgr = cmgr

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

p.connMgr.Start()

if err := p.PeerNotifier.Start(); err != nil {
err = p.PeerNotifier.Start()
if err != nil {
err = fmt.Errorf("PeerNotifier failed to start %w", err)
return
}

atomic.StoreInt32(&p.active, 1)
Expand Down Expand Up @@ -302,8 +304,10 @@ func (p *PeerConnManager) Stop() error {
close(p.quit)
p.wg.Wait()

if err := p.PeerNotifier.Stop(); err != nil {
err = p.PeerNotifier.Stop()
if err != nil {
err = fmt.Errorf("PeerNotifier failed to stop: %w", err)
return
}
})

Expand Down Expand Up @@ -351,6 +355,8 @@ func NewPeerConnManager(nodeKey keychain.SingleKeyECDH,

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

if epochAttempts > 0 &&
atomic.LoadUint32(&epochErrors) >= epochAttempts {

numErr := atomic.LoadUint32(&epochErrors)
if epochAttempts > 0 && numErr >= epochAttempts {
sampleTicker.Stop()

backOff *= 2
if backOff > bootstrapBackOffCeiling {
backOff = bootstrapBackOffCeiling
}

connLog.Debugf("Backing off peer bootstrapper to "+
"%v", backOff)
connLog.Debugf("Backing off peer "+
"bootstrapper to %v", backOff)
sampleTicker = time.NewTicker(backOff)

continue
}

Expand Down Expand Up @@ -589,7 +595,9 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
connLog.Errorf("Unable to "+
"connect to %v: %v",
a, err)
atomic.AddUint32(&epochErrors, 1)
atomic.AddUint32(
&epochErrors, 1,
)
case <-p.quit:
}
}(addr)
Expand All @@ -603,8 +611,8 @@ func (p *PeerConnManager) PeerBootstrapper(numTargetPeers uint32,
// initialPeerBootstrap attempts to continuously connect to peers on startup
// until the target number of peers has been reached. This ensures that nodes
// receive an up to date network view as soon as possible.
func (p *PeerConnManager) initialPeerBootstrap(ignore map[autopilot.NodeID]struct{},
numTargetPeers uint32,
func (p *PeerConnManager) initialPeerBootstrap(
ignore map[autopilot.NodeID]struct{}, numTargetPeers uint32,
bootstrappers []discovery.NetworkPeerBootstrapper) {

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

// If the remote party has announced the channel to us, but we
// haven't yet, then we won't have a policy. However, we don't
// need this to connect to the peer, so we'll log it and move on.
// need this to connect to the peer, so we'll log it and move
// on.
if policy == nil {
connLog.Warnf("No channel policy found for "+
"ChannelPoint(%v): ", chanInfo.ChannelPoint)
Expand Down Expand Up @@ -809,7 +818,8 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
// addresses if Tor outbound support is enabled.
case *tor.OnionAddr:
if p.Config.TorActive {
addrSet[lnAddress.String()] = lnAddress
addrSet[lnAddress.String()] =
lnAddress
}
}
}
Expand All @@ -830,6 +840,7 @@ func (p *PeerConnManager) EstablishPersistentConnections() error {
}

nodeAddrsMap[pubStr] = n

return nil
})
if err != nil && !errors.Is(err, channeldb.ErrGraphNoEdgesFound) {
Expand Down Expand Up @@ -969,6 +980,9 @@ func (p *PeerConnManager) BroadcastMessage(skips map[route.Vertex]struct{},
defer p.wg.Done()
defer wg.Done()

//nolint: errcheck
//
// TODO(yy): check the error returned?
peer.SendMessageLazy(false, msgs...)
}(sPeer)
}
Expand Down Expand Up @@ -1011,6 +1025,7 @@ func (p *PeerConnManager) NotifyWhenOnline(peerKey [33]byte,
p.peerConnectedListeners[pubStr], peerChan,
)
p.mu.Unlock()

return
}

Expand Down Expand Up @@ -1095,7 +1110,9 @@ func (p *PeerConnManager) FindPeerByPubStr(

// findPeerByPubStr is an internal method that retrieves the specified peer from
// the server's internal state using.
func (p *PeerConnManager) findPeerByPubStr(pubStr string) (*peer.Brontide, error) {
func (p *PeerConnManager) findPeerByPubStr(
pubStr string) (*peer.Brontide, error) {

peer, ok := p.peersByPub[pubStr]
if !ok {
return nil, ErrPeerNotConnected
Expand Down Expand Up @@ -1198,6 +1215,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
p, conn.LocalAddr(), conn.RemoteAddr())

conn.Close()

return
}

Expand All @@ -1208,6 +1226,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
connLog.Debugf("Ignoring connection from %v, peer %v already "+
"scheduled", conn.RemoteAddr(), p)
conn.Close()

return
}

Expand Down Expand Up @@ -1240,6 +1259,7 @@ func (p *PeerConnManager) InboundPeerConnected(conn net.Conn) {
"peer %v, but already have outbound "+
"connection, dropping conn", connectedPeer)
conn.Close()

return
}

Expand Down Expand Up @@ -1297,12 +1317,14 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
p.connMgr.Remove(connReq.ID())
}
conn.Close()

return
}
if _, ok := p.persistentConnReqs[pubStr]; !ok && connReq != nil {
connLog.Debugf("Ignoring canceled outbound connection")
p.connMgr.Remove(connReq.ID())
conn.Close()

return
}

Expand All @@ -1315,8 +1337,8 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
if connReq != nil {
p.connMgr.Remove(connReq.ID())
}

conn.Close()

return
}

Expand Down Expand Up @@ -1363,6 +1385,7 @@ func (p *PeerConnManager) OutboundPeerConnected(connReq *connmgr.ConnReq,
p.connMgr.Remove(connReq.ID())
}
conn.Close()

return
}

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

// cancelConnReqs stops all persistent connection requests for a given pubkey.
Expand Down Expand Up @@ -1700,6 +1723,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
delete(p.scheduledPeerConnection, pubStr)
connCallback()
}

return
}

Expand Down Expand Up @@ -1747,6 +1771,7 @@ func (p *PeerConnManager) peerTerminationWatcher(peer *peer.Brontide,
connLog.Debugf("Ignoring reconnection attempt "+
"to inbound peer %v without "+
"advertised address", peer)

return

// We came across an error retrieving an advertised
Expand Down Expand Up @@ -2018,7 +2043,8 @@ func (p *PeerConnManager) ConnectToPeer(addr *lnwire.NetAddress,
// zero.
p.persistentPeers[targetPub] = true
if _, ok := p.persistentPeersBackoff[targetPub]; !ok {
p.persistentPeersBackoff[targetPub] = p.Config.MinBackoff
p.persistentPeersBackoff[targetPub] =
p.Config.MinBackoff
}
p.persistentConnReqs[targetPub] = append(
p.persistentConnReqs[targetPub], connReq,
Expand Down Expand Up @@ -2061,6 +2087,7 @@ func (p *PeerConnManager) connectToPeer(addr *lnwire.NetAddress,
case errChan <- err:
case <-p.quit:
}

return
}

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

var wiggle big.Int
wiggle.SetUint64(uint64(margin))
if _, err := rand.Int(rand.Reader, &wiggle); err != nil {
if _, err := rand.Int(rand.Reader, &wiggle); err != nil { //nolint:gosec
// Randomizing is not mission critical, so we'll just return the
// current backoff.
return nextBackoff
Expand Down Expand Up @@ -2222,12 +2249,13 @@ func (p *PeerConnManager) fetchNodeAdvertisedAddrs(
// noiseDial is a factory function which creates a connmgr compliant dialing
// function by returning a closure which includes the server's identity key.
func noiseDial(idKey keychain.SingleKeyECDH,
netCfg tor.Net, timeout time.Duration) func(net.Addr) (net.Conn, error) {
netCfg tor.Net,
timeout time.Duration) func(net.Addr) (net.Conn, error) {

return func(a net.Addr) (net.Conn, error) {
lnAddr, ok := a.(*lnwire.NetAddress)
if !ok {
return nil, fmt.Errorf("Unexpected network address "+
return nil, fmt.Errorf("unexpected network address "+
"type %v", a)
}

Expand Down
16 changes: 0 additions & 16 deletions peerconn/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) {
connLog = logger
}

// logClosure is used to provide a closure over expensive logging operations
// so they aren't performed when the logging level doesn't warrant it.
type logClosure func() string

// String invokes the underlying function and returns the result.
func (c logClosure) String() string {
return c()
}

// newLogClosure returns a new closure over a function that returns a string
// which itself provides a Stringer interface so that it can be used with the
// logging system.
func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2636,7 +2636,8 @@ func (s *server) createPartialPeerConfig() peer.Config {
//
// NOTE: This function is safe for concurrent access.
func (s *server) OpenChannel(
req *funding.InitFundingMsg) (chan *lnrpc.OpenStatusUpdate, chan error) {
req *funding.InitFundingMsg) (chan *lnrpc.OpenStatusUpdate,
chan error) {

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

0 comments on commit 04200c1

Please sign in to comment.