diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000..6eac74517 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +backend/src/api/database-migration.ts @wiz @softsimon diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index 2fc497650..c6323d041 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -6,7 +6,7 @@ import websocketHandler from '../websocket-handler'; import mempool from '../mempool'; import feeApi from '../fee-api'; import mempoolBlocks from '../mempool-blocks'; -import bitcoinApi from './bitcoin-api-factory'; +import bitcoinApi, { bitcoinCoreApi } from './bitcoin-api-factory'; import { Common } from '../common'; import backendInfo from '../backend-info'; import transactionUtils from '../transaction-utils'; @@ -220,18 +220,17 @@ class BitcoinRoutes { let cpfpInfo; if (config.DATABASE.ENABLED) { cpfpInfo = await transactionRepository.$getCpfpInfo(req.params.txId); + } + if (cpfpInfo) { + res.json(cpfpInfo); + return; } else { res.json({ ancestors: [] }); return; } - if (cpfpInfo) { - res.json(cpfpInfo); - return; - } } - res.status(404).send(`Transaction has no CPFP info available.`); } private getBackendInfo(req: Request, res: Response) { @@ -469,7 +468,7 @@ class BitcoinRoutes { returnBlocks.push(localBlock); nextHash = localBlock.previousblockhash; } else { - const block = await bitcoinApi.$getBlock(nextHash); + const block = await bitcoinCoreApi.$getBlock(nextHash); returnBlocks.push(block); nextHash = block.previousblockhash; } @@ -652,7 +651,7 @@ class BitcoinRoutes { if (result) { res.json(result); } else { - res.status(404).send('not found'); + res.status(204).send(); } } catch (e) { res.status(500).send(e instanceof Error ? e.message : e); diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index aa33f1ff7..ea2985b4f 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -1,5 +1,5 @@ import config from '../config'; -import bitcoinApi from './bitcoin/bitcoin-api-factory'; +import bitcoinApi, { bitcoinCoreApi } from './bitcoin/bitcoin-api-factory'; import logger from '../logger'; import memPool from './mempool'; import { BlockExtended, BlockExtension, BlockSummary, PoolTag, TransactionExtended, TransactionStripped, TransactionMinerInfo } from '../mempool.interfaces'; @@ -484,7 +484,7 @@ class Blocks { loadingIndicators.setProgress('block-indexing', progress, false); } const blockHash = await bitcoinApi.$getBlockHash(blockHeight); - const block: IEsploraApi.Block = await bitcoinApi.$getBlock(blockHash); + const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(blockHash); const transactions = await this.$getTransactionsExtended(blockHash, block.height, true, true); const blockExtended = await this.$getBlockExtended(block, transactions); @@ -532,13 +532,13 @@ class Blocks { if (blockchainInfo.blocks === blockchainInfo.headers) { const heightDiff = blockHeightTip % 2016; const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff); - const block: IEsploraApi.Block = await bitcoinApi.$getBlock(blockHash); + const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(blockHash); this.lastDifficultyAdjustmentTime = block.timestamp; this.currentDifficulty = block.difficulty; if (blockHeightTip >= 2016) { const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 2016); - const previousPeriodBlock: IEsploraApi.Block = await bitcoinApi.$getBlock(previousPeriodBlockHash); + const previousPeriodBlock: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(previousPeriodBlockHash); this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100; logger.debug(`Initial difficulty adjustment data set.`); } @@ -662,7 +662,7 @@ class Blocks { } const blockHash = await bitcoinApi.$getBlockHash(height); - const block: IEsploraApi.Block = await bitcoinApi.$getBlock(blockHash); + const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(blockHash); const transactions = await this.$getTransactionsExtended(blockHash, block.height, true); const blockExtended = await this.$getBlockExtended(block, transactions); @@ -685,11 +685,11 @@ class Blocks { // Not Bitcoin network, return the block as it from the bitcoin backend if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) { - return await bitcoinApi.$getBlock(hash); + return await bitcoinCoreApi.$getBlock(hash); } // Bitcoin network, add our custom data on top - const block: IEsploraApi.Block = await bitcoinApi.$getBlock(hash); + const block: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(hash); return await this.$indexBlock(block.height); } diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index ad4eccabe..f762cfc2c 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -175,6 +175,7 @@ export class Common { case '1y': return '1 YEAR'; case '2y': return '2 YEAR'; case '3y': return '3 YEAR'; + case '4y': return '4 YEAR'; default: return null; } } diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index d40637e74..1ef31c90b 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -7,7 +7,7 @@ import cpfpRepository from '../repositories/CpfpRepository'; import { RowDataPacket } from 'mysql2'; class DatabaseMigration { - private static currentVersion = 57; + private static currentVersion = 58; private queryTimeout = 3600_000; private statisticsAddedIndexed = false; private uniqueLogs: string[] = []; @@ -505,6 +505,11 @@ class DatabaseMigration { await this.$executeQuery(`ALTER TABLE nodes MODIFY updated_at datetime NULL`); await this.updateToSchemaVersion(57); } + + if (databaseSchemaVersion < 58) { + // We only run some migration queries for this version + await this.updateToSchemaVersion(58); + } } /** @@ -632,6 +637,11 @@ class DatabaseMigration { queries.push(`INSERT INTO state(name, number, string) VALUES ('last_weekly_hashrates_indexing', 0, NULL)`); } + if (version < 58) { + queries.push(`DELETE FROM state WHERE name = 'last_hashrates_indexing'`); + queries.push(`DELETE FROM state WHERE name = 'last_weekly_hashrates_indexing'`); + } + return queries; } @@ -1023,10 +1033,11 @@ class DatabaseMigration { await this.$executeQuery(`TRUNCATE blocks`); await this.$executeQuery(`TRUNCATE hashrates`); + await this.$executeQuery(`TRUNCATE difficulty_adjustments`); await this.$executeQuery('DELETE FROM `pools`'); await this.$executeQuery('ALTER TABLE pools AUTO_INCREMENT = 1'); await this.$executeQuery(`UPDATE state SET string = NULL WHERE name = 'pools_json_sha'`); -} + } private async $convertCompactCpfpTables(): Promise { try { diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index 83d37fe3f..01bbb4d3f 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -62,7 +62,7 @@ class DiskCache { } wipeCache() { - logger.notice(`Wipping nodejs backend cache/cache*.json files`); + logger.notice(`Wiping nodejs backend cache/cache*.json files`); try { fs.unlinkSync(DiskCache.FILE_NAME); } catch (e: any) { diff --git a/backend/src/api/mempool-blocks.ts b/backend/src/api/mempool-blocks.ts index 0df125d55..3c2feb0e2 100644 --- a/backend/src/api/mempool-blocks.ts +++ b/backend/src/api/mempool-blocks.ts @@ -97,14 +97,14 @@ class MempoolBlocks { blockSize += tx.size; transactions.push(tx); } else { - mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length)); + mempoolBlocks.push(this.dataToMempoolBlocks(transactions, mempoolBlocks.length)); blockWeight = tx.weight; blockSize = tx.size; transactions = [tx]; } }); if (transactions.length) { - mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length)); + mempoolBlocks.push(this.dataToMempoolBlocks(transactions, mempoolBlocks.length)); } return mempoolBlocks; @@ -281,7 +281,7 @@ class MempoolBlocks { const mempoolBlocks = blocks.map((transactions, blockIndex) => { return this.dataToMempoolBlocks(transactions.map(tx => { return mempool[tx.txid] || null; - }).filter(tx => !!tx), undefined, undefined, blockIndex); + }).filter(tx => !!tx), blockIndex); }); if (saveResults) { @@ -293,18 +293,17 @@ class MempoolBlocks { return mempoolBlocks; } - private dataToMempoolBlocks(transactions: TransactionExtended[], - blockSize: number | undefined, blockWeight: number | undefined, blocksIndex: number): MempoolBlockWithTransactions { - let totalSize = blockSize || 0; - let totalWeight = blockWeight || 0; - if (blockSize === undefined && blockWeight === undefined) { - totalSize = 0; - totalWeight = 0; - transactions.forEach(tx => { - totalSize += tx.size; - totalWeight += tx.weight; - }); - } + private dataToMempoolBlocks(transactions: TransactionExtended[], blocksIndex: number): MempoolBlockWithTransactions { + let totalSize = 0; + let totalWeight = 0; + const fitTransactions: TransactionExtended[] = []; + transactions.forEach(tx => { + totalSize += tx.size; + totalWeight += tx.weight; + if ((totalWeight + tx.weight) <= config.MEMPOOL.BLOCK_WEIGHT_UNITS * 1.2) { + fitTransactions.push(tx); + } + }); let rangeLength = 4; if (blocksIndex === 0) { rangeLength = 8; @@ -322,7 +321,7 @@ class MempoolBlocks { medianFee: Common.percentile(transactions.map((tx) => tx.effectiveFeePerVsize), config.MEMPOOL.RECOMMENDED_FEE_PERCENTILE), feeRange: Common.getFeesInRange(transactions, rangeLength), transactionIds: transactions.map((tx) => tx.txid), - transactions: transactions.map((tx) => Common.stripTransaction(tx)), + transactions: fitTransactions.map((tx) => Common.stripTransaction(tx)), }; } } diff --git a/backend/src/api/mining/mining-routes.ts b/backend/src/api/mining/mining-routes.ts index f7f392068..0198f9ab4 100644 --- a/backend/src/api/mining/mining-routes.ts +++ b/backend/src/api/mining/mining-routes.ts @@ -263,7 +263,7 @@ class MiningRoutes { const audit = await BlocksAuditsRepository.$getBlockAudit(req.params.hash); if (!audit) { - res.status(404).send(`This block has not been audited.`); + res.status(204).send(`This block has not been audited.`); return; } diff --git a/backend/src/api/mining/mining.ts b/backend/src/api/mining/mining.ts index e9b569485..8b4abb0d6 100644 --- a/backend/src/api/mining/mining.ts +++ b/backend/src/api/mining/mining.ts @@ -11,14 +11,13 @@ import DifficultyAdjustmentsRepository from '../../repositories/DifficultyAdjust import config from '../../config'; import BlocksAuditsRepository from '../../repositories/BlocksAuditsRepository'; import PricesRepository from '../../repositories/PricesRepository'; -import bitcoinApiFactory from '../bitcoin/bitcoin-api-factory'; +import { bitcoinCoreApi } from '../bitcoin/bitcoin-api-factory'; import { IEsploraApi } from '../bitcoin/esplora-api.interface'; class Mining { - blocksPriceIndexingRunning = false; - - constructor() { - } + private blocksPriceIndexingRunning = false; + public lastHashrateIndexingDate: number | null = null; + public lastWeeklyHashrateIndexingDate: number | null = null; /** * Get historical block predictions match rate @@ -118,7 +117,7 @@ class Mining { poolsStatistics['lastEstimatedHashrate'] = await bitcoinClient.getNetworkHashPs(totalBlock24h); } catch (e) { poolsStatistics['lastEstimatedHashrate'] = 0; - logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate'); + logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate', logger.tags.mining); } return poolsStatistics; @@ -146,7 +145,7 @@ class Mining { try { currentEstimatedHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h); } catch (e) { - logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate'); + logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate', logger.tags.mining); } return { @@ -178,20 +177,21 @@ class Mining { */ public async $generatePoolHashrateHistory(): Promise { const now = new Date(); - const lastestRunDate = await HashratesRepository.$getLatestRun('last_weekly_hashrates_indexing'); // Run only if: - // * lastestRunDate is set to 0 (node backend restart, reorg) + // * this.lastWeeklyHashrateIndexingDate is set to null (node backend restart, reorg) // * we started a new week (around Monday midnight) - const runIndexing = lastestRunDate === 0 || now.getUTCDay() === 1 && lastestRunDate !== now.getUTCDate(); + const runIndexing = this.lastWeeklyHashrateIndexingDate === null || + now.getUTCDay() === 1 && this.lastWeeklyHashrateIndexingDate !== now.getUTCDate(); if (!runIndexing) { + logger.debug(`Pool hashrate history indexing is up to date, nothing to do`, logger.tags.mining); return; } try { const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp; - const genesisBlock: IEsploraApi.Block = await bitcoinApiFactory.$getBlock(await bitcoinClient.getBlockHash(0)); + const genesisBlock: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(await bitcoinClient.getBlockHash(0)); const genesisTimestamp = genesisBlock.timestamp * 1000; const indexedTimestamp = await HashratesRepository.$getWeeklyHashrateTimestamps(); @@ -208,7 +208,7 @@ class Mining { const startedAt = new Date().getTime() / 1000; let timer = new Date().getTime() / 1000; - logger.debug(`Indexing weekly mining pool hashrate`); + logger.debug(`Indexing weekly mining pool hashrate`, logger.tags.mining); loadingIndicators.setProgress('weekly-hashrate-indexing', 0); while (toTimestamp > genesisTimestamp && toTimestamp > oldestConsecutiveBlockTimestamp) { @@ -245,7 +245,7 @@ class Mining { }); } - newlyIndexed += hashrates.length; + newlyIndexed += hashrates.length / Math.max(1, pools.length); await HashratesRepository.$saveHashrates(hashrates); hashrates.length = 0; } @@ -256,7 +256,7 @@ class Mining { const weeksPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds)); const progress = Math.round(totalIndexed / totalWeekIndexed * 10000) / 100; const formattedDate = new Date(fromTimestamp).toUTCString(); - logger.debug(`Getting weekly pool hashrate for ${formattedDate} | ~${weeksPerSeconds.toFixed(2)} weeks/sec | total: ~${totalIndexed}/${Math.round(totalWeekIndexed)} (${progress}%) | elapsed: ${runningFor} seconds`); + logger.debug(`Getting weekly pool hashrate for ${formattedDate} | ~${weeksPerSeconds.toFixed(2)} weeks/sec | total: ~${totalIndexed}/${Math.round(totalWeekIndexed)} (${progress}%) | elapsed: ${runningFor} seconds`, logger.tags.mining); timer = new Date().getTime() / 1000; indexedThisRun = 0; loadingIndicators.setProgress('weekly-hashrate-indexing', progress, false); @@ -266,16 +266,16 @@ class Mining { ++indexedThisRun; ++totalIndexed; } - await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', new Date().getUTCDate()); + this.lastWeeklyHashrateIndexingDate = new Date().getUTCDate(); if (newlyIndexed > 0) { - logger.notice(`Weekly mining pools hashrates indexing completed: indexed ${newlyIndexed}`, logger.tags.mining); + logger.info(`Weekly mining pools hashrates indexing completed: indexed ${newlyIndexed} weeks`, logger.tags.mining); } else { - logger.debug(`Weekly mining pools hashrates indexing completed: indexed ${newlyIndexed}`, logger.tags.mining); + logger.debug(`Weekly mining pools hashrates indexing completed: indexed ${newlyIndexed} weeks`, logger.tags.mining); } loadingIndicators.setProgress('weekly-hashrate-indexing', 100); } catch (e) { loadingIndicators.setProgress('weekly-hashrate-indexing', 100); - logger.err(`Weekly mining pools hashrates indexing failed. Trying again in 10 seconds. Reason: ${(e instanceof Error ? e.message : e)}`); + logger.err(`Weekly mining pools hashrates indexing failed. Trying again in 10 seconds. Reason: ${(e instanceof Error ? e.message : e)}`, logger.tags.mining); throw e; } } @@ -285,16 +285,16 @@ class Mining { */ public async $generateNetworkHashrateHistory(): Promise { // We only run this once a day around midnight - const latestRunDate = await HashratesRepository.$getLatestRun('last_hashrates_indexing'); - const now = new Date().getUTCDate(); - if (now === latestRunDate) { + const today = new Date().getUTCDate(); + if (today === this.lastHashrateIndexingDate) { + logger.debug(`Network hashrate history indexing is up to date, nothing to do`, logger.tags.mining); return; } const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlock()).timestamp; try { - const genesisBlock: IEsploraApi.Block = await bitcoinApiFactory.$getBlock(await bitcoinClient.getBlockHash(0)); + const genesisBlock: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(await bitcoinClient.getBlockHash(0)); const genesisTimestamp = genesisBlock.timestamp * 1000; const indexedTimestamp = (await HashratesRepository.$getRawNetworkDailyHashrate(null)).map(hashrate => hashrate.timestamp); const lastMidnight = this.getDateMidnight(new Date()); @@ -308,7 +308,7 @@ class Mining { const startedAt = new Date().getTime() / 1000; let timer = new Date().getTime() / 1000; - logger.debug(`Indexing daily network hashrate`); + logger.debug(`Indexing daily network hashrate`, logger.tags.mining); loadingIndicators.setProgress('daily-hashrate-indexing', 0); while (toTimestamp > genesisTimestamp && toTimestamp > oldestConsecutiveBlockTimestamp) { @@ -346,7 +346,7 @@ class Mining { const daysPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds)); const progress = Math.round(totalIndexed / totalDayIndexed * 10000) / 100; const formattedDate = new Date(fromTimestamp).toUTCString(); - logger.debug(`Getting network daily hashrate for ${formattedDate} | ~${daysPerSeconds.toFixed(2)} days/sec | total: ~${totalIndexed}/${Math.round(totalDayIndexed)} (${progress}%) | elapsed: ${runningFor} seconds`); + logger.debug(`Getting network daily hashrate for ${formattedDate} | ~${daysPerSeconds.toFixed(2)} days/sec | total: ~${totalIndexed}/${Math.round(totalDayIndexed)} (${progress}%) | elapsed: ${runningFor} seconds`, logger.tags.mining); timer = new Date().getTime() / 1000; indexedThisRun = 0; loadingIndicators.setProgress('daily-hashrate-indexing', progress); @@ -371,16 +371,16 @@ class Mining { newlyIndexed += hashrates.length; await HashratesRepository.$saveHashrates(hashrates); - await HashratesRepository.$setLatestRun('last_hashrates_indexing', new Date().getUTCDate()); + this.lastHashrateIndexingDate = new Date().getUTCDate(); if (newlyIndexed > 0) { - logger.notice(`Daily network hashrate indexing completed: indexed ${newlyIndexed} days`, logger.tags.mining); + logger.info(`Daily network hashrate indexing completed: indexed ${newlyIndexed} days`, logger.tags.mining); } else { logger.debug(`Daily network hashrate indexing completed: indexed ${newlyIndexed} days`, logger.tags.mining); } loadingIndicators.setProgress('daily-hashrate-indexing', 100); } catch (e) { loadingIndicators.setProgress('daily-hashrate-indexing', 100); - logger.err(`Daily network hashrate indexing failed. Trying again in 10 seconds. Reason: ${(e instanceof Error ? e.message : e)}`, logger.tags.mining); + logger.err(`Daily network hashrate indexing failed. Trying again later. Reason: ${(e instanceof Error ? e.message : e)}`, logger.tags.mining); throw e; } } @@ -396,7 +396,7 @@ class Mining { } const blocks: any = await BlocksRepository.$getBlocksDifficulty(); - const genesisBlock: IEsploraApi.Block = await bitcoinApiFactory.$getBlock(await bitcoinClient.getBlockHash(0)); + const genesisBlock: IEsploraApi.Block = await bitcoinCoreApi.$getBlock(await bitcoinClient.getBlockHash(0)); let currentDifficulty = genesisBlock.difficulty; let totalIndexed = 0; @@ -446,13 +446,13 @@ class Mining { const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer)); if (elapsedSeconds > 5) { const progress = Math.round(totalBlockChecked / blocks.length * 100); - logger.info(`Indexing difficulty adjustment at block #${block.height} | Progress: ${progress}%`); + logger.info(`Indexing difficulty adjustment at block #${block.height} | Progress: ${progress}%`, logger.tags.mining); timer = new Date().getTime() / 1000; } } if (totalIndexed > 0) { - logger.notice(`Indexed ${totalIndexed} difficulty adjustments`, logger.tags.mining); + logger.info(`Indexed ${totalIndexed} difficulty adjustments`, logger.tags.mining); } else { logger.debug(`Indexed ${totalIndexed} difficulty adjustments`, logger.tags.mining); } @@ -499,7 +499,7 @@ class Mining { if (blocksWithoutPrices.length > 200000) { logStr += ` | Progress ${Math.round(totalInserted / blocksWithoutPrices.length * 100)}%`; } - logger.debug(logStr); + logger.debug(logStr, logger.tags.mining); await BlocksRepository.$saveBlockPrices(blocksPrices); blocksPrices.length = 0; } @@ -511,7 +511,7 @@ class Mining { if (blocksWithoutPrices.length > 200000) { logStr += ` | Progress ${Math.round(totalInserted / blocksWithoutPrices.length * 100)}%`; } - logger.debug(logStr); + logger.debug(logStr, logger.tags.mining); await BlocksRepository.$saveBlockPrices(blocksPrices); } } catch (e) { @@ -568,6 +568,7 @@ class Mining { private getTimeRange(interval: string | null, scale = 1): number { switch (interval) { + case '4y': return 43200 * scale; // 12h case '3y': return 43200 * scale; // 12h case '2y': return 28800 * scale; // 8h case '1y': return 28800 * scale; // 8h diff --git a/backend/src/api/pools-parser.ts b/backend/src/api/pools-parser.ts index f17df385a..f94c147a2 100644 --- a/backend/src/api/pools-parser.ts +++ b/backend/src/api/pools-parser.ts @@ -39,6 +39,10 @@ class PoolsParser { * @param pools */ public async migratePoolsJson(): Promise { + // We also need to wipe the backend cache to make sure we don't serve blocks with + // the wrong mining pool (usually happen with unknown blocks) + diskCache.wipeCache(); + await this.$insertUnknownPool(); for (const pool of this.miningPools) { @@ -142,10 +146,6 @@ class PoolsParser { WHERE pool_id = ?`, [pool.id] ); - - // We also need to wipe the backend cache to make sure we don't serve blocks with - // the wrong mining pool (usually happen with unknown blocks) - diskCache.wipeCache(); } private async $deleteUnknownBlocks(): Promise { @@ -156,10 +156,6 @@ class PoolsParser { WHERE pool_id = ? AND height >= 130635`, [unknownPool[0].id] ); - - // We also need to wipe the backend cache to make sure we don't serve blocks with - // the wrong mining pool (usually happen with unknown blocks) - diskCache.wipeCache(); } } diff --git a/backend/src/api/statistics/statistics-api.ts b/backend/src/api/statistics/statistics-api.ts index 56a868f8f..1e8b0b7bb 100644 --- a/backend/src/api/statistics/statistics-api.ts +++ b/backend/src/api/statistics/statistics-api.ts @@ -375,6 +375,17 @@ class StatisticsApi { } } + public async $list4Y(): Promise { + try { + const query = this.getQueryForDays(43200, '4 YEAR'); // 12h interval + const [rows] = await DB.query({ sql: query, timeout: this.queryTimeout }); + return this.mapStatisticToOptimizedStatistic(rows as Statistic[]); + } catch (e) { + logger.err('$list4Y() error' + (e instanceof Error ? e.message : e)); + return []; + } + } + private mapStatisticToOptimizedStatistic(statistic: Statistic[]): OptimizedStatistic[] { return statistic.map((s) => { return { diff --git a/backend/src/api/statistics/statistics.routes.ts b/backend/src/api/statistics/statistics.routes.ts index 4b1b91ce9..2a5871dd6 100644 --- a/backend/src/api/statistics/statistics.routes.ts +++ b/backend/src/api/statistics/statistics.routes.ts @@ -14,10 +14,11 @@ class StatisticsRoutes { .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', this.$getStatisticsByTime.bind(this, '1y')) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2y', this.$getStatisticsByTime.bind(this, '2y')) .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3y', this.$getStatisticsByTime.bind(this, '3y')) + .get(config.MEMPOOL.API_URL_PREFIX + 'statistics/4y', this.$getStatisticsByTime.bind(this, '4y')) ; } - private async $getStatisticsByTime(time: '2h' | '24h' | '1w' | '1m' | '3m' | '6m' | '1y' | '2y' | '3y', req: Request, res: Response) { + private async $getStatisticsByTime(time: '2h' | '24h' | '1w' | '1m' | '3m' | '6m' | '1y' | '2y' | '3y' | '4y', req: Request, res: Response) { res.header('Pragma', 'public'); res.header('Cache-control', 'public'); res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString()); @@ -54,6 +55,9 @@ class StatisticsRoutes { case '3y': result = await statisticsApi.$list3Y(); break; + case '4y': + result = await statisticsApi.$list4Y(); + break; default: result = await statisticsApi.$list2H(); } diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index c1c3b3995..a96264825 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -1,8 +1,8 @@ import logger from '../logger'; import * as WebSocket from 'ws'; import { - BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, MempoolBlockDelta, - OptimizedStatistic, ILoadingIndicators, IConversionRates + BlockExtended, TransactionExtended, WebsocketResponse, + OptimizedStatistic, ILoadingIndicators } from '../mempool.interfaces'; import blocks from './blocks'; import memPool from './mempool'; @@ -20,6 +20,7 @@ import BlocksSummariesRepository from '../repositories/BlocksSummariesRepository import Audit from './audit'; import { deepClone } from '../utils/clone'; import priceUpdater from '../tasks/price-updater'; +import { ApiPrice } from '../repositories/PricesRepository'; class WebsocketHandler { private wss: WebSocket.Server | undefined; @@ -193,7 +194,7 @@ class WebsocketHandler { }); } - handleNewConversionRates(conversionRates: IConversionRates) { + handleNewConversionRates(conversionRates: ApiPrice) { if (!this.wss) { throw new Error('WebSocket.Server is not set'); } @@ -214,7 +215,7 @@ class WebsocketHandler { 'mempoolInfo': memPool.getMempoolInfo(), 'vBytesPerSecond': memPool.getVBytesPerSecond(), 'blocks': _blocks, - 'conversions': priceUpdater.latestPrices, + 'conversions': priceUpdater.getLatestPrices(), 'mempool-blocks': mempoolBlocks.getMempoolBlocks(), 'transactions': memPool.getLatestTransactions(), 'backendInfo': backendInfo.getBackendInfo(), diff --git a/backend/src/index.ts b/backend/src/index.ts index 812b49e15..fbe9c08c2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -38,6 +38,8 @@ import forensicsService from './tasks/lightning/forensics.service'; import priceUpdater from './tasks/price-updater'; import chainTips from './api/chain-tips'; import { AxiosError } from 'axios'; +import v8 from 'v8'; +import { formatBytes, getBytesUnit } from './utils/format'; class Server { private wss: WebSocket.Server | undefined; @@ -45,6 +47,11 @@ class Server { private app: Application; private currentBackendRetryInterval = 5; + private maxHeapSize: number = 0; + private heapLogInterval: number = 60; + private warnedHeapCritical: boolean = false; + private lastHeapLogTime: number | null = null; + constructor() { this.app = express(); @@ -87,9 +94,6 @@ class Server { await databaseMigration.$blocksReindexingTruncate(); } await databaseMigration.$initializeOrMigrateDatabase(); - if (Common.indexingEnabled()) { - await indexer.$resetHashratesIndexingState(); - } } catch (e) { throw new Error(e instanceof Error ? e.message : 'Error'); } @@ -113,6 +117,7 @@ class Server { this.setUpWebsocketHandling(); + await poolsUpdater.updatePoolsJson(); // Needs to be done before loading the disk cache because we sometimes wipe it await syncAssets.syncAssets$(); if (config.MEMPOOL.ENABLED) { diskCache.loadMempoolCache(); @@ -139,6 +144,8 @@ class Server { this.runMainUpdateLoop(); } + setInterval(() => { this.healthCheck(); }, 2500); + if (config.BISQ.ENABLED) { bisq.startBisqService(); bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price)); @@ -171,7 +178,6 @@ class Server { logger.debug(msg); } } - await poolsUpdater.updatePoolsJson(); await blocks.$updateBlocks(); await memPool.$updateMempool(); indexer.$run(); @@ -258,6 +264,26 @@ class Server { channelsRoutes.initRoutes(this.app); } } + + healthCheck(): void { + const now = Date.now(); + const stats = v8.getHeapStatistics(); + this.maxHeapSize = Math.max(stats.used_heap_size, this.maxHeapSize); + const warnThreshold = 0.8 * stats.heap_size_limit; + + const byteUnits = getBytesUnit(Math.max(this.maxHeapSize, stats.heap_size_limit)); + + if (!this.warnedHeapCritical && this.maxHeapSize > warnThreshold) { + this.warnedHeapCritical = true; + logger.warn(`Used ${(this.maxHeapSize / stats.heap_size_limit).toFixed(2)}% of heap limit (${formatBytes(this.maxHeapSize, byteUnits, true)} / ${formatBytes(stats.heap_size_limit, byteUnits)})!`); + } + if (this.lastHeapLogTime === null || (now - this.lastHeapLogTime) > (this.heapLogInterval * 1000)) { + logger.debug(`Memory usage: ${formatBytes(this.maxHeapSize, byteUnits)} / ${formatBytes(stats.heap_size_limit, byteUnits)}`); + this.warnedHeapCritical = false; + this.maxHeapSize = 0; + this.lastHeapLogTime = now; + } + } } ((): Server => new Server())(); diff --git a/backend/src/indexer.ts b/backend/src/indexer.ts index 41c8024e0..3b16ad155 100644 --- a/backend/src/indexer.ts +++ b/backend/src/indexer.ts @@ -3,7 +3,6 @@ import blocks from './api/blocks'; import mempool from './api/mempool'; import mining from './api/mining/mining'; import logger from './logger'; -import HashratesRepository from './repositories/HashratesRepository'; import bitcoinClient from './api/bitcoin/bitcoin-client'; import priceUpdater from './tasks/price-updater'; import PricesRepository from './repositories/PricesRepository'; @@ -77,13 +76,13 @@ class Indexer { this.tasksRunning.push(task); const lastestPriceId = await PricesRepository.$getLatestPriceId(); if (priceUpdater.historyInserted === false || lastestPriceId === null) { - logger.debug(`Blocks prices indexer is waiting for the price updater to complete`); + logger.debug(`Blocks prices indexer is waiting for the price updater to complete`, logger.tags.mining); setTimeout(() => { this.tasksRunning = this.tasksRunning.filter(runningTask => runningTask !== task); this.runSingleTask('blocksPrices'); }, 10000); } else { - logger.debug(`Blocks prices indexer will run now`); + logger.debug(`Blocks prices indexer will run now`, logger.tags.mining); await mining.$indexBlockPrices(); this.tasksRunning = this.tasksRunning.filter(runningTask => runningTask !== task); } @@ -113,7 +112,7 @@ class Indexer { this.runIndexer = false; this.indexerRunning = true; - logger.info(`Running mining indexer`); + logger.debug(`Running mining indexer`); await this.checkAvailableCoreIndexes(); @@ -123,7 +122,7 @@ class Indexer { const chainValid = await blocks.$generateBlockDatabase(); if (chainValid === false) { // Chain of block hash was invalid, so we need to reindex. Stop here and continue at the next iteration - logger.warn(`The chain of block hash is invalid, re-indexing invalid data in 10 seconds.`); + logger.warn(`The chain of block hash is invalid, re-indexing invalid data in 10 seconds.`, logger.tags.mining); setTimeout(() => this.reindex(), 10000); this.indexerRunning = false; return; @@ -131,7 +130,6 @@ class Indexer { this.runSingleTask('blocksPrices'); await mining.$indexDifficultyAdjustments(); - await this.$resetHashratesIndexingState(); // TODO - Remove this as it's not efficient await mining.$generateNetworkHashrateHistory(); await mining.$generatePoolHashrateHistory(); await blocks.$generateBlocksSummariesDatabase(); @@ -150,16 +148,6 @@ class Indexer { logger.debug(`Indexing completed. Next run planned at ${new Date(new Date().getTime() + runEvery).toUTCString()}`); setTimeout(() => this.reindex(), runEvery); } - - async $resetHashratesIndexingState(): Promise { - try { - await HashratesRepository.$setLatestRun('last_hashrates_indexing', 0); - await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', 0); - } catch (e) { - logger.err(`Cannot reset hashrate indexing timestamps. Reason: ` + (e instanceof Error ? e.message : e)); - throw e; - } - } } export default new Indexer(); diff --git a/backend/src/mempool.interfaces.ts b/backend/src/mempool.interfaces.ts index a7937e01d..8662770bc 100644 --- a/backend/src/mempool.interfaces.ts +++ b/backend/src/mempool.interfaces.ts @@ -293,7 +293,6 @@ interface RequiredParams { } export interface ILoadingIndicators { [name: string]: number; } -export interface IConversionRates { [currency: string]: number; } export interface IBackendInfo { hostname: string; diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index 80df1ac92..f2d0a283e 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -748,6 +748,7 @@ class BlocksRepository { SELECT height FROM compact_cpfp_clusters WHERE height <= ? AND height >= ? + GROUP BY height ORDER BY height DESC; `, [currentBlockHeight, minHeight]); diff --git a/backend/src/repositories/DifficultyAdjustmentsRepository.ts b/backend/src/repositories/DifficultyAdjustmentsRepository.ts index 910c65c10..0b19cc640 100644 --- a/backend/src/repositories/DifficultyAdjustmentsRepository.ts +++ b/backend/src/repositories/DifficultyAdjustmentsRepository.ts @@ -1,5 +1,4 @@ import { Common } from '../api/common'; -import config from '../config'; import DB from '../database'; import logger from '../logger'; import { IndexedDifficultyAdjustment } from '../mempool.interfaces'; @@ -21,9 +20,9 @@ class DifficultyAdjustmentsRepository { await DB.query(query, params); } catch (e: any) { if (e.errno === 1062) { // ER_DUP_ENTRY - This scenario is possible upon node backend restart - logger.debug(`Cannot save difficulty adjustment at block ${adjustment.height}, already indexed, ignoring`); + logger.debug(`Cannot save difficulty adjustment at block ${adjustment.height}, already indexed, ignoring`, logger.tags.mining); } else { - logger.err(`Cannot save difficulty adjustment at block ${adjustment.height}. Reason: ${e instanceof Error ? e.message : e}`); + logger.err(`Cannot save difficulty adjustment at block ${adjustment.height}. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.mining); throw e; } } @@ -55,7 +54,7 @@ class DifficultyAdjustmentsRepository { const [rows] = await DB.query(query); return rows as IndexedDifficultyAdjustment[]; } catch (e) { - logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -84,7 +83,7 @@ class DifficultyAdjustmentsRepository { const [rows] = await DB.query(query); return rows as IndexedDifficultyAdjustment[]; } catch (e) { - logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -94,27 +93,27 @@ class DifficultyAdjustmentsRepository { const [rows]: any[] = await DB.query(`SELECT height FROM difficulty_adjustments`); return rows.map(block => block.height); } catch (e: any) { - logger.err(`Cannot get difficulty adjustment block heights. Reason: ${e instanceof Error ? e.message : e}`); + logger.err(`Cannot get difficulty adjustment block heights. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.mining); throw e; } } public async $deleteAdjustementsFromHeight(height: number): Promise { try { - logger.info(`Delete newer difficulty adjustments from height ${height} from the database`); + logger.info(`Delete newer difficulty adjustments from height ${height} from the database`, logger.tags.mining); await DB.query(`DELETE FROM difficulty_adjustments WHERE height >= ?`, [height]); } catch (e: any) { - logger.err(`Cannot delete difficulty adjustments from the database. Reason: ${e instanceof Error ? e.message : e}`); + logger.err(`Cannot delete difficulty adjustments from the database. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.mining); throw e; } } public async $deleteLastAdjustment(): Promise { try { - logger.info(`Delete last difficulty adjustment from the database`); + logger.info(`Delete last difficulty adjustment from the database`, logger.tags.mining); await DB.query(`DELETE FROM difficulty_adjustments ORDER BY time LIMIT 1`); } catch (e: any) { - logger.err(`Cannot delete last difficulty adjustment from the database. Reason: ${e instanceof Error ? e.message : e}`); + logger.err(`Cannot delete last difficulty adjustment from the database. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.mining); throw e; } } diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index e5a193477..875f77b34 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -1,5 +1,6 @@ import { escape } from 'mysql2'; import { Common } from '../api/common'; +import mining from '../api/mining/mining'; import DB from '../database'; import logger from '../logger'; import PoolsRepository from './PoolsRepository'; @@ -24,7 +25,7 @@ class HashratesRepository { try { await DB.query(query); } catch (e: any) { - logger.err('Cannot save indexed hashrate into db. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot save indexed hashrate into db. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -50,7 +51,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query); return rows; } catch (e) { - logger.err('Cannot fetch network hashrate history. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot fetch network hashrate history. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -77,7 +78,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query); return rows; } catch (e) { - logger.err('Cannot fetch network hashrate history. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot fetch network hashrate history. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -92,7 +93,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query); return rows.map(row => row.timestamp); } catch (e) { - logger.err('Cannot retreive indexed weekly hashrate timestamps. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot retreive indexed weekly hashrate timestamps. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -127,7 +128,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query); return rows; } catch (e) { - logger.err('Cannot fetch weekly pools hashrate history. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot fetch weekly pools hashrate history. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -157,7 +158,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query, [pool.id]); boundaries = rows[0]; } catch (e) { - logger.err('Cannot fetch hashrate start/end timestamps for this pool. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot fetch hashrate start/end timestamps for this pool. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); } // Get hashrates entries between boundaries @@ -172,21 +173,7 @@ class HashratesRepository { const [rows]: any[] = await DB.query(query, [boundaries.firstTimestamp, boundaries.lastTimestamp, pool.id]); return rows; } catch (e) { - logger.err('Cannot fetch pool hashrate history for this pool. Reason: ' + (e instanceof Error ? e.message : e)); - throw e; - } - } - - /** - * Set latest run timestamp - */ - public async $setLatestRun(key: string, val: number) { - const query = `UPDATE state SET number = ? WHERE name = ?`; - - try { - await DB.query(query, [val, key]); - } catch (e) { - logger.err(`Cannot set last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err('Cannot fetch pool hashrate history for this pool. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -205,7 +192,7 @@ class HashratesRepository { } return rows[0]['number']; } catch (e) { - logger.err(`Cannot retrieve last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); + logger.err(`Cannot retrieve last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e), logger.tags.mining); throw e; } } @@ -214,7 +201,7 @@ class HashratesRepository { * Delete most recent data points for re-indexing */ public async $deleteLastEntries() { - logger.info(`Delete latest hashrates data points from the database`); + logger.info(`Delete latest hashrates data points from the database`, logger.tags.mining); try { const [rows]: any[] = await DB.query(`SELECT MAX(hashrate_timestamp) as timestamp FROM hashrates GROUP BY type`); @@ -222,10 +209,10 @@ class HashratesRepository { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp = ?`, [row.timestamp]); } // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRun('last_hashrates_indexing', 0); - await this.$setLatestRun('last_weekly_hashrates_indexing', 0); + mining.lastHashrateIndexingDate = null; + mining.lastWeeklyHashrateIndexingDate = null; } catch (e) { - logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); } } @@ -238,10 +225,10 @@ class HashratesRepository { try { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp >= FROM_UNIXTIME(?)`, [timestamp]); // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRun('last_hashrates_indexing', 0); - await this.$setLatestRun('last_weekly_hashrates_indexing', 0); + mining.lastHashrateIndexingDate = null; + mining.lastWeeklyHashrateIndexingDate = null; } catch (e) { - logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); + logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); } } } diff --git a/backend/src/repositories/PricesRepository.ts b/backend/src/repositories/PricesRepository.ts index 6493735ee..4cbc06afd 100644 --- a/backend/src/repositories/PricesRepository.ts +++ b/backend/src/repositories/PricesRepository.ts @@ -1,6 +1,5 @@ import DB from '../database'; import logger from '../logger'; -import { IConversionRates } from '../mempool.interfaces'; import priceUpdater from '../tasks/price-updater'; export interface ApiPrice { @@ -13,6 +12,16 @@ export interface ApiPrice { AUD: number, JPY: number, } +const ApiPriceFields = ` + UNIX_TIMESTAMP(time) as time, + USD, + EUR, + GBP, + CAD, + CHF, + AUD, + JPY +`; export interface ExchangeRates { USDEUR: number, @@ -39,7 +48,7 @@ export const MAX_PRICES = { }; class PricesRepository { - public async $savePrices(time: number, prices: IConversionRates): Promise { + public async $savePrices(time: number, prices: ApiPrice): Promise { if (prices.USD === -1) { // Some historical price entries have no USD prices, so we just ignore them to avoid future UX issues // As of today there are only 4 (on 2013-09-05, 2013-0909, 2013-09-12 and 2013-09-26) so that's fine @@ -60,77 +69,115 @@ class PricesRepository { VALUE (FROM_UNIXTIME(?), ?, ?, ?, ?, ?, ?, ? )`, [time, prices.USD, prices.EUR, prices.GBP, prices.CAD, prices.CHF, prices.AUD, prices.JPY] ); - } catch (e: any) { + } catch (e) { logger.err(`Cannot save exchange rate into db. Reason: ` + (e instanceof Error ? e.message : e)); throw e; } } public async $getOldestPriceTime(): Promise { - const [oldestRow] = await DB.query(`SELECT UNIX_TIMESTAMP(time) as time from prices WHERE USD != 0 ORDER BY time LIMIT 1`); + const [oldestRow] = await DB.query(` + SELECT UNIX_TIMESTAMP(time) AS time + FROM prices + ORDER BY time + LIMIT 1 + `); return oldestRow[0] ? oldestRow[0].time : 0; } public async $getLatestPriceId(): Promise { - const [oldestRow] = await DB.query(`SELECT id from prices WHERE USD != 0 ORDER BY time DESC LIMIT 1`); - return oldestRow[0] ? oldestRow[0].id : null; - } - - public async $getLatestPriceTime(): Promise { - const [oldestRow] = await DB.query(`SELECT UNIX_TIMESTAMP(time) as time from prices WHERE USD != 0 ORDER BY time DESC LIMIT 1`); - return oldestRow[0] ? oldestRow[0].time : 0; - } - - public async $getPricesTimes(): Promise { - const [times]: any[] = await DB.query(`SELECT UNIX_TIMESTAMP(time) as time from prices WHERE USD != 0 ORDER BY time`); - return times.map(time => time.time); - } - - public async $getPricesTimesAndId(): Promise { - const [times]: any[] = await DB.query(`SELECT UNIX_TIMESTAMP(time) as time, id, USD from prices ORDER BY time`); - return times; - } - - public async $getLatestConversionRates(): Promise { - const [rates]: any[] = await DB.query(` - SELECT USD, EUR, GBP, CAD, CHF, AUD, JPY + const [oldestRow] = await DB.query(` + SELECT id FROM prices ORDER BY time DESC LIMIT 1` ); - if (!rates || rates.length === 0) { + return oldestRow[0] ? oldestRow[0].id : null; + } + + public async $getLatestPriceTime(): Promise { + const [oldestRow] = await DB.query(` + SELECT UNIX_TIMESTAMP(time) AS time + FROM prices + ORDER BY time DESC + LIMIT 1` + ); + return oldestRow[0] ? oldestRow[0].time : 0; + } + + public async $getPricesTimes(): Promise { + const [times] = await DB.query(` + SELECT UNIX_TIMESTAMP(time) AS time + FROM prices + WHERE USD != -1 + ORDER BY time + `); + if (!Array.isArray(times)) { + return []; + } + return times.map(time => time.time); + } + + public async $getPricesTimesAndId(): Promise<{time: number, id: number, USD: number}[]> { + const [times] = await DB.query(` + SELECT + UNIX_TIMESTAMP(time) AS time, + id, + USD + FROM prices + ORDER BY time + `); + return times as {time: number, id: number, USD: number}[]; + } + + public async $getLatestConversionRates(): Promise { + const [rates] = await DB.query(` + SELECT ${ApiPriceFields} + FROM prices + ORDER BY time DESC + LIMIT 1` + ); + + if (!Array.isArray(rates) || rates.length === 0) { return priceUpdater.getEmptyPricesObj(); } - return rates[0]; + return rates[0] as ApiPrice; } public async $getNearestHistoricalPrice(timestamp: number | undefined): Promise { try { - const [rates]: any[] = await DB.query(` - SELECT *, UNIX_TIMESTAMP(time) AS time + const [rates] = await DB.query(` + SELECT ${ApiPriceFields} FROM prices WHERE UNIX_TIMESTAMP(time) < ? ORDER BY time DESC LIMIT 1`, [timestamp] ); - if (!rates) { + if (!Array.isArray(rates)) { throw Error(`Cannot get single historical price from the database`); } // Compute fiat exchange rates - const latestPrice = await this.$getLatestConversionRates(); + let latestPrice = rates[0] as ApiPrice; + if (latestPrice.USD === -1) { + latestPrice = priceUpdater.getEmptyPricesObj(); + } + + const computeFx = (usd: number, other: number): number => + Math.round(Math.max(other, 0) / Math.max(usd, 1) * 100) / 100; + const exchangeRates: ExchangeRates = { - USDEUR: Math.round(latestPrice.EUR / latestPrice.USD * 100) / 100, - USDGBP: Math.round(latestPrice.GBP / latestPrice.USD * 100) / 100, - USDCAD: Math.round(latestPrice.CAD / latestPrice.USD * 100) / 100, - USDCHF: Math.round(latestPrice.CHF / latestPrice.USD * 100) / 100, - USDAUD: Math.round(latestPrice.AUD / latestPrice.USD * 100) / 100, - USDJPY: Math.round(latestPrice.JPY / latestPrice.USD * 100) / 100, + USDEUR: computeFx(latestPrice.USD, latestPrice.EUR), + USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), + USDCAD: computeFx(latestPrice.USD, latestPrice.CAD), + USDCHF: computeFx(latestPrice.USD, latestPrice.CHF), + USDAUD: computeFx(latestPrice.USD, latestPrice.AUD), + USDJPY: computeFx(latestPrice.USD, latestPrice.JPY), }; return { - prices: rates, + prices: rates as ApiPrice[], exchangeRates: exchangeRates }; } catch (e) { @@ -141,28 +188,35 @@ class PricesRepository { public async $getHistoricalPrices(): Promise { try { - const [rates]: any[] = await DB.query(` - SELECT *, UNIX_TIMESTAMP(time) AS time + const [rates] = await DB.query(` + SELECT ${ApiPriceFields} FROM prices ORDER BY time DESC `); - if (!rates) { + if (!Array.isArray(rates)) { throw Error(`Cannot get average historical price from the database`); } // Compute fiat exchange rates - const latestPrice: ApiPrice = rates[0]; + let latestPrice = rates[0] as ApiPrice; + if (latestPrice.USD === -1) { + latestPrice = priceUpdater.getEmptyPricesObj(); + } + + const computeFx = (usd: number, other: number): number => + Math.round(Math.max(other, 0) / Math.max(usd, 1) * 100) / 100; + const exchangeRates: ExchangeRates = { - USDEUR: Math.round(latestPrice.EUR / latestPrice.USD * 100) / 100, - USDGBP: Math.round(latestPrice.GBP / latestPrice.USD * 100) / 100, - USDCAD: Math.round(latestPrice.CAD / latestPrice.USD * 100) / 100, - USDCHF: Math.round(latestPrice.CHF / latestPrice.USD * 100) / 100, - USDAUD: Math.round(latestPrice.AUD / latestPrice.USD * 100) / 100, - USDJPY: Math.round(latestPrice.JPY / latestPrice.USD * 100) / 100, + USDEUR: computeFx(latestPrice.USD, latestPrice.EUR), + USDGBP: computeFx(latestPrice.USD, latestPrice.GBP), + USDCAD: computeFx(latestPrice.USD, latestPrice.CAD), + USDCHF: computeFx(latestPrice.USD, latestPrice.CHF), + USDAUD: computeFx(latestPrice.USD, latestPrice.AUD), + USDJPY: computeFx(latestPrice.USD, latestPrice.JPY), }; return { - prices: rates, + prices: rates as ApiPrice[], exchangeRates: exchangeRates }; } catch (e) { diff --git a/backend/src/tasks/lightning/sync-tasks/stats-importer.ts b/backend/src/tasks/lightning/sync-tasks/stats-importer.ts index 14f592a14..d009ce052 100644 --- a/backend/src/tasks/lightning/sync-tasks/stats-importer.ts +++ b/backend/src/tasks/lightning/sync-tasks/stats-importer.ts @@ -411,7 +411,7 @@ class LightningStatsImporter { } if (totalProcessed > 0) { - logger.notice(`Lightning network stats historical import completed`, logger.tags.ln); + logger.info(`Lightning network stats historical import completed`, logger.tags.ln); } } catch (e) { logger.err(`Lightning network stats historical failed. Reason: ${e instanceof Error ? e.message : e}`, logger.tags.ln); diff --git a/backend/src/tasks/pools-updater.ts b/backend/src/tasks/pools-updater.ts index 32de85f3a..dc76382d6 100644 --- a/backend/src/tasks/pools-updater.ts +++ b/backend/src/tasks/pools-updater.ts @@ -12,7 +12,7 @@ import * as https from 'https'; */ class PoolsUpdater { lastRun: number = 0; - currentSha: string | undefined = undefined; + currentSha: string | null = null; poolsUrl: string = config.MEMPOOL.POOLS_JSON_URL; treeUrl: string = config.MEMPOOL.POOLS_JSON_TREE_URL; @@ -33,7 +33,7 @@ class PoolsUpdater { try { const githubSha = await this.fetchPoolsSha(); // Fetch pools-v2.json sha from github - if (githubSha === undefined) { + if (githubSha === null) { return; } @@ -42,12 +42,12 @@ class PoolsUpdater { } logger.debug(`pools-v2.json sha | Current: ${this.currentSha} | Github: ${githubSha}`); - if (this.currentSha !== undefined && this.currentSha === githubSha) { + if (this.currentSha !== null && this.currentSha === githubSha) { return; } // See backend README for more details about the mining pools update process - if (this.currentSha !== undefined && // If we don't have any mining pool, download it at least once + if (this.currentSha !== null && // If we don't have any mining pool, download it at least once config.MEMPOOL.AUTOMATIC_BLOCK_REINDEXING !== true && // Automatic pools update is disabled !process.env.npm_config_update_pools // We're not manually updating mining pool ) { @@ -57,7 +57,7 @@ class PoolsUpdater { } const network = config.SOCKS5PROXY.ENABLED ? 'tor' : 'clearnet'; - if (this.currentSha === undefined) { + if (this.currentSha === null) { logger.info(`Downloading pools-v2.json for the first time from ${this.poolsUrl} over ${network}`, logger.tags.mining); } else { logger.warn(`pools-v2.json is outdated, fetch latest from ${this.poolsUrl} over ${network}`, logger.tags.mining); @@ -82,7 +82,7 @@ class PoolsUpdater { logger.err(`Could not migrate mining pools, rolling back. Exception: ${JSON.stringify(e)}`, logger.tags.mining); await DB.query('ROLLBACK;'); } - logger.notice('PoolsUpdater completed'); + logger.info('PoolsUpdater completed'); } catch (e) { this.lastRun = now - (oneWeek - oneDay); // Try again in 24h instead of waiting next week @@ -108,20 +108,20 @@ class PoolsUpdater { /** * Fetch our latest pools-v2.json sha from the db */ - private async getShaFromDb(): Promise { + private async getShaFromDb(): Promise { try { const [rows]: any[] = await DB.query('SELECT string FROM state WHERE name="pools_json_sha"'); - return (rows.length > 0 ? rows[0].string : undefined); + return (rows.length > 0 ? rows[0].string : null); } catch (e) { logger.err('Cannot fetch pools-v2.json sha from db. Reason: ' + (e instanceof Error ? e.message : e), logger.tags.mining); - return undefined; + return null; } } /** * Fetch our latest pools-v2.json sha from github */ - private async fetchPoolsSha(): Promise { + private async fetchPoolsSha(): Promise { const response = await this.query(this.treeUrl); if (response !== undefined) { @@ -133,7 +133,7 @@ class PoolsUpdater { } logger.err(`Cannot find "pools-v2.json" in git tree (${this.treeUrl})`, logger.tags.mining); - return undefined; + return null; } /** diff --git a/backend/src/tasks/price-feeds/bitfinex-api.ts b/backend/src/tasks/price-feeds/bitfinex-api.ts index 0e06c3af7..30b70e9eb 100644 --- a/backend/src/tasks/price-feeds/bitfinex-api.ts +++ b/backend/src/tasks/price-feeds/bitfinex-api.ts @@ -8,9 +8,6 @@ class BitfinexApi implements PriceFeed { public url: string = 'https://api.bitfinex.com/v1/pubticker/BTC'; public urlHist: string = 'https://api-pub.bitfinex.com/v2/candles/trade:{GRANULARITY}:tBTC{CURRENCY}/hist'; - constructor() { - } - public async $fetchPrice(currency): Promise { const response = await query(this.url + currency); if (response && response['last_price']) { diff --git a/backend/src/tasks/price-feeds/kraken-api.ts b/backend/src/tasks/price-feeds/kraken-api.ts index c6b3c0c11..ebc784c6f 100644 --- a/backend/src/tasks/price-feeds/kraken-api.ts +++ b/backend/src/tasks/price-feeds/kraken-api.ts @@ -98,7 +98,7 @@ class KrakenApi implements PriceFeed { } if (Object.keys(priceHistory).length > 0) { - logger.notice(`Inserted ${Object.keys(priceHistory).length} Kraken EUR, USD, GBP, JPY, CAD, CHF and AUD weekly price history into db`, logger.tags.mining); + logger.info(`Inserted ${Object.keys(priceHistory).length} Kraken EUR, USD, GBP, JPY, CAD, CHF and AUD weekly price history into db`, logger.tags.mining); } } } diff --git a/backend/src/tasks/price-updater.ts b/backend/src/tasks/price-updater.ts index b39e152ae..ccb8d3e68 100644 --- a/backend/src/tasks/price-updater.ts +++ b/backend/src/tasks/price-updater.ts @@ -2,8 +2,7 @@ import * as fs from 'fs'; import path from 'path'; import config from '../config'; import logger from '../logger'; -import { IConversionRates } from '../mempool.interfaces'; -import PricesRepository, { MAX_PRICES } from '../repositories/PricesRepository'; +import PricesRepository, { ApiPrice, MAX_PRICES } from '../repositories/PricesRepository'; import BitfinexApi from './price-feeds/bitfinex-api'; import BitflyerApi from './price-feeds/bitflyer-api'; import CoinbaseApi from './price-feeds/coinbase-api'; @@ -21,18 +20,18 @@ export interface PriceFeed { } export interface PriceHistory { - [timestamp: number]: IConversionRates; + [timestamp: number]: ApiPrice; } class PriceUpdater { public historyInserted = false; - lastRun = 0; - lastHistoricalRun = 0; - running = false; - feeds: PriceFeed[] = []; - currencies: string[] = ['USD', 'EUR', 'GBP', 'CAD', 'CHF', 'AUD', 'JPY']; - latestPrices: IConversionRates; - private ratesChangedCallback: ((rates: IConversionRates) => void) | undefined; + private lastRun = 0; + private lastHistoricalRun = 0; + private running = false; + private feeds: PriceFeed[] = []; + private currencies: string[] = ['USD', 'EUR', 'GBP', 'CAD', 'CHF', 'AUD', 'JPY']; + private latestPrices: ApiPrice; + private ratesChangedCallback: ((rates: ApiPrice) => void) | undefined; constructor() { this.latestPrices = this.getEmptyPricesObj(); @@ -44,8 +43,13 @@ class PriceUpdater { this.feeds.push(new GeminiApi()); } - public getEmptyPricesObj(): IConversionRates { + public getLatestPrices(): ApiPrice { + return this.latestPrices; + } + + public getEmptyPricesObj(): ApiPrice { return { + time: 0, USD: -1, EUR: -1, GBP: -1, @@ -56,7 +60,7 @@ class PriceUpdater { }; } - public setRatesChangedCallback(fn: (rates: IConversionRates) => void) { + public setRatesChangedCallback(fn: (rates: ApiPrice) => void): void { this.ratesChangedCallback = fn; } @@ -156,6 +160,10 @@ class PriceUpdater { } this.lastRun = new Date().getTime() / 1000; + + if (this.latestPrices.USD === -1) { + this.latestPrices = await PricesRepository.$getLatestConversionRates(); + } } /** @@ -224,7 +232,7 @@ class PriceUpdater { // Group them by timestamp and currency, for example // grouped[123456789]['USD'] = [1, 2, 3, 4]; - const grouped: any = {}; + const grouped = {}; for (const historicalEntry of historicalPrices) { for (const time in historicalEntry) { if (existingPriceTimes.includes(parseInt(time, 10))) { @@ -249,7 +257,7 @@ class PriceUpdater { // Average prices and insert everything into the db let totalInserted = 0; for (const time in grouped) { - const prices: IConversionRates = this.getEmptyPricesObj(); + const prices: ApiPrice = this.getEmptyPricesObj(); for (const currency in grouped[time]) { if (grouped[time][currency].length === 0) { continue; diff --git a/backend/src/utils/format.ts b/backend/src/utils/format.ts new file mode 100644 index 000000000..a18ce1892 --- /dev/null +++ b/backend/src/utils/format.ts @@ -0,0 +1,29 @@ +const byteUnits = ['B', 'kB', 'MB', 'GB', 'TB']; + +export function getBytesUnit(bytes: number): string { + if (isNaN(bytes) || !isFinite(bytes)) { + return 'B'; + } + + let unitIndex = 0; + while (unitIndex < byteUnits.length && bytes > 1024) { + unitIndex++; + bytes /= 1024; + } + + return byteUnits[unitIndex]; +} + +export function formatBytes(bytes: number, toUnit: string, skipUnit = false): string { + if (isNaN(bytes) || !isFinite(bytes)) { + return `${bytes}`; + } + + let unitIndex = 0; + while (unitIndex < byteUnits.length && (toUnit && byteUnits[unitIndex] !== toUnit || (!toUnit && bytes > 1024))) { + unitIndex++; + bytes /= 1024; + } + + return `${bytes.toFixed(2)}${skipUnit ? '' : ' ' + byteUnits[unitIndex]}`; +} \ No newline at end of file diff --git a/docker/backend/mempool-config.json b/docker/backend/mempool-config.json index d2aa75c69..78a2c116b 100644 --- a/docker/backend/mempool-config.json +++ b/docker/backend/mempool-config.json @@ -26,7 +26,7 @@ "ADVANCED_GBT_AUDIT": __MEMPOOL_ADVANCED_GBT_AUDIT__, "ADVANCED_GBT_MEMPOOL": __MEMPOOL_ADVANCED_GBT_MEMPOOL__, "CPFP_INDEXING": __MEMPOOL_CPFP_INDEXING__, - "MAX_BLOCKS_BULK_QUERY": __MEMPOOL__MAX_BLOCKS_BULK_QUERY__ + "MAX_BLOCKS_BULK_QUERY": __MEMPOOL_MAX_BLOCKS_BULK_QUERY__ }, "CORE_RPC": { "HOST": "__CORE_RPC_HOST__", @@ -108,4 +108,4 @@ "BISQ_URL": "__EXTERNAL_DATA_SERVER_BISQ_URL__", "BISQ_ONION": "__EXTERNAL_DATA_SERVER_BISQ_ONION__" } -} +} \ No newline at end of file diff --git a/frontend/cypress.config.ts b/frontend/cypress.config.ts index 14d36fc74..4bdbd257d 100644 --- a/frontend/cypress.config.ts +++ b/frontend/cypress.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'cypress' +import { defineConfig } from 'cypress'; export default defineConfig({ projectId: 'ry4br7', @@ -12,12 +12,18 @@ export default defineConfig({ }, chromeWebSecurity: false, e2e: { - // We've imported your old cypress plugins here. - // You may want to clean this up later by importing these. - setupNodeEvents(on, config) { - return require('./cypress/plugins/index.js')(on, config) + setupNodeEvents(on: any, config: any) { + const fs = require('fs'); + const CONFIG_FILE = 'mempool-frontend-config.json'; + if (fs.existsSync(CONFIG_FILE)) { + let contents = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8')); + config.env.BASE_MODULE = contents.BASE_MODULE ? contents.BASE_MODULE : 'mempool'; + } else { + config.env.BASE_MODULE = 'mempool'; + } + return config; }, baseUrl: 'http://localhost:4200', specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}', }, -}) +}); diff --git a/frontend/cypress/e2e/bisq/bisq.spec.ts b/frontend/cypress/e2e/bisq/bisq.spec.ts index e81b17185..ac3b747b2 100644 --- a/frontend/cypress/e2e/bisq/bisq.spec.ts +++ b/frontend/cypress/e2e/bisq/bisq.spec.ts @@ -1,5 +1,5 @@ describe('Bisq', () => { - const baseModule = Cypress.env("BASE_MODULE"); + const baseModule = Cypress.env('BASE_MODULE'); const basePath = ''; beforeEach(() => { @@ -20,7 +20,7 @@ describe('Bisq', () => { cy.waitForSkeletonGone(); }); - describe("transactions", () => { + describe('transactions', () => { it('loads the transactions screen', () => { cy.visit(`${basePath}`); cy.waitForSkeletonGone(); @@ -30,9 +30,9 @@ describe('Bisq', () => { }); const filters = [ - "Asset listing fee", "Blind vote", "Compensation request", - "Genesis", "Irregular", "Lockup", "Pay trade fee", "Proof of burn", - "Proposal", "Reimbursement request", "Transfer BSQ", "Unlock", "Vote reveal" + 'Asset listing fee', 'Blind vote', 'Compensation request', + 'Genesis', 'Irregular', 'Lockup', 'Pay trade fee', 'Proof of burn', + 'Proposal', 'Reimbursement request', 'Transfer BSQ', 'Unlock', 'Vote reveal' ]; filters.forEach((filter) => { it.only(`filters the transaction screen by ${filter}`, () => { @@ -49,7 +49,7 @@ describe('Bisq', () => { }); }); - it("filters using multiple criteria", () => { + it('filters using multiple criteria', () => { const filters = ['Proposal', 'Lockup', 'Unlock']; cy.visit(`${basePath}/transactions`); cy.waitForSkeletonGone(); diff --git a/frontend/cypress/e2e/liquid/liquid.spec.ts b/frontend/cypress/e2e/liquid/liquid.spec.ts index e24b19fad..e22a9b94e 100644 --- a/frontend/cypress/e2e/liquid/liquid.spec.ts +++ b/frontend/cypress/e2e/liquid/liquid.spec.ts @@ -1,5 +1,5 @@ describe('Liquid', () => { - const baseModule = Cypress.env("BASE_MODULE"); + const baseModule = Cypress.env('BASE_MODULE'); const basePath = ''; beforeEach(() => { diff --git a/frontend/cypress/e2e/liquidtestnet/liquidtestnet.spec.ts b/frontend/cypress/e2e/liquidtestnet/liquidtestnet.spec.ts index 5cf6cf331..e1172e51a 100644 --- a/frontend/cypress/e2e/liquidtestnet/liquidtestnet.spec.ts +++ b/frontend/cypress/e2e/liquidtestnet/liquidtestnet.spec.ts @@ -1,5 +1,5 @@ describe('Liquid Testnet', () => { - const baseModule = Cypress.env("BASE_MODULE"); + const baseModule = Cypress.env('BASE_MODULE'); const basePath = '/testnet'; beforeEach(() => { diff --git a/frontend/cypress/e2e/mainnet/mainnet.spec.ts b/frontend/cypress/e2e/mainnet/mainnet.spec.ts index 5ab3f9ce9..71a35ba86 100644 --- a/frontend/cypress/e2e/mainnet/mainnet.spec.ts +++ b/frontend/cypress/e2e/mainnet/mainnet.spec.ts @@ -1,6 +1,6 @@ -import { emitMempoolInfo, dropWebSocket } from "../../support/websocket"; +import { emitMempoolInfo, dropWebSocket } from '../../support/websocket'; -const baseModule = Cypress.env("BASE_MODULE"); +const baseModule = Cypress.env('BASE_MODULE'); //Credit: https://github.com/bahmutov/cypress-examples/blob/6cedb17f83a3bb03ded13cf1d6a3f0656ca2cdf5/docs/recipes/overlapping-elements.md @@ -339,14 +339,14 @@ describe('Mainnet', () => { cy.visit('/'); cy.waitForSkeletonGone(); - cy.changeNetwork("testnet"); - cy.changeNetwork("signet"); - cy.changeNetwork("mainnet"); + cy.changeNetwork('testnet'); + cy.changeNetwork('signet'); + cy.changeNetwork('mainnet'); }); it.skip('loads the dashboard with the skeleton blocks', () => { cy.mockMempoolSocket(); - cy.visit("/"); + cy.visit('/'); cy.get(':nth-child(1) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(2) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(3) > #bitcoin-block-0').should('be.visible'); diff --git a/frontend/cypress/e2e/mainnet/mining.spec.ts b/frontend/cypress/e2e/mainnet/mining.spec.ts index 5c60f3a8c..cfaa40015 100644 --- a/frontend/cypress/e2e/mainnet/mining.spec.ts +++ b/frontend/cypress/e2e/mainnet/mining.spec.ts @@ -1,4 +1,4 @@ -const baseModule = Cypress.env("BASE_MODULE"); +const baseModule = Cypress.env('BASE_MODULE'); describe('Mainnet - Mining Features', () => { beforeEach(() => { diff --git a/frontend/cypress/e2e/signet/signet.spec.ts b/frontend/cypress/e2e/signet/signet.spec.ts index 2f09bc4b8..03cfb3480 100644 --- a/frontend/cypress/e2e/signet/signet.spec.ts +++ b/frontend/cypress/e2e/signet/signet.spec.ts @@ -1,6 +1,6 @@ -import { emitMempoolInfo } from "../../support/websocket"; +import { emitMempoolInfo } from '../../support/websocket'; -const baseModule = Cypress.env("BASE_MODULE"); +const baseModule = Cypress.env('BASE_MODULE'); describe('Signet', () => { beforeEach(() => { @@ -25,7 +25,7 @@ describe('Signet', () => { it.skip('loads the dashboard with the skeleton blocks', () => { cy.mockMempoolSocket(); - cy.visit("/signet"); + cy.visit('/signet'); cy.get(':nth-child(1) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(2) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(3) > #bitcoin-block-0').should('be.visible'); @@ -35,7 +35,7 @@ describe('Signet', () => { emitMempoolInfo({ 'params': { - "network": "signet" + 'network': 'signet' } }); diff --git a/frontend/cypress/e2e/testnet/testnet.spec.ts b/frontend/cypress/e2e/testnet/testnet.spec.ts index b05229a28..4236ca207 100644 --- a/frontend/cypress/e2e/testnet/testnet.spec.ts +++ b/frontend/cypress/e2e/testnet/testnet.spec.ts @@ -1,6 +1,6 @@ -import { confirmAddress, emitMempoolInfo, sendWsMock, showNewTx, startTrackingAddress } from "../../support/websocket"; +import { emitMempoolInfo } from '../../support/websocket'; -const baseModule = Cypress.env("BASE_MODULE"); +const baseModule = Cypress.env('BASE_MODULE'); describe('Testnet', () => { beforeEach(() => { @@ -25,7 +25,7 @@ describe('Testnet', () => { it.skip('loads the dashboard with the skeleton blocks', () => { cy.mockMempoolSocket(); - cy.visit("/testnet"); + cy.visit('/testnet'); cy.get(':nth-child(1) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(2) > #bitcoin-block-0').should('be.visible'); cy.get(':nth-child(3) > #bitcoin-block-0').should('be.visible'); diff --git a/frontend/cypress/plugins/index.js b/frontend/cypress/plugins/index.js deleted file mode 100644 index 11f43df95..000000000 --- a/frontend/cypress/plugins/index.js +++ /dev/null @@ -1,13 +0,0 @@ -const fs = require('fs'); - -const CONFIG_FILE = 'mempool-frontend-config.json'; - -module.exports = (on, config) => { - if (fs.existsSync(CONFIG_FILE)) { - let contents = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8')); - config.env.BASE_MODULE = contents.BASE_MODULE ? contents.BASE_MODULE : 'mempool'; - } else { - config.env.BASE_MODULE = 'mempool'; - } - return config; -} diff --git a/frontend/package-lock.json b/frontend/package-lock.json index f4ae62701..6372f9af6 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -58,7 +58,7 @@ }, "optionalDependencies": { "@cypress/schematic": "^2.4.0", - "cypress": "^12.3.0", + "cypress": "^12.7.0", "cypress-fail-on-console-error": "~4.0.2", "cypress-wait-until": "^1.7.2", "mock-socket": "~9.1.5", @@ -7010,9 +7010,9 @@ "peer": true }, "node_modules/cypress": { - "version": "12.3.0", - "resolved": "https://registry.npmjs.org/cypress/-/cypress-12.3.0.tgz", - "integrity": "sha512-ZQNebibi6NBt51TRxRMYKeFvIiQZ01t50HSy7z/JMgRVqBUey3cdjog5MYEbzG6Ktti5ckDt1tfcC47lmFwXkw==", + "version": "12.7.0", + "resolved": "https://registry.npmjs.org/cypress/-/cypress-12.7.0.tgz", + "integrity": "sha512-7rq+nmhzz0u6yabCFyPtADU2OOrYt6pvUau9qV7xyifJ/hnsaw/vkr0tnLlcuuQKUAOC1v1M1e4Z0zG7S0IAvA==", "hasInstallScript": true, "optional": true, "dependencies": { @@ -7033,7 +7033,7 @@ "commander": "^5.1.0", "common-tags": "^1.8.0", "dayjs": "^1.10.4", - "debug": "^4.3.2", + "debug": "^4.3.4", "enquirer": "^2.3.6", "eventemitter2": "6.4.7", "execa": "4.1.0", @@ -7159,6 +7159,23 @@ "node": ">= 6" } }, + "node_modules/cypress/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "optional": true, + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, "node_modules/cypress/node_modules/execa": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/execa/-/execa-4.1.0.tgz", @@ -22276,9 +22293,9 @@ "peer": true }, "cypress": { - "version": "12.3.0", - "resolved": "https://registry.npmjs.org/cypress/-/cypress-12.3.0.tgz", - "integrity": "sha512-ZQNebibi6NBt51TRxRMYKeFvIiQZ01t50HSy7z/JMgRVqBUey3cdjog5MYEbzG6Ktti5ckDt1tfcC47lmFwXkw==", + "version": "12.7.0", + "resolved": "https://registry.npmjs.org/cypress/-/cypress-12.7.0.tgz", + "integrity": "sha512-7rq+nmhzz0u6yabCFyPtADU2OOrYt6pvUau9qV7xyifJ/hnsaw/vkr0tnLlcuuQKUAOC1v1M1e4Z0zG7S0IAvA==", "optional": true, "requires": { "@cypress/request": "^2.88.10", @@ -22298,7 +22315,7 @@ "commander": "^5.1.0", "common-tags": "^1.8.0", "dayjs": "^1.10.4", - "debug": "^4.3.2", + "debug": "^4.3.4", "enquirer": "^2.3.6", "eventemitter2": "6.4.7", "execa": "4.1.0", @@ -22382,6 +22399,15 @@ "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==", "optional": true }, + "debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "optional": true, + "requires": { + "ms": "2.1.2" + } + }, "execa": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/execa/-/execa-4.1.0.tgz", diff --git a/frontend/package.json b/frontend/package.json index e42c1bd8f..f23866d06 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -110,7 +110,7 @@ }, "optionalDependencies": { "@cypress/schematic": "^2.4.0", - "cypress": "^12.3.0", + "cypress": "^12.7.0", "cypress-fail-on-console-error": "~4.0.2", "cypress-wait-until": "^1.7.2", "mock-socket": "~9.1.5", @@ -119,4 +119,4 @@ "scarfSettings": { "enabled": false } -} +} \ No newline at end of file diff --git a/frontend/src/app/bisq/bisq-block/bisq-block.component.html b/frontend/src/app/bisq/bisq-block/bisq-block.component.html index 9cc2ad699..4f79d8838 100644 --- a/frontend/src/app/bisq/bisq-block/bisq-block.component.html +++ b/frontend/src/app/bisq/bisq-block/bisq-block.component.html @@ -24,7 +24,7 @@ ‎{{ block.time | date:'yyyy-MM-dd HH:mm' }}
- () + ()
diff --git a/frontend/src/app/bisq/bisq-blocks/bisq-blocks.component.html b/frontend/src/app/bisq/bisq-blocks/bisq-blocks.component.html index 750e2e3b1..15f15b258 100644 --- a/frontend/src/app/bisq/bisq-blocks/bisq-blocks.component.html +++ b/frontend/src/app/bisq/bisq-blocks/bisq-blocks.component.html @@ -17,7 +17,7 @@ {{ block.height }} - + {{ calculateTotalOutput(block) / 100 | number: '1.2-2' }} BSQ {{ block.txs.length }} diff --git a/frontend/src/app/bisq/bisq-transaction/bisq-transaction.component.html b/frontend/src/app/bisq/bisq-transaction/bisq-transaction.component.html index 11f981774..3a23688e6 100644 --- a/frontend/src/app/bisq/bisq-transaction/bisq-transaction.component.html +++ b/frontend/src/app/bisq/bisq-transaction/bisq-transaction.component.html @@ -35,7 +35,7 @@ ‎{{ bisqTx.time | date:'yyyy-MM-dd HH:mm' }}
- () + ()
diff --git a/frontend/src/app/bisq/bisq-transactions/bisq-transactions.component.html b/frontend/src/app/bisq/bisq-transactions/bisq-transactions.component.html index 8d34448d8..bc22414ca 100644 --- a/frontend/src/app/bisq/bisq-transactions/bisq-transactions.component.html +++ b/frontend/src/app/bisq/bisq-transactions/bisq-transactions.component.html @@ -37,7 +37,7 @@ {{ calculateTotalOutput(tx.outputs) / 100 | number: '1.2-2' }} BSQ - + {{ tx.blockHeight }} diff --git a/frontend/src/app/components/amount/amount.component.html b/frontend/src/app/components/amount/amount.component.html index ce9c02d78..27fd59110 100644 --- a/frontend/src/app/components/amount/amount.component.html +++ b/frontend/src/app/components/amount/amount.component.html @@ -3,13 +3,15 @@ {{ addPlus && satoshis >= 0 ? '+' : '' }} {{ ( - (blockConversion.price[currency] >= 0 ? blockConversion.price[currency] : null) ?? - (blockConversion.price['USD'] * blockConversion.exchangeRates['USD' + currency]) ?? 0 + (blockConversion.price[currency] > -1 ? blockConversion.price[currency] : null) ?? + (blockConversion.price['USD'] > -1 ? blockConversion.price['USD'] * blockConversion.exchangeRates['USD' + currency] : null) ?? 0 ) * satoshis / 100000000 | fiatCurrency : digitsInfo : currency }} - {{ addPlus && satoshis >= 0 ? '+' : '' }}{{ (conversions ? conversions[currency] : 0) * satoshis / 100000000 | fiatCurrency : digitsInfo : currency }} + {{ addPlus && satoshis >= 0 ? '+' : '' }} + {{ (conversions[currency] > -1 ? conversions[currency] : 0) * satoshis / 100000000 | fiatCurrency : digitsInfo : currency }} + diff --git a/frontend/src/app/components/block-fees-graph/block-fees-graph.component.scss b/frontend/src/app/components/block-fees-graph/block-fees-graph.component.scss index ec1755e7d..65447419a 100644 --- a/frontend/src/app/components/block-fees-graph/block-fees-graph.component.scss +++ b/frontend/src/app/components/block-fees-graph/block-fees-graph.component.scss @@ -54,31 +54,6 @@ max-height: 270px; } -.formRadioGroup { - margin-top: 6px; - display: flex; - flex-direction: column; - @media (min-width: 991px) { - position: relative; - top: -100px; - } - @media (min-width: 830px) and (max-width: 991px) { - position: relative; - top: 0px; - } - @media (min-width: 830px) { - flex-direction: row; - float: right; - margin-top: 0px; - } - .btn-sm { - font-size: 9px; - @media (min-width: 830px) { - font-size: 14px; - } - } -} - .disabled { pointer-events: none; opacity: 0.5; diff --git a/frontend/src/app/components/block-overview-tooltip/block-overview-tooltip.component.html b/frontend/src/app/components/block-overview-tooltip/block-overview-tooltip.component.html index 2fa626a95..e841e291f 100644 --- a/frontend/src/app/components/block-overview-tooltip/block-overview-tooltip.component.html +++ b/frontend/src/app/components/block-overview-tooltip/block-overview-tooltip.component.html @@ -16,7 +16,7 @@ Amount - + Fee diff --git a/frontend/src/app/components/block-prediction-graph/block-prediction-graph.component.scss b/frontend/src/app/components/block-prediction-graph/block-prediction-graph.component.scss index ec1755e7d..65447419a 100644 --- a/frontend/src/app/components/block-prediction-graph/block-prediction-graph.component.scss +++ b/frontend/src/app/components/block-prediction-graph/block-prediction-graph.component.scss @@ -54,31 +54,6 @@ max-height: 270px; } -.formRadioGroup { - margin-top: 6px; - display: flex; - flex-direction: column; - @media (min-width: 991px) { - position: relative; - top: -100px; - } - @media (min-width: 830px) and (max-width: 991px) { - position: relative; - top: 0px; - } - @media (min-width: 830px) { - flex-direction: row; - float: right; - margin-top: 0px; - } - .btn-sm { - font-size: 9px; - @media (min-width: 830px) { - font-size: 14px; - } - } -} - .disabled { pointer-events: none; opacity: 0.5; diff --git a/frontend/src/app/components/block-rewards-graph/block-rewards-graph.component.scss b/frontend/src/app/components/block-rewards-graph/block-rewards-graph.component.scss index ec1755e7d..65447419a 100644 --- a/frontend/src/app/components/block-rewards-graph/block-rewards-graph.component.scss +++ b/frontend/src/app/components/block-rewards-graph/block-rewards-graph.component.scss @@ -54,31 +54,6 @@ max-height: 270px; } -.formRadioGroup { - margin-top: 6px; - display: flex; - flex-direction: column; - @media (min-width: 991px) { - position: relative; - top: -100px; - } - @media (min-width: 830px) and (max-width: 991px) { - position: relative; - top: 0px; - } - @media (min-width: 830px) { - flex-direction: row; - float: right; - margin-top: 0px; - } - .btn-sm { - font-size: 9px; - @media (min-width: 830px) { - font-size: 14px; - } - } -} - .disabled { pointer-events: none; opacity: 0.5; diff --git a/frontend/src/app/components/block-sizes-weights-graph/block-sizes-weights-graph.component.scss b/frontend/src/app/components/block-sizes-weights-graph/block-sizes-weights-graph.component.scss index 85765e0e1..65447419a 100644 --- a/frontend/src/app/components/block-sizes-weights-graph/block-sizes-weights-graph.component.scss +++ b/frontend/src/app/components/block-sizes-weights-graph/block-sizes-weights-graph.component.scss @@ -54,31 +54,6 @@ max-height: 270px; } -.formRadioGroup { - margin-top: 6px; - display: flex; - flex-direction: column; - @media (min-width: 1130px) { - position: relative; - top: -100px; - } - @media (min-width: 830px) and (max-width: 1130px) { - position: relative; - top: 0px; - } - @media (min-width: 830px) { - flex-direction: row; - float: right; - margin-top: 0px; - } - .btn-sm { - font-size: 9px; - @media (min-width: 830px) { - font-size: 14px; - } - } -} - .disabled { pointer-events: none; opacity: 0.5; diff --git a/frontend/src/app/components/blockchain-blocks/blockchain-blocks.component.html b/frontend/src/app/components/blockchain-blocks/blockchain-blocks.component.html index 746c5fa5c..8323cf8c6 100644 --- a/frontend/src/app/components/blockchain-blocks/blockchain-blocks.component.html +++ b/frontend/src/app/components/blockchain-blocks/blockchain-blocks.component.html @@ -47,7 +47,7 @@ {{ i }} transactions
-
+
- + {{ diffChange.height }} - + {{ diffChange.difficultyShorten }} diff --git a/frontend/src/app/components/difficulty/difficulty.component.html b/frontend/src/app/components/difficulty/difficulty.component.html index e030f74fa..ce0bf7eff 100644 --- a/frontend/src/app/components/difficulty/difficulty.component.html +++ b/frontend/src/app/components/difficulty/difficulty.component.html @@ -10,7 +10,7 @@ {{ i }} blocks {{ i }} block
-
+
Estimate
@@ -53,7 +53,7 @@ {{ i }} blocks {{ i }} block
-
+
diff --git a/frontend/src/app/components/graphs/graphs.component.html b/frontend/src/app/components/graphs/graphs.component.html index af5136a38..105c6cbf2 100644 --- a/frontend/src/app/components/graphs/graphs.component.html +++ b/frontend/src/app/components/graphs/graphs.component.html @@ -1,10 +1,9 @@ -