Move EpochSchedule into own module (#4272)

This commit is contained in:
carllin 2019-05-13 16:24:32 -07:00 committed by GitHub
parent 7501ed65e5
commit 3bd921264a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 113 additions and 108 deletions

View File

@ -10,7 +10,7 @@ use crate::gossip_service::discover_nodes;
use crate::locktower::VOTE_THRESHOLD_DEPTH; use crate::locktower::VOTE_THRESHOLD_DEPTH;
use crate::poh_service::PohServiceConfig; use crate::poh_service::PohServiceConfig;
use solana_client::thin_client::create_client; use solana_client::thin_client::create_client;
use solana_runtime::bank::MINIMUM_SLOT_LENGTH; use solana_runtime::epoch_schedule::MINIMUM_SLOT_LENGTH;
use solana_sdk::client::SyncClient; use solana_sdk::client::SyncClient;
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature}; use solana_sdk::signature::{Keypair, KeypairUtil, Signature};

View File

@ -1,7 +1,8 @@
use crate::blocktree::Blocktree; use crate::blocktree::Blocktree;
use crate::leader_schedule::LeaderSchedule; use crate::leader_schedule::LeaderSchedule;
use crate::leader_schedule_utils; use crate::leader_schedule_utils;
use solana_runtime::bank::{Bank, EpochSchedule}; use solana_runtime::bank::Bank;
use solana_runtime::epoch_schedule::EpochSchedule;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
@ -168,7 +169,8 @@ mod tests {
use crate::genesis_utils::create_genesis_block; use crate::genesis_utils::create_genesis_block;
use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS}; use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS};
use crate::voting_keypair::tests::new_vote_account; use crate::voting_keypair::tests::new_vote_account;
use solana_runtime::bank::{Bank, EpochSchedule, MINIMUM_SLOT_LENGTH}; use solana_runtime::bank::Bank;
use solana_runtime::epoch_schedule::{EpochSchedule, MINIMUM_SLOT_LENGTH};
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::sync::Arc; use std::sync::Arc;

View File

@ -451,7 +451,7 @@ impl Drop for LocalCluster {
mod test { mod test {
use super::*; use super::*;
use crate::storage_stage::STORAGE_ROTATE_TEST_COUNT; use crate::storage_stage::STORAGE_ROTATE_TEST_COUNT;
use solana_runtime::bank::MINIMUM_SLOT_LENGTH; use solana_runtime::epoch_schedule::MINIMUM_SLOT_LENGTH;
#[test] #[test]
fn test_local_cluster_start_and_exit() { fn test_local_cluster_start_and_exit() {

View File

@ -7,7 +7,7 @@ use crate::cluster_info::ClusterInfo;
use crate::result::Result; use crate::result::Result;
use crate::service::Service; use crate::service::Service;
use solana_metrics::datapoint; use solana_metrics::datapoint;
use solana_runtime::bank::EpochSchedule; use solana_runtime::epoch_schedule::EpochSchedule;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use std::collections::HashSet; use std::collections::HashSet;
use std::net::UdpSocket; use std::net::UdpSocket;

View File

@ -11,7 +11,7 @@ use crate::staking_utils;
use crate::streamer::BlobReceiver; use crate::streamer::BlobReceiver;
use crate::window_service::WindowService; use crate::window_service::WindowService;
use solana_metrics::{datapoint, inc_new_counter_info}; use solana_metrics::{datapoint, inc_new_counter_info};
use solana_runtime::bank::EpochSchedule; use solana_runtime::epoch_schedule::EpochSchedule;
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;

View File

@ -261,7 +261,7 @@ mod test {
use crate::packet::{index_blobs, Blob}; use crate::packet::{index_blobs, Blob};
use crate::service::Service; use crate::service::Service;
use crate::streamer::{blob_receiver, responder}; use crate::streamer::{blob_receiver, responder};
use solana_runtime::bank::{Bank, MINIMUM_SLOT_LENGTH}; use solana_runtime::epoch_schedule::MINIMUM_SLOT_LENGTH;
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use std::fs::remove_dir_all; use std::fs::remove_dir_all;
use std::net::UdpSocket; use std::net::UdpSocket;

View File

@ -6,7 +6,7 @@ use solana::fullnode::FullnodeConfig;
use solana::gossip_service::discover_nodes; use solana::gossip_service::discover_nodes;
use solana::local_cluster::{ClusterConfig, LocalCluster}; use solana::local_cluster::{ClusterConfig, LocalCluster};
use solana::poh_service::PohServiceConfig; use solana::poh_service::PohServiceConfig;
use solana_runtime::bank::MINIMUM_SLOT_LENGTH; use solana_runtime::epoch_schedule::MINIMUM_SLOT_LENGTH;
use solana_sdk::timing; use solana_sdk::timing;
use std::time::Duration; use std::time::Duration;

View File

@ -17,7 +17,7 @@ use solana::storage_stage::StorageState;
use solana::storage_stage::STORAGE_ROTATE_TEST_COUNT; use solana::storage_stage::STORAGE_ROTATE_TEST_COUNT;
use solana::streamer; use solana::streamer;
use solana::tvu::{Sockets, Tvu}; use solana::tvu::{Sockets, Tvu};
use solana_runtime::bank::MINIMUM_SLOT_LENGTH; use solana_runtime::epoch_schedule::MINIMUM_SLOT_LENGTH;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction; use solana_sdk::system_transaction;
use std::fs::remove_dir_all; use std::fs::remove_dir_all;

View File

@ -6,6 +6,7 @@ use crate::accounts::{AccountLockType, Accounts};
use crate::accounts_db::{ErrorCounters, InstructionAccounts, InstructionLoaders}; use crate::accounts_db::{ErrorCounters, InstructionAccounts, InstructionLoaders};
use crate::accounts_index::Fork; use crate::accounts_index::Fork;
use crate::blockhash_queue::BlockhashQueue; use crate::blockhash_queue::BlockhashQueue;
use crate::epoch_schedule::EpochSchedule;
use crate::locked_accounts_results::LockedAccountsResults; use crate::locked_accounts_results::LockedAccountsResults;
use crate::message_processor::{MessageProcessor, ProcessInstruction}; use crate::message_processor::{MessageProcessor, ProcessInstruction};
use crate::status_cache::StatusCache; use crate::status_cache::StatusCache;
@ -23,111 +24,12 @@ use solana_sdk::signature::{Keypair, Signature};
use solana_sdk::system_transaction; use solana_sdk::system_transaction;
use solana_sdk::timing::{duration_as_ms, duration_as_us, MAX_RECENT_BLOCKHASHES}; use solana_sdk::timing::{duration_as_ms, duration_as_us, MAX_RECENT_BLOCKHASHES};
use solana_sdk::transaction::{Result, Transaction, TransactionError}; use solana_sdk::transaction::{Result, Transaction, TransactionError};
use solana_vote_api::vote_state::MAX_LOCKOUT_HISTORY;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cmp; use std::cmp;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::time::Instant; use std::time::Instant;
pub const MINIMUM_SLOT_LENGTH: usize = MAX_LOCKOUT_HISTORY + 1;
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub struct EpochSchedule {
/// The maximum number of slots in each epoch.
pub slots_per_epoch: u64,
/// A number of slots before slot_index 0. Used to calculate finalized staked nodes.
pub stakers_slot_offset: u64,
/// basically: log2(slots_per_epoch) - log2(MINIMUM_SLOT_LEN)
pub first_normal_epoch: u64,
/// basically: 2.pow(first_normal_epoch) - MINIMUM_SLOT_LEN
pub first_normal_slot: u64,
}
impl EpochSchedule {
pub fn new(slots_per_epoch: u64, stakers_slot_offset: u64, warmup: bool) -> Self {
assert!(slots_per_epoch >= MINIMUM_SLOT_LENGTH as u64);
let (first_normal_epoch, first_normal_slot) = if warmup {
let next_power_of_two = slots_per_epoch.next_power_of_two();
let log2_slots_per_epoch = next_power_of_two
.trailing_zeros()
.saturating_sub(MINIMUM_SLOT_LENGTH.trailing_zeros());
(
u64::from(log2_slots_per_epoch),
next_power_of_two.saturating_sub(MINIMUM_SLOT_LENGTH as u64),
)
} else {
(0, 0)
};
EpochSchedule {
slots_per_epoch,
stakers_slot_offset,
first_normal_epoch,
first_normal_slot,
}
}
/// get the length of the given epoch (in slots)
pub fn get_slots_in_epoch(&self, epoch: u64) -> u64 {
if epoch < self.first_normal_epoch {
2u64.pow(epoch as u32 + MINIMUM_SLOT_LENGTH.trailing_zeros() as u32)
} else {
self.slots_per_epoch
}
}
/// get the epoch for which the given slot should save off
/// information about stakers
pub fn get_stakers_epoch(&self, slot: u64) -> u64 {
if slot < self.first_normal_slot {
// until we get to normal slots, behave as if stakers_slot_offset == slots_per_epoch
self.get_epoch_and_slot_index(slot).0 + 1
} else {
self.first_normal_epoch
+ (slot - self.first_normal_slot + self.stakers_slot_offset) / self.slots_per_epoch
}
}
/// get epoch and offset into the epoch for the given slot
pub fn get_epoch_and_slot_index(&self, slot: u64) -> (u64, u64) {
if slot < self.first_normal_slot {
let epoch = (slot + MINIMUM_SLOT_LENGTH as u64 + 1)
.next_power_of_two()
.trailing_zeros()
- MINIMUM_SLOT_LENGTH.trailing_zeros()
- 1;
let epoch_len = 2u64.pow(epoch + MINIMUM_SLOT_LENGTH.trailing_zeros());
(
u64::from(epoch),
slot - (epoch_len - MINIMUM_SLOT_LENGTH as u64),
)
} else {
(
self.first_normal_epoch + ((slot - self.first_normal_slot) / self.slots_per_epoch),
(slot - self.first_normal_slot) % self.slots_per_epoch,
)
}
}
pub fn get_first_slot_in_epoch(&self, epoch: u64) -> u64 {
if epoch <= self.first_normal_epoch {
(2u64.pow(epoch as u32) - 1) * MINIMUM_SLOT_LENGTH as u64
} else {
(epoch - self.first_normal_epoch) * self.slots_per_epoch + self.first_normal_slot
}
}
pub fn get_last_slot_in_epoch(&self, epoch: u64) -> u64 {
self.get_first_slot_in_epoch(epoch) + self.get_slots_in_epoch(epoch) - 1
}
}
/// cache of staking information /// cache of staking information
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct Stakes { pub struct Stakes {
@ -1098,6 +1000,7 @@ impl Drop for Bank {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::epoch_schedule::MINIMUM_SLOT_LENGTH;
use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS}; use crate::genesis_utils::{create_genesis_block_with_leader, BOOTSTRAP_LEADER_LAMPORTS};
use solana_sdk::genesis_block::create_genesis_block; use solana_sdk::genesis_block::create_genesis_block;
use solana_sdk::hash; use solana_sdk::hash;

View File

@ -0,0 +1,99 @@
use solana_vote_api::vote_state::MAX_LOCKOUT_HISTORY;
pub const MINIMUM_SLOT_LENGTH: usize = MAX_LOCKOUT_HISTORY + 1;
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub struct EpochSchedule {
/// The maximum number of slots in each epoch.
pub slots_per_epoch: u64,
/// A number of slots before slot_index 0. Used to calculate finalized staked nodes.
pub stakers_slot_offset: u64,
/// basically: log2(slots_per_epoch) - log2(MINIMUM_SLOT_LEN)
pub first_normal_epoch: u64,
/// basically: 2.pow(first_normal_epoch) - MINIMUM_SLOT_LEN
pub first_normal_slot: u64,
}
impl EpochSchedule {
pub fn new(slots_per_epoch: u64, stakers_slot_offset: u64, warmup: bool) -> Self {
assert!(slots_per_epoch >= MINIMUM_SLOT_LENGTH as u64);
let (first_normal_epoch, first_normal_slot) = if warmup {
let next_power_of_two = slots_per_epoch.next_power_of_two();
let log2_slots_per_epoch = next_power_of_two
.trailing_zeros()
.saturating_sub(MINIMUM_SLOT_LENGTH.trailing_zeros());
(
u64::from(log2_slots_per_epoch),
next_power_of_two.saturating_sub(MINIMUM_SLOT_LENGTH as u64),
)
} else {
(0, 0)
};
EpochSchedule {
slots_per_epoch,
stakers_slot_offset,
first_normal_epoch,
first_normal_slot,
}
}
/// get the length of the given epoch (in slots)
pub fn get_slots_in_epoch(&self, epoch: u64) -> u64 {
if epoch < self.first_normal_epoch {
2u64.pow(epoch as u32 + MINIMUM_SLOT_LENGTH.trailing_zeros() as u32)
} else {
self.slots_per_epoch
}
}
/// get the epoch for which the given slot should save off
/// information about stakers
pub fn get_stakers_epoch(&self, slot: u64) -> u64 {
if slot < self.first_normal_slot {
// until we get to normal slots, behave as if stakers_slot_offset == slots_per_epoch
self.get_epoch_and_slot_index(slot).0 + 1
} else {
self.first_normal_epoch
+ (slot - self.first_normal_slot + self.stakers_slot_offset) / self.slots_per_epoch
}
}
/// get epoch and offset into the epoch for the given slot
pub fn get_epoch_and_slot_index(&self, slot: u64) -> (u64, u64) {
if slot < self.first_normal_slot {
let epoch = (slot + MINIMUM_SLOT_LENGTH as u64 + 1)
.next_power_of_two()
.trailing_zeros()
- MINIMUM_SLOT_LENGTH.trailing_zeros()
- 1;
let epoch_len = 2u64.pow(epoch + MINIMUM_SLOT_LENGTH.trailing_zeros());
(
u64::from(epoch),
slot - (epoch_len - MINIMUM_SLOT_LENGTH as u64),
)
} else {
(
self.first_normal_epoch + ((slot - self.first_normal_slot) / self.slots_per_epoch),
(slot - self.first_normal_slot) % self.slots_per_epoch,
)
}
}
pub fn get_first_slot_in_epoch(&self, epoch: u64) -> u64 {
if epoch <= self.first_normal_epoch {
(2u64.pow(epoch as u32) - 1) * MINIMUM_SLOT_LENGTH as u64
} else {
(epoch - self.first_normal_epoch) * self.slots_per_epoch + self.first_normal_slot
}
}
pub fn get_last_slot_in_epoch(&self, epoch: u64) -> u64 {
self.get_first_slot_in_epoch(epoch) + self.get_slots_in_epoch(epoch) - 1
}
}

View File

@ -6,6 +6,7 @@ pub mod bank;
pub mod bank_client; pub mod bank_client;
mod blockhash_queue; mod blockhash_queue;
pub mod bloom; pub mod bloom;
pub mod epoch_schedule;
pub mod genesis_utils; pub mod genesis_utils;
pub mod loader_utils; pub mod loader_utils;
pub mod locked_accounts_results; pub mod locked_accounts_results;