From 9fabb8e1e110fd5695c721e71c760c618d0be181 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 27 Aug 2022 00:33:45 +0000 Subject: [PATCH] add min_synced_rpcs to config --- web3_proxy/src/app.rs | 3 +++ web3_proxy/src/bin/web3_proxy.rs | 1 + web3_proxy/src/config.rs | 6 ++++++ web3_proxy/src/rpcs/blockchain.rs | 6 ++---- web3_proxy/src/rpcs/connections.rs | 3 +++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/web3_proxy/src/app.rs b/web3_proxy/src/app.rs index 4b7b577c..7d62c439 100644 --- a/web3_proxy/src/app.rs +++ b/web3_proxy/src/app.rs @@ -255,6 +255,7 @@ impl Web3ProxyApp { redis_pool.clone(), block_map.clone(), Some(head_block_sender), + top_config.app.min_synced_rpcs, Some(pending_tx_sender.clone()), pending_transactions.clone(), ) @@ -276,6 +277,8 @@ impl Web3ProxyApp { block_map, // subscribing to new heads here won't work well. if they are fast, they might be ahead of balanced_rpcs None, + // minimum doesn't really matter on private rpcs + 1, // TODO: subscribe to pending transactions on the private rpcs? they seem to have low rate limits None, pending_transactions.clone(), diff --git a/web3_proxy/src/bin/web3_proxy.rs b/web3_proxy/src/bin/web3_proxy.rs index e6480fc4..1bb57f47 100644 --- a/web3_proxy/src/bin/web3_proxy.rs +++ b/web3_proxy/src/bin/web3_proxy.rs @@ -217,6 +217,7 @@ mod tests { db_url: None, invite_code: None, redis_url: None, + min_synced_rpcs: 1, public_rate_limit_per_minute: 0, response_cache_max_bytes: 10_usize.pow(7), redirect_public_url: "example.com/".to_string(), diff --git a/web3_proxy/src/config.rs b/web3_proxy/src/config.rs index a21798f2..d85fbf39 100644 --- a/web3_proxy/src/config.rs +++ b/web3_proxy/src/config.rs @@ -45,6 +45,8 @@ pub struct AppConfig { pub chain_id: u64, pub db_url: Option, pub invite_code: Option, + #[serde(default = "default_min_synced_rpcs")] + pub min_synced_rpcs: usize, pub redis_url: Option, #[serde(default = "default_public_rate_limit_per_minute")] pub public_rate_limit_per_minute: u64, @@ -56,6 +58,10 @@ pub struct AppConfig { pub redirect_user_url: String, } +fn default_min_synced_rpcs() -> usize { + 1 +} + fn default_public_rate_limit_per_minute() -> u64 { 0 } diff --git a/web3_proxy/src/rpcs/blockchain.rs b/web3_proxy/src/rpcs/blockchain.rs index 72c759a3..7688375f 100644 --- a/web3_proxy/src/rpcs/blockchain.rs +++ b/web3_proxy/src/rpcs/blockchain.rs @@ -366,16 +366,14 @@ impl Web3Connections { drop(blockchain_guard); let soft_limit_met = consensus_soft_limit >= min_sum_soft_limit; - let num_synced_rpcs = consensus_rpcs.len(); - // TODO: put this in config - let min_synced_rpcs = 2; let new_synced_connections = if soft_limit_met { // we have a consensus large enough to serve traffic let head_block_hash = highest_work_block.hash.unwrap(); let head_block_num = highest_work_block.number.unwrap(); + let num_synced_rpcs = consensus_rpcs.len(); - if num_synced_rpcs < min_synced_rpcs { + if num_synced_rpcs < self.min_synced_rpcs { trace!(hash=%head_block_hash, num=?head_block_num, "not enough rpcs are synced to advance"); return Ok(()); diff --git a/web3_proxy/src/rpcs/connections.rs b/web3_proxy/src/rpcs/connections.rs index bd1612ec..ab853365 100644 --- a/web3_proxy/src/rpcs/connections.rs +++ b/web3_proxy/src/rpcs/connections.rs @@ -45,6 +45,7 @@ pub struct Web3Connections { /// TODO: this map is going to grow forever unless we do some sort of pruning. maybe store pruned in redis? /// TODO: what should we use for edges? pub(super) blockchain_graphmap: RwLock>, + pub(super) min_synced_rpcs: usize, } impl Web3Connections { @@ -57,6 +58,7 @@ impl Web3Connections { redis_client_pool: Option, block_map: BlockMap, head_block_sender: Option>>>, + min_synced_rpcs: usize, pending_tx_sender: Option>, pending_transactions: Arc>, ) -> anyhow::Result<(Arc, AnyhowJoinHandle<()>)> { @@ -169,6 +171,7 @@ impl Web3Connections { pending_transactions, block_map: Default::default(), blockchain_graphmap: Default::default(), + min_synced_rpcs, }); let handle = {