@@ -24,6 +24,7 @@ import (
24
24
"github.com/btcsuite/btcd/connmgr"
25
25
"github.com/btcsuite/btcd/txscript"
26
26
"github.com/btcsuite/btcd/wire"
27
+ "github.com/btcsuite/btclog/v2"
27
28
sphinx "github.com/lightningnetwork/lightning-onion"
28
29
"github.com/lightningnetwork/lnd/aliasmgr"
29
30
"github.com/lightningnetwork/lnd/autopilot"
@@ -56,6 +57,7 @@ import (
56
57
"github.com/lightningnetwork/lnd/lnpeer"
57
58
"github.com/lightningnetwork/lnd/lnrpc"
58
59
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
60
+ "github.com/lightningnetwork/lnd/lnutils"
59
61
"github.com/lightningnetwork/lnd/lnwallet"
60
62
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
61
63
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
@@ -4095,6 +4097,11 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
4095
4097
s .peerConnected (conn , nil , true )
4096
4098
4097
4099
case nil :
4100
+ ctx := btclog .WithCtx (
4101
+ context .TODO (),
4102
+ lnutils .LogPubKey ("peer" , connectedPeer .IdentityKey ()),
4103
+ )
4104
+
4098
4105
// We already have a connection with the incoming peer. If the
4099
4106
// connection we've already established should be kept and is
4100
4107
// not of the same type of the new connection (inbound), then
@@ -4104,24 +4111,24 @@ func (s *server) InboundPeerConnected(conn net.Conn) {
4104
4111
if ! connectedPeer .Inbound () &&
4105
4112
! shouldDropLocalConnection (localPub , nodePub ) {
4106
4113
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" ))
4110
4118
conn .Close ()
4111
4119
return
4112
4120
}
4113
4121
4114
4122
// Otherwise, if we should drop the connection, then we'll
4115
4123
// disconnect our already connected peer.
4116
- srvrLog .Debugf ("Disconnecting stale connection to %v" ,
4117
- connectedPeer )
4124
+ srvrLog .DebugS (ctx , "Disconnecting stale connection" )
4118
4125
4119
4126
s .cancelConnReqs (pubStr , nil )
4120
4127
4121
4128
// Remove the current peer from the server's internal state and
4122
4129
// signal that the peer termination watcher does not need to
4123
4130
// execute for this peer.
4124
- s .removePeerUnsafe (connectedPeer )
4131
+ s .removePeerUnsafe (ctx , connectedPeer )
4125
4132
s .ignorePeerTermination [connectedPeer ] = struct {}{}
4126
4133
s .scheduledPeerConnection [pubStr ] = func () {
4127
4134
s .peerConnected (conn , nil , true )
@@ -4210,6 +4217,11 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
4210
4217
s .peerConnected (conn , connReq , false )
4211
4218
4212
4219
case nil :
4220
+ ctx := btclog .WithCtx (
4221
+ context .TODO (),
4222
+ lnutils .LogPubKey ("peer" , connectedPeer .IdentityKey ()),
4223
+ )
4224
+
4213
4225
// We already have a connection with the incoming peer. If the
4214
4226
// connection we've already established should be kept and is
4215
4227
// 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)
4219
4231
if connectedPeer .Inbound () &&
4220
4232
shouldDropLocalConnection (localPub , nodePub ) {
4221
4233
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" ))
4225
4238
if connReq != nil {
4226
4239
s .connMgr .Remove (connReq .ID ())
4227
4240
}
@@ -4232,13 +4245,12 @@ func (s *server) OutboundPeerConnected(connReq *connmgr.ConnReq, conn net.Conn)
4232
4245
// Otherwise, _their_ connection should be dropped. So we'll
4233
4246
// disconnect the peer and send the now obsolete peer to the
4234
4247
// server for garbage collection.
4235
- srvrLog .Debugf ("Disconnecting stale connection to %v" ,
4236
- connectedPeer )
4248
+ srvrLog .DebugS (ctx , "Disconnecting stale connection" )
4237
4249
4238
4250
// Remove the current peer from the server's internal state and
4239
4251
// signal that the peer termination watcher does not need to
4240
4252
// execute for this peer.
4241
- s .removePeerUnsafe (connectedPeer )
4253
+ s .removePeerUnsafe (ctx , connectedPeer )
4242
4254
s .ignorePeerTermination [connectedPeer ] = struct {}{}
4243
4255
s .scheduledPeerConnection [pubStr ] = func () {
4244
4256
s .peerConnected (conn , connReq , false )
@@ -4686,16 +4698,18 @@ func (s *server) peerInitializer(p *peer.Brontide) {
4686
4698
func (s * server ) peerTerminationWatcher (p * peer.Brontide , ready chan struct {}) {
4687
4699
defer s .wg .Done ()
4688
4700
4689
- ctx := context .TODO ()
4701
+ ctx := btclog .WithCtx (
4702
+ context .TODO (), lnutils .LogPubKey ("peer" , p .IdentityKey ()),
4703
+ )
4690
4704
4691
4705
p .WaitForDisconnect (ready )
4692
4706
4693
- srvrLog .Debugf ( "Peer %v has been disconnected" , p )
4707
+ srvrLog .DebugS ( ctx , "Peer has been disconnected" )
4694
4708
4695
4709
// If the server is exiting then we can bail out early ourselves as all
4696
4710
// the other sub-systems will already be shutting down.
4697
4711
if s .Stopped () {
4698
- srvrLog .Debugf ( "Server quitting, exit early for peer %v" , p )
4712
+ srvrLog .DebugS ( ctx , "Server quitting, exit early for peer" )
4699
4713
return
4700
4714
}
4701
4715
@@ -4730,7 +4744,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
4730
4744
4731
4745
// If there were any notification requests for when this peer
4732
4746
// 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" )
4734
4748
pubStr := string (pubKey .SerializeCompressed ())
4735
4749
for _ , offlineChan := range s .peerDisconnectedListeners [pubStr ] {
4736
4750
close (offlineChan )
@@ -4760,7 +4774,7 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
4760
4774
4761
4775
// First, cleanup any remaining state the server has regarding the peer
4762
4776
// in question.
4763
- s .removePeerUnsafe (p )
4777
+ s .removePeerUnsafe (ctx , p )
4764
4778
4765
4779
// Next, check to see if this is a persistent peer or not.
4766
4780
if _ , ok := s .persistentPeers [pubStr ]; ! ok {
@@ -4799,18 +4813,16 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
4799
4813
// the address used by our onion service to dial
4800
4814
// to lnd), so we don't have enough information
4801
4815
// 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" )
4805
4818
return
4806
4819
4807
4820
// We came across an error retrieving an advertised
4808
4821
// address, log it, and fall back to the existing peer
4809
4822
// address.
4810
4823
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 )
4814
4826
}
4815
4827
4816
4828
// 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{}) {
4852
4864
// call can stall for arbitrarily long if we shutdown while an
4853
4865
// outbound connection attempt is being made.
4854
4866
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 )
4858
4870
4859
4871
select {
4860
4872
case <- time .After (backoff ):
@@ -4864,9 +4876,8 @@ func (s *server) peerTerminationWatcher(p *peer.Brontide, ready chan struct{}) {
4864
4876
return
4865
4877
}
4866
4878
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" )
4870
4881
4871
4882
s .connectToPersistentPeer (pubStr )
4872
4883
}()
@@ -4973,12 +4984,12 @@ func (s *server) connectToPersistentPeer(pubKeyStr string) {
4973
4984
// active peers.
4974
4985
//
4975
4986
// 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 ) {
4977
4988
if p == nil {
4978
4989
return
4979
4990
}
4980
4991
4981
- srvrLog .Debugf ( "Removing peer %v" , p )
4992
+ srvrLog .DebugS ( ctx , "Removing peer" )
4982
4993
4983
4994
// Exit early if we have already been instructed to shutdown, the peers
4984
4995
// will be disconnected in the server shutdown process.
@@ -5018,7 +5029,7 @@ func (s *server) removePeerUnsafe(p *peer.Brontide) {
5018
5029
5019
5030
// Remove the peer's access permission from the access manager.
5020
5031
peerPubStr := string (p .IdentityKey ().SerializeCompressed ())
5021
- s .peerAccessMan .removePeerAccess (peerPubStr )
5032
+ s .peerAccessMan .removePeerAccess (ctx , peerPubStr )
5022
5033
5023
5034
// Copy the peer's error buffer across to the server if it has
5024
5035
// any items in it so that we can restore peer errors across
0 commit comments