From 609d21fdab460a4d480b8bd941b7569330f9e02a Mon Sep 17 00:00:00 2001 From: renancloudwalk <53792026+renancloudwalk@users.noreply.github.com> Date: Sat, 22 Jun 2024 14:31:03 -0300 Subject: [PATCH] Fix consensus initialization (#1203) * fix: last arrived block initialization * lint --- src/eth/consensus/mod.rs | 15 ++++++++++----- src/eth/consensus/server.rs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/eth/consensus/mod.rs b/src/eth/consensus/mod.rs index 0389bfb0d..ebd4473a1 100644 --- a/src/eth/consensus/mod.rs +++ b/src/eth/consensus/mod.rs @@ -181,7 +181,7 @@ impl Consensus { rx_blocks: broadcast::Receiver, ) -> Arc { let (broadcast_sender, _) = broadcast::channel(32); //TODO rename to internal_peer_broadcast_sender - let last_arrived_block_number = AtomicU64::new(std::u64::MAX); //we use the max value to ensure that only after receiving the first appendEntry we can start the consensus + let last_arrived_block_number = AtomicU64::new(0); //we use the max value to ensure that only after receiving the first appendEntry we can start the consensus let peers = Arc::new(RwLock::new(HashMap::new())); let my_address = Self::discover_my_address(jsonrpc_address.port(), grpc_address.port()); @@ -354,8 +354,6 @@ impl Consensus { } self.set_role(Role::Leader); - - self.last_arrived_block_number.store(std::u64::MAX, Ordering::SeqCst); //as leader, we don't have a last block number } fn initialize_periodic_peer_discovery(consensus: Arc) { @@ -520,7 +518,7 @@ impl Consensus { let last_arrived_block_number = self.last_arrived_block_number.load(Ordering::SeqCst); - if last_arrived_block_number == std::u64::MAX { + if last_arrived_block_number == 0 { tracing::warn!("no appendEntry has been received yet"); return false; } @@ -533,7 +531,14 @@ impl Consensus { storage_block_number ); - (last_arrived_block_number - 2) <= storage_block_number + if (last_arrived_block_number - 3) <= storage_block_number { + tracing::info!("should serve request"); + true + } else { + let diff = (last_arrived_block_number as i128) - (storage_block_number as i128); + tracing::warn!(diff = diff, "should not serve request"); + false + } } #[cfg(feature = "kubernetes")] diff --git a/src/eth/consensus/server.rs b/src/eth/consensus/server.rs index 43c8dbb5d..0e1f3861b 100644 --- a/src/eth/consensus/server.rs +++ b/src/eth/consensus/server.rs @@ -321,6 +321,6 @@ mod tests { let response = response.unwrap().into_inner(); assert_eq!(response.term, 1); - //XXX assert_eq!(response.vote_granted, true); + assert!(response.vote_granted); } }