From 45ed240b500751eb1c3f89a3bd6fb2a97e56cfc1 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Thu, 10 Jan 2019 17:50:33 +0000 Subject: [PATCH] FIX: limit number of userinvoices feetched per polling request --- LightningCustodianWallet.test.js | 8 +++++++ class/lightning-custodian-wallet.js | 33 +++++++++++++++++++++++++---- screen/lnd/lndViewInvoice.js | 7 ++++-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/LightningCustodianWallet.test.js b/LightningCustodianWallet.test.js index bde3f443f..d6623868c 100644 --- a/LightningCustodianWallet.test.js +++ b/LightningCustodianWallet.test.js @@ -263,6 +263,14 @@ describe('LightningCustodianWallet', () => { await lOld.fetchTransactions(); assert.equal(txLen, lOld.transactions_raw.length, 'tx count should not be changed'); assert.equal(invLen, (await lNew.getUserInvoices()).length, 'invoices count should not be changed'); + + // testing how limiting works: + assert.equal(lNew.user_invoices_raw.length, 1); + await lNew.addInvoice(666, 'test memo 2'); + invoices = await lNew.getUserInvoices(1); + assert.equal(invoices.length, 2); + assert.equal(invoices[0].amt, 1); + assert.equal(invoices[1].amt, 666); }); it('can pay free amount (tip) invoice', async function() { diff --git a/class/lightning-custodian-wallet.js b/class/lightning-custodian-wallet.js index 07379e04a..d6ad45344 100644 --- a/class/lightning-custodian-wallet.js +++ b/class/lightning-custodian-wallet.js @@ -17,6 +17,7 @@ export class LightningCustodianWallet extends LegacyWallet { this._access_token_created_ts = 0; this.refill_addressess = []; this.pending_transactions_raw = []; + this.user_invoices_raw = []; this.info_raw = false; this.preferredBalanceUnit = BitcoinUnit.SATS; } @@ -128,8 +129,10 @@ export class LightningCustodianWallet extends LegacyWallet { * * @return {Promise.} */ - async getUserInvoices() { - let response = await this._api.get('/getuserinvoices', { + async getUserInvoices(limit = false) { + let limitString = ''; + if (limit) limitString = '?limit=' + parseInt(limit); + let response = await this._api.get('/getuserinvoices' + limitString, { headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json', @@ -144,9 +147,31 @@ export class LightningCustodianWallet extends LegacyWallet { if (json && json.error) { throw new Error('API error: ' + json.message + ' (code ' + json.code + ')'); } - this.user_invoices_raw = json; - return json; + if (limit) { + // need to merge existing invoices with the ones that arrived + // but the ones received later should overwrite older ones + + for (let oldInvoice of this.user_invoices_raw) { + // iterate all OLD invoices + let found = false; + for (let newInvoice of json) { + // iterate all NEW invoices + if (newInvoice.payment_request === oldInvoice.payment_request) found = true; + } + + if (!found) { + // if old invoice is not found in NEW array, we simply add it: + json.push(oldInvoice); + } + } + } + + this.user_invoices_raw = json.sort(function(a, b) { + return a.timestamp - b.timestamp; + }); + + return this.user_invoices_raw; } /** diff --git a/screen/lnd/lndViewInvoice.js b/screen/lnd/lndViewInvoice.js index 763120501..9b6f80199 100644 --- a/screen/lnd/lndViewInvoice.js +++ b/screen/lnd/lndViewInvoice.js @@ -38,8 +38,11 @@ export default class LNDViewInvoice extends Component { this.fetchInvoiceInterval = setInterval(async () => { if (this.state.isFetchingInvoices) { try { - const userInvoices = JSON.stringify(await this.state.fromWallet.getUserInvoices()); - const updatedUserInvoice = JSON.parse(userInvoices).filter(invoice => + const userInvoices = await this.state.fromWallet.getUserInvoices(20); + // fetching only last 20 invoices + // for invoice that was created just now - that should be enough (it is basically the last one, so limit=1 would be sufficient) + // but that might not work as intended IF user creates 21 invoices, and then tries to check the status of invoice #0, it just wont be updated + const updatedUserInvoice = userInvoices.filter(invoice => typeof this.state.invoice === 'object' ? invoice.payment_request === this.state.invoice.payment_request : invoice.payment_request === this.state.invoice,