From 25aacb504687539bd7df93c6ee22bfb3d057e832 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 14 Mar 2023 14:52:34 +0900 Subject: [PATCH] Calculate similarity score with audit disabled --- backend/src/api/common.ts | 26 +++++++++++++++++++++++++- backend/src/api/websocket-handler.ts | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index f762cfc2c..df97c0292 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -1,4 +1,4 @@ -import { CpfpInfo, TransactionExtended, TransactionStripped } from '../mempool.interfaces'; +import { CpfpInfo, MempoolBlockWithTransactions, TransactionExtended, TransactionStripped } from '../mempool.interfaces'; import config from '../config'; import { NodeSocket } from '../repositories/NodesSocketsRepository'; import { isIP } from 'net'; @@ -164,6 +164,30 @@ export class Common { return parents; } + // calculates the ratio of matched transactions to projected transactions by weight + static getSimilarity(projectedBlock: MempoolBlockWithTransactions, transactions: TransactionExtended[]): number { + let matchedWeight = 0; + let projectedWeight = 0; + const inBlock = {}; + + for (const tx of transactions) { + inBlock[tx.txid] = tx; + } + + // look for transactions that were expected in the template, but missing from the mined block + for (const tx of projectedBlock.transactions) { + if (inBlock[tx.txid]) { + matchedWeight += tx.vsize * 4; + } + projectedWeight += tx.vsize * 4; + } + + projectedWeight += transactions[0].weight; + matchedWeight += transactions[0].weight; + + return projectedWeight ? matchedWeight / projectedWeight : 1; + } + static getSqlInterval(interval: string | null): string | null { switch (interval) { case '24h': return '1 DAY'; diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index 1eeb17d2e..07aac0790 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -467,6 +467,9 @@ class WebsocketHandler { block.extras.similarity = similarity; } } + } else if (block.extras) { + const mBlocks = mempoolBlocks.getMempoolBlocksWithTransactions(); + block.extras.similarity = Common.getSimilarity(mBlocks[0], transactions); } const removed: string[] = [];