Skip to content

Commit

Permalink
make endpoint the right type
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Nov 29, 2024
1 parent 6753e55 commit d1405d6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
9 changes: 7 additions & 2 deletions BTCPayApp.Core/Data/PeerInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace BTCPayApp.Core.Data;
using System.Net;
using System.Text.Json.Serialization;
using BTCPayApp.Core.JsonConverters;

namespace BTCPayApp.Core.Data;

public record PeerInfo
{
public string Endpoint { get; set; }
[JsonConverter(typeof(EndPointJsonConverter))]
public EndPoint? Endpoint { get; set; }
public bool Persistent { get; set; }
public bool Trusted { get; set; }
public string? Label { get; set; }
Expand Down
25 changes: 24 additions & 1 deletion BTCPayApp.Core/JsonConverters/UInt256JsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NBitcoin;
using System.Net;
using BTCPayApp.Core.Helpers;
using NBitcoin;

namespace BTCPayApp.Core.JsonConverters;

Expand All @@ -8,4 +10,25 @@ public override uint256 Create(string str)
{
return uint256.Parse(str);
}
}

public class EndPointJsonConverter : GenericStringJsonConverter<EndPoint?>
{
public override EndPoint? Create(string str)
{
if(string.IsNullOrEmpty(str))
return null;
if(EndPointParser.TryParse(str, 9735, out var endpoint))
{
return endpoint;
}
throw new FormatException("Invalid endpoint");
}

public override string ToString(EndPoint? value)
{
if (value is null)
return null;
return value.ToEndpointString();
}
}
12 changes: 5 additions & 7 deletions BTCPayApp.Core/LDK/LDKPeerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private async Task BtcPayAppServerClientOnOnServerNodeInfo(object? sender, strin
await _node.Peer(nodeInfo.NodeId, new PeerInfo()
{
Label = "BTCPay Server Node",
Endpoint = endpoint.ToString(),
Endpoint = endpoint,
Persistent = true,
Trusted = true
});
Expand Down Expand Up @@ -104,7 +104,7 @@ private async Task ContinuouslyAttemptToConnectToPersistentPeers(CancellationTok
{
var kv = config.Peers[persistentPeer];
var nodeid = new PubKey(persistentPeer);
if (EndPointParser.TryParse(kv.Endpoint, 9735, out var endpoint))
if (kv.Endpoint is {} endpoint)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(ctsToken);
cts.CancelAfter(10000);
Expand Down Expand Up @@ -195,9 +195,7 @@ await listener.AcceptTcpClientAsync(cancellationToken),

public async Task<LDKTcpDescriptor?> ConnectAsync(PubKey peerNodeId,PeerInfo peerInfo, CancellationToken cancellationToken = default)
{
if (peerInfo.Endpoint is null)
return null;
if (EndPointParser.TryParse(peerInfo.Endpoint, 9735, out var endpoint))
if (peerInfo.Endpoint is {} endpoint)
{
if(peerInfo.Label is not null)
_logger.LogInformation($"Attempting to connect to {peerNodeId} at {endpoint} ({peerInfo.Label})");
Expand Down Expand Up @@ -245,9 +243,9 @@ await listener.AcceptTcpClientAsync(cancellationToken),
peer = new PeerInfo();
}

if (peer.Endpoint != remote.ToString())
if (peer.Endpoint.ToEndpointString() != remote.ToString())
{
peer.Endpoint = remote.ToString()!;
peer.Endpoint = remote;
await _node.Peer(theirNodeId, peer);
}
}
Expand Down
6 changes: 3 additions & 3 deletions BTCPayApp.Core/LSP/JIT/VoltageFlow2Jit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ private async Task ConfigUpdated(object? sender, LightningConfig e)
{
//check if the endpoint matches any of the info ones
if (!_info.ConnectionMethods.Any(a =>
a.ToEndpoint().ToEndpointString().Equals(peer.Endpoint, StringComparison.OrdinalIgnoreCase)))
a.ToEndpoint().ToEndpointString().Equals(peer.Endpoint.ToEndpointString(), StringComparison.OrdinalIgnoreCase)))
{
peer = new PeerInfo
{
Label = ProviderName,
Endpoint = _info.ConnectionMethods.First().ToEndpoint().ToEndpointString(), Persistent = true,
Endpoint = _info.ConnectionMethods.First().ToEndpoint(), Persistent = true,
Trusted = true
};
}
Expand All @@ -284,7 +284,7 @@ private async Task ConfigUpdated(object? sender, LightningConfig e)
peer = new PeerInfo
{
Label = ProviderName,
Endpoint = _info.ConnectionMethods.First().ToEndpoint().ToEndpointString(),
Endpoint = _info.ConnectionMethods.First().ToEndpoint(),
Persistent = true,
Trusted = true
};
Expand Down
4 changes: 2 additions & 2 deletions BTCPayApp.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ await TestUtils.EventuallyAsync(async () =>
await node3.LNManager.Node.Peer(node2.LNManager.Node.NodeId, new PeerInfo()
{
Label = "App2",
Endpoint = node2.LNManager.Node.PeerHandler.Endpoint.ToEndpointString(),
Endpoint = node2.LNManager.Node.PeerHandler.Endpoint,
Persistent = true,
});
await TestUtils.EventuallyAsync(async () =>
Expand All @@ -283,7 +283,7 @@ await TestUtils.EventuallyAsync(async () =>
(await node3.LNManager.Node.GetConfig()).Peers.TryGetValue(node2.LNManager.Node.NodeId.ToString(),
out var peerInfo));
Assert.Equal("App2", peerInfo.Label);
Assert.Equal(node2.LNManager.Node.PeerHandler.Endpoint.ToEndpointString(), peerInfo.Endpoint);
Assert.Equal(node2.LNManager.Node.PeerHandler.Endpoint.ToEndpointString(), peerInfo.Endpoint.ToEndpointString());
Assert.True(peerInfo.Persistent);
Assert.False(peerInfo.Trusted);
});
Expand Down

0 comments on commit d1405d6

Please sign in to comment.