Basic address tracking.
This commit is contained in:
@@ -70,7 +70,7 @@ class ElectrsApi {
|
||||
});
|
||||
}
|
||||
|
||||
getTxIdsForBlock(hash: string): Promise<Block> {
|
||||
getTxIdsForBlock(hash: string): Promise<string[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
request(config.ELECTRS_API_URL + '/block/' + hash + '/txids', { json: true, timeout: 10000 }, (err, res, response) => {
|
||||
if (err) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const config = require('../../mempool-config.json');
|
||||
import bitcoinApi from './bitcoin/electrs-api';
|
||||
import { Block } from '../interfaces';
|
||||
import memPool from './mempool';
|
||||
import { Block, TransactionExtended } from '../interfaces';
|
||||
|
||||
class Blocks {
|
||||
private blocks: Block[] = [];
|
||||
@@ -39,15 +40,36 @@ class Blocks {
|
||||
const block = await bitcoinApi.getBlock(blockHash);
|
||||
const txIds = await bitcoinApi.getTxIdsForBlock(blockHash);
|
||||
|
||||
block.medianFee = 2;
|
||||
block.feeRange = [1, 3];
|
||||
const mempool = memPool.getMempool();
|
||||
let found = 0;
|
||||
let notFound = 0;
|
||||
|
||||
const transactions: TransactionExtended[] = [];
|
||||
|
||||
for (let i = 1; i < txIds.length; i++) {
|
||||
if (mempool[txIds[i]]) {
|
||||
transactions.push(mempool[txIds[i]]);
|
||||
found++;
|
||||
} else {
|
||||
console.log(`Fetching block tx ${i} of ${txIds.length}`);
|
||||
const tx = await memPool.getTransactionExtended(txIds[i]);
|
||||
if (tx) {
|
||||
transactions.push(tx);
|
||||
}
|
||||
notFound++;
|
||||
}
|
||||
}
|
||||
|
||||
transactions.sort((a, b) => b.feePerVsize - a.feePerVsize);
|
||||
block.medianFee = this.median(transactions.map((tx) => tx.feePerVsize));
|
||||
block.feeRange = this.getFeesInRange(transactions, 8);
|
||||
|
||||
this.blocks.push(block);
|
||||
if (this.blocks.length > config.KEEP_BLOCK_AMOUNT) {
|
||||
this.blocks.shift();
|
||||
}
|
||||
|
||||
this.newBlockCallback(block, txIds);
|
||||
this.newBlockCallback(block, txIds, transactions);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const config = require('../../mempool-config.json');
|
||||
import { MempoolBlock, SimpleTransaction } from '../interfaces';
|
||||
import { MempoolBlock, TransactionExtended } from '../interfaces';
|
||||
|
||||
class MempoolBlocks {
|
||||
private mempoolBlocks: MempoolBlock[] = [];
|
||||
@@ -10,9 +10,9 @@ class MempoolBlocks {
|
||||
return this.mempoolBlocks;
|
||||
}
|
||||
|
||||
public updateMempoolBlocks(memPool: { [txid: string]: SimpleTransaction }): void {
|
||||
public updateMempoolBlocks(memPool: { [txid: string]: TransactionExtended }): void {
|
||||
const latestMempool = memPool;
|
||||
const memPoolArray: SimpleTransaction[] = [];
|
||||
const memPoolArray: TransactionExtended[] = [];
|
||||
for (const i in latestMempool) {
|
||||
if (latestMempool.hasOwnProperty(i)) {
|
||||
memPoolArray.push(latestMempool[i]);
|
||||
@@ -23,11 +23,11 @@ class MempoolBlocks {
|
||||
this.mempoolBlocks = this.calculateMempoolBlocks(transactionsSorted);
|
||||
}
|
||||
|
||||
private calculateMempoolBlocks(transactionsSorted: SimpleTransaction[]): MempoolBlock[] {
|
||||
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlock[] {
|
||||
const mempoolBlocks: MempoolBlock[] = [];
|
||||
let blockWeight = 0;
|
||||
let blockSize = 0;
|
||||
let transactions: SimpleTransaction[] = [];
|
||||
let transactions: TransactionExtended[] = [];
|
||||
transactionsSorted.forEach((tx) => {
|
||||
if (blockWeight + tx.vsize < 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT) {
|
||||
blockWeight += tx.vsize;
|
||||
@@ -46,7 +46,7 @@ class MempoolBlocks {
|
||||
return mempoolBlocks;
|
||||
}
|
||||
|
||||
private dataToMempoolBlocks(transactions: SimpleTransaction[], blockSize: number, blockVSize: number, blocksIndex: number): MempoolBlock {
|
||||
private dataToMempoolBlocks(transactions: TransactionExtended[], blockSize: number, blockVSize: number, blocksIndex: number): MempoolBlock {
|
||||
let rangeLength = 3;
|
||||
if (blocksIndex === 0) {
|
||||
rangeLength = 8;
|
||||
@@ -79,7 +79,7 @@ class MempoolBlocks {
|
||||
return medianNr;
|
||||
}
|
||||
|
||||
private getFeesInRange(transactions: SimpleTransaction[], rangeLength: number) {
|
||||
private getFeesInRange(transactions: TransactionExtended[], rangeLength: number) {
|
||||
const arr = [transactions[transactions.length - 1].feePerVsize];
|
||||
const chunk = 1 / (rangeLength - 1);
|
||||
let itemsToAdd = rangeLength - 2;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const config = require('../../mempool-config.json');
|
||||
import bitcoinApi from './bitcoin/electrs-api';
|
||||
import { MempoolInfo, SimpleTransaction, Transaction } from '../interfaces';
|
||||
import { MempoolInfo, TransactionExtended, Transaction } from '../interfaces';
|
||||
|
||||
class Mempool {
|
||||
private mempoolCache: any = {};
|
||||
@@ -21,7 +21,7 @@ class Mempool {
|
||||
this.mempoolChangedCallback = fn;
|
||||
}
|
||||
|
||||
public getMempool(): { [txid: string]: SimpleTransaction } {
|
||||
public getMempool(): { [txid: string]: TransactionExtended } {
|
||||
return this.mempoolCache;
|
||||
}
|
||||
|
||||
@@ -52,16 +52,13 @@ class Mempool {
|
||||
return this.vBytesPerSecond;
|
||||
}
|
||||
|
||||
public async getRawTransaction(txId: string): Promise<SimpleTransaction | false> {
|
||||
public async getTransactionExtended(txId: string): Promise<TransactionExtended | false> {
|
||||
try {
|
||||
const transaction: Transaction = await bitcoinApi.getRawTransaction(txId);
|
||||
return {
|
||||
txid: transaction.txid,
|
||||
fee: transaction.fee,
|
||||
size: transaction.size,
|
||||
return Object.assign({
|
||||
vsize: transaction.weight / 4,
|
||||
feePerVsize: transaction.fee / (transaction.weight / 4)
|
||||
};
|
||||
feePerVsize: transaction.fee / (transaction.weight / 4),
|
||||
}, transaction);
|
||||
} catch (e) {
|
||||
console.log(txId + ' not found');
|
||||
return false;
|
||||
@@ -76,10 +73,11 @@ class Mempool {
|
||||
try {
|
||||
const transactions = await bitcoinApi.getRawMempool();
|
||||
const diff = transactions.length - Object.keys(this.mempoolCache).length;
|
||||
const newTransactions: TransactionExtended[] = [];
|
||||
|
||||
for (const txid of transactions) {
|
||||
if (!this.mempoolCache[txid]) {
|
||||
const transaction = await this.getRawTransaction(txid);
|
||||
const transaction = await this.getTransactionExtended(txid);
|
||||
if (transaction) {
|
||||
this.mempoolCache[txid] = transaction;
|
||||
txCount++;
|
||||
@@ -94,6 +92,7 @@ class Mempool {
|
||||
} else {
|
||||
console.log('Fetched transaction ' + txCount);
|
||||
}
|
||||
newTransactions.push(transaction);
|
||||
} else {
|
||||
console.log('Error finding transaction in mempool.');
|
||||
}
|
||||
@@ -117,7 +116,7 @@ class Mempool {
|
||||
this.mempoolCache = newMempool;
|
||||
|
||||
if (hasChange && this.mempoolChangedCallback) {
|
||||
this.mempoolChangedCallback(this.mempoolCache);
|
||||
this.mempoolChangedCallback(this.mempoolCache, newTransactions);
|
||||
}
|
||||
|
||||
const end = new Date().getTime();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import memPool from './mempool';
|
||||
import { DB } from '../database';
|
||||
|
||||
import { Statistic, SimpleTransaction, OptimizedStatistic } from '../interfaces';
|
||||
import { Statistic, TransactionExtended, OptimizedStatistic } from '../interfaces';
|
||||
|
||||
class Statistics {
|
||||
protected intervalTimer: NodeJS.Timer | undefined;
|
||||
@@ -37,7 +37,7 @@ class Statistics {
|
||||
|
||||
console.log('Running statistics');
|
||||
|
||||
let memPoolArray: SimpleTransaction[] = [];
|
||||
let memPoolArray: TransactionExtended[] = [];
|
||||
for (const i in currentMempool) {
|
||||
if (currentMempool.hasOwnProperty(i)) {
|
||||
memPoolArray.push(currentMempool[i]);
|
||||
|
||||
@@ -13,7 +13,7 @@ import mempoolBlocks from './api/mempool-blocks';
|
||||
import diskCache from './api/disk-cache';
|
||||
import statistics from './api/statistics';
|
||||
|
||||
import { Block, SimpleTransaction, Statistic } from './interfaces';
|
||||
import { Block, TransactionExtended, Statistic } from './interfaces';
|
||||
|
||||
import fiatConversion from './api/fiat-conversion';
|
||||
|
||||
@@ -98,6 +98,15 @@ class Server {
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedMessage && parsedMessage['track-address']) {
|
||||
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,87})$/
|
||||
.test(parsedMessage['track-address'])) {
|
||||
client['track-address'] = parsedMessage['track-address'];
|
||||
} else {
|
||||
client['track-address'] = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedMessage.action === 'init') {
|
||||
const _blocks = blocks.getBlocks();
|
||||
if (!_blocks) {
|
||||
@@ -133,7 +142,7 @@ class Server {
|
||||
});
|
||||
});
|
||||
|
||||
blocks.setNewBlockCallback((block: Block, txIds: string[]) => {
|
||||
blocks.setNewBlockCallback((block: Block, txIds: string[], transactions: TransactionExtended[]) => {
|
||||
this.wss.clients.forEach((client) => {
|
||||
if (client.readyState !== WebSocket.OPEN) {
|
||||
return;
|
||||
@@ -143,21 +152,40 @@ class Server {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = {
|
||||
'block': block
|
||||
};
|
||||
|
||||
if (client['track-tx'] && txIds.indexOf(client['track-tx']) > -1) {
|
||||
client['track-tx'] = null;
|
||||
client.send(JSON.stringify({
|
||||
'block': block,
|
||||
'txConfirmed': true,
|
||||
}));
|
||||
} else {
|
||||
client.send(JSON.stringify({
|
||||
'block': block,
|
||||
}));
|
||||
response['txConfirmed'] = true;
|
||||
}
|
||||
|
||||
if (client['track-address']) {
|
||||
const foundTransactions: TransactionExtended[] = [];
|
||||
|
||||
transactions.forEach((tx) => {
|
||||
const someVin = tx.vin.some((vin) => vin.prevout.scriptpubkey_address === client['track-address']);
|
||||
if (someVin) {
|
||||
foundTransactions.push(tx);
|
||||
return;
|
||||
}
|
||||
const someVout = tx.vout.some((vout) => vout.scriptpubkey_address === client['track-address']);
|
||||
if (someVout) {
|
||||
foundTransactions.push(tx);
|
||||
}
|
||||
});
|
||||
|
||||
if (foundTransactions.length) {
|
||||
response['address-block-transactions'] = foundTransactions;
|
||||
}
|
||||
}
|
||||
|
||||
client.send(JSON.stringify(response));
|
||||
});
|
||||
});
|
||||
|
||||
memPool.setMempoolChangedCallback((newMempool: { [txid: string]: SimpleTransaction }) => {
|
||||
memPool.setMempoolChangedCallback((newMempool: { [txid: string]: TransactionExtended }, newTransactions: TransactionExtended[]) => {
|
||||
mempoolBlocks.updateMempoolBlocks(newMempool);
|
||||
const mBlocks = mempoolBlocks.getMempoolBlocks();
|
||||
const mempoolInfo = memPool.getMempoolInfo();
|
||||
@@ -179,6 +207,27 @@ class Server {
|
||||
response['mempool-blocks'] = mBlocks;
|
||||
}
|
||||
|
||||
// Send all new incoming transactions related to tracked address
|
||||
if (client['track-address']) {
|
||||
const foundTransactions: TransactionExtended[] = [];
|
||||
|
||||
newTransactions.forEach((tx) => {
|
||||
const someVin = tx.vin.some((vin) => vin.prevout.scriptpubkey_address === client['track-address']);
|
||||
if (someVin) {
|
||||
foundTransactions.push(tx);
|
||||
return;
|
||||
}
|
||||
const someVout = tx.vout.some((vout) => vout.scriptpubkey_address === client['track-address']);
|
||||
if (someVout) {
|
||||
foundTransactions.push(tx);
|
||||
}
|
||||
});
|
||||
|
||||
if (foundTransactions.length) {
|
||||
response['address-transactions'] = foundTransactions;
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(response).length) {
|
||||
client.send(JSON.stringify(response));
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface Transaction {
|
||||
status: Status;
|
||||
}
|
||||
|
||||
export interface SimpleTransaction {
|
||||
export interface TransactionExtended extends Transaction {
|
||||
txid: string;
|
||||
fee: number;
|
||||
size: number;
|
||||
|
||||
Reference in New Issue
Block a user