Basic bitcoind/romanz-electrum support to sync the mempool and blocks.

This commit is contained in:
softsimon
2020-12-20 22:36:36 +07:00
parent 00244b706d
commit aabdaea47b
12 changed files with 393 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
import config from '../config';
import bitcoinApi from './bitcoin/electrs-api';
import { MempoolInfo, TransactionExtended, Transaction, VbytesPerSecond } from '../interfaces';
import bitcoinApi from './bitcoin/bitcoin-api-factory';
import { MempoolInfo, TransactionExtended, Transaction, VbytesPerSecond, MempoolEntry, MempoolEntries } from '../interfaces';
import logger from '../logger';
import { Common } from './common';
@@ -18,6 +18,7 @@ class Mempool {
private vBytesPerSecond: number = 0;
private mempoolProtection = 0;
private latestTransactions: any[] = [];
private mempoolEntriesCache: MempoolEntries | null = null;
constructor() {
setInterval(this.updateTxPerSecond.bind(this), 1000);
@@ -75,20 +76,47 @@ class Mempool {
return txTimes;
}
public async getTransactionExtended(txId: string): Promise<TransactionExtended | false> {
public async getTransactionExtended(txId: string, isCoinbase = false): Promise<TransactionExtended | false> {
try {
const transaction: Transaction = await bitcoinApi.getRawTransaction(txId);
return Object.assign({
vsize: transaction.weight / 4,
feePerVsize: (transaction.fee || 0) / (transaction.weight / 4),
firstSeen: Math.round((new Date().getTime() / 1000)),
}, transaction);
let transaction: Transaction;
if (!isCoinbase && config.MEMPOOL.BACKEND === 'bitcoind-electrs') {
transaction = await bitcoinApi.getRawTransactionBitcond(txId);
} else {
transaction = await bitcoinApi.getRawTransaction(txId);
}
if (config.MEMPOOL.BACKEND !== 'electrs' && !isCoinbase) {
transaction = await this.$appendFeeData(transaction);
}
return this.extendTransaction(transaction);
} catch (e) {
logger.debug(txId + ' not found');
return false;
}
}
private async $appendFeeData(transaction: Transaction): Promise<Transaction> {
let mempoolEntry: MempoolEntry;
if (!this.inSync && !this.mempoolEntriesCache) {
this.mempoolEntriesCache = await bitcoinApi.getRawMempoolVerbose();
}
if (this.mempoolEntriesCache && this.mempoolEntriesCache[transaction.txid]) {
mempoolEntry = this.mempoolEntriesCache[transaction.txid];
} else {
mempoolEntry = await bitcoinApi.getMempoolEntry(transaction.txid);
}
transaction.fee = mempoolEntry.fees.base * 100000000;
return transaction;
}
private extendTransaction(transaction: Transaction | MempoolEntry): TransactionExtended {
// @ts-ignore
return Object.assign({
vsize: Math.round(transaction.weight / 4),
feePerVsize: Math.max(1, (transaction.fee || 0) / (transaction.weight / 4)),
firstSeen: Math.round((new Date().getTime() / 1000)),
}, transaction);
}
public async $updateMempool() {
logger.debug('Updating mempool');
const start = new Date().getTime();
@@ -169,6 +197,7 @@ class Mempool {
if (!this.inSync && transactions.length === Object.keys(newMempool).length) {
this.inSync = true;
this.mempoolEntriesCache = null;
logger.info('The mempool is now in sync!');
}