From 42c60fd99125731b3ab670df67c0bf2ee9e184a2 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 4 Jun 2024 20:57:40 +0000 Subject: [PATCH] Defer db access to fix failing tests --- backend/src/api/mempool-blocks.ts | 17 ++++++++--------- backend/src/database.ts | 3 +-- backend/src/index.ts | 2 ++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/src/api/mempool-blocks.ts b/backend/src/api/mempool-blocks.ts index 517ce7957..fdd26799b 100644 --- a/backend/src/api/mempool-blocks.ts +++ b/backend/src/api/mempool-blocks.ts @@ -24,15 +24,6 @@ class MempoolBlocks { private pools: { [id: number]: PoolTag } = {}; - constructor() { - PoolsRepository.$getPools().then(allPools => { - this.pools = {}; - for (const pool of allPools) { - this.pools[pool.uniqueId] = pool; - } - }); - } - public getMempoolBlocks(): MempoolBlock[] { return this.mempoolBlocks.map((block) => { return { @@ -54,6 +45,14 @@ class MempoolBlocks { return this.mempoolBlockDeltas; } + public async updatePools$(): Promise { + const allPools = await PoolsRepository.$getPools(); + this.pools = {}; + for (const pool of allPools) { + this.pools[pool.uniqueId] = pool; + } + } + private calculateMempoolDeltas(prevBlocks: MempoolBlockWithTransactions[], mempoolBlocks: MempoolBlockWithTransactions[]): MempoolBlockDelta[] { const mempoolBlockDeltas: MempoolBlockDelta[] = []; for (let i = 0; i < Math.max(mempoolBlocks.length, prevBlocks.length); i++) { diff --git a/backend/src/database.ts b/backend/src/database.ts index 05f624ff4..595b88c78 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -2,8 +2,7 @@ import * as fs from 'fs'; import path from 'path'; import config from './config'; import { createPool, Pool, PoolConnection } from 'mysql2/promise'; -import { LogLevel } from './logger'; -import logger from './logger'; +import logger, { LogLevel } from './logger'; import { FieldPacket, OkPacket, PoolOptions, ResultSetHeader, RowDataPacket } from 'mysql2/typings/mysql'; import { execSync } from 'child_process'; diff --git a/backend/src/index.ts b/backend/src/index.ts index df9f7dc65..38bb07383 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -45,6 +45,7 @@ import bitcoinCoreRoutes from './api/bitcoin/bitcoin-core.routes'; import bitcoinSecondClient from './api/bitcoin/bitcoin-second-client'; import accelerationRoutes from './api/acceleration/acceleration.routes'; import aboutRoutes from './api/about.routes'; +import mempoolBlocks from './api/mempool-blocks'; class Server { private wss: WebSocket.Server | undefined; @@ -149,6 +150,7 @@ class Server { await poolsUpdater.updatePoolsJson(); // Needs to be done before loading the disk cache because we sometimes wipe it await syncAssets.syncAssets$(); + await mempoolBlocks.updatePools$(); if (config.MEMPOOL.ENABLED) { if (config.MEMPOOL.CACHE_ENABLED) { await diskCache.$loadMempoolCache();