Skip to content

Commit

Permalink
reduced number of locks for HbbftState by holding locks longer and pa…
Browse files Browse the repository at this point in the history
…ssing it throught to join_hbbft_epoch.
  • Loading branch information
SurfingNerd committed Mar 9, 2023
1 parent d455b21 commit deb449e
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions crates/ethcore/src/engines/hbbft/hbbft_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
Expand All @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit deb449e

Please sign in to comment.