Skip to content

Commit

Permalink
cache for flagged validators and respecting of that cache in the deci…
Browse files Browse the repository at this point in the history
…de function.
  • Loading branch information
SurfingNerd committed Nov 23, 2023
1 parent 0c11c4c commit ac0946c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
40 changes: 36 additions & 4 deletions crates/ethcore/src/engines/hbbft/hbbft_early_epoch_end_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub(crate) struct HbbftEarlyEpochEndManager {
/// public keys of all validators for this epoch.
validators: Vec<NodeId>,

/// current flagged validators
/// current flagged validators, unordered list - no performance issue, since this can
/// only grow up to 7 elements for a usual set of 25 nodes.
flagged_validators: Vec<NodeId>,
}

Expand Down Expand Up @@ -70,7 +71,9 @@ impl HbbftEarlyEpochEndManager {
}

/// retrieves the information from smart contracts which validators are currently flagged.
fn get_current_flagged_validators_from_contracts() -> Vec<NodeId> {
fn get_current_flagged_validators_from_contracts(
full_client: &dyn BlockChainClient,
) -> Vec<NodeId> {
// todo: call smart contract.
return Vec::new();
}
Expand All @@ -93,7 +96,21 @@ impl HbbftEarlyEpochEndManager {

// todo: send transaction to smart contract about missing validator.

warn!(target: "engine", "early-epoch-end: notify about missing validator: {:?}", validator);
self.flagged_validators.push(validator.clone());
warn!(target: "engine", "TODO: early-epoch-end: notify about missing validator: {:?}", validator);
}

fn notify_about_validator_reconnect(
&mut self,
validator: &NodeId,
full_client: &dyn BlockChainClient,
) {
if let Some(index) = self.flagged_validators.iter().position(|x| x == validator) {
self.flagged_validators.remove(index);
warn!(target: "engine", "TODO: early-epoch-end: notify about reconnected validator: {:?}", validator);
} else {
error!(target: "engine", " Could not find reconnected validator in flagged validators.");
}
}

/// decides on the memorium data if we should update to contract data.
Expand Down Expand Up @@ -122,6 +139,9 @@ impl HbbftEarlyEpochEndManager {
return;
}

let current_flagged_validators =
Self::get_current_flagged_validators_from_contracts(full_client);

//full_client.best_block_header()
// get current state of missing validators from hbbftMemorium.
if let Some(epoch_history) = memorium.get_staking_epoch_history(block_num) {
Expand All @@ -130,7 +150,19 @@ impl HbbftEarlyEpochEndManager {
let last_sealing_message = node_history.get_sealing_message();

if last_sealing_message < block_num - treshold {
self.notify_about_missing_validator(&validator, full_client);
// we do not have to send notification, if we already did so.

if !current_flagged_validators.contains(validator) {
// this function will also add the validator to the list of flagged validators.
self.notify_about_missing_validator(&validator, full_client);
}
} else {
// this validator is OK.
// maybe it was flagged and we need to unflag it ?

if current_flagged_validators.contains(validator) {
self.notify_about_validator_reconnect(&validator, full_client);
}
}
}
// todo: if the systems switched from block based measurement to time based measurement.
Expand Down
18 changes: 8 additions & 10 deletions crates/ethcore/src/engines/hbbft/hbbft_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ impl HoneyBadgerBFT {
let epoch_num = 0;
let block_num = 0;

// this is currently the only location where we lock early epoch manager -
// this is currently the only location where we lock early epoch manager -
// so this should never cause a deadlock, and we do not have to try_lock_for
let mut lock_guard = self.early_epoch_manager.lock();

Expand All @@ -1002,16 +1002,14 @@ impl HoneyBadgerBFT {
let epoch_start_block = 0;

// let ealry_epoch_manager
*lock_guard =
HbbftEarlyEpochEndManager::create_early_epoch_end_manager(
allowed_devp2p_warmup_time,
block_chain_client,
epoch_num,
epoch_start_block,
);

if let Some(manager) = lock_guard.as_mut() {
*lock_guard = HbbftEarlyEpochEndManager::create_early_epoch_end_manager(
allowed_devp2p_warmup_time,
block_chain_client,
epoch_num,
epoch_start_block,
);

if let Some(manager) = lock_guard.as_mut() {
manager.decide(&memorium, block_num, block_chain_client);
}
}
Expand Down

0 comments on commit ac0946c

Please sign in to comment.