Make sure lightning listener is listening to new bolt11 invoices for partially paid btcpay invoices and that all payments are registered to the invoice.

This commit is contained in:
Kukks
2020-09-24 12:18:19 +02:00
parent dbe1579fc4
commit d1efddd312
3 changed files with 45 additions and 17 deletions

View File

@@ -140,12 +140,17 @@ namespace BTCPayServer.Tests
await CustomerLightningD.Pay(bolt11);
}
public async Task<T> WaitForEvent<T>(Func<Task> action)
public async Task<T> WaitForEvent<T>(Func<Task> action, Func<T, bool> correctEvent = null)
{
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
var sub = PayTester.GetService<EventAggregator>().Subscribe<T>(evt =>
{
tcs.TrySetResult(evt);
if(correctEvent is null)
tcs.TrySetResult(evt);
else if (correctEvent(evt))
{
tcs.TrySetResult(evt);
}
});
await action.Invoke();
var result = await tcs.Task;

View File

@@ -762,34 +762,38 @@ namespace BTCPayServer.Tests
await user.RegisterDerivationSchemeAsync("BTC");
await user.RegisterLightningNodeAsync("BTC", LightningConnectionType.CLightning);
user.SetNetworkFeeMode(NetworkFeeMode.Never);
var invoice = await user.BitPay.CreateInvoiceAsync(new Invoice(0.01m, "BTC"));
await user.ModifyStoreAsync(model => model.SpeedPolicy = SpeedPolicy.HighSpeed);
var invoice = await user.BitPay.CreateInvoiceAsync(new Invoice(0.0001m, "BTC"));
await tester.WaitForEvent<InvoiceNewAddressEvent>(async () =>
{
await tester.ExplorerNode.SendToAddressAsync(
BitcoinAddress.Create(invoice.BitcoinAddress, Network.RegTest), Money.Coins(0.005m));
BitcoinAddress.Create(invoice.BitcoinAddress, Network.RegTest), Money.Coins(0.00005m));
});
await tester.ExplorerNode.GenerateAsync(1);
var newInvoice = await user.BitPay.GetInvoiceAsync(invoice.Id);
var newBolt11 = newInvoice.CryptoInfo.First(o => o.PaymentUrls.BOLT11 != null).PaymentUrls.BOLT11;
var oldBolt11= invoice.CryptoInfo.First(o => o.PaymentUrls.BOLT11 != null).PaymentUrls.BOLT11;
Assert.NotEqual(newBolt11,oldBolt11);
Assert.Equal(newInvoice.BtcDue.GetValue(), BOLT11PaymentRequest.Parse(newBolt11, Network.RegTest).MinimumAmount.ToDecimal(LightMoneyUnit.BTC));
Logs.Tester.LogInformation($"Paying invoice {newInvoice.Id} remaining due amount {newInvoice.BtcDue.GetValue()} via lightning" );
var evt = await tester.WaitForEvent<InvoiceDataChangedEvent>(async () =>
{
await tester.SendLightningPaymentAsync(newInvoice);
});
Assert.Equal(evt.InvoiceId, invoice.Id);
Assert.Equal(InvoiceStatus.Complete, evt.State.Status);
Assert.Equal(InvoiceExceptionStatus.None, evt.State.ExceptionStatus);
}, evt => evt.InvoiceId == invoice.Id);
var fetchedInvoice = await tester.PayTester.InvoiceRepository.GetInvoice(evt.InvoiceId);
Assert.Contains(fetchedInvoice.Status, new []{InvoiceStatus.Complete, InvoiceStatus.Confirmed});
Assert.Equal(InvoiceExceptionStatus.None, fetchedInvoice.ExceptionStatus);
Logs.Tester.LogInformation($"Paying invoice {invoice.Id} original full amount bolt11 invoice " );
evt = await tester.WaitForEvent<InvoiceDataChangedEvent>(async () =>
{
await tester.SendLightningPaymentAsync(invoice);
});
}, evt => evt.InvoiceId == invoice.Id);
Assert.Equal(evt.InvoiceId, invoice.Id);
Assert.Equal(InvoiceStatus.Invalid, evt.State.Status);
Assert.Equal(InvoiceExceptionStatus.PaidOver, evt.State.ExceptionStatus);
fetchedInvoice = await tester.PayTester.InvoiceRepository.GetInvoice(evt.InvoiceId);
Assert.Equal( 3,fetchedInvoice.Payments.Count);
}
[Fact(Timeout = 60 * 2 * 1000)]

View File

@@ -170,11 +170,30 @@ namespace BTCPayServer.Payments.Lightning
await _lightningLikePaymentHandler.CreatePaymentMethodDetails(
logs, supportedMethod,
paymentMethod, store, paymentMethod.Network, prepObj);
var instanceListenerKey = (paymentMethod.Network.CryptoCode,
supportedMethod.GetLightningUrl().ToString());
if (_InstanceListeners.TryGetValue(instanceListenerKey, out var instanceListener))
{
await _InvoiceRepository.NewAddress(invoice.Id, newPaymentMethodDetails,
paymentMethod.Network);
await _InvoiceRepository.NewAddress(invoice.Id, newPaymentMethodDetails,
paymentMethod.Network);
_Aggregator.Publish(new InvoiceNewAddressEvent(invoice.Id, newPaymentMethodDetails.GetPaymentDestination(),paymentMethod.Network ));
instanceListener.AddListenedInvoice(new ListenedInvoice()
{
Expiration = invoice.ExpirationTime,
Uri = supportedMethod.GetLightningUrl().BaseUri.AbsoluteUri,
PaymentMethodDetails = (LightningLikePaymentMethodDetails) newPaymentMethodDetails,
SupportedPaymentMethod = supportedMethod,
PaymentMethod = paymentMethod,
Network = (BTCPayNetwork) paymentMethod.Network,
InvoiceId = invoice.Id
});
_Aggregator.Publish(new InvoiceNewAddressEvent(invoice.Id,
newPaymentMethodDetails.GetPaymentDestination(), paymentMethod.Network));
}
}
catch (Exception e)
{