Skip to content

Commit

Permalink
Node Alias update (#272)
Browse files Browse the repository at this point in the history
Automatic update in the case of a node alias is changed
  • Loading branch information
RodriFS authored Aug 18, 2023
1 parent 454a8e1 commit 62fc4c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
50 changes: 47 additions & 3 deletions src/Jobs/ChannelMonitorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ChannelMonitorJob : IJob
private readonly INodeRepository _nodeRepository;
private readonly ILightningService _lightningService;
private readonly ILightningClientService _lightningClientService;
private readonly Dictionary<string, Node> _remoteNodes = new();
private readonly HashSet<string> _checkedRemoteNodes = new();

public ChannelMonitorJob(ILogger<ChannelMonitorJob> logger, IDbContextFactory<ApplicationDbContext> dbContextFactory, INodeRepository nodeRepository, ILightningService lightningService, ILightningClientService lightningClientService)
{
Expand All @@ -67,13 +69,20 @@ public async Task Execute(IJobExecutionContext context)
return;
}

var result = await _lightningClientService.ListChannels(node1);
var client = _lightningClientService.GetLightningClient(node1.Endpoint);
var result = await _lightningClientService.ListChannels(node1, client);

var channels = result?.Channels.ToList();
await MarkClosedChannelsAsClosed(node1, channels);
foreach (var channel in channels)
foreach (var channel in channels ?? new())
{
var node2 = await _nodeRepository.GetOrCreateByPubKey(channel.RemotePubkey, _lightningService);
var node2 = await GetRemoteNode(channel.RemotePubkey);
if (node2 == null)
{
_logger.LogWarning("The external node {NodeId} was set up for monitoring but the remote node {RemoteNodeId} doesn't exist anymore", nodeId, channel.RemotePubkey);
continue;
}
await RefreshExternalNodeData(node1, node2, client);

// Recover Operations on channels
await RecoverGhostChannels(node1, node2, channel);
Expand All @@ -89,6 +98,41 @@ public async Task Execute(IJobExecutionContext context)
_logger.LogInformation("{JobName} ended", nameof(ChannelMonitorJob));
}

private async Task<Node?> GetRemoteNode(string pubKey)
{
if (_remoteNodes.TryGetValue(pubKey, out var node))
{
return node;
}
node = await _nodeRepository.GetOrCreateByPubKey(pubKey, _lightningService);
_remoteNodes.Add(node.PubKey, node);
return node;
}

private async Task RefreshExternalNodeData(Node managedNode, Node remoteNode, Lightning.LightningClient lightningClient)
{
if (_checkedRemoteNodes.Contains(remoteNode.PubKey))
{
return;
}
var nodeInfo = await _lightningClientService.GetNodeInfo(managedNode, remoteNode.PubKey, lightningClient);
if (nodeInfo == null)
{
_logger.LogWarning("The external node {NodeId} was set up for monitoring but the remote node {RemoteNodeId} doesn't exist anymore", managedNode.Id, remoteNode.PubKey);
return;
}

if (remoteNode.Name == nodeInfo.Alias) return;

remoteNode.Name = nodeInfo.Alias;
var (updated, error) = _nodeRepository.Update(remoteNode);
if (!updated)
{
_logger.LogWarning("Couldn't update Node {NodeId} with Name {Name}: {Error}", remoteNode.Id, remoteNode.Name, error);
}
_checkedRemoteNodes.Add(remoteNode.PubKey);
}

public async Task RecoverGhostChannels(Node source, Node destination, Channel channel)
{
if (!channel.Initiator && destination.IsManaged) return;
Expand Down
18 changes: 5 additions & 13 deletions src/Jobs/ProcessNodeChannelAcceptorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ public class ProcessNodeChannelAcceptorJob : IJob
private readonly IWalletRepository _walletRepository;
private readonly INBXplorerService _nBXplorerService;
private readonly ILogger<ProcessNodeChannelAcceptorJob> _logger;
private readonly ILightningClientService _lightningClientService;

public ProcessNodeChannelAcceptorJob(ILogger<ProcessNodeChannelAcceptorJob> logger,
INodeRepository nodeRepository,
IWalletRepository walletRepository,
INBXplorerService nBXplorerService
INBXplorerService nBXplorerService,
ILightningClientService lightningClientService
)
{
_nodeRepository = nodeRepository;
_walletRepository = walletRepository;
_nBXplorerService = nBXplorerService;
_logger = logger;
_lightningClientService = lightningClientService;
}

public async Task Execute(IJobExecutionContext context)
Expand Down Expand Up @@ -126,18 +129,7 @@ await asyncDuplexStreamingCall.RequestStream.WriteAsync(new ChannelAcceptRespons
{
_logger.LogInformation("Starting {JobName} on node: {NodeName}", nameof(ProcessNodeChannelAcceptorJob), node.Name);

using var grpcChannel = GrpcChannel.ForAddress($"https://{node.Endpoint}",
new GrpcChannelOptions
{
HttpHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
},
LoggerFactory = loggerFactory,
});

var client = new Lightning.LightningClient(grpcChannel);
var client = _lightningClientService.GetLightningClient(node.Endpoint);

if (!string.IsNullOrEmpty(node.ChannelAdminMacaroon))
{
Expand Down
7 changes: 7 additions & 0 deletions src/Services/LightningService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,13 @@ public async Task<Dictionary<ulong, ChannelState>> GetChannelsState()
foreach (var node in nodes)
{
var listChannelsResponse = await _lightningClientService.ListChannels(node);

if (listChannelsResponse == null)
{
_logger.LogError("Error while getting channels for node: {NodeId}", node.Id);
continue;
}

var channels = listChannelsResponse.Channels.ToList();

foreach (var channel in channels)
Expand Down

0 comments on commit 62fc4c7

Please sign in to comment.