Skip to content

Commit 538723e

Browse files
authored
Merge pull request #10016 from ellemouton/fixAccessManLogging
accessman: Fix pub key formatting in logs
2 parents d8a12a5 + 3cb61be commit 538723e

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

accessman.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,7 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
611611

612612
// removePeerAccess removes the peer's access from the maps. This should be
613613
// called when the peer has been disconnected.
614-
func (a *accessMan) removePeerAccess(peerPubKey string) {
615-
ctx := btclog.WithCtx(context.TODO(), "peer", peerPubKey)
614+
func (a *accessMan) removePeerAccess(ctx context.Context, peerPubKey string) {
616615
acsmLog.DebugS(ctx, "Removing access:")
617616

618617
a.banScoreMtx.Lock()

accessman_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ func TestAddPeerAccessOutbound(t *testing.T) {
708708
// accessman's internal state based on the peer's status.
709709
func TestRemovePeerAccess(t *testing.T) {
710710
t.Parallel()
711+
ctx := context.Background()
711712

712713
// Create a testing accessMan.
713714
a := &accessMan{
@@ -758,7 +759,7 @@ func TestRemovePeerAccess(t *testing.T) {
758759
// We now assert `removePeerAccess` behaves as expected.
759760
//
760761
// Remove peer1 should change nothing.
761-
a.removePeerAccess(peer1)
762+
a.removePeerAccess(ctx, peer1)
762763

763764
// peer1 should be removed from peerScores but not peerChanInfo.
764765
_, found := a.peerScores[peer1]
@@ -767,7 +768,7 @@ func TestRemovePeerAccess(t *testing.T) {
767768
require.True(t, found)
768769

769770
// Remove peer2 should change nothing.
770-
a.removePeerAccess(peer2)
771+
a.removePeerAccess(ctx, peer2)
771772

772773
// peer2 should be removed from peerScores but not peerChanInfo.
773774
_, found = a.peerScores[peer2]
@@ -776,7 +777,7 @@ func TestRemovePeerAccess(t *testing.T) {
776777
require.True(t, found)
777778

778779
// Remove peer3 should remove it from the maps.
779-
a.removePeerAccess(peer3)
780+
a.removePeerAccess(ctx, peer3)
780781

781782
// peer3 should be removed from peerScores and peerChanInfo.
782783
_, found = a.peerScores[peer3]
@@ -785,7 +786,7 @@ func TestRemovePeerAccess(t *testing.T) {
785786
require.False(t, found)
786787

787788
// Remove peer4 should remove it from the maps.
788-
a.removePeerAccess(peer4)
789+
a.removePeerAccess(ctx, peer4)
789790

790791
// peer4 should be removed from peerScores and NOT be found in
791792
// peerChanInfo.
@@ -795,7 +796,7 @@ func TestRemovePeerAccess(t *testing.T) {
795796
require.False(t, found)
796797

797798
// Remove peer5 should be NOOP.
798-
a.removePeerAccess(peer5)
799+
a.removePeerAccess(ctx, peer5)
799800

800801
// peer5 should NOT be found.
801802
_, found = a.peerScores[peer5]

server.go

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/btcsuite/btcd/connmgr"
2525
"github.com/btcsuite/btcd/txscript"
2626
"github.com/btcsuite/btcd/wire"
27+
"github.com/btcsuite/btclog/v2"
2728
sphinx "github.com/lightningnetwork/lightning-onion"
2829
"github.com/lightningnetwork/lnd/aliasmgr"
2930
"github.com/lightningnetwork/lnd/autopilot"
@@ -56,6 +57,7 @@ import (
5657
"github.com/lightningnetwork/lnd/lnpeer"
5758
"github.com/lightningnetwork/lnd/lnrpc"
5859
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
60+
"github.com/lightningnetwork/lnd/lnutils"
5961
"github.com/lightningnetwork/lnd/lnwallet"
6062
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
6163
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
@@ -4095,6 +4097,11 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
40954097
s.peerConnected(conn, nil, true)
40964098

40974099
case nil:
4100+
ctx := btclog.WithCtx(
4101+
context.TODO(),
4102+
lnutils.LogPubKey("peer", connectedPeer.IdentityKey()),
4103+
)
4104+
40984105
// We already have a connection with the incoming peer. If the
40994106
// connection we've already established should be kept and is
41004107
// not of the same type of the new connection (inbound), then
@@ -4104,24 +4111,24 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
41044111
if !connectedPeer.Inbound() &&
41054112
!shouldDropLocalConnection(localPub, nodePub) {
41064113

4107-
srvrLog.Warnf("Received inbound connection from "+
4108-
"peer %v, but already have outbound "+
4109-
"connection, dropping conn", connectedPeer)
4114+
srvrLog.WarnS(ctx, "Received inbound connection from "+
4115+
"peer, but already have outbound "+
4116+
"connection, dropping conn",
4117+
fmt.Errorf("already have outbound conn"))
41104118
conn.Close()
41114119
return
41124120
}
41134121

41144122
// Otherwise, if we should drop the connection, then we'll
41154123
// disconnect our already connected peer.
4116-
srvrLog.Debugf("Disconnecting stale connection to %v",
4117-
connectedPeer)
4124+
srvrLog.DebugS(ctx, "Disconnecting stale connection")
41184125

41194126
s.cancelConnReqs(pubStr, nil)
41204127

41214128
// Remove the current peer from the server's internal state and
41224129
// signal that the peer termination watcher does not need to
41234130
// execute for this peer.
4124-
s.removePeerUnsafe(connectedPeer)
4131+
s.removePeerUnsafe(ctx, connectedPeer)
41254132
s.ignorePeerTermination[connectedPeer] = struct{}{}
41264133
s.scheduledPeerConnection[pubStr] = func() {
41274134
s.peerConnected(conn, nil, true)
@@ -4210,6 +4217,11 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42104217
s.peerConnected(conn, connReq, false)
42114218

42124219
case nil:
4220+
ctx := btclog.WithCtx(
4221+
context.TODO(),
4222+
lnutils.LogPubKey("peer", connectedPeer.IdentityKey()),
4223+
)
4224+
42134225
// We already have a connection with the incoming peer. If the
42144226
// connection we've already established should be kept and is
42154227
// not of the same type of the new connection (outbound), then
@@ -4219,9 +4231,10 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42194231
if connectedPeer.Inbound() &&
42204232
shouldDropLocalConnection(localPub, nodePub) {
42214233

4222-
srvrLog.Warnf("Established outbound connection to "+
4223-
"peer %v, but already have inbound "+
4224-
"connection, dropping conn", connectedPeer)
4234+
srvrLog.WarnS(ctx, "Established outbound connection "+
4235+
"to peer, but already have inbound "+
4236+
"connection, dropping conn",
4237+
fmt.Errorf("already have inbound conn"))
42254238
if connReq != nil {
42264239
s.connMgr.Remove(connReq.ID())
42274240
}
@@ -4232,13 +4245,12 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
42324245
// Otherwise, _their_ connection should be dropped. So we'll
42334246
// disconnect the peer and send the now obsolete peer to the
42344247
// server for garbage collection.
4235-
srvrLog.Debugf("Disconnecting stale connection to %v",
4236-
connectedPeer)
4248+
srvrLog.DebugS(ctx, "Disconnecting stale connection")
42374249

42384250
// Remove the current peer from the server's internal state and
42394251
// signal that the peer termination watcher does not need to
42404252
// execute for this peer.
4241-
s.removePeerUnsafe(connectedPeer)
4253+
s.removePeerUnsafe(ctx, connectedPeer)
42424254
s.ignorePeerTermination[connectedPeer] = struct{}{}
42434255
s.scheduledPeerConnection[pubStr] = func() {
42444256
s.peerConnected(conn, connReq, false)
@@ -4686,16 +4698,18 @@ func (s *server) peerInitializer(p *peer.Brontide) {
46864698
func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
46874699
defer s.wg.Done()
46884700

4689-
ctx := context.TODO()
4701+
ctx := btclog.WithCtx(
4702+
context.TODO(), lnutils.LogPubKey("peer", p.IdentityKey()),
4703+
)
46904704

46914705
p.WaitForDisconnect(ready)
46924706

4693-
srvrLog.Debugf("Peer %v has been disconnected", p)
4707+
srvrLog.DebugS(ctx, "Peer has been disconnected")
46944708

46954709
// If the server is exiting then we can bail out early ourselves as all
46964710
// the other sub-systems will already be shutting down.
46974711
if s.Stopped() {
4698-
srvrLog.Debugf("Server quitting, exit early for peer %v", p)
4712+
srvrLog.DebugS(ctx, "Server quitting, exit early for peer")
46994713
return
47004714
}
47014715

@@ -4730,7 +4744,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47304744

47314745
// If there were any notification requests for when this peer
47324746
// disconnected, we can trigger them now.
4733-
srvrLog.Debugf("Notifying that peer %v is offline", p)
4747+
srvrLog.DebugS(ctx, "Notifying that peer is offline")
47344748
pubStr := string(pubKey.SerializeCompressed())
47354749
for _, offlineChan := range s.peerDisconnectedListeners[pubStr] {
47364750
close(offlineChan)
@@ -4760,7 +4774,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47604774

47614775
// First, cleanup any remaining state the server has regarding the peer
47624776
// in question.
4763-
s.removePeerUnsafe(p)
4777+
s.removePeerUnsafe(ctx, p)
47644778

47654779
// Next, check to see if this is a persistent peer or not.
47664780
if _, ok := s.persistentPeers[pubStr]; !ok {
@@ -4799,18 +4813,16 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
47994813
// the address used by our onion service to dial
48004814
// to lnd), so we don't have enough information
48014815
// to attempt a reconnect.
4802-
srvrLog.Debugf("Ignoring reconnection attempt "+
4803-
"to inbound peer %v without "+
4804-
"advertised address", p)
4816+
srvrLog.DebugS(ctx, "Ignoring reconnection attempt "+
4817+
"to inbound peer without advertised address")
48054818
return
48064819

48074820
// We came across an error retrieving an advertised
48084821
// address, log it, and fall back to the existing peer
48094822
// address.
48104823
default:
4811-
srvrLog.Errorf("Unable to retrieve advertised "+
4812-
"address for node %x: %v", p.PubKey(),
4813-
err)
4824+
srvrLog.ErrorS(ctx, "Unable to retrieve advertised "+
4825+
"address for peer", err)
48144826
}
48154827

48164828
// Make an easy lookup map so that we can check if an address
@@ -4852,9 +4864,9 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
48524864
// call can stall for arbitrarily long if we shutdown while an
48534865
// outbound connection attempt is being made.
48544866
go func() {
4855-
srvrLog.Debugf("Scheduling connection re-establishment to "+
4856-
"persistent peer %x in %s",
4857-
p.IdentityKey().SerializeCompressed(), backoff)
4867+
srvrLog.DebugS(ctx, "Scheduling connection "+
4868+
"re-establishment to persistent peer",
4869+
"reconnecting_in", backoff)
48584870

48594871
select {
48604872
case <-time.After(backoff):
@@ -4864,9 +4876,8 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
48644876
return
48654877
}
48664878

4867-
srvrLog.Debugf("Attempting to re-establish persistent "+
4868-
"connection to peer %x",
4869-
p.IdentityKey().SerializeCompressed())
4879+
srvrLog.DebugS(ctx, "Attempting to re-establish persistent "+
4880+
"connection")
48704881

48714882
s.connectToPersistentPeer(pubStr)
48724883
}()
@@ -4973,12 +4984,12 @@ func (s *server) connectToPersistentPeer(pubKeyStr string) {
49734984
// active peers.
49744985
//
49754986
// NOTE: Server mutex must be held when calling this function.
4976-
func (s *server) removePeerUnsafe(p *peer.Brontide) {
4987+
func (s *server) removePeerUnsafe(ctx context.Context, p *peer.Brontide) {
49774988
if p == nil {
49784989
return
49794990
}
49804991

4981-
srvrLog.Debugf("Removing peer %v", p)
4992+
srvrLog.DebugS(ctx, "Removing peer")
49824993

49834994
// Exit early if we have already been instructed to shutdown, the peers
49844995
// will be disconnected in the server shutdown process.
@@ -5018,7 +5029,7 @@ func (s *server) removePeerUnsafe(p *peer.Brontide) {
50185029

50195030
// Remove the peer's access permission from the access manager.
50205031
peerPubStr := string(p.IdentityKey().SerializeCompressed())
5021-
s.peerAccessMan.removePeerAccess(peerPubStr)
5032+
s.peerAccessMan.removePeerAccess(ctx, peerPubStr)
50225033

50235034
// Copy the peer's error buffer across to the server if it has
50245035
// any items in it so that we can restore peer errors across

0 commit comments

Comments
 (0)