Initial solana-test-validator command-line program
This commit is contained in:
committed by
mergify[bot]
parent
13db3eca9f
commit
0a9ff1dc9d
@ -7,15 +7,18 @@ version = "1.5.0"
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
default-run = "solana-validator"
|
||||
|
||||
[dependencies]
|
||||
clap = "2.33.1"
|
||||
chrono = { version = "0.4.11", features = ["serde"] }
|
||||
console = "0.11.3"
|
||||
indicatif = "0.15.0"
|
||||
log = "0.4.8"
|
||||
rand = "0.7.0"
|
||||
serde_json = "1.0.56"
|
||||
solana-clap-utils = { path = "../clap-utils", version = "1.5.0" }
|
||||
solana-cli-config = { path = "../cli-config", version = "1.5.0" }
|
||||
solana-client = { path = "../client", version = "1.5.0" }
|
||||
solana-core = { path = "../core", version = "1.5.0" }
|
||||
solana-download-utils = { path = "../download-utils", version = "1.5.0" }
|
||||
@ -29,18 +32,11 @@ solana-runtime = { path = "../runtime", version = "1.5.0" }
|
||||
solana-sdk = { path = "../sdk", version = "1.5.0" }
|
||||
solana-version = { path = "../version", version = "1.5.0" }
|
||||
solana-vote-program = { path = "../programs/vote", version = "1.5.0" }
|
||||
solana-vote-signer = { path = "../vote-signer", version = "1.5.0" }
|
||||
symlink = "0.1.0"
|
||||
|
||||
[target."cfg(unix)".dependencies]
|
||||
libc = "0.2.72"
|
||||
signal-hook = "0.1.15"
|
||||
|
||||
#[[bin]]
|
||||
#name = "solana-validator"
|
||||
#path = "src/main.rs"
|
||||
#
|
||||
#[lib]
|
||||
#name = "solana_validator"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
5
validator/solana-test-validator
Executable file
5
validator/solana-test-validator
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
here="$(dirname "$0")"
|
||||
set -x
|
||||
exec cargo run --manifest-path="$here"/Cargo.toml --bin solana-test-validator -- "$@"
|
262
validator/src/bin/solana-test-validator.rs
Normal file
262
validator/src/bin/solana-test-validator.rs
Normal file
@ -0,0 +1,262 @@
|
||||
use {
|
||||
clap::{value_t_or_exit, App, Arg},
|
||||
console::style,
|
||||
indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle},
|
||||
solana_clap_utils::{input_parsers::pubkey_of, input_validators::is_pubkey},
|
||||
solana_client::{client_error, rpc_client::RpcClient},
|
||||
solana_core::rpc::JsonRpcConfig,
|
||||
solana_sdk::{
|
||||
clock::{Slot, DEFAULT_TICKS_PER_SLOT, MS_PER_TICK},
|
||||
commitment_config::CommitmentConfig,
|
||||
fee_calculator::FeeRateGovernor,
|
||||
rent::Rent,
|
||||
rpc_port,
|
||||
signature::{read_keypair_file, Signer},
|
||||
},
|
||||
solana_validator::{start_logger, test_validator::*},
|
||||
std::{
|
||||
path::PathBuf,
|
||||
process::exit,
|
||||
thread::sleep,
|
||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Output {
|
||||
None,
|
||||
Log,
|
||||
Dashboard,
|
||||
}
|
||||
|
||||
/// Creates a new process bar for processing that will take an unknown amount of time
|
||||
fn new_spinner_progress_bar() -> ProgressBar {
|
||||
let progress_bar = ProgressBar::new(42);
|
||||
progress_bar.set_draw_target(ProgressDrawTarget::stdout());
|
||||
progress_bar
|
||||
.set_style(ProgressStyle::default_spinner().template("{spinner:.green} {wide_msg}"));
|
||||
progress_bar.enable_steady_tick(100);
|
||||
progress_bar
|
||||
}
|
||||
|
||||
/// Pretty print a "name value"
|
||||
fn println_name_value(name: &str, value: &str) {
|
||||
println!("{} {}", style(name).bold(), value);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let default_rpc_port = rpc_port::DEFAULT_RPC_PORT.to_string();
|
||||
|
||||
let matches = App::new("solana-test-validator").about("Test Validator")
|
||||
.version(solana_version::version!())
|
||||
.arg({
|
||||
let arg = Arg::with_name("config_file")
|
||||
.short("C")
|
||||
.long("config")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Configuration file to use");
|
||||
if let Some(ref config_file) = *solana_cli_config::CONFIG_FILE {
|
||||
arg.default_value(&config_file)
|
||||
} else {
|
||||
arg
|
||||
}
|
||||
})
|
||||
.arg(
|
||||
Arg::with_name("mint_address")
|
||||
.long("mint")
|
||||
.value_name("PUBKEY")
|
||||
.validator(is_pubkey)
|
||||
.takes_value(true)
|
||||
.help("Address of the mint account that will receive all the initial tokens [default: client keypair]"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("ledger_path")
|
||||
.short("l")
|
||||
.long("ledger")
|
||||
.value_name("DIR")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.default_value("test-ledger")
|
||||
.help("Use DIR as ledger location"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("quiet")
|
||||
.short("q")
|
||||
.long("quiet")
|
||||
.takes_value(false)
|
||||
.conflicts_with("log")
|
||||
.help("Quiet mode: suppress normal output")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("log")
|
||||
.long("log")
|
||||
.takes_value(false)
|
||||
.conflicts_with("quiet")
|
||||
.help("Log mode: stream the validator log")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("rpc_port")
|
||||
.long("rpc-port")
|
||||
.value_name("PORT")
|
||||
.takes_value(true)
|
||||
.default_value(&default_rpc_port)
|
||||
.validator(solana_validator::port_validator)
|
||||
.help("Use this port for JSON RPC and the next port for the RPC websocket"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
|
||||
solana_cli_config::Config::load(config_file).unwrap_or_default()
|
||||
} else {
|
||||
solana_cli_config::Config::default()
|
||||
};
|
||||
|
||||
let mint_address = pubkey_of(&matches, "mint_address").unwrap_or_else(|| {
|
||||
read_keypair_file(&cli_config.keypair_path)
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!(
|
||||
"Error: Unable to read keypair file {}: {}",
|
||||
cli_config.keypair_path, err
|
||||
);
|
||||
exit(1);
|
||||
})
|
||||
.pubkey()
|
||||
});
|
||||
|
||||
let ledger_path = value_t_or_exit!(matches, "ledger_path", PathBuf);
|
||||
let output = if matches.is_present("quiet") {
|
||||
Output::None
|
||||
} else if matches.is_present("log") {
|
||||
Output::Log
|
||||
} else {
|
||||
Output::Dashboard
|
||||
};
|
||||
|
||||
let rpc_ports = {
|
||||
let rpc_port = value_t_or_exit!(matches, "rpc_port", u16);
|
||||
(rpc_port, rpc_port + 1)
|
||||
};
|
||||
|
||||
if !ledger_path.exists() {
|
||||
let _progress_bar = if output == Output::Dashboard {
|
||||
println_name_value("Mint address:", &mint_address.to_string());
|
||||
let progress_bar = new_spinner_progress_bar();
|
||||
progress_bar.set_message("Creating ledger...");
|
||||
Some(progress_bar)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
TestValidator::initialize_ledger(
|
||||
Some(&ledger_path),
|
||||
TestValidatorGenesisConfig {
|
||||
mint_address,
|
||||
fee_rate_governor: FeeRateGovernor::default(),
|
||||
rent: Rent::default(),
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!(
|
||||
"Error: failed to initialize ledger at {}: {}",
|
||||
ledger_path.display(),
|
||||
err
|
||||
);
|
||||
exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
let validator_log_symlink = ledger_path.join("validator.log");
|
||||
let logfile = if output != Output::Log {
|
||||
let validator_log_with_timestamp = format!(
|
||||
"validator-{}.log",
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis()
|
||||
);
|
||||
|
||||
let _ = std::fs::remove_file(&validator_log_symlink);
|
||||
symlink::symlink_file(&validator_log_with_timestamp, &validator_log_symlink).unwrap();
|
||||
|
||||
Some(
|
||||
ledger_path
|
||||
.join(validator_log_with_timestamp)
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.unwrap(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let _logger_thread = start_logger(logfile);
|
||||
|
||||
let test_validator = {
|
||||
let _progress_bar = if output == Output::Dashboard {
|
||||
println_name_value("Ledger location:", &format!("{}", ledger_path.display()));
|
||||
println_name_value("Log:", &format!("{}", validator_log_symlink.display()));
|
||||
let progress_bar = new_spinner_progress_bar();
|
||||
progress_bar.set_message("Initializing...");
|
||||
Some(progress_bar)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
TestValidator::start(
|
||||
&ledger_path,
|
||||
TestValidatorStartConfig {
|
||||
rpc_config: JsonRpcConfig {
|
||||
enable_validator_exit: true,
|
||||
enable_rpc_transaction_history: true,
|
||||
..JsonRpcConfig::default()
|
||||
},
|
||||
rpc_ports: Some(rpc_ports),
|
||||
},
|
||||
)
|
||||
}
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!("Error: failed to start validator: {}", err);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
if output == Output::Dashboard {
|
||||
println_name_value("JSON RPC URL:", &test_validator.rpc_url());
|
||||
println_name_value(
|
||||
"JSON RPC PubSub Websocket:",
|
||||
&test_validator.rpc_pubsub_url(),
|
||||
);
|
||||
println_name_value("Gossip Address:", &test_validator.gossip().to_string());
|
||||
println_name_value("TPU Address:", &test_validator.tpu().to_string());
|
||||
|
||||
let progress_bar = new_spinner_progress_bar();
|
||||
let rpc_client = RpcClient::new(test_validator.rpc_url());
|
||||
|
||||
fn get_validator_stats(rpc_client: &RpcClient) -> client_error::Result<(Slot, Slot, u64)> {
|
||||
let max_slot = rpc_client.get_slot_with_commitment(CommitmentConfig::max())?;
|
||||
let recent_slot = rpc_client.get_slot_with_commitment(CommitmentConfig::recent())?;
|
||||
let transaction_count =
|
||||
rpc_client.get_transaction_count_with_commitment(CommitmentConfig::recent())?;
|
||||
Ok((recent_slot, max_slot, transaction_count))
|
||||
}
|
||||
|
||||
loop {
|
||||
match get_validator_stats(&rpc_client) {
|
||||
Ok((recent_slot, max_slot, transaction_count)) => {
|
||||
progress_bar.set_message(&format!(
|
||||
"Recent slot: {} | Max confirmed slot: {} | Transaction count: {}",
|
||||
recent_slot, max_slot, transaction_count,
|
||||
));
|
||||
}
|
||||
Err(err) => {
|
||||
progress_bar.set_message(&format!("{}", err));
|
||||
}
|
||||
}
|
||||
sleep(Duration::from_millis(
|
||||
MS_PER_TICK * DEFAULT_TICKS_PER_SLOT / 2,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
std::thread::park();
|
||||
}
|
74
validator/src/lib.rs
Normal file
74
validator/src/lib.rs
Normal file
@ -0,0 +1,74 @@
|
||||
pub use solana_core::test_validator;
|
||||
use {
|
||||
log::*,
|
||||
std::{env, process::exit, thread::JoinHandle},
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
fn redirect_stderr(filename: &str) {
|
||||
use std::{fs::OpenOptions, os::unix::io::AsRawFd};
|
||||
match OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(filename)
|
||||
{
|
||||
Ok(file) => unsafe {
|
||||
libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO);
|
||||
},
|
||||
Err(err) => eprintln!("Unable to open {}: {}", filename, err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_logger(logfile: Option<String>) -> Option<JoinHandle<()>> {
|
||||
// Default to RUST_BACKTRACE=1 for more informative validator logs
|
||||
if env::var_os("RUST_BACKTRACE").is_none() {
|
||||
env::set_var("RUST_BACKTRACE", "1")
|
||||
}
|
||||
|
||||
let logger_thread = match logfile {
|
||||
None => None,
|
||||
Some(logfile) => {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let signals = signal_hook::iterator::Signals::new(&[signal_hook::SIGUSR1])
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!("Unable to register SIGUSR1 handler: {:?}", err);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
redirect_stderr(&logfile);
|
||||
Some(std::thread::spawn(move || {
|
||||
for signal in signals.forever() {
|
||||
info!(
|
||||
"received SIGUSR1 ({}), reopening log file: {:?}",
|
||||
signal, logfile
|
||||
);
|
||||
redirect_stderr(&logfile);
|
||||
}
|
||||
}))
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
println!("logging to a file is not supported on this platform");
|
||||
()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
solana_logger::setup_with_default(
|
||||
&[
|
||||
"solana=info,solana_runtime::message_processor=error", /* info logging for all solana modules */
|
||||
"rpc=trace", /* json_rpc request/response logging */
|
||||
]
|
||||
.join(","),
|
||||
);
|
||||
|
||||
logger_thread
|
||||
}
|
||||
|
||||
pub fn port_validator(port: String) -> Result<(), String> {
|
||||
port.parse::<u16>()
|
||||
.map(|_| ())
|
||||
.map_err(|e| format!("{:?}", e))
|
||||
}
|
@ -21,7 +21,7 @@ use solana_core::{
|
||||
gossip_service::GossipService,
|
||||
rpc::JsonRpcConfig,
|
||||
rpc_pubsub_service::PubSubConfig,
|
||||
validator::{Validator, ValidatorConfig},
|
||||
validator::{is_snapshot_config_invalid, Validator, ValidatorConfig},
|
||||
};
|
||||
use solana_download_utils::{download_genesis_if_missing, download_snapshot};
|
||||
use solana_ledger::blockstore_db::BlockstoreRecoveryMode;
|
||||
@ -39,6 +39,7 @@ use solana_sdk::{
|
||||
pubkey::Pubkey,
|
||||
signature::{Keypair, Signer},
|
||||
};
|
||||
use solana_validator::start_logger;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
env,
|
||||
@ -51,16 +52,10 @@ use std::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread::{sleep, JoinHandle},
|
||||
thread::sleep,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
fn port_validator(port: String) -> Result<(), String> {
|
||||
port.parse::<u16>()
|
||||
.map(|_| ())
|
||||
.map_err(|e| format!("{:?}", e))
|
||||
}
|
||||
|
||||
fn port_range_validator(port_range: String) -> Result<(), String> {
|
||||
if let Some((start, end)) = solana_net_utils::parse_port_range(&port_range) {
|
||||
if end - start < MINIMUM_VALIDATOR_PORT_RANGE_WIDTH {
|
||||
@ -482,73 +477,6 @@ fn download_then_check_genesis_hash(
|
||||
Ok(genesis_config.hash())
|
||||
}
|
||||
|
||||
fn is_snapshot_config_invalid(
|
||||
snapshot_interval_slots: u64,
|
||||
accounts_hash_interval_slots: u64,
|
||||
) -> bool {
|
||||
snapshot_interval_slots != 0
|
||||
&& (snapshot_interval_slots < accounts_hash_interval_slots
|
||||
|| snapshot_interval_slots % accounts_hash_interval_slots != 0)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn redirect_stderr(filename: &str) {
|
||||
use std::{fs::OpenOptions, os::unix::io::AsRawFd};
|
||||
match OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(filename)
|
||||
{
|
||||
Ok(file) => unsafe {
|
||||
libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO);
|
||||
},
|
||||
Err(err) => eprintln!("Unable to open {}: {}", filename, err),
|
||||
}
|
||||
}
|
||||
|
||||
fn start_logger(logfile: Option<String>) -> Option<JoinHandle<()>> {
|
||||
let logger_thread = match logfile {
|
||||
None => None,
|
||||
Some(logfile) => {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let signals = signal_hook::iterator::Signals::new(&[signal_hook::SIGUSR1])
|
||||
.unwrap_or_else(|err| {
|
||||
eprintln!("Unable to register SIGUSR1 handler: {:?}", err);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
redirect_stderr(&logfile);
|
||||
Some(std::thread::spawn(move || {
|
||||
for signal in signals.forever() {
|
||||
info!(
|
||||
"received SIGUSR1 ({}), reopening log file: {:?}",
|
||||
signal, logfile
|
||||
);
|
||||
redirect_stderr(&logfile);
|
||||
}
|
||||
}))
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
println!("logging to a file is not supported on this platform");
|
||||
()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
solana_logger::setup_with_default(
|
||||
&[
|
||||
"solana=info,solana_runtime::message_processor=error", /* info logging for all solana modules */
|
||||
"rpc=trace", /* json_rpc request/response logging */
|
||||
]
|
||||
.join(","),
|
||||
);
|
||||
|
||||
logger_thread
|
||||
}
|
||||
|
||||
fn verify_reachable_ports(
|
||||
node: &Node,
|
||||
cluster_entrypoint: &ContactInfo,
|
||||
@ -988,8 +916,8 @@ pub fn main() {
|
||||
.long("rpc-port")
|
||||
.value_name("PORT")
|
||||
.takes_value(true)
|
||||
.validator(port_validator)
|
||||
.help("Use this port for JSON RPC, the next port for the RPC websocket, and then third port for the RPC banks API"),
|
||||
.validator(solana_validator::port_validator)
|
||||
.help("Use this port for JSON RPC and the next port for the RPC websocket"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("private_rpc")
|
||||
@ -1715,11 +1643,6 @@ pub fn main() {
|
||||
let use_progress_bar = logfile.is_none();
|
||||
let _logger_thread = start_logger(logfile);
|
||||
|
||||
// Default to RUST_BACKTRACE=1 for more informative validator logs
|
||||
if env::var_os("RUST_BACKTRACE").is_none() {
|
||||
env::set_var("RUST_BACKTRACE", "1")
|
||||
}
|
||||
|
||||
let gossip_host = matches
|
||||
.value_of("gossip_host")
|
||||
.map(|gossip_host| {
|
||||
@ -1820,17 +1743,3 @@ pub fn main() {
|
||||
validator.join().expect("validator exit");
|
||||
info!("Validator exiting..");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_interval_check() {
|
||||
assert!(!is_snapshot_config_invalid(0, 100));
|
||||
assert!(is_snapshot_config_invalid(1, 100));
|
||||
assert!(is_snapshot_config_invalid(230, 100));
|
||||
assert!(!is_snapshot_config_invalid(500, 100));
|
||||
assert!(!is_snapshot_config_invalid(5, 5));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user