@@ -21,7 +21,6 @@ import {
2121 LIT_CURVE ,
2222 LIT_CURVE_VALUES ,
2323 LIT_ENDPOINT ,
24- LIT_ERROR ,
2524 LIT_ERROR_CODE ,
2625 LIT_NETWORK ,
2726 LIT_NETWORKS ,
@@ -69,7 +68,6 @@ import {
6968 NodeClientErrorV0 ,
7069 NodeClientErrorV1 ,
7170 NodeCommandServerKeysResponse ,
72- NodeErrorV3 ,
7371 RejectedNodePromises ,
7472 SendNodeCommand ,
7573 SessionSigsMap ,
@@ -82,6 +80,10 @@ import { composeLitUrl } from './endpoint-version';
8280// eslint-disable-next-line @typescript-eslint/no-explicit-any
8381type Listener = ( ...args : any [ ] ) => void ;
8482
83+ type providerTest < T > = (
84+ provider : ethers . providers . JsonRpcProvider
85+ ) => Promise < T > ;
86+
8587interface CoreNodeConfig {
8688 subnetPubKey : string ;
8789 networkPubKey : string ;
@@ -119,8 +121,8 @@ export type LitNodeClientConfigWithDefaults = Required<
119121const EPOCH_PROPAGATION_DELAY = 45_000 ;
120122// This interval is responsible for keeping latest block hash up to date
121123const BLOCKHASH_SYNC_INTERVAL = 30_000 ;
122- // When fetching the blockhash from a provider (not lit), we use a previous block to avoid a nodes not knowing about the new block yet
123- const BLOCKHASH_COUNT_PROVIDER_DELAY = - 1 ;
124+ // When fetching the blockhash from a provider (not lit), we use a 5 minutes old block to ensure the nodes centralized indexer has it
125+ const BLOCKHASH_COUNT_PROVIDER_DELAY = - 30 ; // 30 blocks ago. Eth block are mined every 12s. 30 blocks is 6 minutes, indexer/nodes must have it by now
124126
125127// Intentionally not including datil-dev here per discussion with Howard
126128const NETWORKS_REQUIRING_SEV : string [ ] = [
@@ -766,24 +768,31 @@ export class LitCore {
766768 } ;
767769 }
768770
769- private _getProviderWithFallback =
770- async ( ) : Promise < ethers . providers . JsonRpcProvider | null > => {
771- for ( const url of FALLBACK_RPC_URLS ) {
772- try {
773- const provider = new ethers . providers . JsonRpcProvider ( {
774- url : url ,
771+ private _getProviderWithFallback = async < T > (
772+ providerTest : providerTest < T >
773+ ) : Promise < {
774+ provider : ethers . providers . JsonRpcProvider ;
775+ testResult : T ;
776+ } | null > => {
777+ for ( const url of FALLBACK_RPC_URLS ) {
778+ try {
779+ const provider = new ethers . providers . JsonRpcProvider ( {
780+ url : url ,
775781
776- // https://docs.ethers.org/v5/api/utils/web/#ConnectionInfo
777- timeout : 60000 ,
778- } ) ;
779- await provider . getBlockNumber ( ) ; // Simple check to see if the provider is working
780- return provider ;
781- } catch ( error ) {
782- logError ( `RPC URL failed: ${ url } ` ) ;
783- }
782+ // https://docs.ethers.org/v5/api/utils/web/#ConnectionInfo
783+ timeout : 60000 ,
784+ } ) ;
785+ const testResult = await providerTest ( provider ) ; // Check to see if the provider is working
786+ return {
787+ provider,
788+ testResult,
789+ } ;
790+ } catch ( error ) {
791+ logError ( `RPC URL failed: ${ url } ` ) ;
784792 }
785- return null ;
786- } ;
793+ }
794+ return null ;
795+ } ;
787796
788797 /**
789798 * Fetches the latest block hash and log any errors that are returned
@@ -854,20 +863,20 @@ export class LitCore {
854863 log (
855864 'Attempting to fetch blockhash manually using ethers with fallback RPC URLs...'
856865 ) ;
857- const provider = await this . _getProviderWithFallback ( ) ;
866+ const { testResult } =
867+ ( await this . _getProviderWithFallback < ethers . providers . Block > (
868+ // We use a previous block to avoid nodes not having received the latest block yet
869+ ( provider ) => provider . getBlock ( BLOCKHASH_COUNT_PROVIDER_DELAY )
870+ ) ) || { } ;
858871
859- if ( ! provider ) {
872+ if ( ! testResult || ! testResult . hash ) {
860873 logError ( 'All fallback RPC URLs failed. Unable to retrieve blockhash.' ) ;
861874 return ;
862875 }
863876
864877 try {
865- // We use a previous block to avoid nodes not having received the latest block yet
866- const priorBlock = await provider . getBlock (
867- BLOCKHASH_COUNT_PROVIDER_DELAY
868- ) ;
869- this . latestBlockhash = priorBlock . hash ;
870- this . lastBlockHashRetrieved = priorBlock . timestamp ;
878+ this . latestBlockhash = testResult . hash ;
879+ this . lastBlockHashRetrieved = testResult . timestamp ;
871880 log (
872881 'Successfully retrieved blockhash manually: ' ,
873882 this . latestBlockhash
0 commit comments