Skip to content

Commit

Permalink
wdb-migrations: add block time cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Sep 22, 2024
1 parent 9d21181 commit a03ddce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/wallet/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -808,6 +809,8 @@ class MigrateTXCountTimeIndex extends AbstractMigration {
this.unconfirmedBatchSize = 1000;

this.UNCONFIRMED_HEIGHT = 0xffffffff;

this.blockTimeCache = new LRU(50);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1128,13 +1135,20 @@ 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)
return null;

const time = encoding.readU64(data, 32);

this.blockTimeCache.set(height, time);

return time;
};

Expand Down
2 changes: 1 addition & 1 deletion test/util/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String[]>} - errors.
*/

exports.checkEntries = async (ldb, options) => {
Expand Down

0 comments on commit a03ddce

Please sign in to comment.