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

draft: latest epoch info hack #11943

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions chain/epoch-manager/src/validator_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ fn select_validators(
min_stake_ratio: Ratio<u128>,
protocol_version: ProtocolVersion,
) -> (Vec<ValidatorStake>, BinaryHeap<OrderedValidatorStake>, Balance) {
println!("RATIO: {} SELECTING: {}", min_stake_ratio, max_number_selected);
let mut total_stake = 0;
let n = cmp::min(max_number_selected, proposals.len());
let mut validators = Vec::with_capacity(n);
Expand All @@ -412,6 +413,7 @@ fn select_validators(
// all slots were filled, so the threshold stake is 1 more than the current
// smallest stake
let threshold = validators.last().unwrap().stake() + 1;
println!("MAX {}", threshold);
(validators, proposals, threshold)
} else {
// the stake ratio condition prevented all slots from being filled,
Expand All @@ -429,6 +431,7 @@ fn select_validators(
} else {
(min_stake_ratio * Ratio::new(total_stake, 1)).ceil().to_integer()
};
println!("CUT {}", threshold);
(validators, proposals, threshold)
}
}
Expand Down
6 changes: 4 additions & 2 deletions tools/state-viewer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use near_primitives::hash::CryptoHash;
use near_primitives::sharding::ChunkHash;
use near_primitives::trie_key::col;
use near_primitives::types::{BlockHeight, ShardId, StateRoot};
use near_primitives_core::types::EpochHeight;
use near_primitives_core::types::{EpochHeight, ProtocolVersion};
use near_store::{Mode, NodeStorage, Store, Temperature};
use nearcore::{load_config, NearConfig};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -567,6 +567,8 @@ pub struct EpochAnalysisCmd {
/// Epoch analysis mode.
#[clap(subcommand)]
mode: EpochAnalysisMode,
#[clap(long)]
force_pv: ProtocolVersion,
}

#[derive(clap::Subcommand)]
Expand All @@ -585,7 +587,7 @@ pub enum EpochAnalysisMode {

impl EpochAnalysisCmd {
pub fn run(self, near_config: NearConfig, store: Store) {
print_epoch_analysis(self.start_height, self.mode, near_config, store);
print_epoch_analysis(self.start_height, self.mode, near_config, store, self.force_pv);
}
}

Expand Down
45 changes: 28 additions & 17 deletions tools/state-viewer/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use near_primitives::trie_key::col::COLUMNS_WITH_ACCOUNT_ID_IN_KEY;
use near_primitives::trie_key::TrieKey;
use near_primitives::types::{BlockHeight, EpochId, ShardId};
use near_primitives::version::PROTOCOL_VERSION;
use near_primitives_core::types::{Balance, EpochHeight};
use near_primitives_core::types::{Balance, EpochHeight, ProtocolVersion};
use near_store::flat::FlatStorageChunkView;
use near_store::flat::FlatStorageManager;
use near_store::test_utils::create_test_store;
Expand Down Expand Up @@ -936,6 +936,7 @@ pub(crate) fn print_epoch_analysis(
mode: EpochAnalysisMode,
near_config: NearConfig,
store: Store,
force_protocol_version: ProtocolVersion,
) {
let epoch_manager =
EpochManager::new_from_genesis_config(store.clone(), &near_config.genesis.config)
Expand Down Expand Up @@ -979,7 +980,7 @@ pub(crate) fn print_epoch_analysis(
let mut next_epoch_info =
epoch_heights_to_infos.get(&min_epoch_height.saturating_add(1)).unwrap().as_ref().clone();
let mut next_next_epoch_config =
epoch_manager.get_config_for_protocol_version(PROTOCOL_VERSION).unwrap();
epoch_manager.get_config_for_protocol_version(force_protocol_version).unwrap();
let mut has_same_shard_layout;
let mut epoch_protocol_version;
let mut next_next_protocol_version;
Expand All @@ -1001,7 +1002,7 @@ pub(crate) fn print_epoch_analysis(
// chain/epoch-manager/src/validator_selection.rs:227:13.
// Probably has something to do with extreme case where all
// proposals are selected.
next_next_epoch_config.validator_selection_config.num_chunk_validator_seats = 100;
// next_next_epoch_config.validator_selection_config.num_chunk_validator_seats = 300;
}
}

Expand All @@ -1012,30 +1013,32 @@ pub(crate) fn print_epoch_analysis(
epoch_heights_to_infos.range(min_epoch_height..=max_epoch_height)
{
let next_epoch_height = epoch_height.saturating_add(1);
// let next_next_epoch_height = epoch_height.saturating_add(1);
let next_next_epoch_height = epoch_height.saturating_add(2);
let next_epoch_id = epoch_heights_to_ids.get(&next_epoch_height).unwrap();
let next_next_epoch_id = epoch_heights_to_ids.get(&next_next_epoch_height).unwrap();
let epoch_summary = epoch_heights_to_validator_infos.get(epoch_height).unwrap();
let next_epoch_config = epoch_manager.get_epoch_config(next_epoch_id).unwrap();
let original_next_next_protocol_version = epoch_summary.next_next_epoch_version;
let original_next_next_protocol_version = force_protocol_version;

match mode {
EpochAnalysisMode::CheckConsistency => {
// Retrieve remaining parameters from the stored information
// about epochs.
next_epoch_info =
epoch_heights_to_infos.get(&next_epoch_height).unwrap().as_ref().clone();
next_next_epoch_config =
epoch_manager.get_epoch_config(next_next_epoch_id).unwrap();
has_same_shard_layout =
next_epoch_config.shard_layout == next_next_epoch_config.shard_layout;
// next_next_epoch_config =
// epoch_manager.get_epoch_config(next_next_epoch_id).unwrap();
has_same_shard_layout = true;
// has_same_shard_layout =
// next_epoch_config.shard_layout == next_next_epoch_config.shard_layout;
epoch_protocol_version = epoch_info.protocol_version();
next_next_protocol_version = original_next_next_protocol_version;
}
EpochAnalysisMode::Backtest => {
has_same_shard_layout = true;
epoch_protocol_version = PROTOCOL_VERSION;
next_next_protocol_version = PROTOCOL_VERSION;
epoch_protocol_version = force_protocol_version;
next_next_protocol_version = force_protocol_version;
}
};

Expand All @@ -1045,10 +1048,13 @@ pub(crate) fn print_epoch_analysis(
epoch_heights_to_infos.get(&next_next_epoch_height).unwrap();
let rng_seed = stored_next_next_epoch_info.rng_seed();

println!("{}", next_next_epoch_config.validator_selection_config.minimum_stake_ratio);
let next_next_epoch_info = near_epoch_manager::proposals_to_epoch_info(
&next_next_epoch_config,
rng_seed,
&next_epoch_info,
// vec![],
// HashMap::default(),
epoch_summary.all_proposals.clone(),
epoch_summary.validator_kickout.clone(),
stored_next_next_epoch_info.validator_reward().clone(),
Expand All @@ -1058,7 +1064,10 @@ pub(crate) fn print_epoch_analysis(
has_same_shard_layout,
)
.unwrap();


const ONE_NEAR: u128 = 1_000_000_000_000_000_000_000_000;
let seat_price_near = next_next_epoch_info.seat_price() / ONE_NEAR;

// Compute difference between chunk producer assignments.
let next_assignment = next_epoch_info.chunk_producers_settlement();
let next_next_assignment = next_next_epoch_info.chunk_producers_settlement();
Expand Down Expand Up @@ -1110,12 +1119,14 @@ pub(crate) fn print_epoch_analysis(
EpochAnalysisMode::Backtest => {
// Print csv-style stats on screen.
println!(
"{next_next_epoch_height},{original_next_next_protocol_version},{state_syncs},{},{},{},{},{}",
validator_num.values().min().unwrap(),
validator_num.values().max().unwrap() - validator_num.values().min().unwrap(),
min_stake,
max_stake - min_stake,
((max_stake - min_stake) as f64) / (*max_stake as f64)
"{},{original_next_next_protocol_version},{state_syncs},{}",
next_epoch_height + 1,
seat_price_near,
// validator_num.values().min().unwrap(),
// validator_num.values().max().unwrap() - validator_num.values().min().unwrap(),
// min_stake,
// max_stake - min_stake,
// ((max_stake - min_stake) as f64) / (*max_stake as f64)
);
// Use the generated epoch info for the next iteration.
next_epoch_info = next_next_epoch_info;
Expand Down
Loading