From 4a871a7d1754df0af1d6f044c5b3e92c8b7d2bd9 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Sat, 6 Apr 2019 20:21:13 +0100 Subject: [PATCH] FIX: listtransactions caching --- class/User.js | 41 +++++++++++++++++++++++++++++++++++++++-- lightning.js | 2 +- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/class/User.js b/class/User.js index 31962db..44ebde9 100644 --- a/class/User.js +++ b/class/User.js @@ -285,7 +285,7 @@ export class User { addr = await this.getAddress(); } if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user'); - let txs = await this._bitcoindrpc.request('listtransactions', ['*', 100500, 0, true]); + let txs = await this._listtransactions(); txs = txs.result; let result = []; for (let tx of txs) { @@ -323,6 +323,42 @@ export class User { return result; } + /** + * Simple caching for this._bitcoindrpc.request('listtransactions', ['*', 100500, 0, true]); + * since its too much to fetch from bitcoind every time + * + * @returns {Promise<*>} + * @private + */ + async _listtransactions() { + const key = 'listtransactions'; + let response = await this._redis.get(key); + if (response) { + try { + let json = JSON.parse(response); + return json; + } catch (_) { + // nop + } + } + + let txs = await this._bitcoindrpc.request('listtransactions', ['*', 100500, 0, true]); + // now, compacting response a bit + let ret = { result: [] }; + for (const tx of txs.result) { + ret.result.push({ + category: tx.category, + amount: tx.amount, + confirmations: tx.confirmations, + address: tx.address, + time: tx.time, + }); + } + await this._redis.set(key, JSON.stringify(ret)); + await this._redis.expire(key, 5 * 60); + return ret; + } + /** * Returning onchain txs for user's address that are less than 3 confs * @@ -335,7 +371,7 @@ export class User { addr = await this.getAddress(); } if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user'); - let txs = await this._bitcoindrpc.request('listtransactions', ['*', 100500, 0, true]); + let txs = await this._listtransactions(); txs = txs.result; let result = []; for (let tx of txs) { @@ -372,6 +408,7 @@ export class User { * @returns {Promise} */ async accountForPosibleTxids() { + return; // TODO: remove let onchain_txs = await this.getTxs(); let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1); for (let tx of onchain_txs) { diff --git a/lightning.js b/lightning.js index ba9f929..4178efe 100644 --- a/lightning.js +++ b/lightning.js @@ -32,7 +32,7 @@ if (config.lnd.password) { var walletUnlocker = new lnrpc.WalletUnlocker(config.lnd.url, creds); walletUnlocker.unlockWallet( { - wallet_password: config.lnd.password, + wallet_password: Buffer.from(config.lnd.password).toString('base64'), }, function(err, response) { if (err) {