Expose the new leader schedule functionality from the bank.
This commit is contained in:
parent
d67211305c
commit
0947ec59c9
|
@ -2243,6 +2243,7 @@ dependencies = [
|
||||||
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -15,6 +15,7 @@ fnv = "1.0.6"
|
||||||
hashbrown = "0.1.8"
|
hashbrown = "0.1.8"
|
||||||
log = "0.4.2"
|
log = "0.4.2"
|
||||||
rand = "0.6.5"
|
rand = "0.6.5"
|
||||||
|
rand_chacha = "0.1.1"
|
||||||
serde = "1.0.88"
|
serde = "1.0.88"
|
||||||
serde_derive = "1.0.88"
|
serde_derive = "1.0.88"
|
||||||
serde_json = "1.0.38"
|
serde_json = "1.0.38"
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
use crate::accounts::{Accounts, ErrorCounters, InstructionAccounts, InstructionLoaders};
|
use crate::accounts::{Accounts, ErrorCounters, InstructionAccounts, InstructionLoaders};
|
||||||
use crate::last_id_queue::LastIdQueue;
|
use crate::last_id_queue::LastIdQueue;
|
||||||
|
use crate::leader_schedule::LeaderSchedule;
|
||||||
use crate::runtime::{self, RuntimeError};
|
use crate::runtime::{self, RuntimeError};
|
||||||
use crate::status_cache::StatusCache;
|
use crate::status_cache::StatusCache;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
|
@ -675,7 +676,7 @@ impl Bank {
|
||||||
|
|
||||||
/// Return the checkpointed bank that should be used to generate a leader schedule.
|
/// Return the checkpointed bank that should be used to generate a leader schedule.
|
||||||
/// Return None if a sufficiently old bank checkpoint doesn't exist.
|
/// Return None if a sufficiently old bank checkpoint doesn't exist.
|
||||||
pub fn leader_schedule_bank(&self) -> Option<Arc<Bank>> {
|
fn leader_schedule_bank(&self) -> Option<Arc<Bank>> {
|
||||||
let epoch_slot_height = self.slot_height() - self.slot_index();
|
let epoch_slot_height = self.slot_height() - self.slot_index();
|
||||||
let expected = epoch_slot_height.saturating_sub(self.leader_schedule_slot_offset);
|
let expected = epoch_slot_height.saturating_sub(self.leader_schedule_slot_offset);
|
||||||
self.parents()
|
self.parents()
|
||||||
|
@ -683,6 +684,19 @@ impl Bank {
|
||||||
.find(|bank| bank.slot_height() <= expected)
|
.find(|bank| bank.slot_height() <= expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the leader schedule for the current epoch.
|
||||||
|
fn leader_schedule(&self) -> LeaderSchedule {
|
||||||
|
match self.leader_schedule_bank() {
|
||||||
|
None => LeaderSchedule::new_with_bank(self),
|
||||||
|
Some(bank) => LeaderSchedule::new_with_bank(&bank),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the leader for the current slot.
|
||||||
|
pub fn slot_leader(&self) -> Pubkey {
|
||||||
|
self.leader_schedule()[self.slot_index() as usize]
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the number of ticks since genesis.
|
/// Return the number of ticks since genesis.
|
||||||
pub fn tick_height(&self) -> u64 {
|
pub fn tick_height(&self) -> u64 {
|
||||||
self.last_id_queue.read().unwrap().tick_height
|
self.last_id_queue.read().unwrap().tick_height
|
||||||
|
@ -1365,4 +1379,11 @@ mod tests {
|
||||||
bank.merge_parents();
|
bank.merge_parents();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_bank_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
use crate::bank::Bank;
|
||||||
use rand::distributions::{Distribution, WeightedIndex};
|
use rand::distributions::{Distribution, WeightedIndex};
|
||||||
use rand::SeedableRng;
|
use rand::SeedableRng;
|
||||||
use rand_chacha::ChaChaRng;
|
use rand_chacha::ChaChaRng;
|
||||||
use solana_runtime::bank::Bank;
|
|
||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
|
|
||||||
/// Round-robin leader schedule.
|
/// Stake-weighted leader schedule for one epoch.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct LeaderSchedule {
|
pub struct LeaderSchedule {
|
||||||
slot_leaders: Vec<Pubkey>,
|
slot_leaders: Vec<Pubkey>,
|
||||||
|
@ -43,27 +43,6 @@ 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 {
|
|
||||||
fn leader_schedule(&self) -> LeaderSchedule {
|
|
||||||
match self.leader_schedule_bank() {
|
|
||||||
None => LeaderSchedule::new_with_bank(self),
|
|
||||||
Some(bank) => LeaderSchedule::new_with_bank(&bank),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn slot_leader(&self) -> Pubkey {
|
|
||||||
self.leader_schedule()[self.slot_index() as usize]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -110,13 +89,5 @@ mod tests {
|
||||||
let len = bank.slots_per_epoch() as usize;
|
let len = bank.slots_per_epoch() as usize;
|
||||||
let expected: Vec<_> = iter::repeat(pubkey).take(len).collect();
|
let expected: Vec<_> = iter::repeat(pubkey).take(len).collect();
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ mod accounts;
|
||||||
pub mod bank;
|
pub mod bank;
|
||||||
pub mod bloom;
|
pub mod bloom;
|
||||||
mod last_id_queue;
|
mod last_id_queue;
|
||||||
|
mod leader_schedule;
|
||||||
mod runtime;
|
mod runtime;
|
||||||
mod status_cache;
|
mod status_cache;
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,6 @@ pub mod fullnode;
|
||||||
pub mod gen_keys;
|
pub mod gen_keys;
|
||||||
pub mod gossip_service;
|
pub mod gossip_service;
|
||||||
pub mod leader_confirmation_service;
|
pub mod leader_confirmation_service;
|
||||||
pub mod leader_schedule;
|
|
||||||
pub mod leader_scheduler;
|
pub mod leader_scheduler;
|
||||||
pub mod local_vote_signer_service;
|
pub mod local_vote_signer_service;
|
||||||
pub mod packet;
|
pub mod packet;
|
||||||
|
|
Loading…
Reference in New Issue