Skip to content

optimize RPCS#1360

Merged
alexcos20 merged 5 commits intomainfrom
feature/rpc_fixes
Apr 29, 2026
Merged

optimize RPCS#1360
alexcos20 merged 5 commits intomainfrom
feature/rpc_fixes

Conversation

@alexcos20
Copy link
Copy Markdown
Member

@alexcos20 alexcos20 commented Apr 29, 2026

PR Description

Summary

This PR improves RPC provider initialization and fallback behavior by refactoring Blockchain construction to use full network configuration objects and by making provider timeouts configurable per network.

What Changed

1) Blockchain network typing extended

  • Updated src/@types/blockchain.ts:
    • Added optional primaryRpcTimeout and fallbackRpcTimeout to SupportedNetwork.

2) BlockchainRegistry now passes full network config

  • Updated src/components/BlockchainRegistry/index.ts:
    • Replaced new Blockchain(this.keyManager, rpc, chainId, fallbackRPCs) with:
      • new Blockchain(this.keyManager, networkConfig)
    • This centralizes network-specific behavior in one config object.

3) Blockchain constructor and provider initialization refactor

  • Updated src/utils/blockchain.ts:
    • Constructor now accepts SupportedNetwork instead of separate RPC params.
    • Initializes:
      • knownRPCs from network.rpc + network.fallbackRPCs
      • primaryRpcTimeout (default 3000ms)
      • fallbackRpcTimeout (default 1500ms)
    • getProvider(force) behavior improved:
      • Rebuilds provider when force=true.
      • Safely handles uninitialized provider state.
    • RPC providers are created with staticNetwork: true to avoid repeated network detection overhead.
    • Removed per-RPC getNetwork() validation loop and now builds fallback config directly from declared network RPCs.

Why

  • Make RPC behavior deterministic and config-driven.
  • Reduce runtime overhead during provider setup.
  • Support network-specific timeout tuning without code changes.
  • Ensure forced provider refresh actually rebuilds provider internals.

@alexcos20

This comment was marked as outdated.

alexcos20

This comment was marked as outdated.

@alexcos20

This comment was marked as outdated.

alexcos20

This comment was marked as outdated.

@alexcos20
Copy link
Copy Markdown
Member Author

/run-security-scan

Copy link
Copy Markdown
Member Author

@alexcos20 alexcos20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI automated code review (Gemini 3).

Overall risk: low

Summary:
This PR introduces an excellent performance improvement by removing blocking network calls (rpcProvider.getNetwork()) during provider initialization and utilizing the staticNetwork: true configuration in ethers v6. It also adds flexible timeouts for primary and fallback RPCs. The code is well-structured, but there is a minor risk of state mutation and potential runtime errors when interacting with networkConfig in the registry.

Comments:
• [WARNING][bug] Mutating networkConfig directly can cause side effects if supportedNetworks is a shared global object/cache. Additionally, if chainId does not exist in supportedNetworks (unlikely but possible), networkConfig will be undefined, causing a TypeError when accessing .chainId. It is safer to use the spread operator to create a new object.

-    if (!networkConfig.chainId) networkConfig.chainId = chainId
-    // Create Blockchain instance with new constructor
-    const blockchain = new Blockchain(this.keyManager, networkConfig)
+    // Create Blockchain instance with new constructor
+    const blockchain = new Blockchain(this.keyManager, { ...networkConfig, chainId })

• [INFO][performance] Excellent optimization! Removing the synchronous await rpcProvider.getNetwork() call inside the loop and using staticNetwork: true will significantly speed up provider initialization and prevent potential node stalls from unresponsive RPCs. LGTM!

@alexcos20 alexcos20 marked this pull request as ready for review April 29, 2026 12:43
@alexcos20 alexcos20 merged commit c22fa7c into main Apr 29, 2026
11 checks passed
@alexcos20 alexcos20 deleted the feature/rpc_fixes branch April 29, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants