From b72c49b5eda008bf5a0a8a7af9767aa33819da7c Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 6 May 2022 06:07:01 +0000 Subject: [PATCH] thats fast --- web3-proxy/src/connection.rs | 13 +++++-- web3-proxy/src/connections.rs | 64 +++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/web3-proxy/src/connection.rs b/web3-proxy/src/connection.rs index b5558bd1..fe30d4d0 100644 --- a/web3-proxy/src/connection.rs +++ b/web3-proxy/src/connection.rs @@ -120,10 +120,17 @@ impl Web3Connection { }) } + #[inline] pub fn active_requests(&self) -> u32 { self.active_requests.load(atomic::Ordering::Acquire) } + #[inline] + pub fn head_block_number(&self) -> u64 { + self.head_block_number.load(atomic::Ordering::Acquire) + } + + #[inline] pub fn url(&self) -> &str { &self.url } @@ -174,7 +181,7 @@ impl Web3Connection { ); if let Some(connections) = &connections { - connections.update_synced_rpcs(&self, block_number)?; + connections.update_synced_rpcs(&self)?; } } } @@ -214,7 +221,7 @@ impl Web3Connection { ); if let Some(connections) = &connections { - connections.update_synced_rpcs(&self, block_number)?; + connections.update_synced_rpcs(&self)?; } while let Some(block) = stream.next().await { @@ -232,7 +239,7 @@ impl Web3Connection { info!("new block on {}: {}", self, block_number); if let Some(connections) = &connections { - connections.update_synced_rpcs(&self, block_number)?; + connections.update_synced_rpcs(&self)?; } } } diff --git a/web3-proxy/src/connections.rs b/web3-proxy/src/connections.rs index f3c5cbdc..ecb35beb 100644 --- a/web3-proxy/src/connections.rs +++ b/web3-proxy/src/connections.rs @@ -187,42 +187,56 @@ impl Web3Connections { } } - pub fn update_synced_rpcs( - &self, - rpc: &Arc, - new_block: u64, - ) -> anyhow::Result<()> { + pub fn update_synced_rpcs(&self, rpc: &Arc) -> anyhow::Result<()> { let mut synced_connections = self.synced_connections.write(); - let current_block_number = synced_connections.head_block_number; + let current_best_block_number = synced_connections.head_block_number; - let best_head_block = self.head_block_number(); + let new_block = rpc.head_block_number(); - match current_block_number.cmp(&best_head_block) { - cmp::Ordering::Equal => { - // this rpc tier is synced, and it isn't the first to this block - } - cmp::Ordering::Less => {} - cmp::Ordering::Greater => {} - } - - match current_block_number.cmp(&new_block) { - cmp::Ordering::Equal => { - // this rpc is synced, and it isn't the first to this block - } - cmp::Ordering::Less => { - // this is a new head block. clear the current synced connections - // TODO: this is too verbose with a bunch of tiers. include the tier - // info!("new head block from {:?}: {}", rpc, new_block); + let overall_best_head_block = self.head_block_number(); + // TODO: double check this logic + match ( + new_block.cmp(&overall_best_head_block), + new_block.cmp(¤t_best_block_number), + ) { + (cmp::Ordering::Greater, cmp::Ordering::Greater) => { + // this newest block is the new overall best block synced_connections.inner.clear(); synced_connections.head_block_number = new_block; } - cmp::Ordering::Greater => { - // not the latest block. return now + (cmp::Ordering::Equal, cmp::Ordering::Less) => { + // no need to do anything return Ok(()); } + (cmp::Ordering::Greater, cmp::Ordering::Less) => { + // this isn't the best block in the tier. don't do anything + return Ok(()); + } + (cmp::Ordering::Equal, cmp::Ordering::Equal) => { + // this rpc tier is synced, and it isn't the first to this block + } + (cmp::Ordering::Less, cmp::Ordering::Less) => { + // this rpc is behind the best and the tier. don't do anything + return Ok(()); + } + (cmp::Ordering::Less, cmp::Ordering::Equal) => { + panic!("Less+Equal should be impossible") + } + (cmp::Ordering::Less, cmp::Ordering::Greater) => { + panic!("Less+greater should be impossible") + } + (cmp::Ordering::Equal, cmp::Ordering::Greater) => { + // we caught up to another tier + synced_connections.inner.clear(); + + synced_connections.head_block_number = new_block; + } + (cmp::Ordering::Greater, cmp::Ordering::Equal) => { + panic!("Greater+Equal should be impossible") + } } let rpc_index = self