Skip to content

Commit

Permalink
fix: display correct number of validators in stats logging (#11671)
Browse files Browse the repository at this point in the history
The number of validators displayed in the `INFO stats` logging currently
does not include chunk validators, which could be misleading post
stateless validation launch. This fix works for both before stateless
validation and post stateless validation. I verified a fix on a
statelessnet node.

---------

Co-authored-by: Longarithm <[email protected]>
  • Loading branch information
bowenwang1996 and Longarithm committed Jun 28, 2024
1 parent 32397b9 commit fe6a736
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
7 changes: 7 additions & 0 deletions chain/chain/src/test_utils/kv_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,13 @@ impl EpochManagerAdapter for MockEpochManager {

#[cfg(feature = "new_epoch_sync")]
fn force_update_aggregator(&self, _epoch_id: &EpochId, _hash: &CryptoHash) {}

fn get_epoch_all_validators(
&self,
_epoch_id: &EpochId,
) -> Result<Vec<ValidatorStake>, EpochError> {
Ok(self.validators.iter().map(|(_, v)| v.clone()).collect())
}
}

impl RuntimeAdapter for KeyValueRuntime {
Expand Down
34 changes: 8 additions & 26 deletions chain/client/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use near_client_primitives::types::StateSyncStatus;
use near_epoch_manager::EpochManagerAdapter;
use near_network::types::NetworkInfo;
use near_primitives::block::Tip;
use near_primitives::hash::CryptoHash;
use near_primitives::network::PeerId;
use near_primitives::telemetry::{
TelemetryAgentInfo, TelemetryChainInfo, TelemetryInfo, TelemetrySystemInfo,
Expand All @@ -27,7 +26,7 @@ use near_primitives::views::{
};
use near_telemetry::TelemetryEvent;
use std::cmp::min;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::fmt::Write;
use std::sync::Arc;
use sysinfo::{get_current_pid, set_open_files_limit, Pid, ProcessExt, System, SystemExt};
Expand Down Expand Up @@ -288,25 +287,10 @@ impl InfoHelper {
&mut self,
epoch_manager: &dyn EpochManagerAdapter,
epoch_id: &EpochId,
last_block_hash: &CryptoHash,
) -> usize {
self.num_validators_per_epoch
.get_or_insert(epoch_id.clone(), || {
let block_producers: HashSet<AccountId> = epoch_manager
.get_epoch_block_producers_ordered(epoch_id, last_block_hash)
.unwrap_or(vec![])
.into_iter()
.map(|(validator_stake, _)| validator_stake.account_id().clone())
.collect();
let chunk_producers: HashSet<AccountId> = epoch_manager
.get_epoch_chunk_producers(epoch_id)
.unwrap_or(vec![])
.into_iter()
.map(|validator_stake| validator_stake.account_id().clone())
.collect();
block_producers.union(&chunk_producers).count()
})
.map_or(0, |num_validators| *num_validators)
*self.num_validators_per_epoch.get_or_insert(epoch_id.clone(), || {
epoch_manager.get_epoch_all_validators(epoch_id).unwrap_or_default().len()
}).unwrap_or(&0)
}

/// Print current summary.
Expand All @@ -321,11 +305,8 @@ impl InfoHelper {
let is_syncing = client.sync_status.is_syncing();
let head = unwrap_or_return!(client.chain.head());
let validator_info = if !is_syncing {
let num_validators = self.get_num_validators(
client.epoch_manager.as_ref(),
&head.epoch_id,
&head.last_block_hash,
);
let num_validators =
self.get_num_validators(client.epoch_manager.as_ref(), &head.epoch_id);
let account_id = signer.as_ref().map(|x| x.validator_id());
let is_validator = if let Some(account_id) = account_id {
match client.epoch_manager.get_validator_by_account_id(
Expand Down Expand Up @@ -923,6 +904,7 @@ mod tests {
use near_epoch_manager::test_utils::*;
use near_epoch_manager::EpochManager;
use near_network::test_utils::peer_id_from_seed;
use near_primitives::hash::CryptoHash;
use near_store::genesis::initialize_genesis_state;

#[test]
Expand Down Expand Up @@ -1058,7 +1040,7 @@ mod tests {
let mut info_helper = InfoHelper::new(Clock::real(), noop().into_sender(), &client_config);
assert_eq!(
num_validators,
info_helper.get_num_validators(&epoch_manager_adapter, &epoch_id, &last_block_hash)
info_helper.get_num_validators(&epoch_manager_adapter, &epoch_id)
);
}
}
15 changes: 15 additions & 0 deletions chain/epoch-manager/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ pub trait EpochManagerAdapter: Send + Sync {
epoch_id: &EpochId,
) -> Result<Vec<ValidatorStake>, EpochError>;

/// Returns all validators for a given epoch.
fn get_epoch_all_validators(
&self,
epoch_id: &EpochId,
) -> Result<Vec<ValidatorStake>, EpochError>;

/// Block producers for given height for the main block. Return EpochError if outside of known boundaries.
fn get_block_producer(
&self,
Expand Down Expand Up @@ -1127,4 +1133,13 @@ impl EpochManagerAdapter for EpochManagerHandle {
let mut epoch_manager = self.write();
epoch_manager.epoch_info_aggregator = EpochInfoAggregator::new(epoch_id.clone(), *hash);
}

/// Returns the set of chunk validators for a given epoch
fn get_epoch_all_validators(
&self,
epoch_id: &EpochId,
) -> Result<Vec<ValidatorStake>, EpochError> {
let epoch_manager = self.read();
Ok(epoch_manager.get_epoch_info(epoch_id)?.validators_iter().collect::<Vec<_>>())
}
}

0 comments on commit fe6a736

Please sign in to comment.