Add LeaderScheduler constructor to Bank
By passing a config and not a Arc'ed LeaderScheduler, callers need to use `Bank::leader_scheduler` to access the scheduler. By using the new constructor, there should be no incentive to reach into the bank for that object.
This commit is contained in:
parent
1809277e05
commit
643384e1ec
18
src/bank.rs
18
src/bank.rs
|
@ -121,20 +121,28 @@ impl Default for Bank {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bank {
|
impl Bank {
|
||||||
pub fn new_with_leader_scheduler_config(
|
pub fn new_with_leader_scheduler(
|
||||||
genesis_block: &GenesisBlock,
|
genesis_block: &GenesisBlock,
|
||||||
leader_scheduler_config: &LeaderSchedulerConfig,
|
leader_scheduler: Arc<RwLock<LeaderScheduler>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut bank = Self::default();
|
let mut bank = Self::default();
|
||||||
bank.leader_scheduler =
|
bank.leader_scheduler = leader_scheduler;
|
||||||
Arc::new(RwLock::new(LeaderScheduler::new(leader_scheduler_config)));
|
|
||||||
bank.process_genesis_block(genesis_block);
|
bank.process_genesis_block(genesis_block);
|
||||||
bank.add_builtin_programs();
|
bank.add_builtin_programs();
|
||||||
bank
|
bank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_with_leader_scheduler_config(
|
||||||
|
genesis_block: &GenesisBlock,
|
||||||
|
leader_scheduler_config: &LeaderSchedulerConfig,
|
||||||
|
) -> Self {
|
||||||
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::new(leader_scheduler_config)));
|
||||||
|
Self::new_with_leader_scheduler(genesis_block, leader_scheduler)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(genesis_block: &GenesisBlock) -> Self {
|
pub fn new(genesis_block: &GenesisBlock) -> Self {
|
||||||
Self::new_with_leader_scheduler_config(genesis_block, &LeaderSchedulerConfig::default())
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::default()));
|
||||||
|
Self::new_with_leader_scheduler(genesis_block, leader_scheduler)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_subscriptions(&self, subscriptions: Arc<RpcSubscriptions>) {
|
pub fn set_subscriptions(&self, subscriptions: Arc<RpcSubscriptions>) {
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::cluster_info::{ClusterInfo, Node, NodeInfo};
|
||||||
use crate::counter::Counter;
|
use crate::counter::Counter;
|
||||||
use crate::genesis_block::GenesisBlock;
|
use crate::genesis_block::GenesisBlock;
|
||||||
use crate::gossip_service::GossipService;
|
use crate::gossip_service::GossipService;
|
||||||
use crate::leader_scheduler::LeaderSchedulerConfig;
|
use crate::leader_scheduler::{LeaderScheduler, LeaderSchedulerConfig};
|
||||||
use crate::poh_service::PohServiceConfig;
|
use crate::poh_service::PohServiceConfig;
|
||||||
use crate::rpc::JsonRpcService;
|
use crate::rpc::JsonRpcService;
|
||||||
use crate::rpc_pubsub::PubSubService;
|
use crate::rpc_pubsub::PubSubService;
|
||||||
|
@ -122,6 +122,9 @@ impl Fullnode {
|
||||||
let id = keypair.pubkey();
|
let id = keypair.pubkey();
|
||||||
assert_eq!(id, node.info.id);
|
assert_eq!(id, node.info.id);
|
||||||
|
|
||||||
|
let leader_scheduler = Arc::new(RwLock::new(LeaderScheduler::new(
|
||||||
|
&config.leader_scheduler_config,
|
||||||
|
)));
|
||||||
let (
|
let (
|
||||||
bank,
|
bank,
|
||||||
entry_height,
|
entry_height,
|
||||||
|
@ -129,11 +132,7 @@ impl Fullnode {
|
||||||
blocktree,
|
blocktree,
|
||||||
ledger_signal_sender,
|
ledger_signal_sender,
|
||||||
ledger_signal_receiver,
|
ledger_signal_receiver,
|
||||||
) = new_bank_from_ledger(
|
) = new_bank_from_ledger(ledger_path, &config.ledger_config(), &leader_scheduler);
|
||||||
ledger_path,
|
|
||||||
&config.ledger_config(),
|
|
||||||
&config.leader_scheduler_config,
|
|
||||||
);
|
|
||||||
|
|
||||||
info!("node info: {:?}", node.info);
|
info!("node info: {:?}", node.info);
|
||||||
info!("node entrypoint_info: {:?}", entrypoint_info_option);
|
info!("node entrypoint_info: {:?}", entrypoint_info_option);
|
||||||
|
@ -456,14 +455,14 @@ impl Fullnode {
|
||||||
pub fn new_bank_from_ledger(
|
pub fn new_bank_from_ledger(
|
||||||
ledger_path: &str,
|
ledger_path: &str,
|
||||||
ledger_config: &BlocktreeConfig,
|
ledger_config: &BlocktreeConfig,
|
||||||
leader_scheduler_config: &LeaderSchedulerConfig,
|
leader_scheduler: &Arc<RwLock<LeaderScheduler>>,
|
||||||
) -> (Bank, u64, Hash, Blocktree, SyncSender<bool>, Receiver<bool>) {
|
) -> (Bank, u64, Hash, Blocktree, SyncSender<bool>, Receiver<bool>) {
|
||||||
let (blocktree, ledger_signal_sender, ledger_signal_receiver) =
|
let (blocktree, ledger_signal_sender, ledger_signal_receiver) =
|
||||||
Blocktree::open_with_config_signal(ledger_path, ledger_config)
|
Blocktree::open_with_config_signal(ledger_path, ledger_config)
|
||||||
.expect("Expected to successfully open database ledger");
|
.expect("Expected to successfully open database ledger");
|
||||||
let genesis_block =
|
let genesis_block =
|
||||||
GenesisBlock::load(ledger_path).expect("Expected to successfully open genesis block");
|
GenesisBlock::load(ledger_path).expect("Expected to successfully open genesis block");
|
||||||
let bank = Bank::new_with_leader_scheduler_config(&genesis_block, leader_scheduler_config);
|
let bank = Bank::new_with_leader_scheduler(&genesis_block, leader_scheduler.clone());
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
info!("processing ledger...");
|
info!("processing ledger...");
|
||||||
|
@ -824,7 +823,7 @@ mod tests {
|
||||||
let (bank, entry_height, _, _, _, _) = new_bank_from_ledger(
|
let (bank, entry_height, _, _, _, _) = new_bank_from_ledger(
|
||||||
&validator_ledger_path,
|
&validator_ledger_path,
|
||||||
&BlocktreeConfig::default(),
|
&BlocktreeConfig::default(),
|
||||||
&LeaderSchedulerConfig::default(),
|
&Arc::new(RwLock::new(LeaderScheduler::default())),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(bank.tick_height() >= bank.leader_scheduler.read().unwrap().ticks_per_epoch);
|
assert!(bank.tick_height() >= bank.leader_scheduler.read().unwrap().ticks_per_epoch);
|
||||||
|
|
|
@ -368,7 +368,9 @@ mod test {
|
||||||
use crate::entry::{next_entry_mut, Entry};
|
use crate::entry::{next_entry_mut, Entry};
|
||||||
use crate::fullnode::new_bank_from_ledger;
|
use crate::fullnode::new_bank_from_ledger;
|
||||||
use crate::genesis_block::GenesisBlock;
|
use crate::genesis_block::GenesisBlock;
|
||||||
use crate::leader_scheduler::{make_active_set_entries, LeaderSchedulerConfig};
|
use crate::leader_scheduler::{
|
||||||
|
make_active_set_entries, LeaderScheduler, LeaderSchedulerConfig,
|
||||||
|
};
|
||||||
use crate::replay_stage::ReplayStage;
|
use crate::replay_stage::ReplayStage;
|
||||||
use crate::voting_keypair::VotingKeypair;
|
use crate::voting_keypair::VotingKeypair;
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
|
@ -395,6 +397,8 @@ mod test {
|
||||||
// Set up the LeaderScheduler so that my_id becomes the leader for epoch 1
|
// Set up the LeaderScheduler so that my_id becomes the leader for epoch 1
|
||||||
let ticks_per_slot = 16;
|
let ticks_per_slot = 16;
|
||||||
let leader_scheduler_config = LeaderSchedulerConfig::new(ticks_per_slot, 1, ticks_per_slot);
|
let leader_scheduler_config = LeaderSchedulerConfig::new(ticks_per_slot, 1, ticks_per_slot);
|
||||||
|
let leader_scheduler =
|
||||||
|
Arc::new(RwLock::new(LeaderScheduler::new(&leader_scheduler_config)));
|
||||||
|
|
||||||
// Create a ledger
|
// Create a ledger
|
||||||
let (
|
let (
|
||||||
|
@ -446,7 +450,7 @@ mod test {
|
||||||
// Set up the bank
|
// Set up the bank
|
||||||
let blocktree_config = BlocktreeConfig::new(ticks_per_slot);
|
let blocktree_config = BlocktreeConfig::new(ticks_per_slot);
|
||||||
let (bank, _entry_height, last_entry_id, blocktree, l_sender, l_receiver) =
|
let (bank, _entry_height, last_entry_id, blocktree, l_sender, l_receiver) =
|
||||||
new_bank_from_ledger(&my_ledger_path, &blocktree_config, &leader_scheduler_config);
|
new_bank_from_ledger(&my_ledger_path, &blocktree_config, &leader_scheduler);
|
||||||
|
|
||||||
// Set up the replay stage
|
// Set up the replay stage
|
||||||
let (rotation_sender, rotation_receiver) = channel();
|
let (rotation_sender, rotation_receiver) = channel();
|
||||||
|
@ -553,7 +557,7 @@ mod test {
|
||||||
new_bank_from_ledger(
|
new_bank_from_ledger(
|
||||||
&my_ledger_path,
|
&my_ledger_path,
|
||||||
&BlocktreeConfig::default(),
|
&BlocktreeConfig::default(),
|
||||||
&LeaderSchedulerConfig::default(),
|
&Arc::new(RwLock::new(LeaderScheduler::default())),
|
||||||
);
|
);
|
||||||
|
|
||||||
let bank = Arc::new(bank);
|
let bank = Arc::new(bank);
|
||||||
|
@ -663,6 +667,8 @@ mod test {
|
||||||
|
|
||||||
let leader_scheduler_config =
|
let leader_scheduler_config =
|
||||||
LeaderSchedulerConfig::new(ticks_per_slot, slots_per_epoch, active_window_tick_length);
|
LeaderSchedulerConfig::new(ticks_per_slot, slots_per_epoch, active_window_tick_length);
|
||||||
|
let leader_scheduler =
|
||||||
|
Arc::new(RwLock::new(LeaderScheduler::new(&leader_scheduler_config)));
|
||||||
|
|
||||||
// Set up the cluster info
|
// Set up the cluster info
|
||||||
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
let cluster_info_me = Arc::new(RwLock::new(ClusterInfo::new(my_node.info.clone())));
|
||||||
|
@ -672,7 +678,7 @@ mod test {
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
{
|
{
|
||||||
let (bank, _entry_height, last_entry_id, blocktree, l_sender, l_receiver) =
|
let (bank, _entry_height, last_entry_id, blocktree, l_sender, l_receiver) =
|
||||||
new_bank_from_ledger(&my_ledger_path, &blocktree_config, &leader_scheduler_config);
|
new_bank_from_ledger(&my_ledger_path, &blocktree_config, &leader_scheduler);
|
||||||
|
|
||||||
let meta = blocktree
|
let meta = blocktree
|
||||||
.meta(0)
|
.meta(0)
|
||||||
|
|
|
@ -8,7 +8,7 @@ use solana::cluster_info::{Node, NodeInfo};
|
||||||
use solana::entry::{reconstruct_entries_from_blobs, Entry};
|
use solana::entry::{reconstruct_entries_from_blobs, Entry};
|
||||||
use solana::fullnode::{new_bank_from_ledger, Fullnode, FullnodeConfig, FullnodeReturnType};
|
use solana::fullnode::{new_bank_from_ledger, Fullnode, FullnodeConfig, FullnodeReturnType};
|
||||||
use solana::gossip_service::{converge, make_listening_node};
|
use solana::gossip_service::{converge, make_listening_node};
|
||||||
use solana::leader_scheduler::{make_active_set_entries, LeaderSchedulerConfig};
|
use solana::leader_scheduler::{make_active_set_entries, LeaderScheduler, LeaderSchedulerConfig};
|
||||||
use solana::result;
|
use solana::result;
|
||||||
use solana::service::Service;
|
use solana::service::Service;
|
||||||
use solana::thin_client::{poll_gossip_for_leader, retry_get_balance};
|
use solana::thin_client::{poll_gossip_for_leader, retry_get_balance};
|
||||||
|
@ -1010,7 +1010,7 @@ fn test_leader_to_validator_transition() {
|
||||||
let bank = new_bank_from_ledger(
|
let bank = new_bank_from_ledger(
|
||||||
&leader_ledger_path,
|
&leader_ledger_path,
|
||||||
&BlocktreeConfig::default(),
|
&BlocktreeConfig::default(),
|
||||||
&LeaderSchedulerConfig::default(),
|
&Arc::new(RwLock::new(LeaderScheduler::default())),
|
||||||
)
|
)
|
||||||
.0;
|
.0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue