From 43b82b31e534a8328e83a9cbf1e0925ea43dc47d Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 25 Nov 2020 17:00:47 -0800 Subject: [PATCH] More TestValidator cleanup --- cli/tests/deploy.rs | 25 ++--- cli/tests/nonce.rs | 79 ++++---------- cli/tests/request_airdrop.rs | 20 ++-- cli/tests/stake.rs | 196 +++++++++++------------------------ cli/tests/transfer.rs | 53 +++------- cli/tests/vote.rs | 19 ++-- client/src/rpc_client.rs | 2 +- core/src/test_validator.rs | 149 ++++++++++++++++++-------- core/tests/client.rs | 17 +-- core/tests/rpc.rs | 79 ++++++-------- runtime/src/genesis_utils.rs | 66 ++++++------ tokens/src/commands.rs | 174 ++++++++++--------------------- tokens/tests/commands.rs | 18 +--- 13 files changed, 345 insertions(+), 552 deletions(-) diff --git a/cli/tests/deploy.rs b/cli/tests/deploy.rs index 88462866df..360a237dff 100644 --- a/cli/tests/deploy.rs +++ b/cli/tests/deploy.rs @@ -9,13 +9,7 @@ use solana_sdk::{ pubkey::Pubkey, signature::{Keypair, Signer}, }; -use std::{ - fs::{remove_dir_all, File}, - io::Read, - path::PathBuf, - str::FromStr, - sync::mpsc::channel, -}; +use std::{fs::File, io::Read, path::PathBuf, str::FromStr, sync::mpsc::channel}; #[test] fn test_cli_deploy_program() { @@ -27,19 +21,13 @@ fn test_cli_deploy_program() { pathbuf.push("noop"); pathbuf.set_extension("so"); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let mut file = File::open(pathbuf.to_str().unwrap()).unwrap(); let mut program_data = Vec::new(); @@ -50,7 +38,7 @@ fn test_cli_deploy_program() { let mut config = CliConfig::recent_for_tests(); let keypair = Keypair::new(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.command = CliCommand::Airdrop { faucet_host: None, faucet_port: faucet_addr.port(), @@ -153,6 +141,5 @@ fn test_cli_deploy_program() { assert_eq!(account2.executable, true); assert_eq!(account0.data, account2.data); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/cli/tests/nonce.rs b/cli/tests/nonce.rs index c059661314..e79c2f68c8 100644 --- a/cli/tests/nonce.rs +++ b/cli/tests/nonce.rs @@ -9,7 +9,6 @@ use solana_client::{ nonce_utils, rpc_client::RpcClient, }; -use solana_core::contact_info::ContactInfo; use solana_core::test_validator::TestValidator; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ @@ -19,69 +18,38 @@ use solana_sdk::{ signature::{keypair_from_seed, Keypair, Signer}, system_program, }; -use std::{fs::remove_dir_all, sync::mpsc::channel}; +use std::sync::mpsc::channel; #[test] fn test_nonce() { - solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - - full_battery_tests(leader_data, alice, None, false); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + full_battery_tests(TestValidator::with_no_fees(), None, false); } #[test] fn test_nonce_with_seed() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - - full_battery_tests(leader_data, alice, Some(String::from("seed")), false); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + full_battery_tests( + TestValidator::with_no_fees(), + Some(String::from("seed")), + false, + ); } #[test] fn test_nonce_with_authority() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - - full_battery_tests(leader_data, alice, None, true); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + full_battery_tests(TestValidator::with_no_fees(), None, true); } fn full_battery_tests( - leader_data: ContactInfo, - alice: Keypair, + test_validator: TestValidator, seed: Option, use_nonce_authority: bool, ) { let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); - let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + let rpc_client = RpcClient::new(test_validator.rpc_url()); + let json_rpc_url = test_validator.rpc_url(); let mut config_payer = CliConfig::recent_for_tests(); config_payer.json_rpc_url = json_rpc_url.clone(); @@ -227,21 +195,17 @@ fn full_battery_tests( check_recent_balance(1000, &rpc_client, &config_payer.signers[0].pubkey()); check_recent_balance(800, &rpc_client, &nonce_account); check_recent_balance(200, &rpc_client, &payee_pubkey); + + test_validator.close(); } #[test] fn test_create_account_with_seed() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice: mint_keypair, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(mint_keypair, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); let offline_nonce_authority_signer = keypair_from_seed(&[1u8; 32]).unwrap(); @@ -250,7 +214,7 @@ fn test_create_account_with_seed() { let config = CliConfig::recent_for_tests(); // Setup accounts - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); request_and_confirm_airdrop( &rpc_client, &faucet_addr, @@ -282,8 +246,7 @@ fn test_create_account_with_seed() { check_recent_balance(0, &rpc_client, &nonce_address); let mut creator_config = CliConfig::recent_for_tests(); - creator_config.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + creator_config.json_rpc_url = test_validator.rpc_url(); creator_config.signers = vec![&online_nonce_creator_signer]; creator_config.command = CliCommand::CreateNonceAccount { nonce_account: 0, @@ -333,8 +296,7 @@ fn test_create_account_with_seed() { // And submit it let mut submit_config = CliConfig::recent_for_tests(); - submit_config.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + submit_config.json_rpc_url = test_validator.rpc_url(); submit_config.signers = vec![&authority_presigner]; submit_config.command = CliCommand::Transfer { amount: SpendAmount::Some(10), @@ -356,6 +318,5 @@ fn test_create_account_with_seed() { check_recent_balance(4000, &rpc_client, &online_nonce_creator_signer.pubkey()); check_recent_balance(10, &rpc_client, &to_address); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/cli/tests/request_airdrop.rs b/cli/tests/request_airdrop.rs index 66f86fce4a..fe460bbee4 100644 --- a/cli/tests/request_airdrop.rs +++ b/cli/tests/request_airdrop.rs @@ -3,23 +3,18 @@ use solana_client::rpc_client::RpcClient; 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}; +use std::sync::mpsc::channel; #[test] fn test_cli_request_airdrop() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); + let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); let mut bob_config = CliConfig::recent_for_tests(); - bob_config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + bob_config.json_rpc_url = test_validator.rpc_url(); bob_config.command = CliCommand::Airdrop { faucet_host: None, faucet_port: faucet_addr.port(), @@ -32,7 +27,7 @@ fn test_cli_request_airdrop() { let sig_response = process_command(&bob_config); sig_response.unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let balance = rpc_client .get_balance_with_commitment(&bob_config.signers[0].pubkey(), CommitmentConfig::recent()) @@ -40,6 +35,5 @@ fn test_cli_request_airdrop() { .value; assert_eq!(balance, 50); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/cli/tests/stake.rs b/cli/tests/stake.rs index 618158a439..a3d11b1b01 100644 --- a/cli/tests/stake.rs +++ b/cli/tests/stake.rs @@ -22,26 +22,20 @@ use solana_stake_program::{ stake_instruction::LockupArgs, stake_state::{Lockup, StakeAuthorize, StakeState}, }; -use std::{fs::remove_dir_all, sync::mpsc::channel}; +use std::sync::mpsc::channel; #[test] fn test_stake_delegation_force() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; request_and_confirm_airdrop( @@ -114,32 +108,23 @@ fn test_stake_delegation_force() { }; process_command(&config).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_seed_stake_delegation_and_deactivation() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - vote_pubkey, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let validator_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); let mut config_validator = CliConfig::recent_for_tests(); - config_validator.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config_validator.json_rpc_url = test_validator.rpc_url(); config_validator.signers = vec![&validator_keypair]; request_and_confirm_airdrop( @@ -180,7 +165,7 @@ fn test_seed_stake_delegation_and_deactivation() { // Delegate stake config_validator.command = CliCommand::DelegateStake { stake_account_pubkey: stake_address, - vote_account_pubkey: vote_pubkey, + vote_account_pubkey: test_validator.vote_account_address(), stake_authority: 0, force: true, sign_only: false, @@ -203,32 +188,23 @@ fn test_seed_stake_delegation_and_deactivation() { }; process_command(&config_validator).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_stake_delegation_and_deactivation() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - vote_pubkey, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let validator_keypair = Keypair::new(); let mut config_validator = CliConfig::recent_for_tests(); - config_validator.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config_validator.json_rpc_url = test_validator.rpc_url(); config_validator.signers = vec![&validator_keypair]; let stake_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); @@ -265,7 +241,7 @@ fn test_stake_delegation_and_deactivation() { config_validator.signers.pop(); config_validator.command = CliCommand::DelegateStake { stake_account_pubkey: stake_keypair.pubkey(), - vote_account_pubkey: vote_pubkey, + vote_account_pubkey: test_validator.vote_account_address(), stake_authority: 0, force: true, sign_only: false, @@ -288,37 +264,27 @@ fn test_stake_delegation_and_deactivation() { }; process_command(&config_validator).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_offline_stake_delegation_and_deactivation() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - vote_pubkey, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let mut config_validator = CliConfig::recent_for_tests(); - config_validator.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config_validator.json_rpc_url = test_validator.rpc_url(); let validator_keypair = Keypair::new(); config_validator.signers = vec![&validator_keypair]; let mut config_payer = CliConfig::recent_for_tests(); - config_payer.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config_payer.json_rpc_url = test_validator.rpc_url(); let stake_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); @@ -372,7 +338,7 @@ fn test_offline_stake_delegation_and_deactivation() { let (blockhash, _) = rpc_client.get_recent_blockhash().unwrap(); config_offline.command = CliCommand::DelegateStake { stake_account_pubkey: stake_keypair.pubkey(), - vote_account_pubkey: vote_pubkey, + vote_account_pubkey: test_validator.vote_account_address(), stake_authority: 0, force: true, sign_only: true, @@ -391,7 +357,7 @@ fn test_offline_stake_delegation_and_deactivation() { config_payer.signers = vec![&offline_presigner]; config_payer.command = CliCommand::DelegateStake { stake_account_pubkey: stake_keypair.pubkey(), - vote_account_pubkey: vote_pubkey, + vote_account_pubkey: test_validator.vote_account_address(), stake_authority: 0, force: true, sign_only: false, @@ -431,32 +397,24 @@ fn test_offline_stake_delegation_and_deactivation() { }; process_command(&config_payer).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_nonced_stake_delegation_and_deactivation() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - vote_pubkey, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let config_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); let mut config = CliConfig::recent_for_tests(); config.signers = vec![&config_keypair]; - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); let minimum_nonce_balance = rpc_client .get_minimum_balance_for_rent_exemption(NonceState::size()) @@ -515,7 +473,7 @@ fn test_nonced_stake_delegation_and_deactivation() { config.signers = vec![&config_keypair]; config.command = CliCommand::DelegateStake { stake_account_pubkey: stake_keypair.pubkey(), - vote_account_pubkey: vote_pubkey, + vote_account_pubkey: test_validator.vote_account_address(), stake_authority: 0, force: true, sign_only: false, @@ -554,30 +512,23 @@ fn test_nonced_stake_delegation_and_deactivation() { }; process_command(&config).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_stake_authorize() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; request_and_confirm_airdrop( @@ -833,8 +784,7 @@ fn test_stake_authorize() { .blockhash; assert_ne!(nonce_hash, new_nonce_hash); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] @@ -842,30 +792,23 @@ fn test_stake_authorize_with_fee_payer() { solana_logger::setup(); const SIG_FEE: u64 = 42; - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(SIG_FEE); + let test_validator = TestValidator::with_custom_fees(SIG_FEE); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let default_pubkey = default_signer.pubkey(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; let payer_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); let mut config_payer = CliConfig::recent_for_tests(); config_payer.signers = vec![&payer_keypair]; - config_payer.json_rpc_url = - format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config_payer.json_rpc_url = test_validator.rpc_url(); let payer_pubkey = config_payer.signers[0].pubkey(); let mut config_offline = CliConfig::recent_for_tests(); @@ -967,31 +910,24 @@ fn test_stake_authorize_with_fee_payer() { // and fee payer check_recent_balance(100_000 - SIG_FEE, &rpc_client, &offline_pubkey); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_stake_split() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let offline_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; let mut config_offline = CliConfig::recent_for_tests(); @@ -1118,31 +1054,24 @@ fn test_stake_split() { &split_account.pubkey(), ); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_stake_set_lockup() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let offline_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; let mut config_offline = CliConfig::recent_for_tests(); @@ -1387,31 +1316,23 @@ fn test_stake_set_lockup() { assert_eq!(current_lockup.epoch, lockup.epoch.unwrap()); assert_eq!(current_lockup.custodian, offline_pubkey); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_offline_nonced_create_stake_account_and_withdraw() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); - + let rpc_client = RpcClient::new(test_validator.rpc_url()); let mut config = CliConfig::recent_for_tests(); let default_signer = keypair_from_seed(&[1u8; 32]).unwrap(); config.signers = vec![&default_signer]; - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); let mut config_offline = CliConfig::recent_for_tests(); let offline_signer = keypair_from_seed(&[2u8; 32]).unwrap(); @@ -1608,6 +1529,5 @@ fn test_offline_nonced_create_stake_account_and_withdraw() { Pubkey::create_with_seed(&stake_pubkey, seed, &solana_stake_program::id()).unwrap(); check_recent_balance(50_000, &rpc_client, &seed_address); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/cli/tests/transfer.rs b/cli/tests/transfer.rs index 826641f661..6d51417634 100644 --- a/cli/tests/transfer.rs +++ b/cli/tests/transfer.rs @@ -17,30 +17,24 @@ use solana_sdk::{ pubkey::Pubkey, signature::{keypair_from_seed, Keypair, NullSigner, Signer}, }; -use std::{fs::remove_dir_all, sync::mpsc::channel}; +use std::sync::mpsc::channel; #[test] fn test_transfer() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice: mint_keypair, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(mint_keypair, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let default_offline_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; let sender_pubkey = config.signers[0].pubkey(); @@ -243,23 +237,16 @@ fn test_transfer() { check_recent_balance(28, &rpc_client, &offline_pubkey); check_recent_balance(40, &rpc_client, &recipient_pubkey); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_transfer_multisession_signing() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice: mint_keypair, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(mint_keypair, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); let to_pubkey = Pubkey::new(&[1u8; 32]); @@ -269,7 +256,7 @@ fn test_transfer_multisession_signing() { let config = CliConfig::recent_for_tests(); // Setup accounts - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); request_and_confirm_airdrop( &rpc_client, &faucet_addr, @@ -351,7 +338,7 @@ fn test_transfer_multisession_signing() { // Finally submit to the cluster let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&fee_payer_presigner, &from_presigner]; config.command = CliCommand::Transfer { amount: SpendAmount::Some(42), @@ -370,31 +357,24 @@ fn test_transfer_multisession_signing() { check_recent_balance(1, &rpc_client, &offline_fee_payer_signer.pubkey()); check_recent_balance(42, &rpc_client, &to_pubkey); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_transfer_all() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice: mint_keypair, - ledger_path, - .. - } = TestValidator::with_custom_fee(1); + let test_validator = TestValidator::with_custom_fees(1); let (sender, receiver) = channel(); - run_local_faucet(mint_keypair, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; let sender_pubkey = config.signers[0].pubkey(); @@ -423,6 +403,5 @@ fn test_transfer_all() { check_recent_balance(0, &rpc_client, &sender_pubkey); check_recent_balance(49_999, &rpc_client, &recipient_pubkey); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/cli/tests/vote.rs b/cli/tests/vote.rs index 545518f0fb..30bd9bf7fc 100644 --- a/cli/tests/vote.rs +++ b/cli/tests/vote.rs @@ -15,26 +15,20 @@ use solana_sdk::{ signature::{Keypair, Signer}, }; use solana_vote_program::vote_state::{VoteAuthorize, VoteState, VoteStateVersions}; -use std::{fs::remove_dir_all, sync::mpsc::channel}; +use std::sync::mpsc::channel; #[test] fn test_vote_authorize_and_withdraw() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); + run_local_faucet(test_validator.mint_keypair(), sender, None); let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let default_signer = Keypair::new(); let mut config = CliConfig::recent_for_tests(); - config.json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); + config.json_rpc_url = test_validator.rpc_url(); config.signers = vec![&default_signer]; request_and_confirm_airdrop( @@ -131,6 +125,5 @@ fn test_vote_authorize_and_withdraw() { }; process_command(&config).unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index dd7d0e9304..9438a93bfd 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -1437,7 +1437,7 @@ fn new_spinner_progress_bar() -> ProgressBar { progress_bar } -pub fn get_rpc_request_str(rpc_addr: SocketAddr, tls: bool) -> String { +fn get_rpc_request_str(rpc_addr: SocketAddr, tls: bool) -> String { if tls { format!("https://{}", rpc_addr) } else { diff --git a/core/src/test_validator.rs b/core/src/test_validator.rs index 62530f23ac..dbc58483d1 100644 --- a/core/src/test_validator.rs +++ b/core/src/test_validator.rs @@ -1,8 +1,6 @@ use { crate::{ cluster_info::Node, - contact_info::ContactInfo, - gossip_service::discover_cluster, validator::{Validator, ValidatorConfig}, }, solana_ledger::create_new_tmp_ledger, @@ -11,83 +9,111 @@ use { hash::Hash, native_token::sol_to_lamports, pubkey::Pubkey, + rent::Rent, signature::{Keypair, Signer}, }, - std::{path::PathBuf, sync::Arc}, + std::{fs::remove_dir_all, net::SocketAddr, 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, -} - -struct TestValidatorConfig { - fee_rate_governor: FeeRateGovernor, - validator_identity_lamports: u64, - validator_stake_lamports: u64, - mint_lamports: u64, +pub struct TestValidatorConfig { + pub fee_rate_governor: FeeRateGovernor, + pub mint_lamports: u64, + pub rent: Rent, + pub validator_identity_keypair: Keypair, + pub validator_identity_lamports: u64, + pub validator_stake_lamports: u64, } impl Default for TestValidatorConfig { fn default() -> Self { - TestValidatorConfig { - fee_rate_governor: FeeRateGovernor::new(0, 0), + Self { + fee_rate_governor: FeeRateGovernor::default(), + mint_lamports: sol_to_lamports(500_000_000.), + rent: Rent::default(), + validator_identity_keypair: Keypair::new(), validator_identity_lamports: sol_to_lamports(500.), validator_stake_lamports: sol_to_lamports(1.), - mint_lamports: sol_to_lamports(500_000_000.), } } } +pub struct TestValidator { + validator: Validator, + ledger_path: PathBuf, + preserve_ledger: bool, + + genesis_hash: Hash, + mint_keypair: Keypair, + vote_account_address: Pubkey, + + tpu: SocketAddr, + rpc_url: String, + rpc_pubsub_url: String, +} + +impl Default for TestValidator { + fn default() -> Self { + Self::new(TestValidatorConfig::default()) + } +} + impl TestValidator { - pub fn with_no_fee() -> Self { + pub fn with_no_fees() -> Self { Self::new(TestValidatorConfig { fee_rate_governor: FeeRateGovernor::new(0, 0), + rent: Rent { + lamports_per_byte_year: 1, + exemption_threshold: 1.0, + ..Rent::default() + }, ..TestValidatorConfig::default() }) } - pub fn with_custom_fee(target_lamports_per_signature: u64) -> Self { + pub fn with_custom_fees(target_lamports_per_signature: u64) -> Self { Self::new(TestValidatorConfig { fee_rate_governor: FeeRateGovernor::new(target_lamports_per_signature, 0), + rent: Rent { + lamports_per_byte_year: 1, + exemption_threshold: 1.0, + ..Rent::default() + }, ..TestValidatorConfig::default() }) } - fn new(config: TestValidatorConfig) -> Self { + pub fn new(config: TestValidatorConfig) -> Self { use solana_ledger::genesis_utils::{ create_genesis_config_with_leader_ex, GenesisConfigInfo, }; let TestValidatorConfig { fee_rate_governor, + mint_lamports, + rent, + validator_identity_keypair, validator_identity_lamports, validator_stake_lamports, - mint_lamports, } = config; - let node_keypair = Arc::new(Keypair::new()); - let node = Node::new_localhost_with_pubkey(&node_keypair.pubkey()); - let contact_info = node.info.clone(); + let validator_identity_keypair = Arc::new(validator_identity_keypair); + + let node = Node::new_localhost_with_pubkey(&validator_identity_keypair.pubkey()); let GenesisConfigInfo { mut genesis_config, mint_keypair, - voting_keypair, + voting_keypair: vote_account_keypair, } = create_genesis_config_with_leader_ex( mint_lamports, - &contact_info.id, + &node.info.id, &Keypair::new(), - &solana_sdk::pubkey::new_rand(), + &Keypair::new().pubkey(), validator_stake_lamports, validator_identity_lamports, solana_sdk::genesis_config::ClusterType::Development, ); - genesis_config.rent.lamports_per_byte_year = 1; - genesis_config.rent.exemption_threshold = 1.0; + + genesis_config.rent = rent; genesis_config.fee_rate_governor = fee_rate_governor; let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config); @@ -96,24 +122,63 @@ impl TestValidator { 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( + + let vote_account_address = vote_account_keypair.pubkey(); + let rpc_url = format!("http://{}:{}", node.info.rpc.ip(), node.info.rpc.port()); + let rpc_pubsub_url = format!("ws://{}/", node.info.rpc_pubsub); + let tpu = node.info.tpu; + + let validator = Validator::new( node, - &node_keypair, + &validator_identity_keypair, &ledger_path, - &voting_keypair.pubkey(), - vec![Arc::new(voting_keypair)], + &vote_account_keypair.pubkey(), + vec![Arc::new(vote_account_keypair)], None, &config, ); - discover_cluster(&contact_info.gossip, 1).expect("Node startup failed"); + TestValidator { - server: node, - leader_data: contact_info, - alice: mint_keypair, + validator, + vote_account_address, + mint_keypair, ledger_path, genesis_hash: blockhash, - vote_pubkey, + tpu, + rpc_url, + rpc_pubsub_url, + preserve_ledger: false, } } + + pub fn close(self) { + self.validator.close().unwrap(); + if !self.preserve_ledger { + remove_dir_all(&self.ledger_path).unwrap(); + } + } + + pub fn tpu(&self) -> &SocketAddr { + &self.tpu + } + + pub fn mint_keypair(&self) -> Keypair { + Keypair::from_bytes(&self.mint_keypair.to_bytes()).unwrap() + } + + pub fn rpc_url(&self) -> String { + self.rpc_url.clone() + } + + pub fn rpc_pubsub_url(&self) -> String { + self.rpc_pubsub_url.clone() + } + + pub fn genesis_hash(&self) -> Hash { + self.genesis_hash + } + + pub fn vote_account_address(&self) -> Pubkey { + self.vote_account_address + } } diff --git a/core/tests/client.rs b/core/tests/client.rs index e896af421a..94d26bd231 100644 --- a/core/tests/client.rs +++ b/core/tests/client.rs @@ -16,7 +16,6 @@ use solana_sdk::{ signature::Signer, system_transaction, }; use std::{ - fs::remove_dir_all, net::{IpAddr, SocketAddr}, sync::{ atomic::{AtomicBool, Ordering}, @@ -31,16 +30,12 @@ use systemstat::Ipv4Addr; fn test_rpc_client() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); + let bob_pubkey = solana_sdk::pubkey::new_rand(); - let client = RpcClient::new_socket(leader_data.rpc); + let client = RpcClient::new(test_validator.rpc_url()); assert_eq!( client.get_version().unwrap().solana_core, @@ -84,9 +79,7 @@ fn test_rpc_client() { client.get_balance(&alice.pubkey()).unwrap(), original_alice_balance - sol_to_lamports(20.0) ); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] diff --git a/core/tests/rpc.rs b/core/tests/rpc.rs index ee6ab7ae0a..6104edca22 100644 --- a/core/tests/rpc.rs +++ b/core/tests/rpc.rs @@ -9,10 +9,9 @@ use reqwest::{self, header::CONTENT_TYPE}; use serde_json::{json, Value}; use solana_account_decoder::UiAccount; use solana_client::{ - rpc_client::{get_rpc_request_str, RpcClient}, + rpc_client::RpcClient, rpc_response::{Response, RpcSignatureResult}, }; -use solana_core::contact_info::ContactInfo; use solana_core::{rpc_pubsub::gen_client::Client as PubsubClient, test_validator::TestValidator}; use solana_sdk::{ commitment_config::CommitmentConfig, hash::Hash, signature::Signer, system_transaction, @@ -20,7 +19,6 @@ use solana_sdk::{ }; use std::{ collections::HashSet, - fs::remove_dir_all, net::UdpSocket, sync::mpsc::channel, thread::sleep, @@ -39,12 +37,10 @@ macro_rules! json_req { }} } -fn post_rpc(request: Value, data: &ContactInfo) -> Value { +fn post_rpc(request: Value, rpc_url: &str) -> Value { let client = reqwest::blocking::Client::new(); - let rpc_addr = data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); let response = client - .post(&rpc_string) + .post(rpc_url) .header(CONTENT_TYPE, "application/json") .body(request.to_string()) .send() @@ -56,17 +52,14 @@ fn post_rpc(request: Value, data: &ContactInfo) -> Value { fn test_rpc_send_tx() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); + let rpc_url = test_validator.rpc_url(); + let bob_pubkey = solana_sdk::pubkey::new_rand(); let req = json_req!("getRecentBlockhash", json!([])); - let json = post_rpc(req, &leader_data); + let json = post_rpc(req, &rpc_url); let blockhash: Hash = json["result"]["value"]["blockhash"] .as_str() @@ -79,7 +72,7 @@ fn test_rpc_send_tx() { let serialized_encoded_tx = bs58::encode(serialize(&tx).unwrap()).into_string(); let req = json_req!("sendTransaction", json!([serialized_encoded_tx])); - let json: Value = post_rpc(req, &leader_data); + let json: Value = post_rpc(req, &rpc_url); let signature = &json["result"]; @@ -88,7 +81,7 @@ fn test_rpc_send_tx() { let request = json_req!("confirmTransaction", [signature]); for _ in 0..solana_sdk::clock::DEFAULT_TICKS_PER_SLOT { - let json = post_rpc(request.clone(), &leader_data); + let json = post_rpc(request.clone(), &rpc_url); if true == json["result"]["value"] { confirmed_tx = true; @@ -111,70 +104,62 @@ fn test_rpc_send_tx() { "getAccountInfo", json!([bs58::encode(bob_pubkey).into_string(), config]) ); - let json: Value = post_rpc(req, &leader_data); + let json: Value = post_rpc(req, &rpc_url); info!("{:?}", json["result"]["value"]); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_rpc_invalid_requests() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - ledger_path, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); + let rpc_url = test_validator.rpc_url(); + let bob_pubkey = solana_sdk::pubkey::new_rand(); // test invalid get_balance request let req = json_req!("getBalance", json!(["invalid9999"])); - let json = post_rpc(req, &leader_data); + let json = post_rpc(req, &rpc_url); let the_error = json["error"]["message"].as_str().unwrap(); assert_eq!(the_error, "Invalid param: Invalid"); // test invalid get_account_info request let req = json_req!("getAccountInfo", json!(["invalid9999"])); - let json = post_rpc(req, &leader_data); + let json = post_rpc(req, &rpc_url); let the_error = json["error"]["message"].as_str().unwrap(); assert_eq!(the_error, "Invalid param: Invalid"); // test invalid get_account_info request let req = json_req!("getAccountInfo", json!([bob_pubkey.to_string()])); - let json = post_rpc(req, &leader_data); + let json = post_rpc(req, &rpc_url); let the_value = &json["result"]["value"]; assert!(the_value.is_null()); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_rpc_subscriptions() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - genesis_hash, - .. - } = TestValidator::with_no_fee(); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); - transactions_socket.connect(leader_data.tpu).unwrap(); + transactions_socket.connect(test_validator.tpu()).unwrap(); // Create transaction signatures to subscribe to let transactions: Vec = (0..1000) .map(|_| { - system_transaction::transfer(&alice, &solana_sdk::pubkey::new_rand(), 1, genesis_hash) + system_transaction::transfer( + &alice, + &solana_sdk::pubkey::new_rand(), + 1, + test_validator.genesis_hash(), + ) }) .collect(); let mut signature_set: HashSet = transactions @@ -195,11 +180,10 @@ fn test_rpc_subscriptions() { // Create the pub sub runtime let mut rt = Runtime::new().unwrap(); - let rpc_pubsub_url = format!("ws://{}/", leader_data.rpc_pubsub); // Subscribe to all signatures rt.spawn({ - let connect = ws::try_connect::(&rpc_pubsub_url).unwrap(); + let connect = ws::try_connect::(&test_validator.rpc_pubsub_url()).unwrap(); let signature_set = signature_set.clone(); connect .and_then(move |client| { @@ -256,7 +240,7 @@ fn test_rpc_subscriptions() { // Wait for signature subscriptions ready_receiver.recv_timeout(Duration::from_secs(2)).unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); + let rpc_client = RpcClient::new(test_validator.rpc_url()); let mut mint_balance = rpc_client .get_balance_with_commitment(&alice.pubkey(), CommitmentConfig::recent()) .unwrap() @@ -326,6 +310,5 @@ fn test_rpc_subscriptions() { } rt.shutdown_now().wait().unwrap(); - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } diff --git a/runtime/src/genesis_utils.rs b/runtime/src/genesis_utils.rs index ceecf6c82c..c35af7631d 100644 --- a/runtime/src/genesis_utils.rs +++ b/runtime/src/genesis_utils.rs @@ -14,8 +14,8 @@ use solana_stake_program::stake_state::StakeState; use solana_vote_program::vote_state; use std::borrow::Borrow; -// Default amount received by the bootstrap validator -const BOOTSTRAP_VALIDATOR_LAMPORTS: u64 = 42; +// Default amount received by the validator +const VALIDATOR_LAMPORTS: u64 = 42; // fun fact: rustc is very close to make this const fn. pub fn bootstrap_validator_stake_lamports() -> u64 { @@ -84,7 +84,7 @@ pub fn create_genesis_config_with_vote_accounts_and_cluster_type( &voting_keypairs[0].borrow().vote_keypair, &voting_keypairs[0].borrow().stake_keypair.pubkey(), stakes[0], - BOOTSTRAP_VALIDATOR_LAMPORTS, + VALIDATOR_LAMPORTS, cluster_type, ); @@ -94,7 +94,7 @@ pub fn create_genesis_config_with_vote_accounts_and_cluster_type( let stake_pubkey = validator_voting_keypairs.borrow().stake_keypair.pubkey(); // Create accounts - let node_account = Account::new(BOOTSTRAP_VALIDATOR_LAMPORTS, 0, &system_program::id()); + let node_account = Account::new(VALIDATOR_LAMPORTS, 0, &system_program::id()); let vote_account = vote_state::create_account(&vote_pubkey, &node_pubkey, 0, *stake); let stake_account = stake_state::create_account( &stake_pubkey, @@ -117,16 +117,16 @@ pub fn create_genesis_config_with_vote_accounts_and_cluster_type( pub fn create_genesis_config_with_leader( mint_lamports: u64, - bootstrap_validator_pubkey: &Pubkey, - bootstrap_validator_stake_lamports: u64, + validator_pubkey: &Pubkey, + validator_stake_lamports: u64, ) -> GenesisConfigInfo { create_genesis_config_with_leader_ex( mint_lamports, - bootstrap_validator_pubkey, + validator_pubkey, &Keypair::new(), &solana_sdk::pubkey::new_rand(), - bootstrap_validator_stake_lamports, - BOOTSTRAP_VALIDATOR_LAMPORTS, + validator_stake_lamports, + VALIDATOR_LAMPORTS, ClusterType::Development, ) } @@ -148,29 +148,30 @@ pub fn activate_all_features(genesis_config: &mut GenesisConfig) { pub fn create_genesis_config_with_leader_ex( mint_lamports: u64, - bootstrap_validator_pubkey: &Pubkey, - bootstrap_validator_voting_keypair: &Keypair, - bootstrap_validator_staking_pubkey: &Pubkey, - bootstrap_validator_stake_lamports: u64, - bootstrap_validator_lamports: u64, + validator_pubkey: &Pubkey, + validator_vote_account_keypair: &Keypair, + validator_stake_account_pubkey: &Pubkey, + validator_stake_lamports: u64, + validator_lamports: u64, cluster_type: ClusterType, ) -> GenesisConfigInfo { let mint_keypair = Keypair::new(); - let bootstrap_validator_vote_account = vote_state::create_account( - &bootstrap_validator_voting_keypair.pubkey(), - &bootstrap_validator_pubkey, + let validator_vote_account = vote_state::create_account( + &validator_vote_account_keypair.pubkey(), + &validator_pubkey, 0, - bootstrap_validator_stake_lamports, + validator_stake_lamports, ); - let rent = Rent::free(); + let fee_rate_governor = FeeRateGovernor::new(0, 0); // most tests can't handle transaction fees + let rent = Rent::free(); // most tests don't expect rent - let bootstrap_validator_stake_account = stake_state::create_account( - bootstrap_validator_staking_pubkey, - &bootstrap_validator_voting_keypair.pubkey(), - &bootstrap_validator_vote_account, + let validator_stake_account = stake_state::create_account( + validator_stake_account_pubkey, + &validator_vote_account_keypair.pubkey(), + &validator_vote_account, &rent, - bootstrap_validator_stake_lamports, + validator_stake_lamports, ); let accounts = [ @@ -179,23 +180,19 @@ pub fn create_genesis_config_with_leader_ex( Account::new(mint_lamports, 0, &system_program::id()), ), ( - *bootstrap_validator_pubkey, - Account::new(bootstrap_validator_lamports, 0, &system_program::id()), + *validator_pubkey, + Account::new(validator_lamports, 0, &system_program::id()), ), ( - bootstrap_validator_voting_keypair.pubkey(), - bootstrap_validator_vote_account, - ), - ( - *bootstrap_validator_staking_pubkey, - bootstrap_validator_stake_account, + validator_vote_account_keypair.pubkey(), + validator_vote_account, ), + (*validator_stake_account_pubkey, validator_stake_account), ] .iter() .cloned() .collect(); - let fee_rate_governor = FeeRateGovernor::new(0, 0); // most tests can't handle transaction fees let mut genesis_config = GenesisConfig { accounts, fee_rate_governor, @@ -212,7 +209,6 @@ pub fn create_genesis_config_with_leader_ex( GenesisConfigInfo { genesis_config, mint_keypair, - voting_keypair: Keypair::from_bytes(&bootstrap_validator_voting_keypair.to_bytes()) - .unwrap(), + voting_keypair: Keypair::from_bytes(&validator_vote_account_keypair.to_bytes()).unwrap(), } } diff --git a/tokens/src/commands.rs b/tokens/src/commands.rs index ce4446bade..a45ff59a9a 100644 --- a/tokens/src/commands.rs +++ b/tokens/src/commands.rs @@ -940,67 +940,60 @@ pub fn test_process_distribute_stake_with_client(client: &RpcClient, sender_keyp #[cfg(test)] mod tests { use super::*; - use solana_client::rpc_client::get_rpc_request_str; use solana_core::test_validator::TestValidator; use solana_sdk::{ clock::DEFAULT_MS_PER_SLOT, signature::{read_keypair_file, write_keypair_file}, }; use solana_stake_program::stake_instruction::StakeInstruction; - use std::fs::remove_dir_all; + + // This is a quick hack until TestValidator can be initialized with fees from block 0 + fn test_validator_block_0_fee_workaround(client: &RpcClient) { + while client + .get_recent_blockhash() + .unwrap() + .1 + .lamports_per_signature + == 0 + { + sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT)); + } + } #[test] fn test_process_token_allocations() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); test_process_distribute_tokens_with_client(&client, alice, None); - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_process_transfer_amount_allocations() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); test_process_distribute_tokens_with_client(&client, alice, Some(sol_to_lamports(1.5))); - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_process_stake_allocations() { - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_no_fees(); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); test_process_distribute_stake_with_client(&client, alice); - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] @@ -1309,28 +1302,16 @@ mod tests { fn test_check_payer_balances_distribute_tokens_single_payer() { let fees = 10_000; let fees_in_sol = lamports_to_sol(fees); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(fees); - let url = get_rpc_request_str(leader_data.rpc, false); + + let test_validator = TestValidator::with_custom_fees(fees); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); let sender_keypair_file = tmp_file_path("keypair_file", &alice.pubkey()); write_keypair_file(&alice, &sender_keypair_file).unwrap(); - // This is a quick hack until TestValidator can be initialized with fees from block 0 - while client - .get_recent_blockhash() - .unwrap() - .1 - .lamports_per_signature - == 0 - { - sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT)); - } + test_validator_block_0_fee_workaround(&client); let allocation_amount = 1000.0; @@ -1402,39 +1383,23 @@ mod tests { panic!("check_payer_balances should have errored"); } - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_check_payer_balances_distribute_tokens_separate_payers() { let fees = 10_000; let fees_in_sol = lamports_to_sol(fees); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(fees); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_custom_fees(fees); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); + test_validator_block_0_fee_workaround(&client); let sender_keypair_file = tmp_file_path("keypair_file", &alice.pubkey()); write_keypair_file(&alice, &sender_keypair_file).unwrap(); - // This is a quick hack until TestValidator can be initialized with fees from block 0 - while client - .get_recent_blockhash() - .unwrap() - .1 - .lamports_per_signature - == 0 - { - sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT)); - } - let allocation_amount = 1000.0; let funded_payer = Keypair::new(); @@ -1491,9 +1456,7 @@ mod tests { panic!("check_payer_balances should have errored"); } - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } fn initialize_stake_account( @@ -1540,30 +1503,15 @@ mod tests { fn test_check_payer_balances_distribute_stakes_single_payer() { let fees = 10_000; let fees_in_sol = lamports_to_sol(fees); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(fees); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_custom_fees(fees); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); + test_validator_block_0_fee_workaround(&client); let sender_keypair_file = tmp_file_path("keypair_file", &alice.pubkey()); write_keypair_file(&alice, &sender_keypair_file).unwrap(); - // This is a quick hack until TestValidator can be initialized with fees from block 0 - while client - .get_recent_blockhash() - .unwrap() - .1 - .lamports_per_signature - == 0 - { - sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT)); - } - let allocation_amount = 1000.0; let unlocked_sol = 1.0; let stake_args = initialize_stake_account( @@ -1657,39 +1605,23 @@ mod tests { panic!("check_payer_balances should have errored"); } - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } #[test] fn test_check_payer_balances_distribute_stakes_separate_payers() { let fees = 10_000; let fees_in_sol = lamports_to_sol(fees); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_custom_fee(fees); - let url = get_rpc_request_str(leader_data.rpc, false); + let test_validator = TestValidator::with_custom_fees(fees); + let alice = test_validator.mint_keypair(); + let url = test_validator.rpc_url(); + let client = RpcClient::new_with_commitment(url, CommitmentConfig::recent()); + test_validator_block_0_fee_workaround(&client); let sender_keypair_file = tmp_file_path("keypair_file", &alice.pubkey()); write_keypair_file(&alice, &sender_keypair_file).unwrap(); - // This is a quick hack until TestValidator can be initialized with fees from block 0 - while client - .get_recent_blockhash() - .unwrap() - .1 - .lamports_per_signature - == 0 - { - sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT)); - } - let allocation_amount = 1000.0; let unlocked_sol = 1.0; let stake_args = initialize_stake_account( @@ -1753,8 +1685,6 @@ mod tests { panic!("check_payer_balances should have errored"); } - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + test_validator.close(); } } diff --git a/tokens/tests/commands.rs b/tokens/tests/commands.rs index 5edd72e667..c33b749880 100644 --- a/tokens/tests/commands.rs +++ b/tokens/tests/commands.rs @@ -1,23 +1,15 @@ use solana_client::rpc_client::RpcClient; use solana_core::test_validator::TestValidator; use solana_tokens::commands::test_process_distribute_tokens_with_client; -use std::fs::remove_dir_all; #[test] fn test_process_distribute_with_rpc_client() { solana_logger::setup(); - let TestValidator { - server, - leader_data, - alice, - ledger_path, - .. - } = TestValidator::with_no_fee(); - let client = RpcClient::new_socket(leader_data.rpc); - test_process_distribute_tokens_with_client(&client, alice, None); + let test_validator = TestValidator::with_no_fees(); - // Explicit cleanup, otherwise "pure virtual method called" crash in Docker - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); + let client = RpcClient::new(test_validator.rpc_url()); + test_process_distribute_tokens_with_client(&client, test_validator.mint_keypair(), None); + + test_validator.close(); }