Query the bank for the current slot leader

This commit is contained in:
Greg Fitzgerald 2019-02-23 14:43:34 -07:00
parent c5876ddca9
commit 264f502ed7
1 changed files with 14 additions and 0 deletions

View File

@ -55,6 +55,9 @@ impl Index<usize> for LeaderSchedule {
pub trait LeaderScheduleUtil {
/// Return the leader schedule for the current epoch.
fn leader_schedule(&self) -> LeaderSchedule;
/// Return the leader id for the current slot.
fn slot_leader(&self) -> Pubkey;
}
impl LeaderScheduleUtil for Bank {
@ -64,6 +67,10 @@ impl LeaderScheduleUtil for Bank {
Some(bank) => LeaderSchedule::new_with_bank(&bank),
}
}
fn slot_leader(&self) -> Pubkey {
self.leader_schedule()[self.slot_index() as usize]
}
}
#[cfg(test)]
@ -114,4 +121,11 @@ mod tests {
assert_eq!(leader_schedule.slot_leaders, expected);
assert_eq!(bank.leader_schedule().slot_leaders, expected); // Same thing, but with the trait
}
#[test]
fn test_leader_schedule_slot_leader_basic() {
let pubkey = Keypair::new().pubkey();
let bank = Bank::new(&GenesisBlock::new_with_leader(2, pubkey, 2).0);
assert_eq!(bank.slot_leader(), pubkey);
}
}