diff --git a/packages/util/src/block-size-cache.ts b/packages/util/src/block-size-cache.ts index d27b466c8..d2c7a2c2a 100644 --- a/packages/util/src/block-size-cache.ts +++ b/packages/util/src/block-size-cache.ts @@ -2,9 +2,11 @@ // Copyright 2022 Vulcanize, Inc. // -import { utils, providers } from 'ethers'; +import { utils, providers, errors } from 'ethers'; import debug from 'debug'; +import { NULL_BLOCK_ERROR } from './constants'; + const log = debug('vulcanize:block-size-cache'); // Number of blocks to cache after current block being processed. @@ -44,16 +46,26 @@ const cacheBlockSizesAsync = async (provider: providers.JsonRpcProvider, blockNu // Start prefetching blocks after latest height in blockSizeMap. for (let i = startBlockHeight; i <= endBlockHeight; i++) { - console.time(`time:misc#cacheBlockSizesAsync-eth_getBlockByNumber-${i}`); - const block = await provider.send('eth_getBlockByNumber', [utils.hexStripZeros(utils.hexlify(i)), false]); - - if (block) { - const { size, hash } = block; - blockSizeMap.set(hash, { size, blockNumber: i }); - } else { - log(`No block found at height ${i}`); + try { + console.time(`time:misc#cacheBlockSizesAsync-eth_getBlockByNumber-${i}`); + const block = await provider.send('eth_getBlockByNumber', [utils.hexStripZeros(utils.hexlify(i)), false]); + + if (block) { + const { size, hash } = block; + blockSizeMap.set(hash, { size, blockNumber: i }); + } else { + log(`No block found at height ${i}`); + } + } catch (err: any) { + // Handle null block error in case of Lotus EVM + if (!(err.code === errors.SERVER_ERROR && err.error && err.error.message === NULL_BLOCK_ERROR)) { + throw err; + } + + log(`Block ${i} requested was null (FEVM); Fetching next block`); + } finally { + console.timeEnd(`time:misc#cacheBlockSizesAsync-eth_getBlockByNumber-${i}`); } - console.timeEnd(`time:misc#cacheBlockSizesAsync-eth_getBlockByNumber-${i}`); } } diff --git a/packages/util/src/common.ts b/packages/util/src/common.ts index f33ddbd8f..f780c33c4 100644 --- a/packages/util/src/common.ts +++ b/packages/util/src/common.ts @@ -10,7 +10,8 @@ import { QUEUE_BLOCK_CHECKPOINT, JOB_KIND_PRUNE, JOB_KIND_INDEX, - UNKNOWN_EVENT_NAME + UNKNOWN_EVENT_NAME, + NULL_BLOCK_ERROR } from './constants'; import { JobQueue } from './job-queue'; import { BlockProgressInterface, IndexerInterface, EventInterface } from './types'; @@ -106,7 +107,7 @@ export const fetchBlocksAtHeight = async ( } } catch (err: any) { // Handle null block error in case of Lotus EVM - if (!(err.code === errors.SERVER_ERROR && err.error && err.error.message === 'requested epoch was a null round')) { + if (!(err.code === errors.SERVER_ERROR && err.error && err.error.message === NULL_BLOCK_ERROR)) { throw err; } @@ -197,7 +198,7 @@ export const _fetchBatchBlocks = async ( // Handle null block error in case of Lotus EVM // Otherwise, rethrow error const err = result.reason; - if (!(err.code === errors.SERVER_ERROR && err.error && err.error.message === 'requested epoch was a null round')) { + if (!(err.code === errors.SERVER_ERROR && err.error && err.error.message === NULL_BLOCK_ERROR)) { throw err; } diff --git a/packages/util/src/constants.ts b/packages/util/src/constants.ts index ba1b464a5..a5b974cfe 100644 --- a/packages/util/src/constants.ts +++ b/packages/util/src/constants.ts @@ -29,3 +29,5 @@ export const DEFAULT_PREFETCH_BATCH_SIZE = 10; export const DEFAULT_MAX_GQL_CACHE_SIZE = Math.pow(2, 20) * 8; // 8 MB export const SUPPORTED_PAID_RPC_METHODS = ['eth_getBlockByHash', 'eth_getStorageAt', 'eth_getBlockByNumber']; + +export const NULL_BLOCK_ERROR = 'requested epoch was a null round';