fix: lock recent blockhash poll to prevent concurrent polling

This commit is contained in:
Justin Starry
2020-08-26 12:59:56 +08:00
committed by Justin Starry
parent d6ecb2699f
commit 7aa8b1c658

View File

@ -1377,6 +1377,7 @@ export class Connection {
transactionSignatures: Array<string>,
};
_disableBlockhashCaching: boolean = false;
_pollingBlockhash: boolean = false;
_accountChangeSubscriptions: {[number]: AccountSubscriptionInfo} = {};
_accountChangeSubscriptionCounter: number = 0;
_programAccountChangeSubscriptions: {
@ -2478,6 +2479,10 @@ export class Connection {
async _recentBlockhash(disableCache: boolean): Promise<Blockhash> {
if (!disableCache) {
// Wait for polling to finish
while (this._pollingBlockhash) {
await sleep(100);
}
// Attempt to use a recent blockhash for up to 30 seconds
const expired =
Date.now() - this._blockhashInfo.lastFetch >=
@ -2491,6 +2496,8 @@ export class Connection {
}
async _pollNewBlockhash(): Promise<Blockhash> {
this._pollingBlockhash = true;
try {
const startTime = Date.now();
for (let i = 0; i < 50; i++) {
const {blockhash} = await this.getRecentBlockhash('max');
@ -2512,6 +2519,9 @@ export class Connection {
throw new Error(
`Unable to obtain a new blockhash after ${Date.now() - startTime}ms`,
);
} finally {
this._pollingBlockhash = false;
}
}
/**