From 450e228a8f4d4d8687b7e928e420f7b2732db927 Mon Sep 17 00:00:00 2001 From: sprites0 <199826320+sprites0@users.noreply.github.com> Date: Sat, 5 Jul 2025 03:07:27 +0000 Subject: [PATCH] refactor: Move official RPC url to HlChainSpec --- src/chainspec/mod.rs | 13 +++++++++++++ src/node/network/mod.rs | 9 ++++++++- src/pseudo_peer/mod.rs | 6 +++++- src/pseudo_peer/service.rs | 20 +++++++++++++++----- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/chainspec/mod.rs b/src/chainspec/mod.rs index 4a0fcb125..79a66a1a4 100644 --- a/src/chainspec/mod.rs +++ b/src/chainspec/mod.rs @@ -137,3 +137,16 @@ impl HlHardforks for Arc { self.as_ref().hl_fork_activation(fork) } } + +impl HlChainSpec { + pub const MAINNET_RPC_URL: &str = "https://rpc.hyperliquid.xyz/evm"; + pub const TESTNET_RPC_URL: &str = "https://rpc.hyperliquid-testnet.xyz/evm"; + + pub fn official_rpc_url(&self) -> &'static str { + match self.inner.chain().id() { + 999 => Self::MAINNET_RPC_URL, + 998 => Self::TESTNET_RPC_URL, + _ => unreachable!("Unreachable since ChainSpecParser won't return other chains"), + } + } +} diff --git a/src/node/network/mod.rs b/src/node/network/mod.rs index 63db98cce..ef2e7eb1e 100644 --- a/src/node/network/mod.rs +++ b/src/node/network/mod.rs @@ -226,11 +226,18 @@ where let network = NetworkManager::builder(network_config).await?; let handle = ctx.start_network(network, pool); let local_node_record = handle.local_node_record(); + let chain_spec = ctx.chain_spec(); info!(target: "reth::cli", enode=%local_node_record, "P2P networking initialized"); ctx.task_executor().spawn_critical("pseudo peer", async move { let block_source = block_source_config.create_cached_block_source().await; - start_pseudo_peer(local_node_record.to_string(), block_source).await.unwrap(); + start_pseudo_peer( + chain_spec, + local_node_record.to_string(), + block_source, + ) + .await + .unwrap(); }); Ok(handle) diff --git a/src/pseudo_peer/mod.rs b/src/pseudo_peer/mod.rs index ac84b6690..991508b11 100644 --- a/src/pseudo_peer/mod.rs +++ b/src/pseudo_peer/mod.rs @@ -12,6 +12,8 @@ pub mod service; pub mod sources; pub mod utils; +use std::sync::Arc; + pub use cli::*; pub use config::*; pub use error::*; @@ -35,10 +37,12 @@ pub mod prelude { }; } +use crate::chainspec::HlChainSpec; use reth_network::{NetworkEvent, NetworkEventListenerProvider}; /// Main function that starts the network manager and processes eth requests pub async fn start_pseudo_peer( + chain_spec: Arc, destination_peer: String, block_source: BlockSourceBoxed, ) -> eyre::Result<()> { @@ -63,7 +67,7 @@ pub async fn start_pseudo_peer( let mut network_events = network_handle.event_listener(); info!("Starting network manager..."); - let mut service = PseudoPeer::new(block_source, blockhash_cache.clone()); + let mut service = PseudoPeer::new(chain_spec, block_source, blockhash_cache.clone()); tokio::spawn(network); let mut first = true; diff --git a/src/pseudo_peer/service.rs b/src/pseudo_peer/service.rs index 23902827b..2853b955e 100644 --- a/src/pseudo_peer/service.rs +++ b/src/pseudo_peer/service.rs @@ -1,7 +1,10 @@ use super::{sources::BlockSource, utils::LruBiMap}; -use crate::node::{ - network::{HlNetworkPrimitives, HlNewBlock}, - types::BlockAndReceipts, +use crate::{ + chainspec::HlChainSpec, + node::{ + network::{HlNetworkPrimitives, HlNewBlock}, + types::BlockAndReceipts, + }, }; use alloy_eips::HashOrNumber; use alloy_primitives::{B256, U128}; @@ -116,6 +119,7 @@ impl BlockImport for BlockPoller { /// A pseudo peer that can process eth requests and feed blocks to reth pub struct PseudoPeer { + chain_spec: Arc, block_source: BS, blockhash_cache: BlockHashCache, warm_cache_size: u64, @@ -127,8 +131,13 @@ pub struct PseudoPeer { } impl PseudoPeer { - pub fn new(block_source: BS, blockhash_cache: BlockHashCache) -> Self { + pub fn new( + chain_spec: Arc, + block_source: BS, + blockhash_cache: BlockHashCache, + ) -> Self { Self { + chain_spec, block_source, blockhash_cache, warm_cache_size: 1000, // reth default chunk size for GetBlockBodies @@ -244,7 +253,8 @@ impl PseudoPeer { use jsonrpsee_core::client::ClientT; debug!("Fallback to official RPC: {hash:?}"); - let client = HttpClientBuilder::default().build("https://rpc.hyperliquid.xyz/evm").unwrap(); + let client = + HttpClientBuilder::default().build(self.chain_spec.official_rpc_url()).unwrap(); let target_block: Block = client.request("eth_getBlockByHash", (hash, false)).await?; debug!("From official RPC: {:?} for {hash:?}", target_block.header.number);