From b9f4a9437d5369e7b1b2204e555b1164270012c1 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 3 May 2022 05:20:11 +0000 Subject: [PATCH] fetch blocks once --- src/connection.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 848028f3..f9e069aa 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -166,21 +166,34 @@ impl Web3Connection { // TODO: it would be faster to get the block number, but subscriptions don't provide that // TODO: maybe we can do provider.subscribe("newHeads") and then parse into a custom struct that only gets the number out? let mut stream = provider.subscribe_blocks().await?; + + // query the block once since the subscription doesn't send the current block + // there is a very small race condition here where the stream could send us a new block right now + // all it does is print "new block" for the same block as current block + let block_number = provider.get_block_number().await.map(|x| x.as_u64())?; + + info!("current block on {}: {}", self, block_number); + + self.head_block_number + .store(block_number, atomic::Ordering::Release); + + if let Some(connections) = &connections { + connections.update_synced_rpcs(&self, block_number)?; + } + while let Some(block) = stream.next().await { let block_number = block.number.unwrap().as_u64(); // TODO: only store if this isn't already stored? // TODO: also send something to the provider_tier so it can sort? - let old_block_number = self - .head_block_number - .swap(block_number, atomic::Ordering::AcqRel); + // TODO: do we need this old block number check? its helpful on http, but here it shouldn't dupe except maybe on the first run + self.head_block_number + .store(block_number, atomic::Ordering::Release); - if old_block_number != block_number { - info!("new block on {}: {}", self, block_number); + info!("new block on {}: {}", self, block_number); - if let Some(connections) = &connections { - connections.update_synced_rpcs(&self, block_number)?; - } + if let Some(connections) = &connections { + connections.update_synced_rpcs(&self, block_number)?; } } }