Fallback to core for stale blocks on $getBlock calls

This commit is contained in:
natsoni 2025-09-23 23:14:48 +02:00
parent effa187c91
commit 5c2bf8f7e1
No known key found for this signature in database
GPG key ID: C65917583181743B
4 changed files with 17 additions and 5 deletions

View file

@ -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[]>;

View file

@ -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;

View file

@ -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> {

View file

@ -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;
}