-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validator health status #3207
Open
viquezclaudio
wants to merge
3
commits into
albatross
Choose a base branch
from
viquezcl/validator
base: albatross
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+416
−60
Open
Validator health status #3207
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use std::cmp; | ||
|
||
use nimiq_keys::Address; | ||
|
||
/// Enum that represents the overall health of a validator | ||
/// - Green means the Validator is working as expected. | ||
/// - Yellow means there has been more than 'VALIDATOR_YELLOW_HEALTH_INACTIVATIONS' consecutive inactivations | ||
/// in the current epoch. | ||
/// - Red means there has been more than 'VALIDATOR_RED_HEALTH_INACTIVATIONS' consecutive inactivations | ||
/// in the current epoch. | ||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum ValidatorHealthState { | ||
Green, | ||
Yellow, | ||
Red, | ||
} | ||
|
||
/// Struct that represents the overall Validator Health | ||
pub struct ValidatorHealth { | ||
/// The current validator health | ||
health: ValidatorHealthState, | ||
/// Validator address | ||
address: Address, | ||
/// Only used for testing purposes controls whether blocks are published by the validator | ||
publish: bool, | ||
/// Number of consecutive inactivations that have occurred in the current epoch | ||
inactivations: u32, | ||
/// Next block number indicating when the re-activate transaction should be sent | ||
reactivate_block_number: u32, | ||
/// Flag that indicates an ongoing reactivation | ||
pending_reactivate: bool, | ||
} | ||
|
||
impl ValidatorHealth { | ||
/// The number of consecutive inactivations from which a validator is considered with yellow health | ||
const VALIDATOR_YELLOW_HEALTH_INACTIVATIONS: u32 = 2; | ||
/// The number of consecutive inactivations from which a validator is considered with red health | ||
const VALIDATOR_RED_HEALTH_INACTIVATIONS: u32 = 4; | ||
// The maximum number of blocks the reactivate transaction can be delayed | ||
const MAX_REACTIVATE_DELAY: u32 = 10_000; | ||
|
||
/// Creates a new instance of the validator health structure | ||
pub fn new(validator_address: &Address) -> ValidatorHealth { | ||
ValidatorHealth { | ||
health: ValidatorHealthState::Green, | ||
publish: true, | ||
inactivations: 0, | ||
reactivate_block_number: 0, | ||
pending_reactivate: false, | ||
address: validator_address.clone(), | ||
} | ||
} | ||
|
||
/// Computes the associated delay based on the current number of inactivations | ||
fn get_reactivate_delay(&self) -> u32 { | ||
cmp::min(self.inactivations.pow(2), Self::MAX_REACTIVATE_DELAY) | ||
} | ||
|
||
/// Returns the current health state of the validator | ||
pub fn health(&self) -> ValidatorHealthState { | ||
self.health | ||
} | ||
|
||
/// This flag should only be set in testing environments. | ||
pub fn _set_publish_flag(&mut self, publish: bool) { | ||
self.publish = publish | ||
} | ||
|
||
/// Decides if blocks should be published by the validator (only used for testing) | ||
pub fn publish_block(&self) -> bool { | ||
self.publish | ||
} | ||
|
||
/// Recomputes the current validator health state | ||
pub fn refresh_validator_health_status(&mut self) { | ||
log::debug!( | ||
address=%self.address, | ||
self.inactivations, | ||
"Current validator Inactivations counter" | ||
); | ||
|
||
match self.health { | ||
ValidatorHealthState::Green => {} | ||
ValidatorHealthState::Yellow => { | ||
if self.inactivations < Self::VALIDATOR_YELLOW_HEALTH_INACTIVATIONS { | ||
log::info!(self.inactivations, "Changed validator health to green"); | ||
self.health = ValidatorHealthState::Green; | ||
} | ||
} | ||
ValidatorHealthState::Red => { | ||
if self.inactivations < Self::VALIDATOR_RED_HEALTH_INACTIVATIONS { | ||
log::info!(self.inactivations, "Changed validator health to yellow"); | ||
self.health = ValidatorHealthState::Yellow; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Decreases the number of consecutive deactivations | ||
pub fn block_produced(&mut self) { | ||
self.inactivations = self.inactivations.saturating_sub(1); | ||
} | ||
|
||
/// Increases the number of consecutive deactivations | ||
pub fn inactivate(&mut self, block_number: u32) { | ||
if !self.pending_reactivate { | ||
self.inactivations = self.inactivations.saturating_add(1); | ||
self.reactivate_block_number = block_number + self.get_reactivate_delay(); | ||
self.pending_reactivate = true; | ||
|
||
match self.health { | ||
ValidatorHealthState::Green => { | ||
if self.inactivations >= Self::VALIDATOR_YELLOW_HEALTH_INACTIVATIONS { | ||
log::warn!(self.inactivations, "Changed validator health to yellow"); | ||
self.health = ValidatorHealthState::Yellow; | ||
} | ||
} | ||
ValidatorHealthState::Yellow => { | ||
if self.inactivations >= Self::VALIDATOR_RED_HEALTH_INACTIVATIONS { | ||
log::warn!(self.inactivations, "Changed validator health to red"); | ||
self.health = ValidatorHealthState::Red; | ||
} | ||
} | ||
ValidatorHealthState::Red => { | ||
log::warn!("Validator health is still red") | ||
} | ||
} | ||
|
||
log::debug!( | ||
address=%self.address, | ||
self.inactivations, | ||
self.reactivate_block_number, | ||
block_number, | ||
"New inactivation, current status", | ||
); | ||
} | ||
} | ||
|
||
/// Resets the internal counters and validator health state for a new epoch | ||
pub fn reset_epoch(&mut self) { | ||
// Reset the inactivations counter | ||
self.inactivations = 0; | ||
// Reset the validator health every epoch | ||
self.health = ValidatorHealthState::Green; | ||
} | ||
|
||
/// Resets the pending reactivation flag | ||
pub fn reset_reactivation(&mut self) { | ||
self.pending_reactivate = false; | ||
} | ||
|
||
/// Returns the block number for the next re-activate transaction | ||
pub fn get_reactivate_block_number(&self) -> u32 { | ||
self.reactivate_block_number | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
extern crate log; | ||
|
||
pub mod aggregation; | ||
pub mod health; | ||
mod jail; | ||
pub mod key_utils; | ||
mod r#macro; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method can be annotated with
#[cfg(test)]
such that it's only included incargo test
and not incargo build
.