diff --git a/src/Services/LightningClientService.cs b/src/Services/LightningClientService.cs index f7b6621c..93eb9d92 100644 --- a/src/Services/LightningClientService.cs +++ b/src/Services/LightningClientService.cs @@ -274,8 +274,24 @@ public AsyncServerStreamingCall SubscribeChannelEvents(Node public async Task ConnectToPeer(Node node, string peerPubKey, Lightning.LightningClient? client = null) { client ??= GetLightningClient(node.Endpoint); + var metadata = new Metadata { { "macaroon", node.ChannelAdminMacaroon } }; var isPeerAlreadyConnected = false; + // If we're already connected to the peer there's nothing to do. + try + { + var peers = await client.ListPeersAsync(new ListPeersRequest(), metadata); + if (peers.Peers.Any(p => p.PubKey == peerPubKey)) + { + _logger.LogInformation("Peer: {Pubkey} already connected", peerPubKey); + return; + } + } + catch (Exception e) + { + _logger.LogWarning(e, "Could not list peers on node {NodeId} before connecting to {Pubkey}", node.Id, peerPubKey); + } + ConnectPeerResponse connectPeerResponse = null; try { @@ -284,14 +300,17 @@ public async Task ConnectToPeer(Node node, string peerPubKey, Lightning.Lightnin //For now, we only rely on pure tcp IPV4 connections var addr = nodeInfo?.Addresses.FirstOrDefault(x => x.Network == "tcp")?.Addr; + if (string.IsNullOrEmpty(addr)) + { + throw new PeerNotOnlineException( + $"peer {peerPubKey} has no known address to connect to and is not already connected"); + } + connectPeerResponse = await client.ConnectPeerAsync(new ConnectPeerRequest { Addr = new LightningAddress { Host = addr, Pubkey = nodeInfo.PubKey }, Perm = true - }, new Metadata - { - { "macaroon", node.ChannelAdminMacaroon } - }); + }, metadata); } //We avoid to stop the method if the peer is already connected catch (RpcException e)