From deb449e51fb871524db2d87c441d080fded3cf49 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 9 Mar 2023 22:47:32 +0100 Subject: [PATCH] reduced number of locks for HbbftState by holding locks longer and passing it throught to join_hbbft_epoch. --- .../ethcore/src/engines/hbbft/hbbft_engine.rs | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/ethcore/src/engines/hbbft/hbbft_engine.rs b/crates/ethcore/src/engines/hbbft/hbbft_engine.rs index dd69543f8..12332d2c9 100644 --- a/crates/ethcore/src/engines/hbbft/hbbft_engine.rs +++ b/crates/ethcore/src/engines/hbbft/hbbft_engine.rs @@ -33,7 +33,7 @@ use std::{ collections::BTreeMap, convert::TryFrom, net::SocketAddr, - ops::BitXor, + ops::{BitXor, DerefMut}, sync::{atomic::AtomicBool, Arc, Mutex, Weak}, time::Duration, }; @@ -520,8 +520,8 @@ impl HoneyBadgerBFT { self.hbbft_message_dispatcher.on_message_received(&message); let message_block = message.epoch(); - - match self.hbbft_state.write().process_message( + let mut hbbft_state_lock = self.hbbft_state.write(); + match hbbft_state_lock.process_message( client.clone(), &self.signer, sender_id, @@ -543,7 +543,7 @@ impl HoneyBadgerBFT { } } self.process_step(client, step, &network_info); - self.join_hbbft_epoch()?; + self.join_hbbft_epoch(hbbft_state_lock.deref_mut())?; } Ok(None) => {} Err(err) => { @@ -699,15 +699,15 @@ impl HoneyBadgerBFT { /// Conditionally joins the current hbbft epoch if the number of received /// contributions exceeds the maximum number of tolerated faulty nodes. - fn join_hbbft_epoch(&self) -> Result<(), EngineError> { + fn join_hbbft_epoch(&self, hbbft_state: &mut HbbftState) -> Result<(), EngineError> { let client = self.client_arc().ok_or(EngineError::RequiresClient)?; if self.is_syncing(&client) { trace!(target: "consensus", "tried to join HBBFT Epoch, but still syncing."); return Ok(()); } - let step = self - .hbbft_state - .write() + + + let step = hbbft_state .contribute_if_contribution_threshold_reached(client.clone(), &self.signer); if let Some((step, network_info)) = step { self.process_step(client, step, &network_info) @@ -773,10 +773,8 @@ impl HoneyBadgerBFT { fn replay_cached_messages(&self) -> Option<()> { let client = self.client_arc()?; - let steps = self - .hbbft_state - .write() - .replay_cached_messages(client.clone()); + let mut hbbft_state_write_lock = self.hbbft_state.write(); + let steps = hbbft_state_write_lock.replay_cached_messages(client.clone()); let mut processed_step = false; if let Some((steps, network_info)) = steps { for step in steps { @@ -792,7 +790,7 @@ impl HoneyBadgerBFT { } if processed_step { - if let Err(e) = self.join_hbbft_epoch() { + if let Err(e) = self.join_hbbft_epoch(hbbft_state_write_lock.deref_mut()) { error!(target: "engine", "Error trying to join epoch: {}", e); } }