diff --git a/web3_proxy/src/rpcs/consensus.rs b/web3_proxy/src/rpcs/consensus.rs index 4d20691b..9a980968 100644 --- a/web3_proxy/src/rpcs/consensus.rs +++ b/web3_proxy/src/rpcs/consensus.rs @@ -49,15 +49,15 @@ impl ConsensusRpcData { #[derive(Constructor, Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)] pub struct RpcRanking { - tier: u8, + tier: u32, backup: bool, head_num: Option, } impl RpcRanking { - pub fn add_offset(&self, offset: u8) -> Self { + pub fn add_offset(&self, offset: u32) -> Self { Self { - tier: self.tier + offset, + tier: self.tier.saturating_add(offset), backup: self.backup, head_num: self.head_num, } @@ -70,7 +70,7 @@ impl RpcRanking { } } - fn sort_key(&self) -> (bool, u8, Reverse>) { + fn sort_key(&self) -> (bool, u32, Reverse>) { // TODO: add soft_limit here? add peak_ewma here? // TODO: should backup or tier be checked first? now that tiers are automated, backups // TODO: should we include a random number in here? @@ -104,7 +104,7 @@ pub enum ShouldWaitForBlock { /// TODO: one data structure of head_rpcs and other_rpcs that is sorted best first #[derive(Clone, Serialize)] pub struct ConsensusWeb3Rpcs { - pub(crate) tier: u8, + pub(crate) tier: u32, pub(crate) backups_needed: bool, // TODO: this is already inside best_rpcs. Don't skip, instead make a shorter serialize @@ -494,16 +494,13 @@ impl ConsensusFinder { trace!("weighted_latencies: {}", encoded); } - // TODO: get someone who is better at math to do something smarter - // this is not a very good use of stddev, but it works for now - let stddev = hist.stdev(); + // TODO: get someone who is better at math to do something smarter. maybe involving stddev? + let divisor = 30f64.max(min_latency as f64 / 2.0); for (rpc, weighted_latency_ms) in weighted_latencies.into_iter() { - let tier = (weighted_latency_ms - min_latency) as f64 / stddev; + let tier = (weighted_latency_ms - min_latency) as f64 / divisor; - let tier = tier.floor() as u64; - - let tier = tier.clamp(u8::MIN.into(), u8::MAX.into()) as u8; + let tier = tier.floor() as u32; // TODO: this should be trace trace!( @@ -745,7 +742,7 @@ impl ConsensusFinder { None } - pub fn worst_tier(&self) -> Option { + pub fn worst_tier(&self) -> Option { self.rpc_heads .iter() .map(|(x, _)| x.tier.load(atomic::Ordering::Relaxed)) diff --git a/web3_proxy/src/rpcs/one.rs b/web3_proxy/src/rpcs/one.rs index 6016205b..12d80f44 100644 --- a/web3_proxy/src/rpcs/one.rs +++ b/web3_proxy/src/rpcs/one.rs @@ -27,7 +27,7 @@ use serde_json::json; use std::cmp::Reverse; use std::fmt; use std::hash::{Hash, Hasher}; -use std::sync::atomic::{self, AtomicU64, AtomicU8, AtomicUsize}; +use std::sync::atomic::{self, AtomicU32, AtomicU64, AtomicUsize}; use std::{cmp::Ordering, sync::Arc}; use tokio::select; use tokio::sync::watch; @@ -68,7 +68,7 @@ pub struct Web3Rpc { /// peak_latency is only inside an Option so that the "Default" derive works. it will always be set. pub(super) peak_latency: Option, /// Automatically set priority - pub(super) tier: AtomicU8, + pub(super) tier: AtomicU32, /// Track total requests served /// TODO: maybe move this to graphana pub(super) total_requests: AtomicUsize, @@ -230,7 +230,7 @@ impl Web3Rpc { /// TODO: tests on this! /// TODO: should tier or block number take priority? /// TODO: should this return a struct that implements sorting traits? - fn sort_on(&self, max_block: Option) -> (bool, u8, Reverse) { + fn sort_on(&self, max_block: Option) -> (bool, u32, Reverse) { let mut head_block = self .head_block .as_ref() @@ -251,7 +251,7 @@ impl Web3Rpc { pub fn sort_for_load_balancing_on( &self, max_block: Option, - ) -> ((bool, u8, Reverse), OrderedFloat) { + ) -> ((bool, u32, Reverse), OrderedFloat) { let sort_on = self.sort_on(max_block); let weighted_peak_ewma_seconds = self.weighted_peak_ewma_seconds(); @@ -267,7 +267,7 @@ impl Web3Rpc { pub fn shuffle_for_load_balancing_on( &self, max_block: Option, - ) -> ((bool, u8, Reverse), u32) { + ) -> ((bool, u32, Reverse), u32) { let sort_on = self.sort_on(max_block); let mut rng = nanorand::tls_rng();