From 0177224ebad3c3b51fd3b9d083929f80d13be551 Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 9 Jun 2020 02:08:46 +0700 Subject: [PATCH] Adding types to callback functions. --- backend/src/api/blocks.ts | 12 +++++++----- backend/src/api/mempool.ts | 8 +++++--- backend/src/api/statistics.ts | 8 +++++--- backend/src/api/websocket-handler.ts | 5 ++--- backend/src/interfaces.ts | 15 ++++++++++++++- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 061269146..a19d10a01 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -1,13 +1,13 @@ const config = require('../../mempool-config.json'); import bitcoinApi from './bitcoin/electrs-api'; import memPool from './mempool'; -import { Block, TransactionExtended } from '../interfaces'; +import { Block, TransactionExtended, TransactionMinerInfo } from '../interfaces'; import { Common } from './common'; class Blocks { private blocks: Block[] = []; private currentBlockHeight = 0; - private newBlockCallback: Function = () => {}; + private newBlockCallback: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void) | undefined; constructor() { } @@ -19,7 +19,7 @@ class Blocks { this.blocks = blocks; } - public setNewBlockCallback(fn: Function) { + public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) { this.newBlockCallback = fn; } @@ -83,7 +83,9 @@ class Blocks { this.blocks.shift(); } - this.newBlockCallback(block, txIds, transactions); + if (this.newBlockCallback) { + this.newBlockCallback(block, txIds, transactions); + } } } catch (err) { @@ -91,7 +93,7 @@ class Blocks { } } - private stripCoinbaseTransaction(tx: TransactionExtended): any { + private stripCoinbaseTransaction(tx: TransactionExtended): TransactionMinerInfo { return { vin: [{ scriptsig: tx.vin[0].scriptsig diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index cc5b84625..9e62528d5 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -6,7 +6,8 @@ class Mempool { private inSync: boolean = false; private mempoolCache: { [txId: string]: TransactionExtended } = {}; private mempoolInfo: MempoolInfo = { size: 0, bytes: 0 }; - private mempoolChangedCallback: Function | undefined; + private mempoolChangedCallback: ((newMempool: { [txId: string]: TransactionExtended; }, newTransactions: TransactionExtended[], + deletedTransactions: TransactionExtended[]) => void) | undefined; private txPerSecondArray: number[] = []; private txPerSecond: number = 0; @@ -22,7 +23,8 @@ class Mempool { return this.inSync; } - public setMempoolChangedCallback(fn: Function) { + public setMempoolChangedCallback(fn: (newMempool: { [txId: string]: TransactionExtended; }, + newTransactions: TransactionExtended[], deletedTransactions: TransactionExtended[]) => void) { this.mempoolChangedCallback = fn; } @@ -122,7 +124,7 @@ class Mempool { // Replace mempool to clear deleted transactions const newMempool = {}; - const deletedTransactions: Transaction[] = []; + const deletedTransactions: TransactionExtended[] = []; for (const tx in this.mempoolCache) { if (transactions.indexOf(tx) > -1) { newMempool[tx] = this.mempoolCache[tx]; diff --git a/backend/src/api/statistics.ts b/backend/src/api/statistics.ts index 116b48e35..fcbb050ad 100644 --- a/backend/src/api/statistics.ts +++ b/backend/src/api/statistics.ts @@ -5,9 +5,9 @@ import { Statistic, TransactionExtended, OptimizedStatistic } from '../interface class Statistics { protected intervalTimer: NodeJS.Timer | undefined; - protected newStatisticsEntryCallback: Function | undefined; + protected newStatisticsEntryCallback: ((stats: OptimizedStatistic) => void) | undefined; - public setNewStatisticsEntryCallback(fn: Function) { + public setNewStatisticsEntryCallback(fn: (stats: OptimizedStatistic) => void) { this.newStatisticsEntryCallback = fn; } @@ -128,7 +128,9 @@ class Statistics { if (this.newStatisticsEntryCallback && insertId) { const newStats = await this.$get(insertId); - this.newStatisticsEntryCallback(newStats); + if (newStats) { + this.newStatisticsEntryCallback(newStats); + } } } diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index 87e187c9f..fef583d4e 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -1,13 +1,12 @@ const config = require('../../mempool-config.json'); import * as WebSocket from 'ws'; -import { Block, TransactionExtended, Statistic, WebsocketResponse, MempoolBlock } from '../interfaces'; +import { Block, TransactionExtended, WebsocketResponse, MempoolBlock, OptimizedStatistic } from '../interfaces'; import blocks from './blocks'; import memPool from './mempool'; import backendInfo from './backend-info'; import mempoolBlocks from './mempool-blocks'; import fiatConversion from './fiat-conversion'; -import * as os from 'os'; import { Common } from './common'; class WebsocketHandler { @@ -102,7 +101,7 @@ class WebsocketHandler { }); } - handleNewStatistic(stats: Statistic) { + handleNewStatistic(stats: OptimizedStatistic) { if (!this.wss) { throw new Error('WebSocket.Server is not set'); } diff --git a/backend/src/interfaces.ts b/backend/src/interfaces.ts index f14561847..a9500d42b 100644 --- a/backend/src/interfaces.ts +++ b/backend/src/interfaces.ts @@ -34,6 +34,19 @@ export interface Transaction { status: Status; } +export interface TransactionMinerInfo { + vin: VinStrippedToScriptsig[]; + vout: VoutStrippedToScriptPubkey[]; +} + +interface VinStrippedToScriptsig { + scriptsig: string; +} + +interface VoutStrippedToScriptPubkey { + scriptpubkey_address: string | undefined; +} + export interface TransactionExtended extends Transaction { vsize: number; feePerVsize: number; @@ -109,7 +122,7 @@ export interface Block { medianFee?: number; feeRange?: number[]; reward?: number; - coinbaseTx?: Transaction; + coinbaseTx?: TransactionMinerInfo; matchRate?: number; }