2020-10-13 15:27:52 +07:00
|
|
|
import logger from '../logger';
|
2020-02-26 17:49:53 +07:00
|
|
|
import * as WebSocket from 'ws';
|
2022-07-04 08:37:36 -07:00
|
|
|
import {
|
|
|
|
BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, MempoolBlockDelta,
|
|
|
|
OptimizedStatistic, ILoadingIndicators, IConversionRates
|
|
|
|
} from '../mempool.interfaces';
|
2020-02-26 17:49:53 +07:00
|
|
|
import blocks from './blocks';
|
|
|
|
import memPool from './mempool';
|
2020-05-27 15:18:04 +07:00
|
|
|
import backendInfo from './backend-info';
|
2020-02-26 17:49:53 +07:00
|
|
|
import mempoolBlocks from './mempool-blocks';
|
|
|
|
import fiatConversion from './fiat-conversion';
|
2020-06-08 18:55:53 +07:00
|
|
|
import { Common } from './common';
|
2021-01-05 18:57:06 +07:00
|
|
|
import loadingIndicators from './loading-indicators';
|
2021-01-10 17:38:59 +07:00
|
|
|
import config from '../config';
|
|
|
|
import transactionUtils from './transaction-utils';
|
2022-03-08 14:49:25 +01:00
|
|
|
import rbfCache from './rbf-cache';
|
2022-03-12 14:55:42 +01:00
|
|
|
import difficultyAdjustment from './difficulty-adjustment';
|
2022-05-27 12:52:40 +02:00
|
|
|
import feeApi from './fee-api';
|
2022-07-06 22:27:45 +02:00
|
|
|
import BlocksAuditsRepository from '../repositories/BlocksAuditsRepository';
|
2022-07-07 19:11:42 +02:00
|
|
|
import BlocksSummariesRepository from '../repositories/BlocksSummariesRepository';
|
2022-10-19 17:10:45 +00:00
|
|
|
import Audit from './audit';
|
2020-02-26 17:49:53 +07:00
|
|
|
|
|
|
|
class WebsocketHandler {
|
|
|
|
private wss: WebSocket.Server | undefined;
|
2020-07-14 21:26:02 +07:00
|
|
|
private extraInitProperties = {};
|
2020-02-26 17:49:53 +07:00
|
|
|
|
2020-05-27 15:18:04 +07:00
|
|
|
constructor() { }
|
2020-02-26 17:49:53 +07:00
|
|
|
|
|
|
|
setWebsocketServer(wss: WebSocket.Server) {
|
|
|
|
this.wss = wss;
|
|
|
|
}
|
|
|
|
|
2020-07-14 21:26:02 +07:00
|
|
|
setExtraInitProperties(property: string, value: any) {
|
|
|
|
this.extraInitProperties[property] = value;
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
setupConnectionHandling() {
|
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.wss.on('connection', (client: WebSocket) => {
|
2020-11-15 11:43:34 +09:00
|
|
|
client.on('error', logger.info);
|
2021-04-27 02:13:48 +04:00
|
|
|
client.on('message', async (message: string) => {
|
2020-02-26 17:49:53 +07:00
|
|
|
try {
|
2020-04-13 02:06:10 +07:00
|
|
|
const parsedMessage: WebsocketResponse = JSON.parse(message);
|
|
|
|
const response = {};
|
2020-02-26 17:49:53 +07:00
|
|
|
|
|
|
|
if (parsedMessage.action === 'want') {
|
|
|
|
client['want-blocks'] = parsedMessage.data.indexOf('blocks') > -1;
|
|
|
|
client['want-mempool-blocks'] = parsedMessage.data.indexOf('mempool-blocks') > -1;
|
|
|
|
client['want-live-2h-chart'] = parsedMessage.data.indexOf('live-2h-chart') > -1;
|
|
|
|
client['want-stats'] = parsedMessage.data.indexOf('stats') > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedMessage && parsedMessage['track-tx']) {
|
|
|
|
if (/^[a-fA-F0-9]{64}$/.test(parsedMessage['track-tx'])) {
|
|
|
|
client['track-tx'] = parsedMessage['track-tx'];
|
2022-03-08 14:49:25 +01:00
|
|
|
// Client is telling the transaction wasn't found
|
2020-04-13 02:06:10 +07:00
|
|
|
if (parsedMessage['watch-mempool']) {
|
2022-03-08 14:49:25 +01:00
|
|
|
const rbfCacheTx = rbfCache.get(client['track-tx']);
|
|
|
|
if (rbfCacheTx) {
|
|
|
|
response['txReplaced'] = {
|
|
|
|
txid: rbfCacheTx.txid,
|
|
|
|
};
|
|
|
|
client['track-tx'] = null;
|
|
|
|
} else {
|
|
|
|
// It might have appeared before we had the time to start watching for it
|
|
|
|
const tx = memPool.getMempool()[client['track-tx']];
|
|
|
|
if (tx) {
|
|
|
|
if (config.MEMPOOL.BACKEND === 'esplora') {
|
|
|
|
response['tx'] = tx;
|
|
|
|
} else {
|
|
|
|
// tx.prevout is missing from transactions when in bitcoind mode
|
|
|
|
try {
|
|
|
|
const fullTx = await transactionUtils.$getTransactionExtended(tx.txid, true);
|
|
|
|
response['tx'] = fullTx;
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug('Error finding transaction: ' + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 13:01:29 +03:00
|
|
|
} else {
|
2021-04-27 02:13:48 +04:00
|
|
|
try {
|
2022-03-08 14:49:25 +01:00
|
|
|
const fullTx = await transactionUtils.$getTransactionExtended(client['track-tx'], true);
|
2021-04-27 02:13:48 +04:00
|
|
|
response['tx'] = fullTx;
|
|
|
|
} catch (e) {
|
2022-03-08 14:49:25 +01:00
|
|
|
logger.debug('Error finding transaction. ' + (e instanceof Error ? e.message : e));
|
|
|
|
client['track-mempool-tx'] = parsedMessage['track-tx'];
|
2021-04-27 02:13:48 +04:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 02:06:10 +07:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 17:49:53 +07:00
|
|
|
} else {
|
|
|
|
client['track-tx'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedMessage && parsedMessage['track-address']) {
|
2021-09-04 23:21:15 +04:00
|
|
|
if (/^([a-km-zA-HJ-NP-Z1-9]{26,35}|[a-km-zA-HJ-NP-Z1-9]{80}|[a-z]{2,5}1[ac-hj-np-z02-9]{8,100}|[A-Z]{2,5}1[AC-HJ-NP-Z02-9]{8,100})$/
|
2020-02-26 17:49:53 +07:00
|
|
|
.test(parsedMessage['track-address'])) {
|
2021-09-05 00:30:24 +04:00
|
|
|
let matchedAddress = parsedMessage['track-address'];
|
|
|
|
if (/^[A-Z]{2,5}1[AC-HJ-NP-Z02-9]{8,100}$/.test(parsedMessage['track-address'])) {
|
|
|
|
matchedAddress = matchedAddress.toLowerCase();
|
|
|
|
}
|
|
|
|
client['track-address'] = matchedAddress;
|
2020-02-26 17:49:53 +07:00
|
|
|
} else {
|
|
|
|
client['track-address'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:26:23 +07:00
|
|
|
if (parsedMessage && parsedMessage['track-asset']) {
|
|
|
|
if (/^[a-fA-F0-9]{64}$/.test(parsedMessage['track-asset'])) {
|
|
|
|
client['track-asset'] = parsedMessage['track-asset'];
|
|
|
|
} else {
|
|
|
|
client['track-asset'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
if (parsedMessage && parsedMessage['track-mempool-block'] !== undefined) {
|
2022-05-30 17:29:30 +00:00
|
|
|
if (Number.isInteger(parsedMessage['track-mempool-block']) && parsedMessage['track-mempool-block'] >= 0) {
|
|
|
|
const index = parsedMessage['track-mempool-block'];
|
|
|
|
client['track-mempool-block'] = index;
|
|
|
|
const mBlocksWithTransactions = mempoolBlocks.getMempoolBlocksWithTransactions();
|
2022-06-06 14:31:17 +04:00
|
|
|
response['projected-block-transactions'] = {
|
|
|
|
index: index,
|
|
|
|
blockTransactions: mBlocksWithTransactions[index]?.transactions || [],
|
|
|
|
};
|
2022-05-30 17:29:30 +00:00
|
|
|
} else {
|
|
|
|
client['track-mempool-block'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
if (parsedMessage.action === 'init') {
|
2021-07-31 17:56:10 +03:00
|
|
|
const _blocks = blocks.getBlocks().slice(-config.MEMPOOL.INITIAL_BLOCKS_AMOUNT);
|
2020-02-26 17:49:53 +07:00
|
|
|
if (!_blocks) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-23 02:38:56 +07:00
|
|
|
client.send(JSON.stringify(this.getInitData(_blocks)));
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
2020-03-06 02:05:26 +07:00
|
|
|
|
|
|
|
if (parsedMessage.action === 'ping') {
|
2020-04-13 02:06:10 +07:00
|
|
|
response['pong'] = true;
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:15:42 +07:00
|
|
|
if (parsedMessage['track-donation'] && parsedMessage['track-donation'].length === 22) {
|
|
|
|
client['track-donation'] = parsedMessage['track-donation'];
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:38:46 +07:00
|
|
|
if (parsedMessage['track-bisq-market']) {
|
|
|
|
if (/^[a-z]{3}_[a-z]{3}$/.test(parsedMessage['track-bisq-market'])) {
|
|
|
|
client['track-bisq-market'] = parsedMessage['track-bisq-market'];
|
|
|
|
} else {
|
|
|
|
client['track-bisq-market'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:06:10 +07:00
|
|
|
if (Object.keys(response).length) {
|
|
|
|
client.send(JSON.stringify(response));
|
2020-03-06 02:05:26 +07:00
|
|
|
}
|
2020-02-26 17:49:53 +07:00
|
|
|
} catch (e) {
|
2021-08-31 15:09:33 +03:00
|
|
|
logger.debug('Error parsing websocket message: ' + (e instanceof Error ? e.message : e));
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:15:42 +07:00
|
|
|
handleNewDonation(id: string) {
|
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:37:36 -07:00
|
|
|
this.wss.clients.forEach((client) => {
|
2020-10-07 20:15:42 +07:00
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (client['track-donation'] === id) {
|
|
|
|
client.send(JSON.stringify({ donationConfirmed: true }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:57:06 +07:00
|
|
|
handleLoadingChanged(indicators: ILoadingIndicators) {
|
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:37:36 -07:00
|
|
|
this.wss.clients.forEach((client) => {
|
2021-01-05 18:57:06 +07:00
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client.send(JSON.stringify({ loadingIndicators: indicators }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-06 23:31:33 +07:00
|
|
|
handleNewConversionRates(conversionRates: IConversionRates) {
|
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:37:36 -07:00
|
|
|
this.wss.clients.forEach((client) => {
|
2021-01-06 23:31:33 +07:00
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
client.send(JSON.stringify({ conversions: conversionRates }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:47:22 +07:00
|
|
|
getInitData(_blocks?: BlockExtended[]) {
|
2020-11-23 02:38:56 +07:00
|
|
|
if (!_blocks) {
|
2021-07-31 17:56:10 +03:00
|
|
|
_blocks = blocks.getBlocks().slice(-config.MEMPOOL.INITIAL_BLOCKS_AMOUNT);
|
2020-11-23 02:38:56 +07:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
'mempoolInfo': memPool.getMempoolInfo(),
|
|
|
|
'vBytesPerSecond': memPool.getVBytesPerSecond(),
|
|
|
|
'blocks': _blocks,
|
2020-11-23 11:46:04 +07:00
|
|
|
'conversions': fiatConversion.getConversionRates(),
|
2020-11-23 02:38:56 +07:00
|
|
|
'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
|
|
|
|
'transactions': memPool.getLatestTransactions(),
|
2021-04-12 22:17:13 +04:00
|
|
|
'backendInfo': backendInfo.getBackendInfo(),
|
2021-01-05 18:57:06 +07:00
|
|
|
'loadingIndicators': loadingIndicators.getLoadingIndicators(),
|
2022-03-12 14:55:42 +01:00
|
|
|
'da': difficultyAdjustment.getDifficultyAdjustment(),
|
2022-06-01 00:03:25 +04:00
|
|
|
'fees': feeApi.getRecommendedFee(),
|
2020-11-23 02:38:56 +07:00
|
|
|
...this.extraInitProperties
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:08:46 +07:00
|
|
|
handleNewStatistic(stats: OptimizedStatistic) {
|
2020-02-26 17:49:53 +07:00
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:37:36 -07:00
|
|
|
this.wss.clients.forEach((client) => {
|
2020-02-26 17:49:53 +07:00
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!client['want-live-2h-chart']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.send(JSON.stringify({
|
|
|
|
'live-2h-chart': stats
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
async handleMempoolChange(newMempool: { [txid: string]: TransactionExtended },
|
|
|
|
newTransactions: TransactionExtended[], deletedTransactions: TransactionExtended[]): Promise<void> {
|
2020-02-26 17:49:53 +07:00
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (config.MEMPOOL.ADVANCED_TRANSACTION_SELECTION) {
|
|
|
|
await mempoolBlocks.makeBlockTemplates(newMempool, 8, null, true);
|
2022-11-16 18:18:59 -06:00
|
|
|
}
|
2022-11-20 16:12:39 +09:00
|
|
|
else {
|
2022-11-16 18:18:59 -06:00
|
|
|
mempoolBlocks.updateMempoolBlocks(newMempool);
|
|
|
|
}
|
2022-11-20 16:12:39 +09:00
|
|
|
const mBlocks = mempoolBlocks.getMempoolBlocks();
|
|
|
|
const mBlockDeltas = mempoolBlocks.getMempoolBlockDeltas();
|
2020-02-26 17:49:53 +07:00
|
|
|
const mempoolInfo = memPool.getMempoolInfo();
|
|
|
|
const vBytesPerSecond = memPool.getVBytesPerSecond();
|
2020-06-08 18:55:53 +07:00
|
|
|
const rbfTransactions = Common.findRbfTransactions(newTransactions, deletedTransactions);
|
2022-03-12 14:55:42 +01:00
|
|
|
const da = difficultyAdjustment.getDifficultyAdjustment();
|
2022-03-08 14:49:25 +01:00
|
|
|
memPool.handleRbfTransactions(rbfTransactions);
|
2022-05-27 12:52:40 +02:00
|
|
|
const recommendedFees = feeApi.getRecommendedFee();
|
2021-03-21 06:06:03 +07:00
|
|
|
|
2022-07-04 08:37:36 -07:00
|
|
|
this.wss.clients.forEach(async (client) => {
|
2020-02-26 17:49:53 +07:00
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = {};
|
|
|
|
|
|
|
|
if (client['want-stats']) {
|
|
|
|
response['mempoolInfo'] = mempoolInfo;
|
|
|
|
response['vBytesPerSecond'] = vBytesPerSecond;
|
2020-10-06 13:31:33 +07:00
|
|
|
response['transactions'] = newTransactions.slice(0, 6).map((tx) => Common.stripTransaction(tx));
|
2022-03-12 14:55:42 +01:00
|
|
|
response['da'] = da;
|
2022-05-27 12:52:40 +02:00
|
|
|
response['fees'] = recommendedFees;
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (client['want-mempool-blocks']) {
|
2020-02-26 17:49:53 +07:00
|
|
|
response['mempool-blocks'] = mBlocks;
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:06:10 +07:00
|
|
|
if (client['track-mempool-tx']) {
|
|
|
|
const tx = newTransactions.find((t) => t.txid === client['track-mempool-tx']);
|
2020-04-13 01:26:53 +07:00
|
|
|
if (tx) {
|
2021-01-10 17:38:59 +07:00
|
|
|
if (config.MEMPOOL.BACKEND !== 'esplora') {
|
2021-01-24 02:51:22 +07:00
|
|
|
try {
|
2021-01-24 04:15:06 +07:00
|
|
|
const fullTx = await transactionUtils.$getTransactionExtended(tx.txid, true);
|
2021-01-10 17:38:59 +07:00
|
|
|
response['tx'] = fullTx;
|
2021-01-24 02:51:22 +07:00
|
|
|
} catch (e) {
|
2021-08-31 15:09:33 +03:00
|
|
|
logger.debug('Error finding transaction in mempool: ' + (e instanceof Error ? e.message : e));
|
2021-01-10 17:38:59 +07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
response['tx'] = tx;
|
|
|
|
}
|
2020-04-13 02:06:10 +07:00
|
|
|
client['track-mempool-tx'] = null;
|
2020-04-13 01:26:53 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:00:50 +07:00
|
|
|
if (client['track-address']) {
|
|
|
|
const foundTransactions: TransactionExtended[] = [];
|
|
|
|
|
2021-01-10 17:38:59 +07:00
|
|
|
for (const tx of newTransactions) {
|
2020-05-20 17:00:50 +07:00
|
|
|
const someVin = tx.vin.some((vin) => !!vin.prevout && vin.prevout.scriptpubkey_address === client['track-address']);
|
|
|
|
if (someVin) {
|
2021-01-10 17:38:59 +07:00
|
|
|
if (config.MEMPOOL.BACKEND !== 'esplora') {
|
2021-01-24 02:51:22 +07:00
|
|
|
try {
|
2021-01-24 04:15:06 +07:00
|
|
|
const fullTx = await transactionUtils.$getTransactionExtended(tx.txid, true);
|
2021-01-10 17:38:59 +07:00
|
|
|
foundTransactions.push(fullTx);
|
2021-01-24 02:51:22 +07:00
|
|
|
} catch (e) {
|
2021-08-31 15:09:33 +03:00
|
|
|
logger.debug('Error finding transaction in mempool: ' + (e instanceof Error ? e.message : e));
|
2021-01-10 17:38:59 +07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
2020-05-20 17:00:50 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const someVout = tx.vout.some((vout) => vout.scriptpubkey_address === client['track-address']);
|
|
|
|
if (someVout) {
|
2021-01-10 17:38:59 +07:00
|
|
|
if (config.MEMPOOL.BACKEND !== 'esplora') {
|
2021-01-24 02:51:22 +07:00
|
|
|
try {
|
2021-01-24 04:15:06 +07:00
|
|
|
const fullTx = await transactionUtils.$getTransactionExtended(tx.txid, true);
|
2021-01-10 17:38:59 +07:00
|
|
|
foundTransactions.push(fullTx);
|
2021-01-24 02:51:22 +07:00
|
|
|
} catch (e) {
|
2021-08-31 15:09:33 +03:00
|
|
|
logger.debug('Error finding transaction in mempool: ' + (e instanceof Error ? e.message : e));
|
2021-01-10 17:38:59 +07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
2020-05-20 17:00:50 +07:00
|
|
|
}
|
2021-01-10 17:38:59 +07:00
|
|
|
}
|
2020-05-20 17:00:50 +07:00
|
|
|
|
|
|
|
if (foundTransactions.length) {
|
|
|
|
response['address-transactions'] = foundTransactions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:26:23 +07:00
|
|
|
if (client['track-asset']) {
|
2020-02-26 17:49:53 +07:00
|
|
|
const foundTransactions: TransactionExtended[] = [];
|
|
|
|
|
|
|
|
newTransactions.forEach((tx) => {
|
2020-05-10 01:34:28 +07:00
|
|
|
|
2021-09-18 13:37:25 +04:00
|
|
|
if (client['track-asset'] === Common.nativeAssetId) {
|
2020-05-10 01:34:28 +07:00
|
|
|
if (tx.vin.some((vin) => !!vin.is_pegin)) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tx.vout.some((vout) => !!vout.pegout)) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tx.vin.some((vin) => !!vin.issuance && vin.issuance.asset_id === client['track-asset'])) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tx.vout.some((vout) => !!vout.asset && vout.asset === client['track-asset'])) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (foundTransactions.length) {
|
2020-05-20 17:00:50 +07:00
|
|
|
response['address-transactions'] = foundTransactions;
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:27:13 +01:00
|
|
|
if (client['track-tx']) {
|
2022-03-07 19:45:09 +01:00
|
|
|
const outspends: object = {};
|
|
|
|
newTransactions.forEach((tx) => tx.vin.forEach((vin, i) => {
|
|
|
|
if (vin.txid === client['track-tx']) {
|
|
|
|
outspends[vin.vout] = {
|
|
|
|
vin: i,
|
|
|
|
txid: tx.txid,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (Object.keys(outspends).length) {
|
|
|
|
response['utxoSpent'] = outspends;
|
2022-03-06 18:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rbfTransactions[client['track-tx']]) {
|
|
|
|
for (const rbfTransaction in rbfTransactions) {
|
|
|
|
if (client['track-tx'] === rbfTransaction) {
|
2022-03-08 18:54:49 +01:00
|
|
|
response['rbfTransaction'] = {
|
|
|
|
txid: rbfTransactions[rbfTransaction].txid,
|
|
|
|
};
|
2022-03-06 18:27:13 +01:00
|
|
|
break;
|
2021-01-10 17:38:59 +07:00
|
|
|
}
|
2020-06-08 18:55:53 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (client['track-mempool-block'] >= 0) {
|
2022-05-30 17:29:30 +00:00
|
|
|
const index = client['track-mempool-block'];
|
2022-05-31 21:36:42 +00:00
|
|
|
if (mBlockDeltas[index]) {
|
2022-06-02 01:29:03 +04:00
|
|
|
response['projected-block-transactions'] = {
|
2022-05-30 17:29:30 +00:00
|
|
|
index: index,
|
2022-05-31 21:36:42 +00:00
|
|
|
delta: mBlockDeltas[index],
|
2022-05-30 17:29:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
if (Object.keys(response).length) {
|
|
|
|
client.send(JSON.stringify(response));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-11-20 16:12:39 +09:00
|
|
|
|
|
|
|
async handleNewBlock(block: BlockExtended, txIds: string[], transactions: TransactionExtended[]): Promise<void> {
|
2022-11-16 18:18:59 -06:00
|
|
|
if (!this.wss) {
|
|
|
|
throw new Error('WebSocket.Server is not set');
|
|
|
|
}
|
|
|
|
|
|
|
|
const _memPool = memPool.getMempool();
|
2022-11-20 16:12:39 +09:00
|
|
|
let matchRate;
|
2022-11-16 18:18:59 -06:00
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (config.MEMPOOL.ADVANCED_TRANSACTION_SELECTION) {
|
|
|
|
await mempoolBlocks.makeBlockTemplates(_memPool, 2);
|
|
|
|
} else {
|
|
|
|
mempoolBlocks.updateMempoolBlocks(_memPool);
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
|
2022-10-31 11:07:28 -06:00
|
|
|
if (Common.indexingEnabled() && memPool.isInSync()) {
|
2022-11-16 18:17:07 -06:00
|
|
|
const projectedBlocks = mempoolBlocks.getMempoolBlocksWithTransactions();
|
2021-03-21 06:06:03 +07:00
|
|
|
|
2022-11-16 18:17:07 -06:00
|
|
|
const { censored, added, score } = Audit.auditBlock(transactions, projectedBlocks, _memPool);
|
2022-10-19 17:10:45 +00:00
|
|
|
matchRate = Math.round(score * 100 * 100) / 100;
|
2022-07-06 22:27:45 +02:00
|
|
|
|
2022-10-28 15:16:03 -06:00
|
|
|
const stripped = projectedBlocks[0]?.transactions ? projectedBlocks[0].transactions.map((tx) => {
|
|
|
|
return {
|
|
|
|
txid: tx.txid,
|
|
|
|
vsize: tx.vsize,
|
|
|
|
fee: tx.fee ? Math.round(tx.fee) : 0,
|
|
|
|
value: tx.value,
|
|
|
|
};
|
|
|
|
}) : [];
|
|
|
|
|
|
|
|
BlocksSummariesRepository.$saveSummary({
|
|
|
|
height: block.height,
|
|
|
|
template: {
|
|
|
|
id: block.id,
|
|
|
|
transactions: stripped
|
|
|
|
}
|
|
|
|
});
|
2020-06-08 02:08:51 +07:00
|
|
|
|
2022-10-28 15:16:03 -06:00
|
|
|
BlocksAuditsRepository.$saveAudit({
|
|
|
|
time: block.timestamp,
|
|
|
|
height: block.height,
|
|
|
|
hash: block.id,
|
|
|
|
addedTxs: added,
|
|
|
|
missingTxs: censored,
|
|
|
|
matchRate: matchRate,
|
|
|
|
});
|
2022-07-07 19:11:42 +02:00
|
|
|
|
2022-10-28 15:16:03 -06:00
|
|
|
if (block.extras) {
|
|
|
|
block.extras.matchRate = matchRate;
|
2022-07-06 22:27:45 +02:00
|
|
|
}
|
2020-06-08 02:08:51 +07:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:16:03 -06:00
|
|
|
// Update mempool to remove transactions included in the new block
|
|
|
|
for (const txId of txIds) {
|
|
|
|
delete _memPool[txId];
|
2022-02-04 12:51:45 +09:00
|
|
|
}
|
2020-06-08 02:08:51 +07:00
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (config.MEMPOOL.ADVANCED_TRANSACTION_SELECTION) {
|
|
|
|
await mempoolBlocks.makeBlockTemplates(_memPool, 2);
|
|
|
|
} else {
|
2022-11-16 18:18:59 -06:00
|
|
|
mempoolBlocks.updateMempoolBlocks(_memPool);
|
|
|
|
}
|
2022-11-20 16:12:39 +09:00
|
|
|
const mBlocks = mempoolBlocks.getMempoolBlocks();
|
|
|
|
const mBlockDeltas = mempoolBlocks.getMempoolBlockDeltas();
|
2022-10-28 15:16:03 -06:00
|
|
|
|
2022-06-01 00:03:25 +04:00
|
|
|
const da = difficultyAdjustment.getDifficultyAdjustment();
|
|
|
|
const fees = feeApi.getRecommendedFee();
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
this.wss.clients.forEach((client) => {
|
|
|
|
if (client.readyState !== WebSocket.OPEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!client['want-blocks']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = {
|
2020-07-20 10:44:05 +07:00
|
|
|
'block': block,
|
|
|
|
'mempoolInfo': memPool.getMempoolInfo(),
|
2022-06-01 00:03:25 +04:00
|
|
|
'da': da,
|
|
|
|
'fees': fees,
|
2020-02-26 17:49:53 +07:00
|
|
|
};
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (mBlocks && client['want-mempool-blocks']) {
|
2020-06-08 02:08:51 +07:00
|
|
|
response['mempool-blocks'] = mBlocks;
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
if (client['track-tx'] && txIds.indexOf(client['track-tx']) > -1) {
|
|
|
|
response['txConfirmed'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client['track-address']) {
|
|
|
|
const foundTransactions: TransactionExtended[] = [];
|
|
|
|
|
|
|
|
transactions.forEach((tx) => {
|
2020-03-09 17:53:54 +07:00
|
|
|
if (tx.vin && tx.vin.some((vin) => !!vin.prevout && vin.prevout.scriptpubkey_address === client['track-address'])) {
|
2020-02-26 17:49:53 +07:00
|
|
|
foundTransactions.push(tx);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-20 17:00:50 +07:00
|
|
|
if (tx.vout && tx.vout.some((vout) => vout.scriptpubkey_address === client['track-address'])) {
|
2020-05-05 15:26:23 +07:00
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (foundTransactions.length) {
|
|
|
|
foundTransactions.forEach((tx) => {
|
|
|
|
tx.status = {
|
|
|
|
confirmed: true,
|
|
|
|
block_height: block.height,
|
|
|
|
block_hash: block.id,
|
|
|
|
block_time: block.timestamp,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-20 17:00:50 +07:00
|
|
|
response['block-transactions'] = foundTransactions;
|
2020-05-05 15:26:23 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client['track-asset']) {
|
|
|
|
const foundTransactions: TransactionExtended[] = [];
|
|
|
|
|
|
|
|
transactions.forEach((tx) => {
|
2021-09-18 13:37:25 +04:00
|
|
|
if (client['track-asset'] === Common.nativeAssetId) {
|
2020-05-10 01:34:28 +07:00
|
|
|
if (tx.vin && tx.vin.some((vin) => !!vin.is_pegin)) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tx.vout && tx.vout.some((vout) => !!vout.pegout)) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tx.vin && tx.vin.some((vin) => !!vin.issuance && vin.issuance.asset_id === client['track-asset'])) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tx.vout && tx.vout.some((vout) => !!vout.asset && vout.asset === client['track-asset'])) {
|
|
|
|
foundTransactions.push(tx);
|
|
|
|
}
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (foundTransactions.length) {
|
2020-02-26 23:21:16 +07:00
|
|
|
foundTransactions.forEach((tx) => {
|
|
|
|
tx.status = {
|
|
|
|
confirmed: true,
|
|
|
|
block_height: block.height,
|
|
|
|
block_hash: block.id,
|
|
|
|
block_time: block.timestamp,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-20 17:00:50 +07:00
|
|
|
response['block-transactions'] = foundTransactions;
|
2020-02-26 17:49:53 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 16:12:39 +09:00
|
|
|
if (client['track-mempool-block'] >= 0) {
|
2022-05-31 21:36:42 +00:00
|
|
|
const index = client['track-mempool-block'];
|
|
|
|
if (mBlockDeltas && mBlockDeltas[index]) {
|
2022-06-02 01:29:03 +04:00
|
|
|
response['projected-block-transactions'] = {
|
2022-05-31 21:36:42 +00:00
|
|
|
index: index,
|
|
|
|
delta: mBlockDeltas[index],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:49:53 +07:00
|
|
|
client.send(JSON.stringify(response));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new WebsocketHandler();
|