Skip to content

Commit

Permalink
LND and Eclair shouldn't crash of payment isn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Nov 13, 2024
1 parent 562105d commit fc53710
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/BTCPayServer.Lightning.Eclair/EclairLightningClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private async Task<LightningInvoice> ToLightningInvoice(InvoiceResponse invoice,
public async Task<LightningPayment> GetPayment(string paymentHash, CancellationToken cancellation = default)
{
var result = await _eclairClient.GetSentInfo(paymentHash, null, cancellation);

if (result.Count == 0)
return null;
var sentInfo = result.First();
var fees = sentInfo.Status.FeesPaid;
var payment = new LightningPayment
Expand Down
12 changes: 8 additions & 4 deletions src/BTCPayServer.Lightning.LND/LndClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private async Task ListenLoop(HttpRequestMessage request)
else if (line.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase))
{
var errorString = JObject.Parse(line)["error"].ToString();
var error = _Parent.Deserialize<LndError>(errorString);
var error = _Parent.Deserialize<LNDError>(errorString);
throw new LndException(error);
}
else
Expand Down Expand Up @@ -218,7 +218,7 @@ private async Task ListenLoop(HttpRequestMessage request)
else if (line.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase))
{
var errorString = JObject.Parse(line)["error"].ToString();
var error = _Parent.Deserialize<LndError>(errorString);
var error = _Parent.Deserialize<LNDError>(errorString);
throw new LndException(error);
}
else
Expand Down Expand Up @@ -519,9 +519,13 @@ async Task<LightningPayment> GetPayment(string paymentHash, CancellationToken ca

return payment;
}
catch (SwaggerException ex) when (ex.AsLNDError() is {} lndError)
catch (LndException ex) when (ex.Error is { Code: 5 } lndError)
{
throw new LndException(lndError.Error);
return null;
}
catch (LndException ex) when (ex.Error is { Message: "payment isn't initiated" } lndError)
{
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/BTCPayServer.Lightning.LND/LndError.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BTCPayServer.Lightning.LND
{
class LNDError
public class LNDError
{
public string Error { get; set; }
public string Message { get; set; }
Expand Down
18 changes: 3 additions & 15 deletions src/BTCPayServer.Lightning.LND/LndSwaggerClient.partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,23 @@ public LndException(string message) : base(message)
{

}
public LndException(LndError error) : base(error.Message)
public LndException(LNDError error) : base(error.Message)
{
if (error == null)
throw new ArgumentNullException(nameof(error));
_Error = error;
}


private readonly LndError _Error;
public LndError Error
private readonly LNDError _Error;
public LNDError Error
{
get
{
return _Error;
}
}
}
// {"grpc_code":2,"http_code":500,"message":"rpc error: code = Unknown desc = expected 1 macaroon, got 0","http_status":"Internal Server Error"}
public class LndError
{
[JsonProperty("grpc_code")]
public int GRPCCode { get; set; }
[JsonProperty("http_code")]
public int HttpCode { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("http_status")]
public string HttpStatus { get; set; }
}
public partial class LndSwaggerClient
{
internal HttpClient _DefaultHttpClient;
Expand Down
1 change: 1 addition & 0 deletions tests/CommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ public async Task CanPayInvoiceAndReceive()
var hash = GetInvoicePaymentHash(invoice).ToString();
var payment = await GetInvoicePayment(invoice, test.Customer);
await test.Customer.GetPayment(hash);
Assert.Null(await test.Customer.GetPayment(new uint256(0).ToString()));
Assert.Equal(hash, payment.PaymentHash);
Assert.Equal(amount, payment.Amount);
Assert.Equal(amount, payment.AmountSent);
Expand Down

0 comments on commit fc53710

Please sign in to comment.