Skip to content

Commit

Permalink
change slot leader calculus. Remove rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
musitdev committed Jan 6, 2024
1 parent 032232f commit 29478ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
43 changes: 19 additions & 24 deletions stake_vote/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Vec<Pubkey>, 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<EpochVoteStakes>,
Expand Down
16 changes: 16 additions & 0 deletions stake_vote/src/leader_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Vec<usize>>,
) -> Result<Vec<Pubkey>, 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
Expand Down

0 comments on commit 29478ad

Please sign in to comment.