Skip to content

Commit

Permalink
More robust defaultProvider start-up when a backend fails on bootstrap (
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent e28b7ff commit 2fc9a27
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src.ts/providers/provider-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,38 @@ interface Config extends FallbackProviderState {
_updateNumber: null | Promise<any>;
_network: null | Network;
_totalTime: number;
_lastFatalError: null | Error;
_lastFatalErrorTimestamp: number;
}

const defaultState = {
blockNumber: -2, requests: 0, lateResponses: 0, errorResponses: 0,
outOfSync: -1, unsupportedEvents: 0, rollingDuration: 0, score: 0,
_network: null, _updateNumber: null, _totalTime: 0
_network: null, _updateNumber: null, _totalTime: 0,
_lastFatalError: null, _lastFatalErrorTimestamp: 0
};


async function waitForSync(config: Config, blockNumber: number): Promise<void> {
while (config.blockNumber < 0 || config.blockNumber < blockNumber) {
if (!config._updateNumber) {
config._updateNumber = (async () => {
const blockNumber = await config.provider.getBlockNumber();
if (blockNumber > config.blockNumber) {
config.blockNumber = blockNumber;
try {
const blockNumber = await config.provider.getBlockNumber();
if (blockNumber > config.blockNumber) {
config.blockNumber = blockNumber;
}
} catch (error: any) {
config.blockNumber = -2;
config._lastFatalError = error;
config._lastFatalErrorTimestamp = getTime();
}
config._updateNumber = null;
})();
}
await config._updateNumber;
config.outOfSync++;
if (config._lastFatalError) { break; }
}
}

Expand Down Expand Up @@ -413,6 +423,7 @@ export class FallbackProvider extends AbstractProvider {
allConfigs.sort((a, b) => (b.priority - a.priority));

for (const config of allConfigs) {
if (config._lastFatalError) { continue; }
if (configs.indexOf(config) === -1) { return config; }
}

Expand Down Expand Up @@ -471,9 +482,11 @@ export class FallbackProvider extends AbstractProvider {
if (!initialSync) {
const promises: Array<Promise<any>> = [ ];
this.#configs.forEach((config) => {
promises.push(waitForSync(config, 0));
promises.push((async () => {
config._network = await config.provider.getNetwork();
await waitForSync(config, 0);
if (!config._lastFatalError) {
config._network = await config.provider.getNetwork();
}
})());
});

Expand All @@ -484,6 +497,7 @@ export class FallbackProvider extends AbstractProvider {
// Check all the networks match
let chainId: null | bigint = null;
for (const config of this.#configs) {
if (config._lastFatalError) { continue; }
const network = <Network>(config._network);
if (chainId == null) {
chainId = network.chainId;
Expand Down Expand Up @@ -519,7 +533,7 @@ export class FallbackProvider extends AbstractProvider {
case "getBlockNumber": {
// We need to get the bootstrap block height
if (this.#height === -2) {
this.#height = Math.ceil(getNumber(<bigint>getMedian(this.quorum, this.#configs.map((c) => ({
this.#height = Math.ceil(getNumber(<bigint>getMedian(this.quorum, this.#configs.filter((c) => (!c._lastFatalError)).map((c) => ({
value: c.blockNumber,
tag: getNumber(c.blockNumber).toString(),
weight: c.weight
Expand Down

0 comments on commit 2fc9a27

Please sign in to comment.