Skip to content

Commit

Permalink
Fix consensus initialization (#1203)
Browse files Browse the repository at this point in the history
* fix: last arrived block initialization

* lint
  • Loading branch information
renancloudwalk authored Jun 22, 2024
1 parent 4f8d1bd commit 609d21f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/eth/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Consensus {
rx_blocks: broadcast::Receiver<Block>,
) -> Arc<Self> {
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());

Expand Down Expand Up @@ -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<Consensus>) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion src/eth/consensus/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 609d21f

Please sign in to comment.