Skip to content

Commit c22fa7c

Browse files
authored
optimize RPCS (#1360)
* optimize RPCS
1 parent 4169759 commit c22fa7c

3 files changed

Lines changed: 26 additions & 45 deletions

File tree

src/@types/blockchain.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface SupportedNetwork {
55
chunkSize?: number
66
startBlock?: number
77
fallbackRPCs?: string[]
8+
primaryRpcTimeout?: number
9+
fallbackRpcTimeout?: number
810
}
911

1012
export interface RPCS {

src/components/BlockchainRegistry/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ export class BlockchainRegistry {
3939

4040
// Get network configuration
4141
const networkConfig = supportedNetworks[chainId.toString()]
42-
const { rpc } = networkConfig
43-
const { fallbackRPCs } = networkConfig
44-
42+
if (!networkConfig.chainId) networkConfig.chainId = chainId
4543
// Create Blockchain instance with new constructor
46-
const blockchain = new Blockchain(this.keyManager, rpc, chainId, fallbackRPCs)
44+
const blockchain = new Blockchain(this.keyManager, networkConfig)
4745

4846
// Cache the instance
4947
this.blockchains.set(chainId, blockchain)

src/utils/blockchain.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
TransactionReceipt
1212
} from 'ethers'
1313
import { CORE_LOGGER } from './logging/common.js'
14-
import { ConnectionStatus } from '../@types/blockchain.js'
15-
14+
import { ConnectionStatus, SupportedNetwork } from '../@types/blockchain.js'
1615
import { KeyManager } from '../components/KeyManager/index.js'
1716

1817
export class Blockchain {
@@ -21,26 +20,23 @@ export class Blockchain {
2120
private provider: FallbackProvider
2221
private chainId: number
2322
private knownRPCs: string[] = []
23+
private primaryRpcTimeout: number
24+
private fallbackRpcTimeout: number
2425

2526
/**
26-
* Constructor overloads:
27-
* 1. New pattern: (rpc, chainId, signer, fallbackRPCs?) - signer provided by KeyManager
28-
* 2. Old pattern: (rpc, chainId, config, fallbackRPCs?) - for backward compatibility
27+
* Creates a new Blockchain instance utilizing KeyManager and a SupportedNetwork configuration
2928
*/
30-
public constructor(
31-
keyManager: KeyManager,
32-
rpc: string,
33-
chainId: number,
34-
fallbackRPCs?: string[]
35-
) {
36-
this.chainId = chainId
29+
public constructor(keyManager: KeyManager, network: SupportedNetwork) {
30+
this.chainId = network.chainId || 0
3731
this.keyManager = keyManager
38-
this.knownRPCs.push(rpc)
39-
if (fallbackRPCs && fallbackRPCs.length > 0) {
40-
this.knownRPCs.push(...fallbackRPCs)
32+
this.knownRPCs.push(network.rpc)
33+
if (network.fallbackRPCs && network.fallbackRPCs.length > 0) {
34+
this.knownRPCs.push(...network.fallbackRPCs)
4135
}
4236
this.provider = undefined as undefined as FallbackProvider
4337
this.signer = undefined as unknown as Signer
38+
this.primaryRpcTimeout = network.primaryRpcTimeout || 3000
39+
this.fallbackRpcTimeout = network.fallbackRpcTimeout || 1500
4440
}
4541

4642
public getSupportedChain(): number {
@@ -68,45 +64,30 @@ export class Blockchain {
6864
}
6965
}
7066

67+
// eslint-disable-next-line require-await
7168
public async getProvider(force: boolean = false): Promise<FallbackProvider> {
72-
if (!this.provider) {
69+
if (force || !this.provider?.providerConfigs?.length) {
7370
const configs: {
7471
provider: JsonRpcProvider
7572
priority: number
7673
stallTimeout: number
7774
}[] = []
7875

79-
const PRIMARY_RPC_TIMEOUT = 3000
80-
const FALLBACK_RPC_TIMEOUT = 1500
8176
for (let i = 0; i < this.knownRPCs.length; i++) {
8277
const rpc = this.knownRPCs[i]
83-
const rpcProvider = new JsonRpcProvider(rpc)
84-
if (!force) {
85-
try {
86-
const { chainId } = await rpcProvider.getNetwork()
87-
if (chainId.toString() === this.chainId.toString()) {
88-
// primary RPC gets lowest priority = is first to be called
89-
configs.push({
90-
provider: rpcProvider,
91-
priority: i + 1,
92-
stallTimeout: i === 0 ? PRIMARY_RPC_TIMEOUT : FALLBACK_RPC_TIMEOUT
93-
})
94-
}
95-
} catch (error) {
96-
CORE_LOGGER.error(`Error getting network for RPC ${rpc}: ${error}`)
97-
}
98-
} else {
99-
configs.push({
100-
provider: rpcProvider,
101-
priority: i + 1,
102-
stallTimeout: i === 0 ? PRIMARY_RPC_TIMEOUT : FALLBACK_RPC_TIMEOUT
103-
})
104-
}
78+
const rpcProvider = new JsonRpcProvider(rpc, this.chainId, {
79+
staticNetwork: true
80+
})
81+
configs.push({
82+
provider: rpcProvider,
83+
priority: i + 1,
84+
stallTimeout: i === 0 ? this.primaryRpcTimeout : this.fallbackRpcTimeout
85+
})
10586
}
10687
// quorum=1: accept the first response to avoid calls to all configured rpcs
10788
this.provider =
10889
configs.length > 0
109-
? new FallbackProvider(configs, undefined, { quorum: 1 })
90+
? new FallbackProvider(configs, this.chainId, { quorum: 1 })
11091
: new FallbackProvider([])
11192
}
11293
return this.provider

0 commit comments

Comments
 (0)