TestValidator public interface cleanup
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
use crate::{
|
||||
cluster_info::Node,
|
||||
contact_info::ContactInfo,
|
||||
gossip_service::discover_cluster,
|
||||
validator::{Validator, ValidatorConfig},
|
||||
use {
|
||||
crate::{
|
||||
cluster_info::Node,
|
||||
contact_info::ContactInfo,
|
||||
gossip_service::discover_cluster,
|
||||
validator::{Validator, ValidatorConfig},
|
||||
},
|
||||
solana_ledger::create_new_tmp_ledger,
|
||||
solana_sdk::{
|
||||
fee_calculator::FeeRateGovernor,
|
||||
hash::Hash,
|
||||
native_token::sol_to_lamports,
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signer},
|
||||
},
|
||||
std::{path::PathBuf, sync::Arc},
|
||||
};
|
||||
use solana_ledger::create_new_tmp_ledger;
|
||||
use solana_runtime::genesis_utils::{
|
||||
bootstrap_validator_stake_lamports, BOOTSTRAP_VALIDATOR_LAMPORTS,
|
||||
};
|
||||
use solana_sdk::{
|
||||
clock::DEFAULT_DEV_SLOTS_PER_EPOCH,
|
||||
hash::Hash,
|
||||
native_token::sol_to_lamports,
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signer},
|
||||
};
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
pub struct TestValidator {
|
||||
pub server: Validator,
|
||||
@@ -26,67 +25,54 @@ pub struct TestValidator {
|
||||
pub vote_pubkey: Pubkey,
|
||||
}
|
||||
|
||||
pub struct TestValidatorOptions {
|
||||
pub fees: u64,
|
||||
pub bootstrap_validator_lamports: u64,
|
||||
pub mint_lamports: u64,
|
||||
struct TestValidatorConfig {
|
||||
fee_rate_governor: FeeRateGovernor,
|
||||
validator_identity_lamports: u64,
|
||||
validator_stake_lamports: u64,
|
||||
mint_lamports: u64,
|
||||
}
|
||||
|
||||
impl Default for TestValidatorOptions {
|
||||
impl Default for TestValidatorConfig {
|
||||
fn default() -> Self {
|
||||
TestValidatorOptions {
|
||||
fees: 0,
|
||||
bootstrap_validator_lamports: BOOTSTRAP_VALIDATOR_LAMPORTS,
|
||||
mint_lamports: sol_to_lamports(1_000_000.0),
|
||||
TestValidatorConfig {
|
||||
fee_rate_governor: FeeRateGovernor::new(0, 0),
|
||||
validator_identity_lamports: sol_to_lamports(500.),
|
||||
validator_stake_lamports: sol_to_lamports(1.),
|
||||
mint_lamports: sol_to_lamports(500_000_000.),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TestValidator {
|
||||
pub fn run() -> Self {
|
||||
Self::run_with_options(TestValidatorOptions::default())
|
||||
}
|
||||
|
||||
/// Instantiates a TestValidator with custom fees. The bootstrap_validator_lamports will
|
||||
/// default to enough to cover 1 epoch of votes. This is an abitrary value based on current and
|
||||
/// foreseen uses of TestValidator. May need to be bumped if uses change in the future.
|
||||
pub fn run_with_fees(fees: u64) -> Self {
|
||||
let bootstrap_validator_lamports = fees * DEFAULT_DEV_SLOTS_PER_EPOCH * 5;
|
||||
Self::run_with_options(TestValidatorOptions {
|
||||
fees,
|
||||
bootstrap_validator_lamports,
|
||||
..TestValidatorOptions::default()
|
||||
pub fn with_no_fee() -> Self {
|
||||
Self::new(TestValidatorConfig {
|
||||
fee_rate_governor: FeeRateGovernor::new(0, 0),
|
||||
..TestValidatorConfig::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Instantiates a TestValidator with completely customized options.
|
||||
///
|
||||
/// Note: if `fees` are non-zero, be sure to set a value for `bootstrap_validator_lamports`
|
||||
/// that can cover enough vote transaction fees for the test. TestValidatorOptions::default()
|
||||
/// may not be sufficient.
|
||||
pub fn run_with_options(options: TestValidatorOptions) -> Self {
|
||||
pub fn with_custom_fee(target_lamports_per_signature: u64) -> Self {
|
||||
Self::new(TestValidatorConfig {
|
||||
fee_rate_governor: FeeRateGovernor::new(target_lamports_per_signature, 0),
|
||||
..TestValidatorConfig::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn new(config: TestValidatorConfig) -> 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,
|
||||
let TestValidatorConfig {
|
||||
fee_rate_governor,
|
||||
validator_identity_lamports,
|
||||
validator_stake_lamports,
|
||||
mint_lamports,
|
||||
} = options;
|
||||
} = config;
|
||||
let node_keypair = Arc::new(Keypair::new());
|
||||
let node = Node::new_localhost_with_pubkey(&node_keypair.pubkey());
|
||||
let contact_info = node.info.clone();
|
||||
|
||||
if fees > 0 && bootstrap_validator_lamports < fees * DEFAULT_DEV_SLOTS_PER_EPOCH {
|
||||
warn!(
|
||||
"TestValidator::bootstrap_validator_lamports less than one epoch. \
|
||||
Only enough to cover {:?} slots",
|
||||
bootstrap_validator_lamports / fees
|
||||
);
|
||||
}
|
||||
|
||||
let GenesisConfigInfo {
|
||||
mut genesis_config,
|
||||
mint_keypair,
|
||||
@@ -96,13 +82,13 @@ impl TestValidator {
|
||||
&contact_info.id,
|
||||
&Keypair::new(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
bootstrap_validator_stake_lamports(),
|
||||
bootstrap_validator_lamports,
|
||||
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.fee_rate_governor = FeeRateGovernor::new(fees, 0);
|
||||
genesis_config.fee_rate_governor = fee_rate_governor;
|
||||
|
||||
let (ledger_path, blockhash) = create_new_tmp_ledger!(&genesis_config);
|
||||
|
||||
|
@@ -37,7 +37,7 @@ fn test_rpc_client() {
|
||||
alice,
|
||||
ledger_path,
|
||||
..
|
||||
} = TestValidator::run();
|
||||
} = TestValidator::with_no_fee();
|
||||
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
||||
|
||||
let client = RpcClient::new_socket(leader_data.rpc);
|
||||
@@ -51,10 +51,7 @@ fn test_rpc_client() {
|
||||
|
||||
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
|
||||
|
||||
assert_eq!(
|
||||
client.get_balance(&alice.pubkey()).unwrap(),
|
||||
sol_to_lamports(1_000_000.0)
|
||||
);
|
||||
let original_alice_balance = client.get_balance(&alice.pubkey()).unwrap();
|
||||
|
||||
let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap();
|
||||
|
||||
@@ -85,7 +82,7 @@ fn test_rpc_client() {
|
||||
);
|
||||
assert_eq!(
|
||||
client.get_balance(&alice.pubkey()).unwrap(),
|
||||
sol_to_lamports(999_980.0)
|
||||
original_alice_balance - sol_to_lamports(20.0)
|
||||
);
|
||||
|
||||
server.close().unwrap();
|
||||
|
@@ -62,7 +62,7 @@ fn test_rpc_send_tx() {
|
||||
alice,
|
||||
ledger_path,
|
||||
..
|
||||
} = TestValidator::run();
|
||||
} = TestValidator::with_no_fee();
|
||||
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
||||
|
||||
let req = json_req!("getRecentBlockhash", json!([]));
|
||||
@@ -127,7 +127,7 @@ fn test_rpc_invalid_requests() {
|
||||
leader_data,
|
||||
ledger_path,
|
||||
..
|
||||
} = TestValidator::run();
|
||||
} = TestValidator::with_no_fee();
|
||||
let bob_pubkey = solana_sdk::pubkey::new_rand();
|
||||
|
||||
// test invalid get_balance request
|
||||
@@ -166,7 +166,7 @@ fn test_rpc_subscriptions() {
|
||||
ledger_path,
|
||||
genesis_hash,
|
||||
..
|
||||
} = TestValidator::run();
|
||||
} = TestValidator::with_no_fee();
|
||||
|
||||
let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
||||
transactions_socket.connect(leader_data.tpu).unwrap();
|
||||
|
Reference in New Issue
Block a user