From 5266d3052b610cf71f863f4f710a82c8be400d7d Mon Sep 17 00:00:00 2001 From: brentstone Date: Mon, 7 Oct 2024 18:36:12 -0700 Subject: [PATCH] fix up liveness queries --- crates/proof_of_stake/src/storage.rs | 23 ---------- crates/proof_of_stake/src/types/mod.rs | 22 ++++++++++ crates/sdk/src/queries/vp/pos.rs | 58 ++++++++++++++------------ crates/sdk/src/rpc.rs | 6 +-- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/crates/proof_of_stake/src/storage.rs b/crates/proof_of_stake/src/storage.rs index 29882c8668..a3d0a61cea 100644 --- a/crates/proof_of_stake/src/storage.rs +++ b/crates/proof_of_stake/src/storage.rs @@ -710,29 +710,6 @@ where .collect() } -/// Read all validator addresses from the consensus set -pub fn read_active_validator_addresses( - storage: &S, - epoch: namada_core::chain::Epoch, -) -> Result> -where - S: StorageRead, - Gov: governance::Read, -{ - let params = read_pos_params::(storage)?; - Ok(validator_addresses_handle() - .at(&epoch) - .iter(storage)? - .map(Result::unwrap) - .filter(|address| { - matches!( - validator_state_handle(address).get(storage, epoch, ¶ms), - Ok(Some(ValidatorState::Consensus)) - ) - }) - .collect()) -} - /// Update PoS total deltas. /// Note: for EpochedDelta, write the value to change storage by pub fn update_total_deltas( diff --git a/crates/proof_of_stake/src/types/mod.rs b/crates/proof_of_stake/src/types/mod.rs index bff68a04bb..988d1a7396 100644 --- a/crates/proof_of_stake/src/types/mod.rs +++ b/crates/proof_of_stake/src/types/mod.rs @@ -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)] +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, +} // -------------------------------------------------------------------------------------------- /// A genesis validator definition. diff --git a/crates/sdk/src/queries/vp/pos.rs b/crates/sdk/src/queries/vp/pos.rs index 24856e4716..1105011169 100644 --- a/crates/sdk/src/queries/vp/pos.rs +++ b/crates/sdk/src/queries/vp/pos.rs @@ -18,8 +18,9 @@ use namada_proof_of_stake::slashing::{ }; use namada_proof_of_stake::storage::{ bond_handle, get_consensus_key, liveness_sum_missed_votes_handle, - read_active_validator_addresses, read_all_validator_addresses, + 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, @@ -32,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}; @@ -52,7 +54,7 @@ router! {POS, ( "addresses" / [epoch: opt Epoch] ) -> HashSet
= validator_addresses, - ( "livenesses" ) -> Vec<(Address, String, u64)> = validator_livenesses, + ( "livenesses" ) -> LivenessInfo = validator_livenesses, ( "stake" / [validator: Address] / [epoch: opt Epoch] ) -> Option = validator_stake, @@ -252,41 +254,43 @@ where read_all_validator_addresses(ctx.state, epoch) } -/// Get all the validator livenesses. +/// Get liveness information for all consensus validators in the current epoch. fn validator_livenesses( ctx: RequestCtx<'_, D, H, V, T>, -) -> namada_storage::Result> +) -> namada_storage::Result 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![]; - let active_validator_set = read_active_validator_addresses::< - _, - governance::Store<_>, - >(ctx.state, epoch)?; - for validator_address in active_validator_set.iter() { + for validator in consensus_validators.iter() { if let Some(pubkey) = get_consensus_key::<_, governance::Store<_>>( - ctx.state, - validator_address, - epoch, + ctx.state, validator, epoch, )? { - let tendermint_address = tm_consensus_key_raw_hash(&pubkey); + let comet_address = tm_consensus_key_raw_hash(&pubkey); let sum_liveness_handle = liveness_sum_missed_votes_handle(); - if let Some(missed_counter) = - sum_liveness_handle.get(ctx.state, validator_address)? + if let Some(missed_votes) = + sum_liveness_handle.get(ctx.state, validator)? { - result.push(( - validator_address.to_owned(), - tendermint_address, - missed_counter, - )) + result.push(ValidatorLiveness { + native_address: validator.to_owned(), + comet_address, + missed_votes, + }) } }; } - Ok(result) + 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 @@ -879,9 +883,11 @@ mod test { }; let result = POS.handle(ctx, &request); assert!(result.is_err()); - assert!(result - .unwrap_err() - .to_string() - .contains("Invalid Tendermint address")) + assert!( + result + .unwrap_err() + .to_string() + .contains("Invalid Tendermint address") + ) } } diff --git a/crates/sdk/src/rpc.rs b/crates/sdk/src/rpc.rs index bbca3e168c..67894495e5 100644 --- a/crates/sdk/src/rpc.rs +++ b/crates/sdk/src/rpc.rs @@ -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; @@ -754,10 +754,10 @@ pub async fn get_all_validators( ) } -/// Get validators livenesses in the given epoch +/// Get liveness info for all consensus validators in the current epoch pub async fn get_validators_livenesses( client: &C, -) -> Result, error::Error> { +) -> Result { convert_response::(RPC.vp().pos().validator_livenesses(client).await) }