From a03ddce60405aa8cb61b995ffc83493856e42e5c Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Sun, 22 Sep 2024 16:23:51 +0400 Subject: [PATCH] wdb-migrations: add block time cache. --- lib/wallet/migrations.js | 14 ++++++++++++++ test/util/migrations.js | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/wallet/migrations.js b/lib/wallet/migrations.js index 8f810f2f5..01d367b79 100644 --- a/lib/wallet/migrations.js +++ b/lib/wallet/migrations.js @@ -11,6 +11,7 @@ const Logger = require('blgr'); const bdb = require('bdb'); const bio = require('bufio'); const {BufferSet} = require('buffer-map'); +const LRU = require('blru'); const HDPublicKey = require('../hd/public'); const binary = require('../utils/binary'); const {encoding} = bio; @@ -808,6 +809,8 @@ class MigrateTXCountTimeIndex extends AbstractMigration { this.unconfirmedBatchSize = 1000; this.UNCONFIRMED_HEIGHT = 0xffffffff; + + this.blockTimeCache = new LRU(50); } /** @@ -1101,6 +1104,10 @@ class MigrateTXCountTimeIndex extends AbstractMigration { return null; const rawPath = await this.ldb.get(this.layout.wdb.P.encode(wid, hash)); + + if (!rawPath) + return null; + const account = encoding.readU32(rawPath, 0); return account; } @@ -1128,6 +1135,11 @@ class MigrateTXCountTimeIndex extends AbstractMigration { async getMedianTime(height) { const getBlockTime = async (height) => { + const cache = this.blockTimeCache.get(height); + + if (cache != null) + return cache; + const data = await this.ldb.get(this.layout.wdb.h.encode(height)); if (!data) @@ -1135,6 +1147,8 @@ class MigrateTXCountTimeIndex extends AbstractMigration { const time = encoding.readU64(data, 32); + this.blockTimeCache.set(height, time); + return time; }; diff --git a/test/util/migrations.js b/test/util/migrations.js index 02106d902..e3760ca54 100644 --- a/test/util/migrations.js +++ b/test/util/migrations.js @@ -235,7 +235,7 @@ exports.dumpChainDB = async (chaindb, prefixes) => { * @param {Boolean} options.throw - throw on error. * @param {Boolean} options.bail - bail on first error. * @param {Boolean} options.logErrors - log errors. - * @returns {Promise<[]String>} - errors. + * @returns {Promise} - errors. */ exports.checkEntries = async (ldb, options) => {