consolidate logic for epoch and slot_index into Bank (#3144)

This commit is contained in:
Rob Walker
2019-03-06 14:44:21 -08:00
committed by GitHub
parent a3782d699d
commit 63a4ed74a4
3 changed files with 37 additions and 8 deletions

View File

@ -32,8 +32,7 @@ fn sort_stakes(stakes: &mut Vec<(Pubkey, u64)>) {
/// Return the leader for the given slot.
pub fn slot_leader_at(slot: u64, bank: &Bank) -> Pubkey {
let slot_index = slot % bank.slots_per_epoch();
let epoch = slot / bank.slots_per_epoch();
let (epoch, slot_index) = bank.get_epoch_and_slot_index(slot);
let leader_schedule = leader_schedule(epoch, bank);
leader_schedule[slot_index as usize]

View File

@ -176,7 +176,12 @@ mod tests {
fn test_epoch_stakes_and_lockouts() {
let validator = Keypair::new();
let (genesis_block, mint_keypair) = GenesisBlock::new(500);
let (mut genesis_block, mint_keypair) = GenesisBlock::new(500);
// makes the bank generate epoch_vote_accounts right on epoch boundaries
// handy for tests
genesis_block.stakers_slot_offset = 0;
let bank = Bank::new(&genesis_block);
let bank_voter = Keypair::new();
@ -190,13 +195,11 @@ mod tests {
// should show up in the active set
voting_keypair_tests::new_vote_account_with_vote(&mint_keypair, &bank_voter, &bank, 499, 0);
// Have to wait until the epoch at (stakers_slot_offset / slots_per_epoch) + 1
// for the new votes to take effect. Earlier epochs were generated by genesis
let epoch = (bank.stakers_slot_offset() / bank.slots_per_epoch()) + 1;
// Build a bank in the next epoch, it will generate an epoch_vote_accounts() for epoch 1
let epoch = 1;
let epoch_slot = epoch * bank.slots_per_epoch();
let epoch_slot_offset = epoch_slot - bank.stakers_slot_offset();
let bank = new_from_parent(&Arc::new(bank), epoch_slot_offset);
let bank = new_from_parent(&Arc::new(bank), epoch_slot);
let result: Vec<_> = epoch_stakes_and_lockouts(&bank, 0);
assert_eq!(result, vec![(1, None)]);