Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch bitcoin fallback data provider error #187

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions src/services/bitcoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
import { HttpStatusCode, isAxiosError } from 'axios';
import * as Sentry from '@sentry/node';
import { Cradle } from '../../container';
Expand Down Expand Up @@ -50,6 +52,9 @@ interface IBitcoinClient extends IBitcoinDataProvider {
getBlockchainInfo(): Promise<ChainInfo>;
}

type MethodParameters<T, K extends keyof T> = T[K] extends (...args: infer P) => any ? P : never;
type MethodReturnType<T, K extends keyof T> = T[K] extends (...args: any[]) => infer R ? R : never;

export default class BitcoinClient implements IBitcoinClient {
private cradle: Cradle;
private source: IBitcoinDataProvider;
Expand Down Expand Up @@ -93,34 +98,35 @@ export default class BitcoinClient implements IBitcoinClient {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async call<K extends keyof IBitcoinDataProvider>(
method: K,
args: Parameters<IBitcoinDataProvider[K]>,
): Promise<ReturnType<IBitcoinDataProvider[K]>> {
...args: MethodParameters<IBitcoinDataProvider, K>
): Promise<MethodReturnType<IBitcoinDataProvider, K>> {
try {
this.cradle.logger.debug(`Calling ${method} with args: ${JSON.stringify(args)}`);
// @ts-expect-error args: A spread argument must either have a tuple type or be passed to a rest parameter
const result = await this.source[method].call(this.source, ...args);
// @ts-expect-error return type is correct
return result;
const result = await (this.source[method] as Function).apply(this.source, args);
return result as MethodReturnType<IBitcoinDataProvider, K>;
} catch (err) {
let calledError = err;
this.cradle.logger.error(err);
Sentry.captureException(err);
if (this.fallback) {
this.cradle.logger.warn(
`Fallback to ${this.fallback.constructor.name} due to error: ${(err as Error).message}`,
);
// @ts-expect-error same as above
const result = await this.fallback[method].call(this.fallback, ...args);
// @ts-expect-error return type is correct
return result;
try {
const result = await (this.fallback[method] as Function).apply(this.fallback, args);
return result as MethodReturnType<IBitcoinDataProvider, K>;
} catch (fallbackError) {
this.cradle.logger.error(fallbackError);
Sentry.captureException(fallbackError);
calledError = fallbackError;
}
}
this.cradle.logger.error(err);
if (isAxiosError(err)) {
const error = new BitcoinClientAPIError(err.response?.data ?? err.message);
if (err.response?.status) {
error.statusCode = err.response.status;
if (isAxiosError(calledError)) {
const error = new BitcoinClientAPIError(calledError.response?.data ?? calledError.message);
if (calledError.response?.status) {
error.statusCode = calledError.response.status;
}
throw error;
}
Expand Down Expand Up @@ -172,11 +178,11 @@ export default class BitcoinClient implements IBitcoinClient {
}

public async getFeesRecommended() {
return this.call('getFeesRecommended', []);
return this.call('getFeesRecommended');
}

public async postTx({ txhex }: { txhex: string }) {
const txid = await this.call('postTx', [{ txhex }]);
const txid = await this.call('postTx', { txhex });
Promise.all(
this.backupers.map(async (backuper) => {
const baseURL = await backuper.getBaseURL();
Expand All @@ -194,38 +200,38 @@ export default class BitcoinClient implements IBitcoinClient {
}

public async getAddressTxsUtxo({ address }: { address: string }) {
return this.call('getAddressTxsUtxo', [{ address }]);
return this.call('getAddressTxsUtxo', { address });
}

public async getAddressTxs({ address, after_txid }: { address: string; after_txid?: string }) {
return this.call('getAddressTxs', [{ address, after_txid }]);
return this.call('getAddressTxs', { address, after_txid });
}

public async getTx({ txid }: { txid: string }) {
return this.call('getTx', [{ txid }]);
return this.call('getTx', { txid });
}

public async getTxHex({ txid }: { txid: string }) {
return this.call('getTxHex', [{ txid }]);
return this.call('getTxHex', { txid });
}

public async getBlock({ hash }: { hash: string }) {
return this.call('getBlock', [{ hash }]);
return this.call('getBlock', { hash });
}

public async getBlockHeight({ height }: { height: number }) {
return this.call('getBlockHeight', [{ height }]);
return this.call('getBlockHeight', { height });
}

public async getBlockHeader({ hash }: { hash: string }) {
return this.call('getBlockHeader', [{ hash }]);
return this.call('getBlockHeader', { hash });
}

public async getBlockTxids({ hash }: { hash: string }) {
return this.call('getBlockTxids', [{ hash }]);
return this.call('getBlockTxids', { hash });
}

public async getBlocksTipHash() {
return this.call('getBlocksTipHash', []);
return this.call('getBlocksTipHash');
}
}
19 changes: 17 additions & 2 deletions test/services/bitcoin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import container from '../../src/container';
import { describe, test, beforeEach, expect } from 'vitest';
import BitcoinClient from '../../src/services/bitcoin';
import { describe, test, beforeEach, expect, vi } from 'vitest';
import BitcoinClient, { BitcoinClientAPIError } from '../../src/services/bitcoin';
import { ElectrsClient } from '../../src/services/bitcoin/electrs';
import { MempoolClient } from '../../src/services/bitcoin/mempool';
import { AxiosError } from 'axios';

describe('BitcoinClient', () => {
let bitcoin: BitcoinClient;
Expand All @@ -21,4 +22,18 @@ describe('BitcoinClient', () => {
expect(bitcoin['fallback']?.constructor).toBe(MempoolClient);
}
});

test('BitcoinClient: throw BitcoinClientError when source provider failed', async () => {
bitcoin['fallback'] = undefined;
vi.spyOn(bitcoin['source'], 'postTx').mockRejectedValue(new AxiosError('source provider error'));
expect(bitcoin.postTx({ txhex: 'test' })).rejects.toBeInstanceOf(BitcoinClientAPIError);
expect(bitcoin.postTx({ txhex: 'test' })).rejects.toThrowError('source provider error');
});

test('BitcoinClient: throw BitcoinClientError when fallback provider failed', async () => {
vi.spyOn(bitcoin['source'], 'postTx').mockRejectedValue(new AxiosError('source provider error'));
vi.spyOn(bitcoin['fallback']!, 'postTx').mockRejectedValue(new AxiosError('fallback provider error'));
expect(bitcoin.postTx({ txhex: 'test' })).rejects.toBeInstanceOf(BitcoinClientAPIError);
expect(bitcoin.postTx({ txhex: 'test' })).rejects.toThrowError('fallback provider error');
});
});
Loading