Improved block fetching performance.

This commit is contained in:
softsimon 2020-12-30 01:47:07 +07:00
parent 62c78f5b08
commit 5390629e41
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 14 additions and 16 deletions

View File

@ -2,13 +2,13 @@ import config from '../config';
import bitcoinApi from './bitcoin/bitcoin-api-factory';
import logger from '../logger';
import memPool from './mempool';
import { BlockExtended, TransactionExtended, TransactionMinerInfo } from '../mempool.interfaces';
import { BlockExtended, TransactionExtended } from '../mempool.interfaces';
import { Common } from './common';
import diskCache from './disk-cache';
import transactionUtils from './transaction-utils';
class Blocks {
private static KEEP_BLOCK_AMOUNT = 8;
private static INITIAL_BLOCK_AMOUNT = 8;
private blocks: BlockExtended[] = [];
private currentBlockHeight = 0;
private lastDifficultyAdjustmentTime = 0;
@ -32,14 +32,14 @@ class Blocks {
const blockHeightTip = await bitcoinApi.$getBlockHeightTip();
if (this.blocks.length === 0) {
this.currentBlockHeight = blockHeightTip - Blocks.KEEP_BLOCK_AMOUNT;
this.currentBlockHeight = blockHeightTip - Blocks.INITIAL_BLOCK_AMOUNT;
} else {
this.currentBlockHeight = this.blocks[this.blocks.length - 1].height;
}
if (blockHeightTip - this.currentBlockHeight > Blocks.KEEP_BLOCK_AMOUNT * 2) {
logger.info(`${blockHeightTip - this.currentBlockHeight} blocks since tip. Fast forwarding to the ${Blocks.KEEP_BLOCK_AMOUNT} recent blocks`);
this.currentBlockHeight = blockHeightTip - Blocks.KEEP_BLOCK_AMOUNT;
if (blockHeightTip - this.currentBlockHeight > Blocks.INITIAL_BLOCK_AMOUNT * 2) {
logger.info(`${blockHeightTip - this.currentBlockHeight} blocks since tip. Fast forwarding to the ${Blocks.INITIAL_BLOCK_AMOUNT} recent blocks`);
this.currentBlockHeight = blockHeightTip - Blocks.INITIAL_BLOCK_AMOUNT;
}
if (!this.lastDifficultyAdjustmentTime) {
@ -109,8 +109,8 @@ class Blocks {
}
this.blocks.push(blockExtended);
if (this.blocks.length > Blocks.KEEP_BLOCK_AMOUNT) {
this.blocks = this.blocks.slice(-Blocks.KEEP_BLOCK_AMOUNT);
if (this.blocks.length > Blocks.INITIAL_BLOCK_AMOUNT * 4) {
this.blocks = this.blocks.slice(-Blocks.INITIAL_BLOCK_AMOUNT * 4);
}
if (this.newBlockCallbacks.length) {

View File

@ -77,7 +77,7 @@ class WebsocketHandler {
}
if (parsedMessage.action === 'init') {
const _blocks = blocks.getBlocks();
const _blocks = blocks.getBlocks().slice(-8);
if (!_blocks) {
return;
}
@ -119,7 +119,7 @@ class WebsocketHandler {
getInitData(_blocks?: BlockExtended[]) {
if (!_blocks) {
_blocks = blocks.getBlocks();
_blocks = blocks.getBlocks().slice(-8);
}
return {
'mempoolInfo': memPool.getMempoolInfo(),

View File

@ -554,12 +554,10 @@ class Routes {
public async getBlocks(req: Request, res: Response) {
try {
const returnBlocks: IEsploraApi.Block[] = [];
const latestBlockHeight = blocks.getCurrentBlockHeight();
const fromHeight = parseInt(req.params.height, 10) || latestBlockHeight;
const localBlocks = blocks.getBlocks();
const fromHeight = parseInt(req.params.height, 10) || blocks.getCurrentBlockHeight();
// See if block hight exist in local cache to skip the hash lookup
const blockByHeight = localBlocks.find((b) => b.height === fromHeight);
// Check if block height exist in local cache to skip the hash lookup
const blockByHeight = blocks.getBlocks().find((b) => b.height === fromHeight);
let startFromHash: string | null = null;
if (blockByHeight) {
startFromHash = blockByHeight.id;
@ -569,7 +567,7 @@ class Routes {
let nextHash = startFromHash;
for (let i = 0; i < 10; i++) {
const localBlock = localBlocks.find((b) => b.id === nextHash);
const localBlock = blocks.getBlocks().find((b) => b.id === nextHash);
if (localBlock) {
returnBlocks.push(localBlock);
nextHash = localBlock.previousblockhash;