Query the bank for the current slot leader
This commit is contained in:
parent
c5876ddca9
commit
264f502ed7
|
@ -55,6 +55,9 @@ impl Index<usize> for LeaderSchedule {
|
||||||
pub trait LeaderScheduleUtil {
|
pub trait LeaderScheduleUtil {
|
||||||
/// Return the leader schedule for the current epoch.
|
/// Return the leader schedule for the current epoch.
|
||||||
fn leader_schedule(&self) -> LeaderSchedule;
|
fn leader_schedule(&self) -> LeaderSchedule;
|
||||||
|
|
||||||
|
/// Return the leader id for the current slot.
|
||||||
|
fn slot_leader(&self) -> Pubkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LeaderScheduleUtil for Bank {
|
impl LeaderScheduleUtil for Bank {
|
||||||
|
@ -64,6 +67,10 @@ impl LeaderScheduleUtil for Bank {
|
||||||
Some(bank) => LeaderSchedule::new_with_bank(&bank),
|
Some(bank) => LeaderSchedule::new_with_bank(&bank),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn slot_leader(&self) -> Pubkey {
|
||||||
|
self.leader_schedule()[self.slot_index() as usize]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -114,4 +121,11 @@ mod tests {
|
||||||
assert_eq!(leader_schedule.slot_leaders, expected);
|
assert_eq!(leader_schedule.slot_leaders, expected);
|
||||||
assert_eq!(bank.leader_schedule().slot_leaders, expected); // Same thing, but with the trait
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue