diff --git a/core/src/leader_schedule_utils.rs b/core/src/leader_schedule_utils.rs index 9f85a24f15..f0982a946e 100644 --- a/core/src/leader_schedule_utils.rs +++ b/core/src/leader_schedule_utils.rs @@ -30,65 +30,13 @@ fn sort_stakes(stakes: &mut Vec<(Pubkey, u64)>) { stakes.dedup(); } -/// Return the leader for the slot at the slot_index and epoch_height returned -/// by the given function. -fn slot_leader_by(bank: &Bank, get_slot_index: F) -> Pubkey -where - F: Fn(u64, u64, u64) -> (u64, u64), -{ - let (slot_index, epoch_height) = get_slot_index( - bank.slot_index(), - bank.epoch_height(), - bank.slots_per_epoch(), - ); - let leader_schedule = leader_schedule(epoch_height, bank); - leader_schedule[slot_index as usize] -} - -/// Return the leader for the current slot. -pub fn slot_leader(bank: &Bank) -> Pubkey { - slot_leader_by(bank, |slot_index, epoch_height, _| { - (slot_index, epoch_height) - }) -} - /// Return the leader for the given slot. pub fn slot_leader_at(slot: u64, bank: &Bank) -> Pubkey { - slot_leader_by(bank, |_, _, _| { - (slot % bank.slots_per_epoch(), slot / bank.slots_per_epoch()) - }) -} + let slot_index = slot % bank.slots_per_epoch(); + let epoch = slot / bank.slots_per_epoch(); -/// Return the epoch height and slot index of the slot before the current slot. -fn prev_slot_leader_index(slot_index: u64, epoch_height: u64, slots_per_epoch: u64) -> (u64, u64) { - if epoch_height == 0 && slot_index == 0 { - return (0, 0); - } - - if slot_index == 0 { - (slots_per_epoch - 1, epoch_height - 1) - } else { - (slot_index - 1, epoch_height) - } -} - -/// Return the slot_index and epoch height of the slot following the current slot. -fn next_slot_leader_index(slot_index: u64, epoch_height: u64, slots_per_epoch: u64) -> (u64, u64) { - if slot_index + 1 == slots_per_epoch { - (0, epoch_height + 1) - } else { - (slot_index + 1, epoch_height) - } -} - -/// Return the leader for the slot before the current slot. -pub fn prev_slot_leader(bank: &Bank) -> Pubkey { - slot_leader_by(bank, prev_slot_leader_index) -} - -/// Return the leader for the slot following the current slot. -pub fn next_slot_leader(bank: &Bank) -> Pubkey { - slot_leader_by(bank, next_slot_leader_index) + let leader_schedule = leader_schedule(epoch, bank); + leader_schedule[slot_index as usize] } // Returns the number of ticks remaining from the specified tick_height to the end of the @@ -128,20 +76,7 @@ mod tests { GenesisBlock::new_with_leader(BOOTSTRAP_LEADER_TOKENS, pubkey, BOOTSTRAP_LEADER_TOKENS) .0; let bank = Bank::new(&genesis_block); - assert_eq!(slot_leader(&bank), pubkey); - } - - #[test] - fn test_leader_scheduler1_prev_slot_leader_index() { - assert_eq!(prev_slot_leader_index(0, 0, 2), (0, 0)); - assert_eq!(prev_slot_leader_index(1, 0, 2), (0, 0)); - assert_eq!(prev_slot_leader_index(0, 1, 2), (1, 0)); - } - - #[test] - fn test_leader_scheduler1_next_slot_leader_index() { - assert_eq!(next_slot_leader_index(0, 0, 2), (1, 0)); - assert_eq!(next_slot_leader_index(1, 0, 2), (0, 1)); + assert_eq!(slot_leader_at(bank.slot(), &bank), pubkey); } #[test] diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 24fc828312..a36a0ee7e9 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -771,12 +771,7 @@ impl Bank { self.tick_height.load(Ordering::SeqCst) as u64 } - /// Return the number of ticks since the last slot boundary. - pub fn tick_index(&self) -> u64 { - self.tick_height() % self.ticks_per_slot() - } - - /// Return the number of slots per tick. + /// Return the number of slots per epoch for this epoch pub fn slots_per_epoch(&self) -> u64 { self.slots_per_epoch } @@ -796,16 +791,6 @@ impl Bank { pub fn epoch_vote_accounts(&self, epoch: u64) -> Option<&HashMap> { self.epoch_vote_accounts.get(&epoch) } - - /// Return the number of slots since the last epoch boundary. - pub fn slot_index(&self) -> u64 { - self.slot() % self.slots_per_epoch() - } - - /// Return the epoch height of the last registered tick. - pub fn epoch_height(&self) -> u64 { - self.slot() / self.slots_per_epoch() - } } #[cfg(test)] @@ -814,7 +799,6 @@ mod tests { use bincode::serialize; use hashbrown::HashSet; use solana_sdk::genesis_block::{GenesisBlock, BOOTSTRAP_LEADER_TOKENS}; - use solana_sdk::hash::hash; use solana_sdk::native_program::ProgramError; use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::system_instruction::SystemInstruction; @@ -1159,53 +1143,6 @@ mod tests { assert_eq!(bank.get_balance(&dummy_leader_id), 2); } - // Register n ticks and return the tick, slot and epoch indexes. - fn register_ticks(bank: &mut Arc, tick_hash: &mut Hash, n: u64) -> (u64, u64, u64) { - let mut max_tick_height = (bank.slot() + 1) * bank.ticks_per_slot() - 1; - for _ in 0..n { - if bank.tick_height() == max_tick_height { - *bank = Arc::new(Bank::new_from_parent( - &bank, - Pubkey::default(), - bank.slot() + 1, - )); - max_tick_height = (bank.slot() + 1) * bank.ticks_per_slot() - 1; - } - - *tick_hash = hash(&serialize(tick_hash).unwrap()); - bank.register_tick(&tick_hash); - } - (bank.tick_index(), bank.slot_index(), bank.epoch_height()) - } - - #[test] - fn test_tick_slot_epoch_indexes() { - let (genesis_block, _) = GenesisBlock::new(5); - let mut tick_hash = genesis_block.hash(); - let mut bank = Arc::new(Bank::new(&genesis_block)); - let ticks_per_slot = bank.ticks_per_slot(); - let slots_per_epoch = bank.slots_per_epoch(); - let ticks_per_epoch = ticks_per_slot * slots_per_epoch; - - // All indexes are zero-based. - assert_eq!(register_ticks(&mut bank, &mut tick_hash, 0), (0, 0, 0)); - - // Slot index remains zero through the last tick. - assert_eq!( - register_ticks(&mut bank, &mut tick_hash, ticks_per_slot - 1), - (ticks_per_slot - 1, 0, 0) - ); - - // Cross a slot boundary. - assert_eq!(register_ticks(&mut bank, &mut tick_hash, 1), (0, 1, 0)); - - // Cross an epoch boundary. - assert_eq!( - register_ticks(&mut bank, &mut tick_hash, ticks_per_epoch), - (0, 1, 1) - ); - } - #[test] fn test_interleaving_locks() { let (genesis_block, mint_keypair) = GenesisBlock::new(3);