Skip to content

Commit

Permalink
Better provider internal block management (#1084, #1208, #1221, #1235).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 13, 2021
1 parent 2df9dd1 commit 20f6e16
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/providers/src.ts/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,20 +585,35 @@ export class BaseProvider extends Provider implements EnsProvider {
async _getInternalBlockNumber(maxAge: number): Promise<number> {
await this._ready();

const internalBlockNumber = this._internalBlockNumber;
// Allowing stale data up to maxAge old
if (maxAge > 0) {

if (maxAge > 0 && internalBlockNumber) {
try {
const result = await internalBlockNumber;
if ((getTime() - result.respTime) <= maxAge) {
return result.blockNumber;
}
} catch(error) {
// Don't null the dead (rejected) fetch, if it has already been updated
if (this._internalBlockNumber === internalBlockNumber) {
this._internalBlockNumber = null;
// While there are pending internal block requests...
while (this._internalBlockNumber) {

// ..."remember" which fetch we started with
const internalBlockNumber = this._internalBlockNumber;

try {
// Check the result is not too stale
const result = await internalBlockNumber;
if ((getTime() - result.respTime) <= maxAge) {
return result.blockNumber;
}

// Too old; fetch a new value
break;

} catch(error) {

// The fetch rejected; if we are the first to get the
// rejection, drop through so we replace it with a new
// fetch; all others blocked will then get that fetch
// which won't match the one they "remembered" and loop
if (this._internalBlockNumber === internalBlockNumber) {
break;
}
}
throw error;
}
}

Expand Down

1 comment on commit 20f6e16

@andrevmatos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine for me, I can't see where it wouldn't hold the premises, so let's test. Thank you very much.

Please sign in to comment.