Skip to content

Commit

Permalink
polish, fix local mode delay (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan authored Feb 22, 2023
1 parent f18fd87 commit 5b2b3eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
38 changes: 22 additions & 16 deletions eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ export abstract class BaseProvider extends AbstractProvider {
};

_isTransactionFinalized = async (txHash: string): Promise<boolean> => {
const tx = await this._getMinedReceipt(txHash);
const tx = await this.getReceiptByHash(txHash);
if (!tx) return false;

return this._isBlockFinalized(tx.blockHash);
Expand Down Expand Up @@ -1600,7 +1600,7 @@ export abstract class BaseProvider extends AbstractProvider {
txHash: string,
blockHash: string
) => {
const receipt = await this._getMinedReceipt(txHash);
const receipt = await this.getReceiptByHash(txHash);
return receipt?.blockHash === blockHash
? receipt
: null;
Expand Down Expand Up @@ -1670,10 +1670,7 @@ export abstract class BaseProvider extends AbstractProvider {
if (pendingTX) return pendingTX;
}

const receipt = this.localMode
? await runWithRetries(this._getMinedReceipt.bind(this), [txHash])
: await this._getMinedReceipt(txHash);

const receipt = await this.getReceiptByHash(txHash);
if (!receipt) return null;

// TODO: in the future can save parsed extraData in FullReceipt for ultimate performance
Expand All @@ -1686,13 +1683,11 @@ export abstract class BaseProvider extends AbstractProvider {
getTransactionReceipt = async (txHash: string): Promise<TransactionReceipt> =>
throwNotImplemented('getTransactionReceipt (please use `getReceiptByHash` instead)');

getReceiptByHash = async (txHash: string): Promise<TXReceipt | null> => {
const receipt = this.localMode
getReceiptByHash = async (txHash: string): Promise<TransactionReceipt | null> => (
this.localMode
? await runWithRetries(this._getMinedReceipt.bind(this), [txHash])
: await this._getMinedReceipt(txHash);

return receipt;
};
: await this._getMinedReceipt(txHash)
);

_sanitizeRawFilter = async (rawFilter: LogFilter): Promise<SanitizedLogFilter> => {
const { fromBlock, toBlock, blockHash, address, topics } = rawFilter;
Expand Down Expand Up @@ -1778,9 +1773,17 @@ export abstract class BaseProvider extends AbstractProvider {
const getBlockPromise = runWithTiming(async () => this.getBlockData(pastNblock, false));
const getFullBlockPromise = runWithTiming(async () => this.getBlockData(pastNblock, true));

const [gasPriceTime, estimateGasTime, getBlockTime, getFullBlockTime] = (
await Promise.all([gasPricePromise, estimateGasPromise, getBlockPromise, getFullBlockPromise])
).map((res) => Math.floor(res.time));
const [
gasPriceTime,
estimateGasTime,
getBlockTime,
getFullBlockTime,
] = (await Promise.all([
gasPricePromise,
estimateGasPromise,
getBlockPromise,
getFullBlockPromise,
])).map((res) => Math.floor(res.time));

return {
gasPriceTime,
Expand All @@ -1791,7 +1794,10 @@ export abstract class BaseProvider extends AbstractProvider {
};

healthCheck = async (): Promise<HealthResult> => {
const [indexerMeta, ethCallTiming] = await Promise.all([this.getIndexerMetadata(), this._timeEthCalls()]);
const [indexerMeta, ethCallTiming] = await Promise.all([
this.getIndexerMetadata(),
this._timeEthCalls(),
]);

const cacheInfo = this.getCachInfo();
const curFinalizedHeight = this.latestFinalizedBlockNumber;
Expand Down
24 changes: 13 additions & 11 deletions eth-providers/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,26 @@ export const promiseWithTimeout = <T = any>(value: any, interval = 1000): Promis
export const runWithRetries = async <F extends AnyFunction>(
fn: F,
args: any[] = [],
maxRetries: number = 20,
interval: number = 1000
maxRetries: number = 50,
interval: number = 200
): Promise<F extends (...args: any[]) => infer R ? R : never> => {
let res;
let tries = 0;

while (!res && tries++ < maxRetries) {
try {
res = await fn(...args);
} catch (e) {
if (tries === maxRetries) throw e;
}
res = await fn(...args);

if ((tries === 1 || tries % 10 === 0) && !res) {
console.log(`<local mode runWithRetries> still waiting for result # ${tries}/${maxRetries}`);
}
if (res) {
return res;
} else {
if (tries === maxRetries) return res;

await sleep(interval);
if ((tries === 1 || tries % 10 === 0) && !res) {
console.log(`<local mode runWithRetries> still waiting for result # ${tries}/${maxRetries}`);
}

await sleep(interval);
}
}

return res;
Expand Down

0 comments on commit 5b2b3eb

Please sign in to comment.