Skip to content

Commit

Permalink
fix up liveness queries
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Oct 8, 2024
1 parent cd5112d commit 5266d30
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
23 changes: 0 additions & 23 deletions crates/proof_of_stake/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,29 +710,6 @@ where
.collect()
}

/// Read all validator addresses from the consensus set
pub fn read_active_validator_addresses<S, Gov>(
storage: &S,
epoch: namada_core::chain::Epoch,
) -> Result<HashSet<Address>>
where
S: StorageRead,
Gov: governance::Read<S>,
{
let params = read_pos_params::<S, Gov>(storage)?;
Ok(validator_addresses_handle()
.at(&epoch)
.iter(storage)?
.map(Result::unwrap)
.filter(|address| {
matches!(
validator_state_handle(address).get(storage, epoch, &params),
Ok(Some(ValidatorState::Consensus))
)
})
.collect())
}

/// Update PoS total deltas.
/// Note: for EpochedDelta, write the value to change storage by
pub fn update_total_deltas<S, Gov>(
Expand Down
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)]
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
58 changes: 32 additions & 26 deletions crates/sdk/src/queries/vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};
Expand All @@ -52,7 +54,7 @@ router! {POS,
( "addresses" / [epoch: opt Epoch] )
-> HashSet<Address> = validator_addresses,

( "livenesses" ) -> Vec<(Address, String, u64)> = validator_livenesses,
( "livenesses" ) -> LivenessInfo = validator_livenesses,

( "stake" / [validator: Address] / [epoch: opt Epoch] )
-> Option<token::Amount> = validator_stake,
Expand Down Expand Up @@ -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<D, H, V, T>(
ctx: RequestCtx<'_, D, H, V, T>,
) -> namada_storage::Result<Vec<(Address, String, u64)>>
) -> 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![];
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
Expand Down Expand Up @@ -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")
)
}
}
6 changes: 3 additions & 3 deletions 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,10 +754,10 @@ pub async fn get_all_validators<C: namada_io::Client + Sync>(
)
}

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

Expand Down

0 comments on commit 5266d30

Please sign in to comment.