From 96a486866c3cf3fdb3c7726837229550fac581b9 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 13 Nov 2022 21:05:37 +0000 Subject: [PATCH] add optional display name --- config/example.toml | 14 ++++++++++++++ web3_proxy/src/bin/web3_proxy.rs | 20 ++++++++++++++++++-- web3_proxy/src/config.rs | 10 +++++++++- web3_proxy/src/rpcs/blockchain.rs | 8 ++++---- web3_proxy/src/rpcs/connection.rs | 15 ++++++++++----- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/config/example.toml b/config/example.toml index aa550970..19fce51f 100644 --- a/config/example.toml +++ b/config/example.toml @@ -41,48 +41,57 @@ response_cache_max_bytes = 10_000_000_000 [balanced_rpcs] [balanced_rpcs.ankr] + display_name = "Ankr" url = "https://rpc.ankr.com/eth" soft_limit = 1_000 weight = 0 [balanced_rpcs.cloudflare] + display_name = "Cloudflare" url = "https://cloudflare-eth.com" soft_limit = 1_000 weight = 10 [balanced_rpcs.blastapi] + display_name = "Blast" url = "https://eth-mainnet.public.blastapi.io" soft_limit = 1_000 weight = 10 [balanced_rpcs.mycryptoapi] + display_name = "MyCrypto" disabled = true url = "https://api.mycryptoapi.com/eth" soft_limit = 1_000 weight = 25 [balanced_rpcs.pokt-v1] + display_name = "Pokt #1" url = "https://eth-mainnet.gateway.pokt.network/v1/5f3453978e354ab992c4da79" soft_limit = 500 weight = 25 [balanced_rpcs.pokt] + display_name = "Pokt #2" url = "https://eth-rpc.gateway.pokt.network" soft_limit = 500 weight = 50 [balanced_rpcs.runonflux] + display_name = "Run on Flux (light)" url = "https://ethereumnodelight.app.runonflux.io" soft_limit = 1_000 weight = 75 [balanced_rpcs.linkpool] + display_name = "Linkpool" url = "https://main-rpc.linkpool.io" soft_limit = 500 weight = 75 # load balanced light nodes are not very reliable [balanced_rpcs.linkpool-light] + display_name = "Linkpool (light)" disabled = true url = "https://main-light.eth.linkpool.io" soft_limit = 100 @@ -94,30 +103,35 @@ response_cache_max_bytes = 10_000_000_000 [private_rpcs.eden] disabled = true + display_name = "Eden network" url = "https://api.edennetwork.io/v1/" soft_limit = 1_805 weight = 0 [private_rpcs.eden_beta] disabled = true + display_name = "Eden network beta" url = "https://api.edennetwork.io/v1/beta" soft_limit = 5_861 weight = 0 [private_rpcs.ethermine] disabled = true + display_name = "Ethermine" url = "https://rpc.ethermine.org" soft_limit = 5_861 weight = 0 [private_rpcs.flashbots] disabled = true + display_name = "Flashbots Fast" url = "https://rpc.flashbots.net/fast" soft_limit = 7_074 weight = 0 [private_rpcs.securerpc] disabled = true + display_name = "SecureRPC" url = "https://gibson.securerpc.com/v1" soft_limit = 4_560 weight = 0 diff --git a/web3_proxy/src/bin/web3_proxy.rs b/web3_proxy/src/bin/web3_proxy.rs index c951ed60..f67ac589 100644 --- a/web3_proxy/src/bin/web3_proxy.rs +++ b/web3_proxy/src/bin/web3_proxy.rs @@ -275,11 +275,27 @@ mod tests { balanced_rpcs: HashMap::from([ ( "anvil".to_string(), - Web3ConnectionConfig::new(false, anvil.endpoint(), 100, None, 1, Some(false)), + Web3ConnectionConfig::new( + false, + None, + anvil.endpoint(), + 100, + None, + 1, + Some(false), + ), ), ( "anvil_ws".to_string(), - Web3ConnectionConfig::new(false, anvil.ws_endpoint(), 100, None, 0, Some(true)), + Web3ConnectionConfig::new( + false, + None, + anvil.ws_endpoint(), + 100, + None, + 0, + Some(true), + ), ), ]), private_rpcs: None, diff --git a/web3_proxy/src/config.rs b/web3_proxy/src/config.rs index 6967236b..460e77a2 100644 --- a/web3_proxy/src/config.rs +++ b/web3_proxy/src/config.rs @@ -182,6 +182,8 @@ pub struct Web3ConnectionConfig { /// simple way to disable a connection without deleting the row #[serde(default)] pub disabled: bool, + /// a name used in /status and other user facing messages + pub display_name: Option, /// websocket (or http if no websocket) pub url: String, /// the requests per second at which the server starts slowing down @@ -189,6 +191,7 @@ pub struct Web3ConnectionConfig { /// the requests per second at which the server throws errors (rate limit or otherwise) pub hard_limit: Option, /// All else equal, a server with a lower weight receives more requests. Ranges 0-100 + #[serde(default = "default_weight")] pub weight: u32, /// Subscribe to the firehose of pending transactions /// Don't do this with free rpcs @@ -196,9 +199,13 @@ pub struct Web3ConnectionConfig { pub subscribe_txs: Option, } +fn default_weight() -> u32 { + 0 +} + impl Web3ConnectionConfig { /// Create a Web3Connection from config - /// TODO: move this into Web3Connection (just need to make things pub(crate)) + /// TODO: move this into Web3Connection? (just need to make things pub(crate)) #[allow(clippy::too_many_arguments)] pub async fn spawn( self, @@ -232,6 +239,7 @@ impl Web3ConnectionConfig { Web3Connection::spawn( name, + self.display_name, chain_id, db_conn, self.url, diff --git a/web3_proxy/src/rpcs/blockchain.rs b/web3_proxy/src/rpcs/blockchain.rs index 267174fd..c64e002f 100644 --- a/web3_proxy/src/rpcs/blockchain.rs +++ b/web3_proxy/src/rpcs/blockchain.rs @@ -501,7 +501,7 @@ impl Web3Connections { if consensus_head_block_id.hash == old_block_id.hash { // no change in hash. no need to use head_block_sender debug!( - "con {}/{}/{}. con head={}. rpc={}. rpc head={}", + "con {}/{}/{} con_head={} rpc={} rpc_head={}", num_consensus_rpcs, num_connection_heads, total_conns, @@ -512,7 +512,7 @@ impl Web3Connections { } else { // hash changed debug!( - "unc {}/{}/{} con_head={}. old={}. rpc_head={}. rpc={}", + "unc {}/{}/{} con_head={} old={} rpc_head={} rpc={}", num_consensus_rpcs, num_connection_heads, total_conns, @@ -534,7 +534,7 @@ impl Web3Connections { Ordering::Less => { // this is unlikely but possible // TODO: better log - warn!("chain rolled back {}/{}/{}. con_head={} old_head={}. rpc_head={}. rpc={}", num_consensus_rpcs, num_connection_heads, total_conns, consensus_head_block_id, old_block_id, rpc_head_str, rpc); + warn!("chain rolled back {}/{}/{} con_head={} old_head={} rpc_head={} rpc={}", num_consensus_rpcs, num_connection_heads, total_conns, consensus_head_block_id, old_block_id, rpc_head_str, rpc); // TODO: tell save_block to remove any higher block numbers from the cache. not needed because we have other checks on requested blocks being > head, but still seems slike a good idea self.save_block(&consensus_head_block, true).await.context( @@ -547,7 +547,7 @@ impl Web3Connections { } Ordering::Greater => { debug!( - "new {}/{}/{} conn_head={}. rpc_head={}. rpc={}", + "new {}/{}/{} con head={} rpc_head={} rpc={}", num_consensus_rpcs, num_connection_heads, total_conns, diff --git a/web3_proxy/src/rpcs/connection.rs b/web3_proxy/src/rpcs/connection.rs index d6aa2260..01c65056 100644 --- a/web3_proxy/src/rpcs/connection.rs +++ b/web3_proxy/src/rpcs/connection.rs @@ -30,6 +30,7 @@ use tokio::time::{interval, sleep, sleep_until, Duration, Instant, MissedTickBeh /// An active connection to a Web3 RPC server like geth or erigon. pub struct Web3Connection { pub name: String, + pub display_name: Option, /// TODO: can we get this from the provider? do we even need it? url: String, /// Some connections use an http_client. we keep a clone for reconnecting @@ -62,6 +63,7 @@ impl Web3Connection { #[allow(clippy::too_many_arguments)] pub async fn spawn( name: String, + display_name: Option, chain_id: u64, db_conn: Option, url_str: String, @@ -95,6 +97,7 @@ impl Web3Connection { let new_connection = Self { name, + display_name, http_client, url: url_str, active_requests: 0.into(), @@ -737,7 +740,7 @@ impl Web3Connection { authorization: Arc, tx_id_sender: flume::Sender<(TxHash, Arc)>, ) -> anyhow::Result<()> { - info!( "watching pending transactions on {}", self); + info!("watching pending transactions on {}", self); // TODO: is a RwLock of an Option the right thing here? if let Some(provider) = self.provider.read().await.clone() { @@ -790,7 +793,7 @@ impl Web3Connection { // TODO: is this always an error? // TODO: we probably don't want a warn and to return error - warn!( "pending_transactions subscription ended on {}", self); + warn!("pending_transactions subscription ended on {}", self); return Err(anyhow::anyhow!("pending_transactions subscription ended")); } } @@ -859,7 +862,7 @@ impl Web3Connection { // save the smallest retry_after. if nothing succeeds, return an Err with retry_after in it // TODO: use tracing better // TODO: i'm seeing "Exhausted rate limit on moralis: 0ns". How is it getting 0? - warn!( "Exhausted rate limit on {}. Retry at {:?}", self, retry_at); + warn!("Exhausted rate limit on {}. Retry at {:?}", self, retry_at); return Ok(OpenRequestResult::RetryAt(retry_at)); } @@ -915,10 +918,12 @@ impl Serialize for Web3Connection { S: Serializer, { // 3 is the number of fields in the struct. - let mut state = serializer.serialize_struct("Web3Connection", 7)?; + let mut state = serializer.serialize_struct("Web3Connection", 8)?; - // the url is excluded because it likely includes private information. just show the name + // the url is excluded because it likely includes private information. just show the name that we use in keys state.serialize_field("name", &self.name)?; + // a longer name for display to users + state.serialize_field("display_name", &self.display_name)?; let block_data_limit = self.block_data_limit.load(atomic::Ordering::Relaxed); if block_data_limit == u64::MAX {