Skip to content

Commit

Permalink
Fallback from batch provider to singular provider (#144)
Browse files Browse the repository at this point in the history
* handle batch ETH client not supported

* update changelog

* check Invalid Response as fallback option
  • Loading branch information
guplersaxanoid authored Aug 21, 2023
1 parent 346f19c commit 925650d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fallback to singular provider if batch provider is not supported (#144)

### Fixed
- Fix missing ds option for event in dictionary query entries (#145)
Expand Down
39 changes: 28 additions & 11 deletions packages/node/src/ethereum/api.ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,34 @@ export class EthereumApi implements ApiWrapper<EthereumBlockWrapper> {

async init(): Promise<void> {
this.injectClient();
const [genesisBlock, network, supportsFinalization, supportsSafe] =
await Promise.all([
this.client.getBlock('earliest'),
this.client.getNetwork(),
this.getSupportsTag('finalized'),
this.getSupportsTag('safe'),
]);
this.genesisBlock = genesisBlock;
this.supportsFinalization = supportsFinalization && supportsSafe;
this.chainId = network.chainId;
this.name = network.name;

try {
const [genesisBlock, network, supportsFinalization, supportsSafe] =
await Promise.all([
this.client.getBlock('earliest'),
this.client.getNetwork(),
this.getSupportsTag('finalized'),
this.getSupportsTag('safe'),
]);

this.genesisBlock = genesisBlock;
this.supportsFinalization = supportsFinalization && supportsSafe;
this.chainId = network.chainId;
this.name = network.name;
} catch (e) {
if ((e as Error).message.startsWith('Invalid response')) {
this.client = this.nonBatchClient;

logger.warn(
`The RPC Node at ${this.endpoint} cannot process batch requests. ` +
`Switching to non-batch mode for subsequent requests. Please consider checking if batch processing is supported on the RPC node.`,
);

return this.init();
}

throw e;
}
}

private async getSupportsTag(tag: BlockTag): Promise<boolean> {
Expand Down

0 comments on commit 925650d

Please sign in to comment.