2019-01-11 16:49:18 -08:00
|
|
|
use clap::{crate_version, App, Arg, ArgMatches};
|
2018-12-14 17:42:46 -08:00
|
|
|
use log::*;
|
2018-07-31 22:07:53 -07:00
|
|
|
use solana::client::mk_client;
|
2018-12-14 17:42:46 -08:00
|
|
|
use solana::cluster_info::{Node, NodeInfo, FULLNODE_PORT_RANGE};
|
2019-01-29 08:51:01 -08:00
|
|
|
use solana::fullnode::{Fullnode, FullnodeConfig};
|
2018-10-10 16:49:41 -07:00
|
|
|
use solana::leader_scheduler::LeaderScheduler;
|
2019-01-10 09:21:38 -08:00
|
|
|
use solana::local_vote_signer_service::LocalVoteSignerService;
|
2018-12-14 17:42:46 -08:00
|
|
|
use solana::socketaddr;
|
2019-01-11 16:49:18 -08:00
|
|
|
use solana::thin_client::{poll_gossip_for_leader, ThinClient};
|
2019-01-11 12:58:31 -08:00
|
|
|
use solana::vote_signer_proxy::{RemoteVoteSigner, VoteSignerProxy};
|
2019-01-11 16:49:18 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-01-10 09:21:38 -08:00
|
|
|
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;
|
2018-05-23 13:03:19 -07:00
|
|
|
use std::fs::File;
|
2019-01-11 16:49:18 -08:00
|
|
|
use std::io::{Error, ErrorKind, Result};
|
2018-08-31 00:10:39 -07:00
|
|
|
use std::net::{Ipv4Addr, SocketAddr};
|
2018-04-19 07:06:19 -07:00
|
|
|
use std::process::exit;
|
2018-10-25 16:58:40 -07:00
|
|
|
use std::sync::Arc;
|
2019-01-26 00:28:08 -08:00
|
|
|
use std::sync::RwLock;
|
2018-08-31 00:10:39 -07:00
|
|
|
use std::thread::sleep;
|
2018-08-25 18:24:25 -07:00
|
|
|
use std::time::Duration;
|
2018-02-28 17:04:35 -08:00
|
|
|
|
2019-01-11 16:49:18 -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 {
|
2019-01-11 16:49:18 -08:00
|
|
|
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 {
|
2019-01-11 16:49:18 -08:00
|
|
|
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();
|
2019-01-22 11:08:23 -08:00
|
|
|
info!("create_and_fund_vote_account last_id={:?}", last_id);
|
2019-01-11 16:49:18 -08:00
|
|
|
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) {
|
2019-01-22 11:08:23 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2019-01-11 16:49:18 -08:00
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:08:23 -08:00
|
|
|
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-11 16:49:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 20:03:20 -08:00
|
|
|
Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"expected successful vote account registration",
|
|
|
|
))
|
2019-01-11 16:49:18 -08:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
fn main() {
|
2018-12-14 12:36:50 -08:00
|
|
|
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
|
|
|
|
2018-07-09 18:04:49 -07:00
|
|
|
let matches = App::new("fullnode")
|
2018-08-06 20:51:12 -07:00
|
|
|
.version(crate_version!())
|
2019-01-29 00:21:27 -08:00
|
|
|
.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")
|
|
|
|
)
|
2018-12-07 19:01:28 -08:00
|
|
|
.arg(
|
2018-07-09 18:04:49 -07:00
|
|
|
Arg::with_name("identity")
|
2018-07-12 15:45:41 -07:00
|
|
|
.short("i")
|
|
|
|
.long("identity")
|
2018-09-14 15:32:57 -07:00
|
|
|
.value_name("PATH")
|
2018-07-09 18:04:49 -07:00
|
|
|
.takes_value(true)
|
2018-09-14 15:32:57 -07:00
|
|
|
.help("Run with the identity found in FILE"),
|
2018-12-07 19:01:28 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-01-28 11:40:24 -08:00
|
|
|
Arg::with_name("init_complete_file")
|
|
|
|
.long("init-complete-file")
|
|
|
|
.value_name("FILE")
|
2019-01-05 12:57:52 -08:00
|
|
|
.takes_value(true)
|
2019-01-28 11:40:24 -08:00
|
|
|
.help("Create this file, if it doesn't already exist, once node initialization is complete"),
|
2019-01-05 12:57:52 -08:00
|
|
|
)
|
2018-12-07 19:01:28 -08:00
|
|
|
.arg(
|
2018-07-12 14:29:36 -07:00
|
|
|
Arg::with_name("ledger")
|
2018-08-03 11:06:06 -07:00
|
|
|
.short("l")
|
2018-07-12 14:29:36 -07:00
|
|
|
.long("ledger")
|
2018-08-03 11:06:06 -07:00
|
|
|
.value_name("DIR")
|
2018-07-09 18:04:49 -07:00
|
|
|
.takes_value(true)
|
2018-08-03 11:06:06 -07:00
|
|
|
.required(true)
|
2018-11-05 09:50:58 -08:00
|
|
|
.help("Use DIR as persistent ledger location"),
|
2018-12-07 19:01:28 -08:00
|
|
|
)
|
|
|
|
.arg(
|
2019-01-28 11:40:24 -08:00
|
|
|
Arg::with_name("network")
|
|
|
|
.short("n")
|
|
|
|
.long("network")
|
|
|
|
.value_name("HOST:PORT")
|
2018-11-05 09:50:58 -08:00
|
|
|
.takes_value(true)
|
2019-01-28 11:40:24 -08:00
|
|
|
.help("Rendezvous with the cluster at this gossip entry point"),
|
2018-12-07 19:01:28 -08:00
|
|
|
)
|
2019-01-22 11:34:12 -08:00
|
|
|
.arg(
|
2019-01-28 11:40:24 -08:00
|
|
|
Arg::with_name("no_leader_rotation")
|
|
|
|
.long("no-leader-rotation")
|
|
|
|
.help("Disable leader rotation"),
|
2019-01-22 11:34:12 -08:00
|
|
|
)
|
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"),
|
|
|
|
)
|
2019-01-28 11:40:24 -08:00
|
|
|
.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"),
|
|
|
|
)
|
2018-12-07 19:01:28 -08:00
|
|
|
.get_matches();
|
2018-04-21 06:12:57 -07:00
|
|
|
|
2019-01-29 08:51:01 -08: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");
|
2019-01-28 11:40:24 -08:00
|
|
|
let use_only_bootstrap_leader = matches.is_present("no_leader_rotation");
|
2019-01-11 16:49:18 -08:00
|
|
|
let (keypair, gossip) = parse_identity(&matches);
|
2018-08-03 11:06:06 -07:00
|
|
|
let ledger_path = matches.value_of("ledger").unwrap();
|
2019-01-19 20:03:20 -08:00
|
|
|
let cluster_entrypoint = matches
|
2018-08-31 00:10:39 -07:00
|
|
|
.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)
|
|
|
|
};
|
2019-01-17 08:09:52 -08:00
|
|
|
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")
|
2018-11-15 10:42:02 -08:00
|
|
|
};
|
2019-01-29 08:51:01 -08:00
|
|
|
fullnode_config.rpc_port = Some(rpc_port);
|
2019-01-22 11:34:12 -08:00
|
|
|
let init_complete_file = matches.value_of("init_complete_file");
|
2019-01-29 08:51:01 -08:00
|
|
|
fullnode_config.entry_stream = matches.value_of("entry_stream").map(|s| s.to_string());
|
2018-11-15 10:42:02 -08:00
|
|
|
|
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-29 08:51:01 -08:00
|
|
|
|
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-10 09:21:38 -08:00
|
|
|
|
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;
|
2019-01-29 08:51:01 -08:00
|
|
|
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
|
|
|
|
};
|
2019-01-10 09:21:38 -08:00
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
let mut fullnode = Fullnode::new(
|
|
|
|
node,
|
2018-10-25 16:58:40 -07:00
|
|
|
keypair.clone(),
|
2019-01-28 20:10:38 -08:00
|
|
|
ledger_path,
|
|
|
|
Arc::new(RwLock::new(leader_scheduler)),
|
2019-01-29 08:51:01 -08:00
|
|
|
vote_signer_option,
|
2019-01-28 20:10:38 -08:00
|
|
|
cluster_entrypoint
|
|
|
|
.map(|i| NodeInfo::new_entry_point(&i))
|
|
|
|
.as_ref(),
|
2019-01-29 08:51:01 -08:00
|
|
|
fullnode_config,
|
2018-10-10 16:49:41 -07:00
|
|
|
);
|
2018-07-31 22:07:53 -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);
|
2019-01-05 12:57:52 -08:00
|
|
|
}
|
2018-11-15 17:05:31 -08:00
|
|
|
}
|
2018-07-31 22:07:53 -07:00
|
|
|
|
2019-01-22 11:34:12 -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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
2018-05-23 13:03:19 -07:00
|
|
|
}
|