solana/fullnode/src/main.rs

317 lines
11 KiB
Rust
Raw Normal View History

use clap::{crate_version, App, Arg, ArgMatches};
use log::*;
use solana::client::mk_client;
use solana::cluster_info::{Node, NodeInfo, FULLNODE_PORT_RANGE};
use solana::fullnode::{Fullnode, FullnodeConfig};
Leader scheduler plumbing (#1440) * Added LeaderScheduler module and tests * plumbing for LeaderScheduler in Fullnode + tests. Add vote processing for active set to ReplicateStage and WriteStage * Add LeaderScheduler plumbing for Tvu, window, and tests * Fix bank and switch tests to use new LeaderScheduler * move leader rotation check from window service to replicate stage * Add replicate_stage leader rotation exit test * removed leader scheduler from the window service and associated modules/tests * Corrected is_leader calculation in repair() function in window.rs * Integrate LeaderScheduler with write_stage for leader to validator transitions * Integrated LeaderScheduler with BroadcastStage * Removed gossip leader rotation from crdt * Add multi validator, leader test * Comments and cleanup * Remove unneeded checks from broadcast stage * Fix case where a validator/leader need to immediately transition on startup after reading ledger and seeing they are not in the correct role * Set new leader in validator -> validator transitions * Clean up for PR comments, refactor LeaderScheduler from process_entry/process_ledger_tail * Cleaned out LeaderScheduler options, implemented LeaderScheduler strategy that only picks the bootstrap leader to support existing tests, drone/airdrops * Ignore test_full_leader_validator_network test due to bug where the next leader in line fails to get the last entry before rotation (b/c it hasn't started up yet). Added a test test_dropped_handoff_recovery go track this bug
2018-10-10 16:49:41 -07:00
use solana::leader_scheduler::LeaderScheduler;
use solana::local_vote_signer_service::LocalVoteSignerService;
use solana::socketaddr;
use solana::thin_client::{poll_gossip_for_leader, ThinClient};
use solana::vote_signer_proxy::{RemoteVoteSigner, VoteSignerProxy};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
2018-12-04 07:45:32 -08:00
use solana_sdk::vote_program::VoteProgram;
2018-12-04 15:05:41 -08:00
use solana_sdk::vote_transaction::VoteTransaction;
use std::fs::File;
use std::io::{Error, ErrorKind, Result};
use std::net::{Ipv4Addr, SocketAddr};
2018-04-19 07:06:19 -07:00
use std::process::exit;
use std::sync::Arc;
use std::sync::RwLock;
use std::thread::sleep;
use std::time::Duration;
2018-02-28 17:04:35 -08:00
fn parse_identity(matches: &ArgMatches<'_>) -> (Keypair, SocketAddr) {
if let Some(i) = matches.value_of("identity") {
let path = i.to_string();
if let Ok(file) = File::open(path.clone()) {
let parse: serde_json::Result<solana_fullnode_config::Config> =
serde_json::from_reader(file);
if let Ok(config_data) = parse {
let keypair = config_data.keypair();
let node_info = NodeInfo::new_with_pubkey_socketaddr(
keypair.pubkey(),
&config_data.bind_addr(FULLNODE_PORT_RANGE.0),
);
(keypair, node_info.gossip)
} else {
eprintln!("failed to parse {}", path);
exit(1);
}
} else {
eprintln!("failed to read {}", path);
exit(1);
}
} else {
(Keypair::new(), socketaddr!(0, 8000))
}
}
fn create_and_fund_vote_account(
client: &mut ThinClient,
vote_account: Pubkey,
node_keypair: &Arc<Keypair>,
) -> Result<()> {
let pubkey = node_keypair.pubkey();
2019-01-19 20:03:20 -08:00
let node_balance = client.poll_get_balance(&pubkey)?;
info!("node balance is {}", node_balance);
if node_balance < 1 {
return Err(Error::new(
ErrorKind::Other,
"insufficient tokens, one token required",
));
}
// Create the vote account if necessary
if client.poll_get_balance(&vote_account).unwrap_or(0) == 0 {
// Need at least two tokens as one token will be spent on a vote_account_new() transaction
2019-01-19 20:03:20 -08:00
if node_balance < 2 {
error!("insufficient tokens, two tokens required");
return Err(Error::new(
ErrorKind::Other,
"insufficient tokens, two tokens required",
));
}
loop {
let last_id = client.get_last_id();
info!("create_and_fund_vote_account last_id={:?}", last_id);
let transaction =
VoteTransaction::vote_account_new(node_keypair, vote_account, last_id, 1, 1);
2019-01-19 20:03:20 -08:00
match client.transfer_signed(&transaction) {
Ok(signature) => {
match client.poll_for_signature(&signature) {
Ok(_) => match client.poll_get_balance(&vote_account) {
Ok(balance) => {
info!("vote account balance: {}", balance);
break;
}
Err(e) => {
info!("Failed to get vote account balance: {:?}", e);
}
},
Err(e) => {
info!(
"vote_account_new signature not found: {:?}: {:?}",
signature, e
);
}
};
}
2019-01-19 20:03:20 -08:00
Err(e) => {
info!("Failed to send vote_account_new transaction: {:?}", e);
}
};
sleep(Duration::from_secs(2));
}
}
info!("Checking for vote account registration");
2019-01-19 20:03:20 -08:00
let vote_account_user_data = client.get_account_userdata(&vote_account);
if let Ok(Some(vote_account_user_data)) = vote_account_user_data {
if let Ok(vote_state) = VoteProgram::deserialize(&vote_account_user_data) {
if vote_state.node_id == pubkey {
return Ok(());
}
}
}
2019-01-19 20:03:20 -08:00
Err(Error::new(
ErrorKind::Other,
"expected successful vote account registration",
))
}
fn main() {
solana_logger::setup();
2018-11-16 08:45:59 -08:00
solana_metrics::set_panic_hook("fullnode");
2019-01-19 20:03:20 -08:00
let matches = App::new("fullnode")
.version(crate_version!())
.arg(
Arg::with_name("entry_stream")
.long("entry-stream")
.takes_value(true)
.value_name("UNIX DOMAIN SOCKET")
.help("Open entry stream at this unix domain socket location")
)
.arg(
Arg::with_name("identity")
.short("i")
.long("identity")
2018-09-14 15:32:57 -07:00
.value_name("PATH")
.takes_value(true)
2018-09-14 15:32:57 -07:00
.help("Run with the identity found in FILE"),
)
.arg(
Arg::with_name("init_complete_file")
.long("init-complete-file")
.value_name("FILE")
.takes_value(true)
.help("Create this file, if it doesn't already exist, once node initialization is complete"),
)
.arg(
Arg::with_name("ledger")
.short("l")
.long("ledger")
.value_name("DIR")
.takes_value(true)
.required(true)
2018-11-05 09:50:58 -08:00
.help("Use DIR as persistent ledger location"),
)
.arg(
Arg::with_name("network")
.short("n")
.long("network")
.value_name("HOST:PORT")
2018-11-05 09:50:58 -08:00
.takes_value(true)
.help("Rendezvous with the cluster at this gossip entry point"),
)
.arg(
Arg::with_name("no_leader_rotation")
.long("no-leader-rotation")
.help("Disable leader rotation"),
)
2019-01-23 18:05:06 -08:00
.arg(
Arg::with_name("no_signer")
.long("no-signer")
.takes_value(false)
2019-01-24 10:14:09 -08:00
.conflicts_with("signer")
2019-01-23 18:05:06 -08:00
.help("Launch node without vote signer"),
)
.arg(
Arg::with_name("no_sigverify")
.short("v")
.long("no-sigverify")
.help("Run without signature verification"),
)
.arg(
Arg::with_name("rpc_port")
.long("rpc-port")
.value_name("PORT")
.takes_value(true)
.help("RPC port to use for this node"),
)
.arg(
Arg::with_name("signer")
.short("s")
.long("signer")
.value_name("HOST:PORT")
.takes_value(true)
.help("Rendezvous with the vote signer at this RPC end point"),
)
.get_matches();
2018-04-21 06:12:57 -07:00
let mut fullnode_config = FullnodeConfig::default();
fullnode_config.sigverify_disabled = matches.is_present("no_sigverify");
2019-01-23 18:05:06 -08:00
let no_signer = matches.is_present("no_signer");
let use_only_bootstrap_leader = matches.is_present("no_leader_rotation");
let (keypair, gossip) = parse_identity(&matches);
let ledger_path = matches.value_of("ledger").unwrap();
2019-01-19 20:03:20 -08:00
let cluster_entrypoint = matches
.value_of("network")
.map(|network| network.parse().expect("failed to parse network address"));
2019-01-19 20:03:20 -08:00
let (_signer_service, signer_addr) = if let Some(signer_addr) = matches.value_of("signer") {
(
None,
signer_addr.to_string().parse().expect("Signer IP Address"),
)
} else {
// Run a local vote signer if a vote signer service address was not provided
let (signer_service, signer_addr) = LocalVoteSignerService::new();
(Some(signer_service), signer_addr)
};
let rpc_port = if let Some(port) = matches.value_of("rpc_port") {
2018-11-05 09:50:58 -08:00
let port_number = port.to_string().parse().expect("integer");
if port_number == 0 {
eprintln!("Invalid RPC port requested: {:?}", port);
exit(1);
}
2019-01-19 20:03:20 -08:00
port_number
2018-11-05 09:50:58 -08:00
} else {
2019-01-19 20:03:20 -08:00
solana_netutil::find_available_port_in_range(FULLNODE_PORT_RANGE)
.expect("unable to allocate rpc port")
};
fullnode_config.rpc_port = Some(rpc_port);
let init_complete_file = matches.value_of("init_complete_file");
fullnode_config.entry_stream = matches.value_of("entry_stream").map(|s| s.to_string());
2019-01-19 20:03:20 -08:00
let keypair = Arc::new(keypair);
let node = Node::new_with_external_ip(keypair.pubkey(), &gossip);
let mut node_info = node.info.clone();
2019-01-19 20:03:20 -08:00
node_info.rpc.set_port(rpc_port);
node_info.rpc_pubsub.set_port(rpc_port + 1);
2018-11-05 09:50:58 -08:00
2019-01-19 20:03:20 -08:00
let mut leader_scheduler = LeaderScheduler::default();
leader_scheduler.use_only_bootstrap_leader = use_only_bootstrap_leader;
2019-01-19 20:03:20 -08:00
info!("Node ID: {}", node.info.id);
2019-01-23 18:05:06 -08:00
let vote_account;
let vote_signer_option = if !no_signer {
2019-01-23 18:05:06 -08:00
let vote_signer =
VoteSignerProxy::new(&keypair, Box::new(RemoteVoteSigner::new(signer_addr)));
vote_account = vote_signer.vote_account;
info!("Signer service address: {:?}", signer_addr);
info!("New vote account ID is {:?}", vote_account);
Some(Arc::new(vote_signer))
} else {
vote_account = Pubkey::default();
None
};
Leader scheduler plumbing (#1440) * Added LeaderScheduler module and tests * plumbing for LeaderScheduler in Fullnode + tests. Add vote processing for active set to ReplicateStage and WriteStage * Add LeaderScheduler plumbing for Tvu, window, and tests * Fix bank and switch tests to use new LeaderScheduler * move leader rotation check from window service to replicate stage * Add replicate_stage leader rotation exit test * removed leader scheduler from the window service and associated modules/tests * Corrected is_leader calculation in repair() function in window.rs * Integrate LeaderScheduler with write_stage for leader to validator transitions * Integrated LeaderScheduler with BroadcastStage * Removed gossip leader rotation from crdt * Add multi validator, leader test * Comments and cleanup * Remove unneeded checks from broadcast stage * Fix case where a validator/leader need to immediately transition on startup after reading ledger and seeing they are not in the correct role * Set new leader in validator -> validator transitions * Clean up for PR comments, refactor LeaderScheduler from process_entry/process_ledger_tail * Cleaned out LeaderScheduler options, implemented LeaderScheduler strategy that only picks the bootstrap leader to support existing tests, drone/airdrops * Ignore test_full_leader_validator_network test due to bug where the next leader in line fails to get the last entry before rotation (b/c it hasn't started up yet). Added a test test_dropped_handoff_recovery go track this bug
2018-10-10 16:49:41 -07:00
let mut fullnode = Fullnode::new(
node,
keypair.clone(),
ledger_path,
Arc::new(RwLock::new(leader_scheduler)),
vote_signer_option,
cluster_entrypoint
.map(|i| NodeInfo::new_entry_point(&i))
.as_ref(),
fullnode_config,
Leader scheduler plumbing (#1440) * Added LeaderScheduler module and tests * plumbing for LeaderScheduler in Fullnode + tests. Add vote processing for active set to ReplicateStage and WriteStage * Add LeaderScheduler plumbing for Tvu, window, and tests * Fix bank and switch tests to use new LeaderScheduler * move leader rotation check from window service to replicate stage * Add replicate_stage leader rotation exit test * removed leader scheduler from the window service and associated modules/tests * Corrected is_leader calculation in repair() function in window.rs * Integrate LeaderScheduler with write_stage for leader to validator transitions * Integrated LeaderScheduler with BroadcastStage * Removed gossip leader rotation from crdt * Add multi validator, leader test * Comments and cleanup * Remove unneeded checks from broadcast stage * Fix case where a validator/leader need to immediately transition on startup after reading ledger and seeing they are not in the correct role * Set new leader in validator -> validator transitions * Clean up for PR comments, refactor LeaderScheduler from process_entry/process_ledger_tail * Cleaned out LeaderScheduler options, implemented LeaderScheduler strategy that only picks the bootstrap leader to support existing tests, drone/airdrops * Ignore test_full_leader_validator_network test due to bug where the next leader in line fails to get the last entry before rotation (b/c it hasn't started up yet). Added a test test_dropped_handoff_recovery go track this bug
2018-10-10 16:49:41 -07:00
);
2019-01-23 18:05:06 -08:00
if !no_signer {
2019-01-19 20:03:20 -08:00
let leader_node_info = loop {
info!("Looking for leader...");
match poll_gossip_for_leader(node_info.gossip, Some(10)) {
Ok(leader_node_info) => {
info!("Found leader: {:?}", leader_node_info);
break leader_node_info;
}
Err(err) => {
info!("Unable to find leader: {:?}", err);
}
};
};
let mut client = mk_client(&leader_node_info);
if let Err(err) = create_and_fund_vote_account(&mut client, vote_account, &keypair) {
panic!("Failed to create_and_fund_vote_account: {:?}", err);
}
2018-11-15 17:05:31 -08:00
}
if let Some(filename) = init_complete_file {
File::create(filename).unwrap_or_else(|_| panic!("Unable to create: {}", filename));
}
info!("Node initialized");
2019-01-19 20:03:20 -08:00
loop {
let status = fullnode.handle_role_transition();
match status {
Ok(Some(transition)) => {
info!("role_transition complete: {:?}", transition);
}
_ => {
panic!(
"Fullnode TPU/TVU exited for some unexpected reason: {:?}",
status
);
}
};
}
}