Skip to main content

solana_ledger/
leader_schedule_utils.rs

1use {
2    crate::leader_schedule::{
3        IdentityKeyedLeaderSchedule, LeaderSchedule, VoteKeyedLeaderSchedule,
4    },
5    solana_clock::{Epoch, Slot, NUM_CONSECUTIVE_LEADER_SLOTS},
6    solana_pubkey::Pubkey,
7    solana_runtime::bank::Bank,
8    std::collections::HashMap,
9};
10
11/// Return the leader schedule for the given epoch.
12pub fn leader_schedule(epoch: Epoch, bank: &Bank) -> Option<LeaderSchedule> {
13    let use_new_leader_schedule = bank.should_use_vote_keyed_leader_schedule(epoch)?;
14    if use_new_leader_schedule {
15        bank.epoch_vote_accounts(epoch).map(|vote_accounts_map| {
16            Box::new(VoteKeyedLeaderSchedule::new(
17                vote_accounts_map,
18                epoch,
19                bank.get_slots_in_epoch(epoch),
20                NUM_CONSECUTIVE_LEADER_SLOTS,
21            )) as LeaderSchedule
22        })
23    } else {
24        bank.epoch_staked_nodes(epoch).map(|stakes| {
25            Box::new(IdentityKeyedLeaderSchedule::new(
26                &stakes,
27                epoch,
28                bank.get_slots_in_epoch(epoch),
29                NUM_CONSECUTIVE_LEADER_SLOTS,
30            )) as LeaderSchedule
31        })
32    }
33}
34
35/// Map of leader base58 identity pubkeys to the slot indices relative to the first epoch slot
36pub type LeaderScheduleByIdentity = HashMap<String, Vec<usize>>;
37
38pub fn leader_schedule_by_identity<'a>(
39    upcoming_leaders: impl Iterator<Item = (usize, &'a Pubkey)>,
40) -> LeaderScheduleByIdentity {
41    let mut leader_schedule_by_identity = HashMap::new();
42
43    for (slot_index, identity_pubkey) in upcoming_leaders {
44        leader_schedule_by_identity
45            .entry(identity_pubkey)
46            .or_insert_with(Vec::new)
47            .push(slot_index);
48    }
49
50    leader_schedule_by_identity
51        .into_iter()
52        .map(|(identity_pubkey, slot_indices)| (identity_pubkey.to_string(), slot_indices))
53        .collect()
54}
55
56/// Return the leader for the given slot.
57pub fn slot_leader_at(slot: Slot, bank: &Bank) -> Option<Pubkey> {
58    let (epoch, slot_index) = bank.get_epoch_and_slot_index(slot);
59
60    leader_schedule(epoch, bank).map(|leader_schedule| leader_schedule[slot_index])
61}
62
63// Returns the number of ticks remaining from the specified tick_height to the end of the
64// slot implied by the tick_height
65pub fn num_ticks_left_in_slot(bank: &Bank, tick_height: u64) -> u64 {
66    bank.ticks_per_slot() - tick_height % bank.ticks_per_slot()
67}
68
69pub fn first_of_consecutive_leader_slots(slot: Slot) -> Slot {
70    (slot / NUM_CONSECUTIVE_LEADER_SLOTS) * NUM_CONSECUTIVE_LEADER_SLOTS
71}
72
73/// Returns the last slot in the leader window that contains `slot`
74#[inline]
75pub fn last_of_consecutive_leader_slots(slot: Slot) -> Slot {
76    first_of_consecutive_leader_slots(slot) + NUM_CONSECUTIVE_LEADER_SLOTS - 1
77}
78
79/// Returns the index within the leader slot range that contains `slot`
80#[inline]
81pub fn leader_slot_index(slot: Slot) -> usize {
82    (slot % NUM_CONSECUTIVE_LEADER_SLOTS) as usize
83}
84
85/// Returns the number of slots left after `slot` in the leader window
86/// that contains `slot`
87#[inline]
88pub fn remaining_slots_in_window(slot: Slot) -> u64 {
89    NUM_CONSECUTIVE_LEADER_SLOTS
90        .checked_sub(leader_slot_index(slot) as u64)
91        .unwrap()
92}
93
94#[cfg(test)]
95mod tests {
96    use {
97        super::*,
98        solana_runtime::genesis_utils::{
99            bootstrap_validator_stake_lamports, create_genesis_config_with_leader,
100            deactivate_features,
101        },
102        test_case::test_case,
103    };
104
105    #[test_case(true; "vote keyed leader schedule")]
106    #[test_case(false; "identity keyed leader schedule")]
107    fn test_leader_schedule_via_bank(use_vote_keyed_leader_schedule: bool) {
108        let pubkey = solana_pubkey::new_rand();
109        let mut genesis_config =
110            create_genesis_config_with_leader(0, &pubkey, bootstrap_validator_stake_lamports())
111                .genesis_config;
112
113        if !use_vote_keyed_leader_schedule {
114            deactivate_features(
115                &mut genesis_config,
116                &vec![agave_feature_set::enable_vote_address_leader_schedule::id()],
117            );
118        }
119
120        let bank = Bank::new_for_tests(&genesis_config);
121        let leader_schedule = leader_schedule(0, &bank).unwrap();
122
123        assert_eq!(
124            leader_schedule.get_vote_key_at_slot_index(0).is_some(),
125            use_vote_keyed_leader_schedule
126        );
127
128        assert_eq!(leader_schedule[0], pubkey);
129        assert_eq!(leader_schedule[1], pubkey);
130        assert_eq!(leader_schedule[2], pubkey);
131    }
132
133    #[test]
134    fn test_leader_scheduler1_basic() {
135        let pubkey = solana_pubkey::new_rand();
136        let genesis_config =
137            create_genesis_config_with_leader(42, &pubkey, bootstrap_validator_stake_lamports())
138                .genesis_config;
139        let bank = Bank::new_for_tests(&genesis_config);
140        assert_eq!(slot_leader_at(bank.slot(), &bank).unwrap(), pubkey);
141    }
142
143    #[test]
144    fn test_leader_span_math() {
145        // All of the test cases assume a 4 slot leader span and need to be
146        // adjusted if it changes.
147        assert_eq!(NUM_CONSECUTIVE_LEADER_SLOTS, 4);
148
149        assert_eq!(first_of_consecutive_leader_slots(0), 0);
150        assert_eq!(first_of_consecutive_leader_slots(1), 0);
151        assert_eq!(first_of_consecutive_leader_slots(2), 0);
152        assert_eq!(first_of_consecutive_leader_slots(3), 0);
153        assert_eq!(first_of_consecutive_leader_slots(4), 4);
154
155        assert_eq!(last_of_consecutive_leader_slots(0), 3);
156        assert_eq!(last_of_consecutive_leader_slots(1), 3);
157        assert_eq!(last_of_consecutive_leader_slots(2), 3);
158        assert_eq!(last_of_consecutive_leader_slots(3), 3);
159        assert_eq!(last_of_consecutive_leader_slots(4), 7);
160
161        assert_eq!(leader_slot_index(0), 0);
162        assert_eq!(leader_slot_index(1), 1);
163        assert_eq!(leader_slot_index(2), 2);
164        assert_eq!(leader_slot_index(3), 3);
165        assert_eq!(leader_slot_index(4), 0);
166        assert_eq!(leader_slot_index(5), 1);
167        assert_eq!(leader_slot_index(6), 2);
168        assert_eq!(leader_slot_index(7), 3);
169
170        assert_eq!(remaining_slots_in_window(0), 4);
171        assert_eq!(remaining_slots_in_window(1), 3);
172        assert_eq!(remaining_slots_in_window(2), 2);
173        assert_eq!(remaining_slots_in_window(3), 1);
174        assert_eq!(remaining_slots_in_window(4), 4);
175    }
176}