From 0fbb643524e4f16bc919280934a81768faf36629 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 29 Apr 2026 15:22:20 +0300 Subject: [PATCH 1/4] optimize RPCS --- src/@types/blockchain.ts | 2 + src/components/BlockchainRegistry/index.ts | 4 +- src/utils/blockchain.ts | 56 ++++++++-------------- 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/src/@types/blockchain.ts b/src/@types/blockchain.ts index 36b158e24..0e67c6ada 100644 --- a/src/@types/blockchain.ts +++ b/src/@types/blockchain.ts @@ -5,6 +5,8 @@ export interface SupportedNetwork { chunkSize?: number startBlock?: number fallbackRPCs?: string[] + primaryRpcTimeout?: number + fallbackRpcTimeout?: number } export interface RPCS { diff --git a/src/components/BlockchainRegistry/index.ts b/src/components/BlockchainRegistry/index.ts index c95f2ecba..08ca36f36 100644 --- a/src/components/BlockchainRegistry/index.ts +++ b/src/components/BlockchainRegistry/index.ts @@ -39,11 +39,9 @@ export class BlockchainRegistry { // Get network configuration const networkConfig = supportedNetworks[chainId.toString()] - const { rpc } = networkConfig - const { fallbackRPCs } = networkConfig // Create Blockchain instance with new constructor - const blockchain = new Blockchain(this.keyManager, rpc, chainId, fallbackRPCs) + const blockchain = new Blockchain(this.keyManager, networkConfig) // Cache the instance this.blockchains.set(chainId, blockchain) diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index 0bf9458f7..2e2722351 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -12,7 +12,7 @@ import { } from 'ethers' import { getConfiguration } from './config.js' import { CORE_LOGGER } from './logging/common.js' -import { ConnectionStatus } from '../@types/blockchain.js' +import { ConnectionStatus, SupportedNetwork } from '../@types/blockchain.js' import { ValidateChainId } from '../@types/commands.js' import { KeyManager } from '../components/KeyManager/index.js' @@ -22,26 +22,25 @@ export class Blockchain { private provider: FallbackProvider private chainId: number private knownRPCs: string[] = [] + private primaryRpcTimeout: number + private fallbackRpcTimeout: number /** * Constructor overloads: * 1. New pattern: (rpc, chainId, signer, fallbackRPCs?) - signer provided by KeyManager * 2. Old pattern: (rpc, chainId, config, fallbackRPCs?) - for backward compatibility */ - public constructor( - keyManager: KeyManager, - rpc: string, - chainId: number, - fallbackRPCs?: string[] - ) { - this.chainId = chainId + public constructor(keyManager: KeyManager, network: SupportedNetwork) { + this.chainId = network.chainId || 0 this.keyManager = keyManager - this.knownRPCs.push(rpc) - if (fallbackRPCs && fallbackRPCs.length > 0) { - this.knownRPCs.push(...fallbackRPCs) + this.knownRPCs.push(network.rpc) + if (network.fallbackRPCs && network.fallbackRPCs.length > 0) { + this.knownRPCs.push(...network.fallbackRPCs) } this.provider = undefined as undefined as FallbackProvider this.signer = undefined as unknown as Signer + this.primaryRpcTimeout = network.primaryRpcTimeout || 3000 + this.fallbackRpcTimeout = network.fallbackRpcTimeout || 1500 } public getSupportedChain(): number { @@ -56,40 +55,25 @@ export class Blockchain { return await this.signer.getAddress() } + // eslint-disable-next-line require-await public async getProvider(force: boolean = false): Promise { - if (!this.provider) { + if (force || !this.provider?.providerConfigs?.length) { const configs: { provider: JsonRpcProvider priority: number stallTimeout: number }[] = [] - const PRIMARY_RPC_TIMEOUT = 3000 - const FALLBACK_RPC_TIMEOUT = 1500 for (let i = 0; i < this.knownRPCs.length; i++) { const rpc = this.knownRPCs[i] - const rpcProvider = new JsonRpcProvider(rpc) - if (!force) { - try { - const { chainId } = await rpcProvider.getNetwork() - if (chainId.toString() === this.chainId.toString()) { - // primary RPC gets lowest priority = is first to be called - configs.push({ - provider: rpcProvider, - priority: i + 1, - stallTimeout: i === 0 ? PRIMARY_RPC_TIMEOUT : FALLBACK_RPC_TIMEOUT - }) - } - } catch (error) { - CORE_LOGGER.error(`Error getting network for RPC ${rpc}: ${error}`) - } - } else { - configs.push({ - provider: rpcProvider, - priority: i + 1, - stallTimeout: i === 0 ? PRIMARY_RPC_TIMEOUT : FALLBACK_RPC_TIMEOUT - }) - } + const rpcProvider = new ethers.JsonRpcProvider(rpc, undefined, { + staticNetwork: true + }) + configs.push({ + provider: rpcProvider, + priority: i + 1, + stallTimeout: i === 0 ? this.primaryRpcTimeout : this.fallbackRpcTimeout + }) } // quorum=1: accept the first response to avoid calls to all configured rpcs this.provider = From f5b58ee79fa587e03151becbc125df6e2599b52f Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 29 Apr 2026 15:29:03 +0300 Subject: [PATCH 2/4] fix --- src/components/BlockchainRegistry/index.ts | 2 +- src/utils/blockchain.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/BlockchainRegistry/index.ts b/src/components/BlockchainRegistry/index.ts index 686e01678..b1ce9b7aa 100644 --- a/src/components/BlockchainRegistry/index.ts +++ b/src/components/BlockchainRegistry/index.ts @@ -39,7 +39,7 @@ export class BlockchainRegistry { // Get network configuration const networkConfig = supportedNetworks[chainId.toString()] - + if (!networkConfig.chainId) networkConfig.chainId = chainId // Create Blockchain instance with new constructor const blockchain = new Blockchain(this.keyManager, networkConfig) diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index 026ca9fe2..458a3c12c 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -12,7 +12,6 @@ import { } from 'ethers' import { CORE_LOGGER } from './logging/common.js' import { ConnectionStatus, SupportedNetwork } from '../@types/blockchain.js' -import { ValidateChainId } from '../@types/commands.js' import { KeyManager } from '../components/KeyManager/index.js' export class Blockchain { @@ -68,6 +67,7 @@ export class Blockchain { } } + // eslint-disable-next-line require-await public async getProvider(force: boolean = false): Promise { if (force || !this.provider?.providerConfigs?.length) { const configs: { From 1543676c3304217832842a46eb68516a0bc392d3 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 29 Apr 2026 15:38:31 +0300 Subject: [PATCH 3/4] fix --- src/utils/blockchain.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index 458a3c12c..b364bffc4 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -24,9 +24,7 @@ export class Blockchain { private fallbackRpcTimeout: number /** - * Constructor overloads: - * 1. New pattern: (rpc, chainId, signer, fallbackRPCs?) - signer provided by KeyManager - * 2. Old pattern: (rpc, chainId, config, fallbackRPCs?) - for backward compatibility + * Creates a new Blockchain instance utilizing KeyManager and a SupportedNetwork configuration */ public constructor(keyManager: KeyManager, network: SupportedNetwork) { this.chainId = network.chainId || 0 @@ -53,7 +51,6 @@ export class Blockchain { return await this.signer.getAddress() } - // eslint-disable-next-line require-await public stop() { if (this.provider) { this.provider.providerConfigs.forEach((config) => { @@ -78,7 +75,7 @@ export class Blockchain { for (let i = 0; i < this.knownRPCs.length; i++) { const rpc = this.knownRPCs[i] - const rpcProvider = new ethers.JsonRpcProvider(rpc, undefined, { + const rpcProvider = new ethers.JsonRpcProvider(rpc, this.chainId, { staticNetwork: true }) configs.push({ @@ -90,7 +87,7 @@ export class Blockchain { // quorum=1: accept the first response to avoid calls to all configured rpcs this.provider = configs.length > 0 - ? new FallbackProvider(configs, undefined, { quorum: 1 }) + ? new FallbackProvider(configs, this.chainId, { quorum: 1 }) : new FallbackProvider([]) } return this.provider From 2bcff57c90765aa9663e034e9c55e371e23bee4d Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 29 Apr 2026 15:40:57 +0300 Subject: [PATCH 4/4] fix --- src/utils/blockchain.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index b364bffc4..962246031 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -75,7 +75,7 @@ export class Blockchain { for (let i = 0; i < this.knownRPCs.length; i++) { const rpc = this.knownRPCs[i] - const rpcProvider = new ethers.JsonRpcProvider(rpc, this.chainId, { + const rpcProvider = new JsonRpcProvider(rpc, this.chainId, { staticNetwork: true }) configs.push({