2019-03-29 20:00:36 -07:00
|
|
|
use crate::blocktree::Blocktree;
|
2019-02-27 14:41:46 -08:00
|
|
|
use crate::leader_schedule::LeaderSchedule;
|
2019-02-28 13:15:25 -08:00
|
|
|
use crate::staking_utils;
|
2019-02-27 14:41:46 -08:00
|
|
|
use solana_runtime::bank::Bank;
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-03-19 06:36:45 -07:00
|
|
|
use solana_sdk::timing::NUM_CONSECUTIVE_LEADER_SLOTS;
|
2019-02-27 14:41:46 -08:00
|
|
|
|
|
|
|
/// Return the leader schedule for the given epoch.
|
2019-03-07 15:49:07 -08:00
|
|
|
fn leader_schedule(epoch_height: u64, bank: &Bank) -> Option<LeaderSchedule> {
|
|
|
|
staking_utils::delegated_stakes_at_epoch(bank, epoch_height).map(|stakes| {
|
|
|
|
let mut seed = [0u8; 32];
|
|
|
|
seed[0..8].copy_from_slice(&epoch_height.to_le_bytes());
|
|
|
|
let mut stakes: Vec<_> = stakes.into_iter().collect();
|
|
|
|
sort_stakes(&mut stakes);
|
2019-03-19 06:36:45 -07:00
|
|
|
LeaderSchedule::new(
|
|
|
|
&stakes,
|
|
|
|
seed,
|
|
|
|
bank.get_slots_in_epoch(epoch_height),
|
|
|
|
NUM_CONSECUTIVE_LEADER_SLOTS,
|
|
|
|
)
|
2019-03-07 15:49:07 -08:00
|
|
|
})
|
2019-02-27 14:41:46 -08:00
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:45 -08:00
|
|
|
fn sort_stakes(stakes: &mut Vec<(Pubkey, u64)>) {
|
|
|
|
// Sort first by stake. If stakes are the same, sort by pubkey to ensure a
|
|
|
|
// deterministic result.
|
|
|
|
// Note: Use unstable sort, because we dedup right after to remove the equal elements.
|
|
|
|
stakes.sort_unstable_by(|(l_id, l_stake), (r_id, r_stake)| {
|
|
|
|
if r_stake == l_stake {
|
|
|
|
r_id.cmp(&l_id)
|
|
|
|
} else {
|
|
|
|
r_stake.cmp(&l_stake)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now that it's sorted, we can do an O(n) dedup.
|
|
|
|
stakes.dedup();
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:41:46 -08:00
|
|
|
/// Return the leader for the given slot.
|
2019-03-07 15:49:07 -08:00
|
|
|
pub fn slot_leader_at(slot: u64, bank: &Bank) -> Option<Pubkey> {
|
2019-03-06 14:44:21 -08:00
|
|
|
let (epoch, slot_index) = bank.get_epoch_and_slot_index(slot);
|
2019-02-27 14:41:46 -08:00
|
|
|
|
2019-03-14 16:06:56 -07:00
|
|
|
leader_schedule(epoch, bank).map(|leader_schedule| leader_schedule[slot_index])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the next slot after the given current_slot that the given node will be leader
|
2019-03-29 20:00:36 -07:00
|
|
|
pub fn next_leader_slot(
|
|
|
|
pubkey: &Pubkey,
|
|
|
|
mut current_slot: u64,
|
|
|
|
bank: &Bank,
|
|
|
|
blocktree: Option<&Blocktree>,
|
|
|
|
) -> Option<u64> {
|
2019-03-21 07:43:21 -07:00
|
|
|
let (mut epoch, mut start_index) = bank.get_epoch_and_slot_index(current_slot + 1);
|
|
|
|
while let Some(leader_schedule) = leader_schedule(epoch, bank) {
|
2019-03-14 16:06:56 -07:00
|
|
|
// clippy thinks I should do this:
|
|
|
|
// for (i, <item>) in leader_schedule
|
|
|
|
// .iter()
|
|
|
|
// .enumerate()
|
|
|
|
// .take(bank.get_slots_in_epoch(epoch))
|
|
|
|
// .skip(from_slot_index + 1) {
|
|
|
|
//
|
|
|
|
// but leader_schedule doesn't implement Iter...
|
|
|
|
#[allow(clippy::needless_range_loop)]
|
2019-03-21 07:43:21 -07:00
|
|
|
for i in start_index..bank.get_slots_in_epoch(epoch) {
|
|
|
|
current_slot += 1;
|
2019-03-14 16:06:56 -07:00
|
|
|
if *pubkey == leader_schedule[i] {
|
2019-03-29 20:00:36 -07:00
|
|
|
if let Some(blocktree) = blocktree {
|
|
|
|
if let Some(meta) = blocktree.meta(current_slot).unwrap() {
|
|
|
|
// We have already sent a blob for this slot, so skip it
|
|
|
|
if meta.received > 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
return Some(current_slot);
|
2019-03-14 16:06:56 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-21 07:43:21 -07:00
|
|
|
|
|
|
|
epoch += 1;
|
|
|
|
start_index = 0;
|
2019-03-14 16:06:56 -07:00
|
|
|
}
|
|
|
|
None
|
2019-02-27 14:41:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of ticks remaining from the specified tick_height to the end of the
|
|
|
|
// slot implied by the tick_height
|
|
|
|
pub fn num_ticks_left_in_slot(bank: &Bank, tick_height: u64) -> u64 {
|
|
|
|
bank.ticks_per_slot() - tick_height % bank.ticks_per_slot() - 1
|
|
|
|
}
|
|
|
|
|
2019-03-12 17:42:53 -07:00
|
|
|
pub fn tick_height_to_slot(ticks_per_slot: u64, tick_height: u64) -> u64 {
|
|
|
|
tick_height / ticks_per_slot
|
2019-03-05 17:56:51 -08:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:41:46 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-29 20:00:36 -07:00
|
|
|
use crate::blocktree::get_tmp_ledger_path;
|
|
|
|
use crate::blocktree::tests::make_slot_entries;
|
2019-02-28 13:15:25 -08:00
|
|
|
use crate::staking_utils;
|
2019-04-10 17:52:47 -07:00
|
|
|
use crate::voting_keypair::tests::new_vote_account;
|
2019-03-05 16:28:14 -08:00
|
|
|
use solana_sdk::genesis_block::{GenesisBlock, BOOTSTRAP_LEADER_LAMPORTS};
|
2019-02-27 14:41:46 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
2019-03-21 07:43:21 -07:00
|
|
|
use std::sync::Arc;
|
2019-02-27 14:41:46 -08:00
|
|
|
|
2019-03-14 16:06:56 -07:00
|
|
|
#[test]
|
|
|
|
fn test_next_leader_slot() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-14 16:06:56 -07:00
|
|
|
let mut genesis_block = GenesisBlock::new_with_leader(
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
&pubkey,
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
)
|
|
|
|
.0;
|
|
|
|
genesis_block.epoch_warmup = false;
|
|
|
|
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
assert_eq!(slot_leader_at(bank.slot(), &bank).unwrap(), pubkey);
|
2019-03-29 20:00:36 -07:00
|
|
|
assert_eq!(next_leader_slot(&pubkey, 0, &bank, None), Some(1));
|
|
|
|
assert_eq!(next_leader_slot(&pubkey, 1, &bank, None), Some(2));
|
2019-03-14 16:06:56 -07:00
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(
|
|
|
|
&pubkey,
|
|
|
|
2 * genesis_block.slots_per_epoch - 1, // no schedule generated for epoch 2
|
2019-03-29 20:00:36 -07:00
|
|
|
&bank,
|
|
|
|
None
|
2019-03-14 16:06:56 -07:00
|
|
|
),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(
|
2019-03-30 20:37:33 -07:00
|
|
|
&Pubkey::new_rand(), // not in leader_schedule
|
2019-03-14 16:06:56 -07:00
|
|
|
0,
|
2019-03-29 20:00:36 -07:00
|
|
|
&bank,
|
|
|
|
None
|
2019-03-14 16:06:56 -07:00
|
|
|
),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
#[test]
|
|
|
|
fn test_next_leader_slot_blocktree() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-29 20:00:36 -07:00
|
|
|
let mut genesis_block = GenesisBlock::new_with_leader(
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
&pubkey,
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
)
|
|
|
|
.0;
|
|
|
|
genesis_block.epoch_warmup = false;
|
|
|
|
|
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
|
|
|
let blocktree = Arc::new(
|
|
|
|
Blocktree::open(&ledger_path).expect("Expected to be able to open database ledger"),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(slot_leader_at(bank.slot(), &bank).unwrap(), pubkey);
|
|
|
|
// Check that the next leader slot after 0 is slot 1
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(&pubkey, 0, &bank, Some(&blocktree)),
|
|
|
|
Some(1)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Write a blob into slot 2 that chains to slot 1,
|
|
|
|
// but slot 1 is empty so should not be skipped
|
|
|
|
let (blobs, _) = make_slot_entries(2, 1, 1);
|
|
|
|
blocktree.write_blobs(&blobs[..]).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(&pubkey, 0, &bank, Some(&blocktree)),
|
|
|
|
Some(1)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Write a blob into slot 1
|
|
|
|
let (blobs, _) = make_slot_entries(1, 0, 1);
|
|
|
|
|
|
|
|
// Check that slot 1 and 2 are skipped
|
|
|
|
blocktree.write_blobs(&blobs[..]).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(&pubkey, 0, &bank, Some(&blocktree)),
|
|
|
|
Some(3)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Integrity checks
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(
|
|
|
|
&pubkey,
|
|
|
|
2 * genesis_block.slots_per_epoch - 1, // no schedule generated for epoch 2
|
|
|
|
&bank,
|
|
|
|
Some(&blocktree)
|
|
|
|
),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
next_leader_slot(
|
2019-03-30 20:37:33 -07:00
|
|
|
&Pubkey::new_rand(), // not in leader_schedule
|
2019-03-29 20:00:36 -07:00
|
|
|
0,
|
|
|
|
&bank,
|
|
|
|
Some(&blocktree)
|
|
|
|
),
|
|
|
|
None
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Blocktree::destroy(&ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-03-21 07:43:21 -07:00
|
|
|
#[test]
|
|
|
|
fn test_next_leader_slot_next_epoch() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-21 07:43:21 -07:00
|
|
|
let (mut genesis_block, mint_keypair) = GenesisBlock::new_with_leader(
|
|
|
|
2 * BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
&pubkey,
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
);
|
|
|
|
genesis_block.epoch_warmup = false;
|
|
|
|
|
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-03-30 20:37:33 -07:00
|
|
|
let delegate_id = Pubkey::new_rand();
|
2019-03-21 07:43:21 -07:00
|
|
|
|
|
|
|
// Create new vote account
|
|
|
|
let new_voting_keypair = Keypair::new();
|
2019-04-10 17:52:47 -07:00
|
|
|
new_vote_account(
|
2019-03-21 07:43:21 -07:00
|
|
|
&mint_keypair,
|
|
|
|
&new_voting_keypair,
|
|
|
|
&delegate_id,
|
|
|
|
&bank,
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Have to wait until the epoch at after the epoch stakes generated at genesis
|
|
|
|
// for the new votes to take effect.
|
|
|
|
let mut target_slot = 1;
|
|
|
|
let epoch = bank.get_stakers_epoch(0);
|
|
|
|
while bank.get_stakers_epoch(target_slot) == epoch {
|
|
|
|
target_slot += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bank = Bank::new_from_parent(&Arc::new(bank), &Pubkey::default(), target_slot);
|
|
|
|
let mut expected_slot = 0;
|
|
|
|
let epoch = bank.get_stakers_epoch(target_slot);
|
|
|
|
for i in 0..epoch {
|
|
|
|
expected_slot += bank.get_slots_in_epoch(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let schedule = leader_schedule(epoch, &bank).unwrap();
|
|
|
|
let mut index = 0;
|
|
|
|
while schedule[index] != delegate_id {
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
expected_slot += index;
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-03-29 20:00:36 -07:00
|
|
|
next_leader_slot(&delegate_id, 0, &bank, None),
|
|
|
|
Some(expected_slot),
|
2019-03-21 07:43:21 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:41:46 -08:00
|
|
|
#[test]
|
|
|
|
fn test_leader_schedule_via_bank() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-05 16:28:14 -08:00
|
|
|
let (genesis_block, _mint_keypair) = GenesisBlock::new_with_leader(
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
2019-03-09 19:28:43 -08:00
|
|
|
&pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
);
|
2019-02-27 14:41:46 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
|
|
|
|
2019-03-03 18:04:13 -08:00
|
|
|
let ids_and_stakes: Vec<_> = staking_utils::delegated_stakes(&bank).into_iter().collect();
|
2019-02-27 14:41:46 -08:00
|
|
|
let seed = [0u8; 32];
|
2019-03-19 06:36:45 -07:00
|
|
|
let leader_schedule = LeaderSchedule::new(
|
|
|
|
&ids_and_stakes,
|
|
|
|
seed,
|
|
|
|
genesis_block.slots_per_epoch,
|
|
|
|
NUM_CONSECUTIVE_LEADER_SLOTS,
|
|
|
|
);
|
2019-02-27 14:41:46 -08:00
|
|
|
|
|
|
|
assert_eq!(leader_schedule[0], pubkey);
|
|
|
|
assert_eq!(leader_schedule[1], pubkey);
|
|
|
|
assert_eq!(leader_schedule[2], pubkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leader_scheduler1_basic() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-05 16:28:14 -08:00
|
|
|
let genesis_block = GenesisBlock::new_with_leader(
|
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
2019-03-09 19:28:43 -08:00
|
|
|
&pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
BOOTSTRAP_LEADER_LAMPORTS,
|
|
|
|
)
|
|
|
|
.0;
|
2019-02-27 14:41:46 -08:00
|
|
|
let bank = Bank::new(&genesis_block);
|
2019-03-07 15:49:07 -08:00
|
|
|
assert_eq!(slot_leader_at(bank.slot(), &bank).unwrap(), pubkey);
|
2019-02-27 14:41:46 -08:00
|
|
|
}
|
2019-02-28 17:08:45 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sort_stakes_basic() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey0 = Pubkey::new_rand();
|
|
|
|
let pubkey1 = Pubkey::new_rand();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut stakes = vec![(pubkey0, 1), (pubkey1, 2)];
|
|
|
|
sort_stakes(&mut stakes);
|
|
|
|
assert_eq!(stakes, vec![(pubkey1, 2), (pubkey0, 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sort_stakes_with_dup() {
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey0 = Pubkey::new_rand();
|
|
|
|
let pubkey1 = Pubkey::new_rand();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut stakes = vec![(pubkey0, 1), (pubkey1, 2), (pubkey0, 1)];
|
|
|
|
sort_stakes(&mut stakes);
|
|
|
|
assert_eq!(stakes, vec![(pubkey1, 2), (pubkey0, 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sort_stakes_with_equal_stakes() {
|
|
|
|
let pubkey0 = Pubkey::default();
|
2019-03-30 20:37:33 -07:00
|
|
|
let pubkey1 = Pubkey::new_rand();
|
2019-02-28 17:08:45 -08:00
|
|
|
let mut stakes = vec![(pubkey0, 1), (pubkey1, 1)];
|
|
|
|
sort_stakes(&mut stakes);
|
|
|
|
assert_eq!(stakes, vec![(pubkey1, 1), (pubkey0, 1)]);
|
|
|
|
}
|
2019-02-27 14:41:46 -08:00
|
|
|
}
|