Improve TestValidator instantiation (#13627)

* Add TestValidator::new_with_fees constructor, and warning for low bootstrap_validator_lamports

* Add logging to solana-tokens integration test to help catch low bootstrap_validator_lamports in the future

* Reasonable TestValidator mint_lamports
This commit is contained in:
Tyera Eulberg
2020-11-16 23:27:36 -07:00
committed by GitHub
parent bde1e3d004
commit ef99689592
8 changed files with 62 additions and 50 deletions

View File

@@ -6,7 +6,9 @@ use crate::{
};
use solana_ledger::create_new_tmp_ledger;
use solana_sdk::{
clock::DEFAULT_DEV_SLOTS_PER_EPOCH,
hash::Hash,
native_token::sol_to_lamports,
pubkey::Pubkey,
signature::{Keypair, Signer},
};
@@ -33,7 +35,7 @@ impl Default for TestValidatorOptions {
TestValidatorOptions {
fees: 0,
bootstrap_validator_lamports: BOOTSTRAP_VALIDATOR_LAMPORTS,
mint_lamports: 1_000_000,
mint_lamports: sol_to_lamports(1_000_000.0),
}
}
}
@@ -43,6 +45,23 @@ impl TestValidator {
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()
})
}
/// 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 {
use solana_ledger::genesis_utils::{
create_genesis_config_with_leader_ex, GenesisConfigInfo,
@@ -58,6 +77,14 @@ impl TestValidator {
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,

View File

@@ -12,7 +12,8 @@ use solana_runtime::{
genesis_utils::{create_genesis_config, GenesisConfigInfo},
};
use solana_sdk::{
commitment_config::CommitmentConfig, rpc_port, signature::Signer, system_transaction,
commitment_config::CommitmentConfig, native_token::sol_to_lamports, rpc_port,
signature::Signer, system_transaction,
};
use std::{
fs::remove_dir_all,
@@ -50,11 +51,14 @@ fn test_rpc_client() {
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 1_000_000);
assert_eq!(
client.get_balance(&alice.pubkey()).unwrap(),
sol_to_lamports(1_000_000.0)
);
let (blockhash, _fee_calculator) = client.get_recent_blockhash().unwrap();
let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash);
let tx = system_transaction::transfer(&alice, &bob_pubkey, sol_to_lamports(20.0), blockhash);
let signature = client.send_transaction(&tx).unwrap();
let mut confirmed_tx = false;
@@ -75,8 +79,14 @@ fn test_rpc_client() {
assert!(confirmed_tx);
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 20);
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 999_980);
assert_eq!(
client.get_balance(&bob_pubkey).unwrap(),
sol_to_lamports(20.0)
);
assert_eq!(
client.get_balance(&alice.pubkey()).unwrap(),
sol_to_lamports(999_980.0)
);
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();