Skip to content
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

feat: Add new rpc query function to check validators' liveness(uptime) #3899

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- SDK query to get liveness information for the network and consensus validator
set. ([\#3899](https://github.com/anoma/namada/pull/3899))
22 changes: 22 additions & 0 deletions crates/proof_of_stake/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,28 @@ pub struct Redelegation {
/// Redelegation amount
pub amount: token::Amount,
}

/// Some liveness data for a consensus validator
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, BorshDeserializer)]
brentstone marked this conversation as resolved.
Show resolved Hide resolved
pub struct ValidatorLiveness {
/// Validator address
pub native_address: Address,
/// CometBFT address
pub comet_address: String,
/// Validator missed votes
pub missed_votes: u64,
}

/// Liveness data related to the network and set of consensus validators
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, BorshDeserializer)]
pub struct LivenessInfo {
/// Length of liveness window
pub liveness_window_len: u64,
/// Liveness threshold
pub liveness_threshold: Dec,
/// Validators' liveness info
pub validators: Vec<ValidatorLiveness>,
}
// --------------------------------------------------------------------------------------------

/// A genesis validator definition.
Expand Down
49 changes: 46 additions & 3 deletions crates/sdk/src/queries/vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use namada_core::address::Address;
use namada_core::arith::{self, checked};
use namada_core::chain::Epoch;
use namada_core::collections::{HashMap, HashSet};
use namada_core::key::common;
use namada_core::key::{common, tm_consensus_key_raw_hash};
use namada_core::token;
use namada_proof_of_stake::parameters::PosParams;
use namada_proof_of_stake::queries::{
Expand All @@ -17,8 +17,10 @@ use namada_proof_of_stake::slashing::{
find_all_enqueued_slashes, find_all_slashes,
};
use namada_proof_of_stake::storage::{
bond_handle, read_all_validator_addresses,
bond_handle, get_consensus_key, liveness_sum_missed_votes_handle,
read_all_validator_addresses,
read_below_capacity_validator_set_addresses_with_stake,
read_consensus_validator_set_addresses,
read_consensus_validator_set_addresses_with_stake, read_pos_params,
read_total_active_stake, read_total_stake, read_validator_avatar,
read_validator_description, read_validator_discord_handle,
Expand All @@ -31,7 +33,8 @@ use namada_proof_of_stake::storage::{
pub use namada_proof_of_stake::types::ValidatorStateInfo;
use namada_proof_of_stake::types::{
BondId, BondsAndUnbondsDetail, BondsAndUnbondsDetails, CommissionPair,
Slash, ValidatorMetaData, WeightedValidator,
LivenessInfo, Slash, ValidatorLiveness, ValidatorMetaData,
WeightedValidator,
};
use namada_proof_of_stake::{bond_amount, query_reward_tokens};
use namada_state::{DBIter, KeySeg, StorageHasher, DB};
Expand All @@ -51,6 +54,8 @@ router! {POS,
( "addresses" / [epoch: opt Epoch] )
-> HashSet<Address> = validator_addresses,

( "liveness_info" ) -> LivenessInfo = liveness_info,

( "stake" / [validator: Address] / [epoch: opt Epoch] )
-> Option<token::Amount> = validator_stake,

Expand Down Expand Up @@ -249,6 +254,44 @@ where
read_all_validator_addresses(ctx.state, epoch)
}

/// Get liveness information for all consensus validators in the current epoch.
fn liveness_info<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
) -> namada_storage::Result<LivenessInfo>
where
D: 'static + DB + for<'iter> DBIter<'iter> + Sync,
H: 'static + StorageHasher + Sync,
{
let epoch = ctx.state.in_mem().last_epoch;
let consensus_validators =
read_consensus_validator_set_addresses(ctx.state, epoch)?;
let params = read_pos_params::<_, governance::Store<_>>(ctx.state)?;

let mut result = vec![];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more small thing, now that the size of this vec is same as consensus_validators.len() we can allocate it to its size

tzemanovic marked this conversation as resolved.
Show resolved Hide resolved
for validator in consensus_validators {
if let Some(pubkey) = get_consensus_key::<_, governance::Store<_>>(
ctx.state, &validator, epoch,
)? {
let comet_address = tm_consensus_key_raw_hash(&pubkey);
let sum_liveness_handle = liveness_sum_missed_votes_handle();
let missed_votes = sum_liveness_handle
.get(ctx.state, &validator)?
.unwrap_or_default();
result.push(ValidatorLiveness {
native_address: validator,
comet_address,
missed_votes,
})
};
}

Ok(LivenessInfo {
liveness_window_len: params.liveness_window_check,
liveness_threshold: params.liveness_threshold,
validators: result,
})
}

/// Get the validator commission rate and max commission rate change per epoch
fn validator_commission<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
Expand Down
9 changes: 8 additions & 1 deletion crates/sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use namada_io::{display_line, edisplay_line, Client, Io};
use namada_parameters::{storage as params_storage, EpochDuration};
use namada_proof_of_stake::parameters::PosParams;
use namada_proof_of_stake::types::{
BondsAndUnbondsDetails, CommissionPair, ValidatorMetaData,
BondsAndUnbondsDetails, CommissionPair, LivenessInfo, ValidatorMetaData,
WeightedValidator,
};
use namada_state::LastBlock;
Expand Down Expand Up @@ -754,6 +754,13 @@ pub async fn get_all_validators<C: namada_io::Client + Sync>(
)
}

/// Get liveness info for all consensus validators in the current epoch
pub async fn get_validators_liveness_info<C: namada_io::Client + Sync>(
client: &C,
) -> Result<LivenessInfo, error::Error> {
convert_response::<C, _>(RPC.vp().pos().liveness_info(client).await)
}

/// Get all consensus validators in the given epoch
pub async fn get_all_consensus_validators<C: namada_io::Client + Sync>(
client: &C,
Expand Down