From ad80b22a1b11d4a1d80ebb98a47fa23d450950dc Mon Sep 17 00:00:00 2001 From: Bo QIU <35757521+boqiu@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:48:40 +0800 Subject: [PATCH] Optimize rpc config (#213) --- node/rpc/src/config.rs | 22 ++++++++++++--- node/rpc/src/lib.rs | 11 ++++---- node/src/config/convert.rs | 27 ------------------- node/src/config/mod.rs | 11 +++----- node/src/main.rs | 3 +-- run/config-testnet-standard.toml | 46 +++++++++++++++++--------------- run/config-testnet-turbo.toml | 46 +++++++++++++++++--------------- run/config.toml | 46 +++++++++++++++++--------------- tests/test_framework/zgs_node.py | 10 ++++--- 9 files changed, 107 insertions(+), 115 deletions(-) diff --git a/node/rpc/src/config.rs b/node/rpc/src/config.rs index 902a096..4c1d76d 100644 --- a/node/rpc/src/config.rs +++ b/node/rpc/src/config.rs @@ -1,11 +1,27 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, str::FromStr}; -#[derive(Clone)] +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[serde(default)] pub struct Config { pub enabled: bool, pub listen_address: SocketAddr, - pub listen_address_admin: Option, + pub listen_address_admin: SocketAddr, pub chunks_per_segment: usize, pub max_request_body_size: u32, pub max_cache_file_size: usize, } + +impl Default for Config { + fn default() -> Self { + Self { + enabled: true, + listen_address: SocketAddr::from_str("0.0.0.0:5678").unwrap(), + listen_address_admin: SocketAddr::from_str("127.0.0.1:5679").unwrap(), + chunks_per_segment: 1024, + max_request_body_size: 100 * 1024 * 1024, // 100MB + max_cache_file_size: 10 * 1024 * 1024, // 10MB + } + } +} diff --git a/node/rpc/src/lib.rs b/node/rpc/src/lib.rs index 9056e91..810c5d1 100644 --- a/node/rpc/src/lib.rs +++ b/node/rpc/src/lib.rs @@ -20,7 +20,6 @@ use jsonrpsee::http_server::{HttpServerBuilder, HttpServerHandle}; use network::NetworkGlobals; use network::NetworkMessage; use std::error::Error; -use std::net::SocketAddr; use std::sync::Arc; use storage_async::Store; use sync::{SyncRequest, SyncResponse, SyncSender}; @@ -69,9 +68,10 @@ impl Context { pub async fn run_server( ctx: Context, ) -> Result<(HttpServerHandle, Option), Box> { - let handles = match ctx.config.listen_address_admin { - Some(listen_addr_private) => run_server_public_private(ctx, listen_addr_private).await?, - None => (run_server_all(ctx).await?, None), + let handles = if ctx.config.listen_address.port() != ctx.config.listen_address_admin.port() { + run_server_public_private(ctx).await? + } else { + (run_server_all(ctx).await?, None) }; info!("Server started"); @@ -107,7 +107,6 @@ async fn run_server_all(ctx: Context) -> Result /// Run 2 RPC servers (public & private) for different namespace RPCs. async fn run_server_public_private( ctx: Context, - listen_addr_private: SocketAddr, ) -> Result<(HttpServerHandle, Option), Box> { // public rpc let zgs = (zgs::RpcServerImpl { ctx: ctx.clone() }).into_rpc(); @@ -127,7 +126,7 @@ async fn run_server_public_private( .start(zgs)?; let handle_private = server_builder(ctx.clone()) - .build(listen_addr_private) + .build(ctx.config.listen_address_admin) .await? .start(admin)?; diff --git a/node/src/config/convert.rs b/node/src/config/convert.rs index 865c4f1..e871b84 100644 --- a/node/src/config/convert.rs +++ b/node/src/config/convert.rs @@ -7,7 +7,6 @@ use log_entry_sync::{CacheConfig, ContractAddress, LogSyncConfig}; use miner::MinerConfig; use network::NetworkConfig; use pruner::PrunerConfig; -use rpc::RPCConfig; use shared_types::{NetworkIdentity, ProtocolVersion}; use std::net::IpAddr; use std::time::Duration; @@ -107,32 +106,6 @@ impl ZgsConfig { }) } - pub fn rpc_config(&self) -> Result { - let listen_address = self - .rpc_listen_address - .parse::() - .map_err(|e| format!("Unable to parse rpc_listen_address: {:?}", e))?; - - let listen_address_admin = if self.rpc_listen_address_admin.is_empty() { - None - } else { - Some( - self.rpc_listen_address_admin - .parse::() - .map_err(|e| format!("Unable to parse rpc_listen_address_admin: {:?}", e))?, - ) - }; - - Ok(RPCConfig { - enabled: self.rpc_enabled, - listen_address, - listen_address_admin, - max_request_body_size: self.max_request_body_size, - chunks_per_segment: self.rpc_chunks_per_segment, - max_cache_file_size: self.rpc_max_cache_file_size, - }) - } - pub fn log_sync_config(&self) -> Result { let contract_address = self .log_contract_address diff --git a/node/src/config/mod.rs b/node/src/config/mod.rs index 376f2e3..bad72b9 100644 --- a/node/src/config/mod.rs +++ b/node/src/config/mod.rs @@ -48,14 +48,6 @@ build_config! { (remove_finalized_block_interval_minutes, (u64), 30) (watch_loop_wait_time_ms, (u64), 500) - // rpc - (rpc_enabled, (bool), true) - (rpc_listen_address, (String), "0.0.0.0:5678".to_string()) - (rpc_listen_address_admin, (String), "127.0.0.1:5679".to_string()) - (max_request_body_size, (u32), 100*1024*1024) // 100MB - (rpc_chunks_per_segment, (usize), 1024) - (rpc_max_cache_file_size, (usize), 10*1024*1024) //10MB - // chunk pool (chunk_pool_write_window_size, (usize), 4) (chunk_pool_max_cached_chunks_all, (usize), 4*1024*1024) // 1G @@ -103,6 +95,9 @@ pub struct ZgsConfig { // file location cache config, configured by [file_location_cache] section by `config` crate. pub file_location_cache: file_location_cache::Config, + // rpc config, configured by [rpc] section by `config` crate. + pub rpc: rpc::RPCConfig, + // metrics config, configured by [metrics] section by `config` crate. pub metrics: metrics::MetricsConfiguration, } diff --git a/node/src/main.rs b/node/src/main.rs index 1c7d25b..cfac0b7 100644 --- a/node/src/main.rs +++ b/node/src/main.rs @@ -13,7 +13,6 @@ use std::error::Error; async fn start_node(context: RuntimeContext, config: ZgsConfig) -> Result { let network_config = config.network_config().await?; let storage_config = config.storage_config()?; - let rpc_config = config.rpc_config()?; let log_sync_config = config.log_sync_config()?; let miner_config = config.mine_config()?; let router_config = config.router_config(&network_config)?; @@ -33,7 +32,7 @@ async fn start_node(context: RuntimeContext, config: ZgsConfig) -> Result