Fix flaky test ln payout (#4334)

This commit is contained in:
Nicolas Dorier 2022-11-23 21:02:47 +09:00 committed by GitHub
parent d2b10ef4e6
commit bf0a8c1e62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -94,7 +94,7 @@ namespace BTCPayServer.Data.Payouts.LightningLike
public async Task<IActionResult> ConfirmLightningPayout(string cryptoCode, string[] payoutIds)
{
await SetStoreContext();
var pmi = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
await using var ctx = _applicationDbContextFactory.CreateContext();
@ -118,9 +118,9 @@ namespace BTCPayServer.Data.Payouts.LightningLike
public async Task<IActionResult> ProcessLightningPayout(string cryptoCode, string[] payoutIds, CancellationToken cancellationToken)
{
await SetStoreContext();
var pmi = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
var payoutHandler = (LightningLikePayoutHandler) _payoutHandlers.FindPayoutHandler(pmi);
var payoutHandler = (LightningLikePayoutHandler)_payoutHandlers.FindPayoutHandler(pmi);
await using var ctx = _applicationDbContextFactory.CreateContext();
@ -129,7 +129,7 @@ namespace BTCPayServer.Data.Payouts.LightningLike
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(pmi.CryptoCode);
//we group per store and init the transfers by each
var authorizedForInternalNode = (await _authorizationService.AuthorizeAsync(User, null, new PolicyRequirement(Policies.CanModifyServerSettings))).Succeeded;
foreach (var payoutDatas in payouts)
{
@ -184,11 +184,11 @@ namespace BTCPayServer.Data.Payouts.LightningLike
break;
case BoltInvoiceClaimDestination item1:
result = await TrypayBolt(client, blob, payoutData, item1.PaymentRequest, pmi, cancellationToken);
result = await TrypayBolt(client, blob, payoutData, item1.PaymentRequest, pmi, cancellationToken);
break;
default:
result= new ResultVM
result = new ResultVM
{
PayoutId = payoutData.Id,
Result = PayResult.Error,
@ -216,7 +216,7 @@ namespace BTCPayServer.Data.Payouts.LightningLike
return View("LightningPayoutResult", results);
}
public static async Task<(BOLT11PaymentRequest, ResultVM)> GetInvoiceFromLNURL(PayoutData payoutData,
LightningLikePayoutHandler handler,PayoutBlob blob, LNURLPayClaimDestinaton lnurlPayClaimDestinaton, Network network, CancellationToken cancellationToken)
LightningLikePayoutHandler handler, PayoutBlob blob, LNURLPayClaimDestinaton lnurlPayClaimDestinaton, Network network, CancellationToken cancellationToken)
{
var endpoint = lnurlPayClaimDestinaton.LNURL.IsValidEmail()
? LNURL.LNURL.ExtractUriFromInternetIdentifier(lnurlPayClaimDestinaton.LNURL)
@ -237,7 +237,7 @@ namespace BTCPayServer.Data.Payouts.LightningLike
$"The LNURL provided would not generate an invoice of {lm.MilliSatoshi}msats"
});
}
try
{
var lnurlPayRequestCallbackResponse =
@ -257,17 +257,17 @@ namespace BTCPayServer.Data.Payouts.LightningLike
});
}
}
public static readonly TimeSpan SendTimeout = TimeSpan.FromSeconds(20);
public static async Task<ResultVM> TrypayBolt(
ILightningClient lightningClient, PayoutBlob payoutBlob, PayoutData payoutData, BOLT11PaymentRequest bolt11PaymentRequest,
ILightningClient lightningClient, PayoutBlob payoutBlob, PayoutData payoutData, BOLT11PaymentRequest bolt11PaymentRequest,
PaymentMethodId pmi, CancellationToken cancellationToken)
{
var boltAmount = bolt11PaymentRequest.MinimumAmount.ToDecimal(LightMoneyUnit.BTC);
if (boltAmount != payoutBlob.CryptoAmount)
{
payoutData.State = PayoutState.Cancelled;
return new ResultVM
{
@ -278,13 +278,13 @@ namespace BTCPayServer.Data.Payouts.LightningLike
};
}
var proofBlob = new PayoutLightningBlob() {PaymentHash = bolt11PaymentRequest.PaymentHash.ToString()};
var proofBlob = new PayoutLightningBlob() { PaymentHash = bolt11PaymentRequest.PaymentHash.ToString() };
try
{
// TODO: Incorporate the changes from this PR here:
// https://github.com/btcpayserver/BTCPayServer.Lightning/pull/106
using var timeout = new CancellationTokenSource(SendTimeout);
using var c = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken);
using var c = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, cancellationToken);
var result = await lightningClient.Pay(bolt11PaymentRequest.ToString(),
new PayInvoiceParams()
{
@ -301,15 +301,15 @@ namespace BTCPayServer.Data.Payouts.LightningLike
payoutData.State = PayoutState.Completed;
try
{
var payment = await lightningClient.GetPayment(bolt11PaymentRequest.PaymentHash.ToString());
proofBlob.Preimage = payment.Preimage;
var payment = await lightningClient.GetPayment(bolt11PaymentRequest.PaymentHash.ToString(), cancellationToken);
proofBlob.Preimage = payment.Preimage;
}
catch (Exception)
{
// ignored
}
}
payoutData.SetProofBlob(proofBlob, null);
return new ResultVM
{
@ -324,7 +324,7 @@ namespace BTCPayServer.Data.Payouts.LightningLike
// Timeout, potentially caused by hold invoices
// Payment will be saved as pending, the LightningPendingPayoutListener will handle settling/cancelling
payoutData.State = PayoutState.InProgress;
payoutData.SetProofBlob(proofBlob, null);
return new ResultVM
{
@ -340,8 +340,9 @@ namespace BTCPayServer.Data.Payouts.LightningLike
private async Task SetStoreContext()
{
var storeId = HttpContext.GetUserPrefsCookie()?.CurrentStoreId;
if (string.IsNullOrEmpty(storeId)) return;
if (string.IsNullOrEmpty(storeId))
return;
var userId = _userManager.GetUserId(User);
var store = await _storeRepository.FindStore(storeId, userId);
if (store != null)