Make a batch query for all pending invoice (Fix #2022)

This commit is contained in:
nicolas.dorier 2020-10-28 23:21:46 +09:00
parent 440ce0221a
commit b9ca02088d
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 25 additions and 3 deletions

View File

@ -138,9 +138,7 @@ namespace BTCPayServer.Payments.Bitcoin
switch (newEvent)
{
case NBXplorer.Models.NewBlockEvent evt:
await Task.WhenAll((await _InvoiceRepository.GetPendingInvoices())
.Select(invoiceId => UpdatePaymentStates(wallet, invoiceId))
.ToArray());
await UpdatePaymentStates(wallet, await _InvoiceRepository.GetPendingInvoices());
_Aggregator.Publish(new Events.NewBlockEvent() { CryptoCode = evt.CryptoCode });
break;
case NBXplorer.Models.NewTransactionEvent evt:
@ -206,11 +204,21 @@ namespace BTCPayServer.Payments.Bitcoin
}
}
async Task UpdatePaymentStates(BTCPayWallet wallet, string[] invoiceIds)
{
var invoices = await _InvoiceRepository.GetInvoices(invoiceIds);
await Task.WhenAll(invoices.Select(i => UpdatePaymentStates(wallet, i)).ToArray());
}
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, string invoiceId)
{
var invoice = await _InvoiceRepository.GetInvoice(invoiceId, false);
if (invoice == null)
return null;
return await UpdatePaymentStates(wallet, invoice);
}
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, InvoiceEntity invoice)
{
List<PaymentEntity> updatedPaymentEntities = new List<PaymentEntity>();
var transactions = await wallet.GetTransactions(invoice.GetAllBitcoinPaymentData()
.Select(p => p.Outpoint.Hash)

View File

@ -467,6 +467,20 @@ retry:
var res = await GetInvoiceRaw(id, inludeAddressData);
return res == null ? null : ToEntity(res);
}
public async Task<InvoiceEntity[]> GetInvoices(string[] invoiceIds)
{
var invoiceIdSet = invoiceIds.ToHashSet();
using (var context = _ContextFactory.CreateContext())
{
IQueryable<Data.InvoiceData> query =
context
.Invoices
.Include(o => o.Payments)
.Where(o => invoiceIdSet.Contains(o.Id));
return (await query.ToListAsync()).Select(o => ToEntity(o)).ToArray();
}
}
private async Task<InvoiceData> GetInvoiceRaw(string id, bool inludeAddressData = false)
{