2020-11-25 14:14:45 -08:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
cluster_info::Node,
|
2020-12-01 13:54:15 -08:00
|
|
|
gossip_service::discover_cluster,
|
2020-12-08 23:18:27 -08:00
|
|
|
rpc::JsonRpcConfig,
|
2020-11-25 14:14:45 -08:00
|
|
|
validator::{Validator, ValidatorConfig},
|
|
|
|
},
|
2020-12-08 23:18:27 -08:00
|
|
|
solana_ledger::{blockstore::create_new_ledger, create_new_tmp_ledger},
|
|
|
|
solana_runtime::{
|
|
|
|
bank_forks::{CompressionType, SnapshotConfig, SnapshotVersion},
|
|
|
|
genesis_utils::create_genesis_config_with_leader_ex,
|
|
|
|
hardened_unpack::MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
|
|
|
|
},
|
2020-11-25 14:14:45 -08:00
|
|
|
solana_sdk::{
|
|
|
|
fee_calculator::FeeRateGovernor,
|
|
|
|
native_token::sol_to_lamports,
|
|
|
|
pubkey::Pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
rent::Rent,
|
2020-12-08 23:18:27 -08:00
|
|
|
signature::{read_keypair_file, write_keypair_file, Keypair, Signer},
|
|
|
|
},
|
|
|
|
std::{
|
|
|
|
fs::remove_dir_all,
|
|
|
|
net::SocketAddr,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
2020-11-25 14:14:45 -08:00
|
|
|
},
|
2020-09-18 22:21:44 -07:00
|
|
|
};
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
pub struct TestValidatorGenesisConfig {
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fee_rate_governor: FeeRateGovernor,
|
2020-12-08 23:18:27 -08:00
|
|
|
pub mint_address: Pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
pub rent: Rent,
|
2020-09-18 22:21:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TestValidatorStartConfig {
|
|
|
|
pub rpc_config: JsonRpcConfig,
|
|
|
|
pub rpc_ports: Option<(u16, u16)>, // (JsonRpc, JsonRpcPubSub), None == random ports
|
2020-09-18 22:21:44 -07:00
|
|
|
}
|
|
|
|
|
2020-11-25 17:00:47 -08:00
|
|
|
pub struct TestValidator {
|
|
|
|
ledger_path: PathBuf,
|
|
|
|
rpc_pubsub_url: String,
|
2020-12-08 23:18:27 -08:00
|
|
|
rpc_url: String,
|
|
|
|
tpu: SocketAddr,
|
|
|
|
gossip: SocketAddr,
|
|
|
|
validator: Validator,
|
|
|
|
vote_account_address: Pubkey,
|
2020-11-25 17:00:47 -08:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:21:44 -07:00
|
|
|
impl TestValidator {
|
2020-12-08 23:18:27 -08:00
|
|
|
/// The default test validator is intended to be generically suitable for unit testing.
|
|
|
|
///
|
|
|
|
/// It uses a unique temporary ledger that is deleted on `close` and randomly assigned ports.
|
|
|
|
/// All test tokens will be minted into `mint_address`
|
|
|
|
///
|
|
|
|
/// This function panics on initialization failure.
|
|
|
|
pub fn new(mint_address: Pubkey) -> Self {
|
|
|
|
let ledger_path = Self::initialize_ledger(
|
|
|
|
None,
|
|
|
|
TestValidatorGenesisConfig {
|
|
|
|
fee_rate_governor: FeeRateGovernor::default(),
|
|
|
|
mint_address,
|
|
|
|
rent: Rent::default(),
|
2020-11-25 17:00:47 -08:00
|
|
|
},
|
2020-12-08 23:18:27 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
Self::start(&ledger_path, TestValidatorStartConfig::default()).unwrap()
|
2020-09-18 22:21:44 -07:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Create a `TestValidator` with no transaction fees and minimal rent.
|
|
|
|
///
|
|
|
|
/// This function panics on initialization failure.
|
|
|
|
pub fn with_no_fees(mint_address: Pubkey) -> Self {
|
|
|
|
let ledger_path = Self::initialize_ledger(
|
|
|
|
None,
|
|
|
|
TestValidatorGenesisConfig {
|
|
|
|
fee_rate_governor: FeeRateGovernor::new(0, 0),
|
|
|
|
mint_address,
|
|
|
|
rent: Rent {
|
|
|
|
lamports_per_byte_year: 1,
|
|
|
|
exemption_threshold: 1.0,
|
|
|
|
..Rent::default()
|
|
|
|
},
|
2020-11-25 17:00:47 -08:00
|
|
|
},
|
2020-12-08 23:18:27 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
Self::start(&ledger_path, TestValidatorStartConfig::default()).unwrap()
|
2020-11-16 23:27:36 -07:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Create a `TestValidator` with custom transaction fees and minimal rent.
|
|
|
|
///
|
|
|
|
/// This function panics on initialization failure.
|
|
|
|
pub fn with_custom_fees(mint_address: Pubkey, target_lamports_per_signature: u64) -> Self {
|
|
|
|
let ledger_path = Self::initialize_ledger(
|
|
|
|
None,
|
|
|
|
TestValidatorGenesisConfig {
|
|
|
|
fee_rate_governor: FeeRateGovernor::new(target_lamports_per_signature, 0),
|
|
|
|
mint_address,
|
|
|
|
rent: Rent {
|
|
|
|
lamports_per_byte_year: 1,
|
|
|
|
exemption_threshold: 1.0,
|
|
|
|
..Rent::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
Self::start(&ledger_path, TestValidatorStartConfig::default()).unwrap()
|
|
|
|
}
|
2020-09-18 22:21:44 -07:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Initialize the test validator's ledger directory
|
|
|
|
///
|
|
|
|
/// If `ledger_path` is `None`, a temporary ledger will be created. Otherwise the ledger will
|
|
|
|
/// be initialized in the provided directory.
|
|
|
|
///
|
|
|
|
/// Returns the path to the ledger directory.
|
|
|
|
pub fn initialize_ledger(
|
|
|
|
ledger_path: Option<&Path>,
|
|
|
|
config: TestValidatorGenesisConfig,
|
|
|
|
) -> Result<PathBuf, Box<dyn std::error::Error>> {
|
|
|
|
let TestValidatorGenesisConfig {
|
2020-11-25 14:14:45 -08:00
|
|
|
fee_rate_governor,
|
2020-12-08 23:18:27 -08:00
|
|
|
mint_address,
|
2020-11-25 17:00:47 -08:00
|
|
|
rent,
|
2020-11-25 14:14:45 -08:00
|
|
|
} = config;
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let validator_identity_keypair = Keypair::new();
|
|
|
|
let validator_vote_account = Keypair::new();
|
|
|
|
let validator_stake_account = Keypair::new();
|
|
|
|
let validator_identity_lamports = sol_to_lamports(500.);
|
|
|
|
let validator_stake_lamports = sol_to_lamports(1_000_000.);
|
|
|
|
let mint_lamports = sol_to_lamports(500_000_000.);
|
2020-09-18 22:21:44 -07:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let initial_accounts = solana_program_test::programs::spl_programs(&rent);
|
|
|
|
let genesis_config = create_genesis_config_with_leader_ex(
|
2020-09-18 22:21:44 -07:00
|
|
|
mint_lamports,
|
2020-12-08 23:18:27 -08:00
|
|
|
&mint_address,
|
|
|
|
&validator_identity_keypair.pubkey(),
|
|
|
|
&validator_vote_account.pubkey(),
|
|
|
|
&validator_stake_account.pubkey(),
|
2020-11-25 14:14:45 -08:00
|
|
|
validator_stake_lamports,
|
|
|
|
validator_identity_lamports,
|
2020-12-08 23:18:27 -08:00
|
|
|
fee_rate_governor,
|
|
|
|
rent,
|
2020-09-24 12:23:09 -07:00
|
|
|
solana_sdk::genesis_config::ClusterType::Development,
|
2020-12-08 23:18:27 -08:00
|
|
|
initial_accounts,
|
2020-09-18 22:21:44 -07:00
|
|
|
);
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let ledger_path = match ledger_path {
|
|
|
|
None => create_new_tmp_ledger!(&genesis_config).0,
|
|
|
|
Some(ledger_path) => {
|
|
|
|
let _ = create_new_ledger(
|
|
|
|
ledger_path,
|
|
|
|
&genesis_config,
|
|
|
|
MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
|
|
|
|
solana_ledger::blockstore_db::AccessType::PrimaryOnly,
|
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
format!(
|
|
|
|
"Failed to create ledger at {}: {}",
|
|
|
|
ledger_path.display(),
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
ledger_path.to_path_buf()
|
|
|
|
}
|
|
|
|
};
|
2020-09-18 22:21:44 -07:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
write_keypair_file(
|
|
|
|
&validator_identity_keypair,
|
|
|
|
ledger_path.join("validator-keypair.json").to_str().unwrap(),
|
|
|
|
)?;
|
|
|
|
write_keypair_file(
|
|
|
|
&validator_vote_account,
|
|
|
|
ledger_path
|
|
|
|
.join("vote-account-keypair.json")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)?;
|
2020-09-18 22:21:44 -07:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
Ok(ledger_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a TestValidator at the provided ledger directory
|
|
|
|
pub fn start(
|
|
|
|
ledger_path: &Path,
|
|
|
|
config: TestValidatorStartConfig,
|
|
|
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
|
|
|
let validator_identity_keypair =
|
|
|
|
read_keypair_file(ledger_path.join("validator-keypair.json").to_str().unwrap())?;
|
|
|
|
let validator_vote_account = read_keypair_file(
|
|
|
|
ledger_path
|
|
|
|
.join("vote-account-keypair.json")
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut node = Node::new_localhost_with_pubkey(&validator_identity_keypair.pubkey());
|
|
|
|
if let Some((rpc, rpc_pubsub)) = config.rpc_ports {
|
|
|
|
node.info.rpc = SocketAddr::new(node.info.gossip.ip(), rpc);
|
|
|
|
node.info.rpc_pubsub = SocketAddr::new(node.info.gossip.ip(), rpc_pubsub);
|
|
|
|
}
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let vote_account_address = validator_vote_account.pubkey();
|
2020-11-25 17:00:47 -08:00
|
|
|
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;
|
2020-12-01 13:54:15 -08:00
|
|
|
let gossip = node.info.gossip;
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
let validator_config = ValidatorConfig {
|
|
|
|
rpc_addrs: Some((node.info.rpc, node.info.rpc_pubsub)),
|
|
|
|
rpc_config: config.rpc_config,
|
|
|
|
accounts_hash_interval_slots: 100,
|
|
|
|
account_paths: vec![ledger_path.join("accounts")],
|
|
|
|
poh_verify: false, // Skip PoH verification of ledger on startup for speed
|
|
|
|
snapshot_config: Some(SnapshotConfig {
|
|
|
|
snapshot_interval_slots: 100,
|
|
|
|
snapshot_path: ledger_path.join("snapshot"),
|
|
|
|
snapshot_package_output_path: ledger_path.to_path_buf(),
|
|
|
|
compression: CompressionType::NoCompression,
|
|
|
|
snapshot_version: SnapshotVersion::default(),
|
|
|
|
}),
|
|
|
|
..ValidatorConfig::default()
|
|
|
|
};
|
|
|
|
|
2020-11-25 17:00:47 -08:00
|
|
|
let validator = Validator::new(
|
2020-09-18 22:21:44 -07:00
|
|
|
node,
|
2020-12-08 23:18:27 -08:00
|
|
|
&Arc::new(validator_identity_keypair),
|
2020-09-18 22:21:44 -07:00
|
|
|
&ledger_path,
|
2020-12-08 23:18:27 -08:00
|
|
|
&validator_vote_account.pubkey(),
|
|
|
|
vec![Arc::new(validator_vote_account)],
|
2020-09-18 22:21:44 -07:00
|
|
|
None,
|
2020-12-08 23:18:27 -08:00
|
|
|
&validator_config,
|
2020-09-18 22:21:44 -07:00
|
|
|
);
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-01 13:54:15 -08:00
|
|
|
// Needed to avoid panics in `solana-responder-gossip` in tests that create a number of
|
|
|
|
// test validators concurrently...
|
|
|
|
discover_cluster(&gossip, 1).expect("TestValidator startup failed");
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
Ok(TestValidator {
|
|
|
|
ledger_path: ledger_path.to_path_buf(),
|
|
|
|
rpc_pubsub_url,
|
|
|
|
rpc_url,
|
|
|
|
gossip,
|
|
|
|
tpu,
|
2020-11-25 17:00:47 -08:00
|
|
|
validator,
|
|
|
|
vote_account_address,
|
2020-12-08 23:18:27 -08:00
|
|
|
})
|
2020-11-25 17:00:47 -08:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Stop the test validator and delete its ledger directory
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fn close(self) {
|
|
|
|
self.validator.close().unwrap();
|
2020-12-08 23:18:27 -08:00
|
|
|
remove_dir_all(&self.ledger_path).unwrap();
|
2020-09-18 22:21:44 -07:00
|
|
|
}
|
2020-11-25 17:00:47 -08:00
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Return the test validator's TPU address
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fn tpu(&self) -> &SocketAddr {
|
|
|
|
&self.tpu
|
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Return the test validator's Gossip address
|
|
|
|
pub fn gossip(&self) -> &SocketAddr {
|
|
|
|
&self.gossip
|
2020-11-25 17:00:47 -08:00
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Return the test validator's JSON RPC URL
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fn rpc_url(&self) -> String {
|
|
|
|
self.rpc_url.clone()
|
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Return the test validator's JSON RPC PubSub URL
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fn rpc_pubsub_url(&self) -> String {
|
|
|
|
self.rpc_pubsub_url.clone()
|
|
|
|
}
|
|
|
|
|
2020-12-08 23:18:27 -08:00
|
|
|
/// Return the vote account address of the validator
|
2020-11-25 17:00:47 -08:00
|
|
|
pub fn vote_account_address(&self) -> Pubkey {
|
|
|
|
self.vote_account_address
|
|
|
|
}
|
2020-09-18 22:21:44 -07:00
|
|
|
}
|