From 208dd1de3afc9c1f9d39ace9a65ee2834f288dbf Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 18 Sep 2020 22:21:44 -0700 Subject: [PATCH] Move TestValidator into its own module --- cli/tests/deploy.rs | 2 +- cli/tests/nonce.rs | 2 +- cli/tests/request_airdrop.rs | 2 +- cli/tests/stake.rs | 2 +- cli/tests/transfer.rs | 2 +- cli/tests/vote.rs | 2 +- core/src/lib.rs | 1 + core/src/test_validator.rs | 103 +++++++++++++++++++++++++++++++++++ core/src/validator.rs | 95 +------------------------------- core/tests/client.rs | 2 +- core/tests/rpc.rs | 2 +- tokens/tests/commands.rs | 2 +- 12 files changed, 115 insertions(+), 102 deletions(-) create mode 100644 core/src/test_validator.rs diff --git a/cli/tests/deploy.rs b/cli/tests/deploy.rs index f605d5658e..3161e17f8d 100644 --- a/cli/tests/deploy.rs +++ b/cli/tests/deploy.rs @@ -1,7 +1,7 @@ use serde_json::Value; use solana_cli::cli::{process_command, CliCommand, CliConfig}; use solana_client::rpc_client::RpcClient; -use solana_core::validator::TestValidator; +use solana_core::test_validator::TestValidator; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ bpf_loader, diff --git a/cli/tests/nonce.rs b/cli/tests/nonce.rs index 12f786e42b..baec3ed00f 100644 --- a/cli/tests/nonce.rs +++ b/cli/tests/nonce.rs @@ -11,7 +11,7 @@ use solana_cli::{ }; use solana_client::rpc_client::RpcClient; use solana_core::contact_info::ContactInfo; -use solana_core::validator::{TestValidator, TestValidatorOptions}; +use solana_core::test_validator::{TestValidator, TestValidatorOptions}; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ commitment_config::CommitmentConfig, diff --git a/cli/tests/request_airdrop.rs b/cli/tests/request_airdrop.rs index fd04d3441e..d6764b535e 100644 --- a/cli/tests/request_airdrop.rs +++ b/cli/tests/request_airdrop.rs @@ -1,6 +1,6 @@ use solana_cli::cli::{process_command, CliCommand, CliConfig}; use solana_client::rpc_client::RpcClient; -use solana_core::validator::TestValidator; +use solana_core::test_validator::TestValidator; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair}; use std::{fs::remove_dir_all, sync::mpsc::channel}; diff --git a/cli/tests/stake.rs b/cli/tests/stake.rs index 1975616762..6b6843aaeb 100644 --- a/cli/tests/stake.rs +++ b/cli/tests/stake.rs @@ -10,7 +10,7 @@ use solana_cli::{ test_utils::{check_ready, check_recent_balance}, }; use solana_client::rpc_client::RpcClient; -use solana_core::validator::{TestValidator, TestValidatorOptions}; +use solana_core::test_validator::{TestValidator, TestValidatorOptions}; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ account_utils::StateMut, diff --git a/cli/tests/transfer.rs b/cli/tests/transfer.rs index 86bb6fd54d..3c849b026a 100644 --- a/cli/tests/transfer.rs +++ b/cli/tests/transfer.rs @@ -10,7 +10,7 @@ use solana_cli::{ test_utils::{check_ready, check_recent_balance}, }; use solana_client::rpc_client::RpcClient; -use solana_core::validator::{TestValidator, TestValidatorOptions}; +use solana_core::test_validator::{TestValidator, TestValidatorOptions}; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ commitment_config::CommitmentConfig, diff --git a/cli/tests/vote.rs b/cli/tests/vote.rs index 3273adf75f..bb32dba825 100644 --- a/cli/tests/vote.rs +++ b/cli/tests/vote.rs @@ -5,7 +5,7 @@ use solana_cli::{ test_utils::check_recent_balance, }; use solana_client::rpc_client::RpcClient; -use solana_core::validator::TestValidator; +use solana_core::test_validator::TestValidator; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ account_utils::StateMut, diff --git a/core/src/lib.rs b/core/src/lib.rs index 7a555e39ae..492610a59b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -67,6 +67,7 @@ pub mod sigverify; pub mod sigverify_shreds; pub mod sigverify_stage; pub mod snapshot_packager_service; +pub mod test_validator; pub mod tpu; pub mod transaction_status_service; pub mod tree_diff; diff --git a/core/src/test_validator.rs b/core/src/test_validator.rs new file mode 100644 index 0000000000..0873b3ea8d --- /dev/null +++ b/core/src/test_validator.rs @@ -0,0 +1,103 @@ +use crate::{ + cluster_info::Node, + contact_info::ContactInfo, + gossip_service::discover_cluster, + validator::{Validator, ValidatorConfig}, +}; +use solana_ledger::create_new_tmp_ledger; +use solana_sdk::{ + hash::Hash, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; +use std::{path::PathBuf, sync::Arc}; + +pub struct TestValidator { + pub server: Validator, + pub leader_data: ContactInfo, + pub alice: Keypair, + pub ledger_path: PathBuf, + pub genesis_hash: Hash, + pub vote_pubkey: Pubkey, +} + +pub struct TestValidatorOptions { + pub fees: u64, + pub bootstrap_validator_lamports: u64, + pub mint_lamports: u64, +} + +impl Default for TestValidatorOptions { + fn default() -> Self { + use solana_ledger::genesis_utils::BOOTSTRAP_VALIDATOR_LAMPORTS; + TestValidatorOptions { + fees: 0, + bootstrap_validator_lamports: BOOTSTRAP_VALIDATOR_LAMPORTS, + mint_lamports: 1_000_000, + } + } +} + +impl TestValidator { + pub fn run() -> Self { + Self::run_with_options(TestValidatorOptions::default()) + } + + pub fn run_with_options(options: TestValidatorOptions) -> Self { + use solana_ledger::genesis_utils::{ + create_genesis_config_with_leader_ex, GenesisConfigInfo, + }; + use solana_sdk::fee_calculator::FeeRateGovernor; + + let TestValidatorOptions { + fees, + bootstrap_validator_lamports, + mint_lamports, + } = options; + let node_keypair = Arc::new(Keypair::new()); + let node = Node::new_localhost_with_pubkey(&node_keypair.pubkey()); + let contact_info = node.info.clone(); + + let GenesisConfigInfo { + mut genesis_config, + mint_keypair, + voting_keypair, + } = create_genesis_config_with_leader_ex( + mint_lamports, + &contact_info.id, + &Keypair::new(), + &Pubkey::new_rand(), + 42, + bootstrap_validator_lamports, + ); + genesis_config.rent.lamports_per_byte_year = 1; + genesis_config.rent.exemption_threshold = 1.0; + genesis_config.fee_rate_governor = FeeRateGovernor::new(fees, 0); + + let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config); + + let config = ValidatorConfig { + rpc_addrs: Some((node.info.rpc, node.info.rpc_pubsub, node.info.rpc_banks)), + ..ValidatorConfig::default() + }; + let vote_pubkey = voting_keypair.pubkey(); + let node = Validator::new( + node, + &node_keypair, + &ledger_path, + &voting_keypair.pubkey(), + vec![Arc::new(voting_keypair)], + None, + &config, + ); + discover_cluster(&contact_info.gossip, 1).expect("Node startup failed"); + TestValidator { + server: node, + leader_data: contact_info, + alice: mint_keypair, + ledger_path, + genesis_hash: blockhash, + vote_pubkey, + } + } +} diff --git a/core/src/validator.rs b/core/src/validator.rs index 57520a6c31..2541969337 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -8,7 +8,7 @@ use crate::{ completed_data_sets_service::CompletedDataSetsService, consensus::{reconcile_blockstore_roots_with_tower, Tower, TowerError}, contact_info::ContactInfo, - gossip_service::{discover_cluster, GossipService}, + gossip_service::GossipService, poh_recorder::{PohRecorder, GRACE_TICKS_FACTOR, MAX_GRACE_SLOTS}, poh_service::PohService, rewards_recorder_service::{RewardsRecorderSender, RewardsRecorderService}, @@ -32,7 +32,6 @@ use solana_ledger::{ blockstore::{Blockstore, BlockstoreSignals, CompletedSlotsReceiver, PurgeType}, blockstore_db::BlockstoreRecoveryMode, blockstore_processor::{self, TransactionStatusSender}, - create_new_tmp_ledger, leader_schedule::FixedSchedule, leader_schedule_cache::LeaderScheduleCache, }; @@ -974,96 +973,6 @@ fn wait_for_supermajority( false } -pub struct TestValidator { - pub server: Validator, - pub leader_data: ContactInfo, - pub alice: Keypair, - pub ledger_path: PathBuf, - pub genesis_hash: Hash, - pub vote_pubkey: Pubkey, -} - -pub struct TestValidatorOptions { - pub fees: u64, - pub bootstrap_validator_lamports: u64, - pub mint_lamports: u64, -} - -impl Default for TestValidatorOptions { - fn default() -> Self { - use solana_ledger::genesis_utils::BOOTSTRAP_VALIDATOR_LAMPORTS; - TestValidatorOptions { - fees: 0, - bootstrap_validator_lamports: BOOTSTRAP_VALIDATOR_LAMPORTS, - mint_lamports: 1_000_000, - } - } -} - -impl TestValidator { - pub fn run() -> Self { - Self::run_with_options(TestValidatorOptions::default()) - } - - pub fn run_with_options(options: TestValidatorOptions) -> Self { - use solana_ledger::genesis_utils::{ - create_genesis_config_with_leader_ex, GenesisConfigInfo, - }; - use solana_sdk::fee_calculator::FeeRateGovernor; - - let TestValidatorOptions { - fees, - bootstrap_validator_lamports, - mint_lamports, - } = options; - let node_keypair = Arc::new(Keypair::new()); - let node = Node::new_localhost_with_pubkey(&node_keypair.pubkey()); - let contact_info = node.info.clone(); - - let GenesisConfigInfo { - mut genesis_config, - mint_keypair, - voting_keypair, - } = create_genesis_config_with_leader_ex( - mint_lamports, - &contact_info.id, - &Keypair::new(), - &Pubkey::new_rand(), - 42, - bootstrap_validator_lamports, - ); - genesis_config.rent.lamports_per_byte_year = 1; - genesis_config.rent.exemption_threshold = 1.0; - genesis_config.fee_rate_governor = FeeRateGovernor::new(fees, 0); - - let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config); - - let config = ValidatorConfig { - rpc_addrs: Some((node.info.rpc, node.info.rpc_pubsub, node.info.rpc_banks)), - ..ValidatorConfig::default() - }; - let vote_pubkey = voting_keypair.pubkey(); - let node = Validator::new( - node, - &node_keypair, - &ledger_path, - &voting_keypair.pubkey(), - vec![Arc::new(voting_keypair)], - None, - &config, - ); - discover_cluster(&contact_info.gossip, 1).expect("Node startup failed"); - TestValidator { - server: node, - leader_data: contact_info, - alice: mint_keypair, - ledger_path, - genesis_hash: blockhash, - vote_pubkey, - } - } -} - fn report_target_features() { warn!( "CUDA is {}abled", @@ -1186,7 +1095,7 @@ fn cleanup_accounts_path(account_path: &std::path::Path) { #[cfg(test)] mod tests { use super::*; - use solana_ledger::genesis_utils::create_genesis_config_with_leader; + use solana_ledger::{create_new_tmp_ledger, genesis_utils::create_genesis_config_with_leader}; use std::fs::remove_dir_all; #[test] diff --git a/core/tests/client.rs b/core/tests/client.rs index bffe2e913d..bebe1a7773 100644 --- a/core/tests/client.rs +++ b/core/tests/client.rs @@ -1,7 +1,7 @@ use solana_client::{pubsub_client::PubsubClient, rpc_client::RpcClient, rpc_response::SlotInfo}; use solana_core::{ rpc_pubsub_service::PubSubService, rpc_subscriptions::RpcSubscriptions, - validator::TestValidator, + test_validator::TestValidator, }; use solana_runtime::{ bank::Bank, diff --git a/core/tests/rpc.rs b/core/tests/rpc.rs index 59cc32ff36..61406587bd 100644 --- a/core/tests/rpc.rs +++ b/core/tests/rpc.rs @@ -13,7 +13,7 @@ use solana_client::{ rpc_response::{Response, RpcSignatureResult}, }; use solana_core::contact_info::ContactInfo; -use solana_core::{rpc_pubsub::gen_client::Client as PubsubClient, validator::TestValidator}; +use solana_core::{rpc_pubsub::gen_client::Client as PubsubClient, test_validator::TestValidator}; use solana_sdk::{ commitment_config::CommitmentConfig, hash::Hash, pubkey::Pubkey, signature::Signer, system_transaction, transaction::Transaction, diff --git a/tokens/tests/commands.rs b/tokens/tests/commands.rs index 9e9c9e4d5b..1570a69dae 100644 --- a/tokens/tests/commands.rs +++ b/tokens/tests/commands.rs @@ -1,5 +1,5 @@ use solana_banks_client::start_tcp_client; -use solana_core::validator::{TestValidator, TestValidatorOptions}; +use solana_core::test_validator::{TestValidator, TestValidatorOptions}; use solana_sdk::native_token::sol_to_lamports; use solana_tokens::commands::test_process_distribute_tokens_with_client; use std::fs::remove_dir_all;