mirror of
https://github.com/mempool/mempool.git
synced 2026-08-13 12:33:11 +02:00
Fallback to core for stale blocks on $getBlock calls
This commit is contained in:
parent
effa187c91
commit
5c2bf8f7e1
4 changed files with 17 additions and 5 deletions
|
|
@ -15,7 +15,7 @@ export interface AbstractBitcoinApi {
|
|||
$getTxsForBlock(hash: string, fallbackToCore?: boolean): Promise<IEsploraApi.Transaction[]>;
|
||||
$getBlockHash(height: number): Promise<string>;
|
||||
$getBlockHeader(hash: string): Promise<string>;
|
||||
$getBlock(hash: string): Promise<IEsploraApi.Block>;
|
||||
$getBlock(hash: string, fallbackToCore?: boolean): Promise<IEsploraApi.Block>;
|
||||
$getRawBlock(hash: string): Promise<Buffer>;
|
||||
$getAddress(address: string): Promise<IEsploraApi.Address>;
|
||||
$getAddressTransactions(address: string, lastSeenTxId: string): Promise<IEsploraApi.Transaction[]>;
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ class BitcoinApi implements AbstractBitcoinApi {
|
|||
return this.bitcoindClient.getBlockHeader(hash, false);
|
||||
}
|
||||
|
||||
async $getBlock(hash: string): Promise<IEsploraApi.Block> {
|
||||
async $getBlock(hash: string, fallbackToCore = false): Promise<IEsploraApi.Block> {
|
||||
const foundBlock = blocks.getBlocks().find((block) => block.id === hash);
|
||||
if (foundBlock) {
|
||||
return foundBlock;
|
||||
|
|
|
|||
|
|
@ -449,8 +449,20 @@ class ElectrsApi implements AbstractBitcoinApi {
|
|||
return this.failoverRouter.$get<string>('/block/' + hash + '/header');
|
||||
}
|
||||
|
||||
$getBlock(hash: string): Promise<IEsploraApi.Block> {
|
||||
return this.failoverRouter.$get<IEsploraApi.Block>('/block/' + hash);
|
||||
async $getBlock(hash: string, fallbackToCore = false): Promise<IEsploraApi.Block> {
|
||||
try {
|
||||
const block = await this.failoverRouter.$get<IEsploraApi.Block>('/block/' + hash);
|
||||
return block;
|
||||
} catch (e: any) {
|
||||
if (fallbackToCore && isAxiosError(e) && e.response?.status === 404) {
|
||||
// might be a stale block, see if Core has it?
|
||||
const coreBlock = await bitcoinCoreApi.$getBlock(hash);
|
||||
if (coreBlock?.stale) {
|
||||
return coreBlock;
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
$getRawBlock(hash: string): Promise<Buffer> {
|
||||
|
|
|
|||
|
|
@ -1324,7 +1324,7 @@ class Blocks {
|
|||
summaryVersion = 1;
|
||||
}
|
||||
if (height == null) {
|
||||
const block = await bitcoinApi.$getBlock(hash);
|
||||
const block = await bitcoinApi.$getBlock(hash, true);
|
||||
height = block.height;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue