Add electrs sync progress updates
This commit is contained in:
parent
db715a1dba
commit
e59c961f25
@ -3,7 +3,7 @@ import { IEsploraApi } from './esplora-api.interface';
|
|||||||
export interface AbstractBitcoinApi {
|
export interface AbstractBitcoinApi {
|
||||||
$getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
|
$getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
|
||||||
$getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, lazyPrevouts?: boolean): Promise<IEsploraApi.Transaction>;
|
$getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, lazyPrevouts?: boolean): Promise<IEsploraApi.Transaction>;
|
||||||
$getMempoolTransactions(expectedCount: number);
|
$getMempoolTransactions(lastTxid: string);
|
||||||
$getTransactionHex(txId: string): Promise<string>;
|
$getTransactionHex(txId: string): Promise<string>;
|
||||||
$getBlockHeightTip(): Promise<number>;
|
$getBlockHeightTip(): Promise<number>;
|
||||||
$getBlockHashTip(): Promise<string>;
|
$getBlockHashTip(): Promise<string>;
|
||||||
|
@ -59,7 +59,7 @@ class BitcoinApi implements AbstractBitcoinApi {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> {
|
$getMempoolTransactions(lastTxid: string): Promise<IEsploraApi.Transaction[]> {
|
||||||
return Promise.resolve([]);
|
return Promise.resolve([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,33 +69,8 @@ class ElectrsApi implements AbstractBitcoinApi {
|
|||||||
return this.$queryWrapper<IEsploraApi.Transaction>(config.ESPLORA.REST_API_URL + '/tx/' + txId);
|
return this.$queryWrapper<IEsploraApi.Transaction>(config.ESPLORA.REST_API_URL + '/tx/' + txId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async $getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> {
|
async $getMempoolTransactions(lastSeenTxid?: string): Promise<IEsploraApi.Transaction[]> {
|
||||||
const transactions: IEsploraApi.Transaction[] = [];
|
return this.$queryWrapper<IEsploraApi.Transaction[]>(config.ESPLORA.REST_API_URL + '/mempool/txs' + (lastSeenTxid ? '/' + lastSeenTxid : ''));
|
||||||
let count = 0;
|
|
||||||
let done = false;
|
|
||||||
let last_txid = '';
|
|
||||||
while (!done) {
|
|
||||||
try {
|
|
||||||
const result = await this.$queryWrapper<IEsploraApi.Transaction[]>(config.ESPLORA.REST_API_URL + '/mempool/txs' + (last_txid ? '/' + last_txid : ''));
|
|
||||||
if (result) {
|
|
||||||
for (const tx of result) {
|
|
||||||
transactions.push(tx);
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`);
|
|
||||||
if (result.length > 0) {
|
|
||||||
last_txid = result[result.length - 1].txid;
|
|
||||||
} else {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
} catch(err) {
|
|
||||||
logger.err('failed to fetch bulk mempool transactions from esplora');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return transactions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$getTransactionHex(txId: string): Promise<string> {
|
$getTransactionHex(txId: string): Promise<string> {
|
||||||
|
@ -9,6 +9,7 @@ import loadingIndicators from './loading-indicators';
|
|||||||
import bitcoinClient from './bitcoin/bitcoin-client';
|
import bitcoinClient from './bitcoin/bitcoin-client';
|
||||||
import bitcoinSecondClient from './bitcoin/bitcoin-second-client';
|
import bitcoinSecondClient from './bitcoin/bitcoin-second-client';
|
||||||
import rbfCache from './rbf-cache';
|
import rbfCache from './rbf-cache';
|
||||||
|
import { IEsploraApi } from './bitcoin/esplora-api.interface';
|
||||||
|
|
||||||
class Mempool {
|
class Mempool {
|
||||||
private inSync: boolean = false;
|
private inSync: boolean = false;
|
||||||
@ -104,11 +105,34 @@ class Mempool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async $reloadMempool(expectedCount: number): Promise<void> {
|
public async $reloadMempool(expectedCount: number): Promise<void> {
|
||||||
const rawTransactions = await bitcoinApi.$getMempoolTransactions(expectedCount);
|
let count = 0;
|
||||||
logger.info(`Inserting loaded mempool transactions into local cache`);
|
let done = false;
|
||||||
for (const transaction of rawTransactions) {
|
let last_txid;
|
||||||
const extendedTransaction = transactionUtils.extendMempoolTransaction(transaction);
|
loadingIndicators.setProgress('mempool', count / expectedCount * 100);
|
||||||
this.mempoolCache[extendedTransaction.txid] = extendedTransaction;
|
while (!done) {
|
||||||
|
try {
|
||||||
|
const result = await bitcoinApi.$getMempoolTransactions(last_txid);
|
||||||
|
if (result) {
|
||||||
|
for (const tx of result) {
|
||||||
|
const extendedTransaction = transactionUtils.extendMempoolTransaction(tx);
|
||||||
|
this.mempoolCache[extendedTransaction.txid] = extendedTransaction;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`);
|
||||||
|
if (result.length > 0) {
|
||||||
|
last_txid = result[result.length - 1].txid;
|
||||||
|
} else {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
if (count < expectedCount) {
|
||||||
|
loadingIndicators.setProgress('mempool', count / expectedCount * 100);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
logger.err('failed to fetch bulk mempool transactions from esplora');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logger.info(`Done inserting loaded mempool transactions into local cache`);
|
logger.info(`Done inserting loaded mempool transactions into local cache`);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user