From 29478ad262b1dc37a29cd5a2cc9f1d55f5f91fb8 Mon Sep 17 00:00:00 2001 From: musitdev Date: Sat, 6 Jan 2024 16:59:59 +0100 Subject: [PATCH] change slot leader calculus. Remove rpc call --- stake_vote/src/bootstrap.rs | 43 ++++++++++++++----------------- stake_vote/src/leader_schedule.rs | 16 ++++++++++++ 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/stake_vote/src/bootstrap.rs b/stake_vote/src/bootstrap.rs index cc749dbc..3d73894b 100644 --- a/stake_vote/src/bootstrap.rs +++ b/stake_vote/src/bootstrap.rs @@ -77,12 +77,15 @@ pub fn bootstrap_leaderschedule_from_rpc( kind: ClientErrorKind::Custom("RPC return no leader schedule".to_string()), })?; - let first_epoch_slot = epoch_schedule.get_first_slot_in_epoch(current_epoch.epoch); - let current_schedule_by_slot = get_rpc_slot_leaders( - rpc_url.clone(), - first_epoch_slot, - epoch_schedule.slots_per_epoch, - )?; + //Calculate the slot leaders by from the node schedule because RPC call get_slot_leaders is limited to 5000 slots. + let current_schedule_by_slot = + crate::leader_schedule::calculate_slot_leaders_from_schedule(¤t_schedule_by_node) + .map_err(|err| ClientError { + request: None, + kind: ClientErrorKind::Custom(format!( + "Leader schedule from RPC can't generate slot leaders because:{err}" + )), + })?; //get next epoch rpc schedule let next_epoch = current_epoch.epoch + 1; @@ -94,11 +97,16 @@ pub fn bootstrap_leaderschedule_from_rpc( kind: ClientErrorKind::Custom("RPC return no leader schedule".to_string()), }, )?; - let next_schedule_by_slot = get_rpc_slot_leaders( - rpc_url, - next_first_epoch_slot, - epoch_schedule.slots_per_epoch, - )?; + + //Calculate the slot leaders by from the node schedule because RPC call get_slot_leaders is limited to 5000 slots. + let next_schedule_by_slot = + crate::leader_schedule::calculate_slot_leaders_from_schedule(&next_schedule_by_node) + .map_err(|err| ClientError { + request: None, + kind: ClientErrorKind::Custom(format!( + "Leader schedule from RPC can't generate slot leaders because:{err}" + )), + })?; Ok(CalculatedSchedule { current: Some(LeaderScheduleData { @@ -426,19 +434,6 @@ fn get_rpc_leader_schedule( rpc_client.get_leader_schedule(slot) } -fn get_rpc_slot_leaders( - rpc_url: String, - start_slot: u64, - limit: u64, -) -> Result, ClientError> { - let rpc_client = RpcClient::new_with_timeout_and_commitment( - rpc_url.clone(), - Duration::from_secs(600), - CommitmentConfig::finalized(), - ); - rpc_client.get_slot_leaders(start_slot, limit) -} - // pub struct BootstrapScheduleResult { // schedule: CalculatedSchedule, // vote_stakes: Vec, diff --git a/stake_vote/src/leader_schedule.rs b/stake_vote/src/leader_schedule.rs index 7a674a69..7d217c24 100644 --- a/stake_vote/src/leader_schedule.rs +++ b/stake_vote/src/leader_schedule.rs @@ -13,7 +13,9 @@ use solana_lite_rpc_core::structures::leaderschedule::LeaderScheduleData; use solana_sdk::clock::NUM_CONSECUTIVE_LEADER_SLOTS; use solana_sdk::pubkey::Pubkey; use solana_sdk::stake_history::StakeHistory; +use std::collections::BTreeMap; use std::collections::HashMap; +use std::str::FromStr; use std::sync::Arc; use tokio::task::JoinHandle; @@ -307,6 +309,20 @@ pub fn calculate_leader_schedule( LeaderSchedule::new(&stakes, seed, slots_in_epoch, NUM_CONSECUTIVE_LEADER_SLOTS) } +pub fn calculate_slot_leaders_from_schedule( + leader_scheudle: &HashMap>, +) -> Result, String> { + let mut slot_leaders_map = BTreeMap::new(); + for (pk, index_list) in leader_scheudle { + for index in index_list { + let pubkey = Pubkey::from_str(pk) + .map_err(|err| format!("Pubkey from leader schedule not a plublic key:{err}"))?; + slot_leaders_map.insert(index, pubkey); + } + } + Ok(slot_leaders_map.into_values().collect()) +} + // Cribbed from leader_schedule_utils fn sort_stakes(stakes: &mut Vec<(Pubkey, u64)>) { // Sort first by stake. If stakes are the same, sort by pubkey to ensure a