From a1006d8372e876ab6eb4121a8738231e30fbaf7a Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 1 Jul 2025 07:43:26 +0200 Subject: [PATCH] accessman: fix structured logging formatting --- accessman.go | 3 +-- accessman_test.go | 11 ++++++----- server.go | 10 ++++++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/accessman.go b/accessman.go index 66ec71646..971132ff7 100644 --- a/accessman.go +++ b/accessman.go @@ -611,8 +611,7 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey, // removePeerAccess removes the peer's access from the maps. This should be // called when the peer has been disconnected. -func (a *accessMan) removePeerAccess(peerPubKey string) { - ctx := btclog.WithCtx(context.TODO(), "peer", peerPubKey) +func (a *accessMan) removePeerAccess(ctx context.Context, peerPubKey string) { acsmLog.DebugS(ctx, "Removing access:") a.banScoreMtx.Lock() diff --git a/accessman_test.go b/accessman_test.go index 30a315eec..06adab1cd 100644 --- a/accessman_test.go +++ b/accessman_test.go @@ -708,6 +708,7 @@ func TestAddPeerAccessOutbound(t *testing.T) { // accessman's internal state based on the peer's status. func TestRemovePeerAccess(t *testing.T) { t.Parallel() + ctx := context.Background() // Create a testing accessMan. a := &accessMan{ @@ -758,7 +759,7 @@ func TestRemovePeerAccess(t *testing.T) { // We now assert `removePeerAccess` behaves as expected. // // Remove peer1 should change nothing. - a.removePeerAccess(peer1) + a.removePeerAccess(ctx, peer1) // peer1 should be removed from peerScores but not peerChanInfo. _, found := a.peerScores[peer1] @@ -767,7 +768,7 @@ func TestRemovePeerAccess(t *testing.T) { require.True(t, found) // Remove peer2 should change nothing. - a.removePeerAccess(peer2) + a.removePeerAccess(ctx, peer2) // peer2 should be removed from peerScores but not peerChanInfo. _, found = a.peerScores[peer2] @@ -776,7 +777,7 @@ func TestRemovePeerAccess(t *testing.T) { require.True(t, found) // Remove peer3 should remove it from the maps. - a.removePeerAccess(peer3) + a.removePeerAccess(ctx, peer3) // peer3 should be removed from peerScores and peerChanInfo. _, found = a.peerScores[peer3] @@ -785,7 +786,7 @@ func TestRemovePeerAccess(t *testing.T) { require.False(t, found) // Remove peer4 should remove it from the maps. - a.removePeerAccess(peer4) + a.removePeerAccess(ctx, peer4) // peer4 should be removed from peerScores and NOT be found in // peerChanInfo. @@ -795,7 +796,7 @@ func TestRemovePeerAccess(t *testing.T) { require.False(t, found) // Remove peer5 should be NOOP. - a.removePeerAccess(peer5) + a.removePeerAccess(ctx, peer5) // peer5 should NOT be found. _, found = a.peerScores[peer5] diff --git a/server.go b/server.go index 7e35c33ec..71f10465f 100644 --- a/server.go +++ b/server.go @@ -24,6 +24,7 @@ import ( "github.com/btcsuite/btcd/connmgr" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" + "github.com/btcsuite/btclog/v2" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/aliasmgr" "github.com/lightningnetwork/lnd/autopilot" @@ -56,6 +57,7 @@ import ( "github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" + "github.com/lightningnetwork/lnd/lnutils" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chanfunding" @@ -4974,7 +4976,11 @@ func (s *server) removePeerUnsafe(p *peer.Brontide) { return } - srvrLog.Debugf("Removing peer %v", p) + ctx := btclog.WithCtx( + context.TODO(), lnutils.LogPubKey("peer", p.IdentityKey()), + ) + + srvrLog.DebugS(ctx, "Removing peer") // Exit early if we have already been instructed to shutdown, the peers // will be disconnected in the server shutdown process. @@ -5014,7 +5020,7 @@ func (s *server) removePeerUnsafe(p *peer.Brontide) { // Remove the peer's access permission from the access manager. peerPubStr := string(p.IdentityKey().SerializeCompressed()) - s.peerAccessMan.removePeerAccess(peerPubStr) + s.peerAccessMan.removePeerAccess(ctx, peerPubStr) // Copy the peer's error buffer across to the server if it has // any items in it so that we can restore peer errors across