2019-01-05 12:57:52 -08:00
|
|
|
extern crate serde_json;
|
|
|
|
|
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::*;
|
2019-01-05 12:57:52 -08:00
|
|
|
|
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};
|
|
|
|
use solana::fullnode::{Fullnode, FullnodeReturnType};
|
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;
|
|
|
|
use solana::service::Service;
|
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 22:06:19 +08:00
|
|
|
use std::process::exit;
|
2018-10-25 16:58:40 -07:00
|
|
|
use std::sync::Arc;
|
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 18:04:35 -07: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();
|
|
|
|
let balance = client.poll_get_balance(&pubkey).unwrap_or(0);
|
|
|
|
info!("balance is {}", balance);
|
|
|
|
if balance < 1 {
|
|
|
|
error!("insufficient tokens, one token required");
|
|
|
|
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
|
|
|
|
if 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();
|
|
|
|
let transaction =
|
|
|
|
VoteTransaction::vote_account_new(node_keypair, vote_account, last_id, 1, 1);
|
|
|
|
if client.transfer_signed(&transaction).is_err() {
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let balance = client.poll_get_balance(&vote_account).unwrap_or(0);
|
|
|
|
if balance > 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(Duration::from_secs(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait_for_vote_account_registeration(
|
|
|
|
client: &mut ThinClient,
|
|
|
|
vote_account: &Pubkey,
|
|
|
|
node_id: Pubkey,
|
|
|
|
) {
|
|
|
|
loop {
|
|
|
|
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 == node_id {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Expected successful vote account registration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_forever_and_do_role_transition(fullnode: &mut Fullnode) {
|
|
|
|
loop {
|
|
|
|
let status = fullnode.handle_role_transition();
|
|
|
|
match status {
|
|
|
|
Ok(Some(FullnodeReturnType::LeaderToValidatorRotation)) => (),
|
|
|
|
Ok(Some(FullnodeReturnType::ValidatorToLeaderRotation)) => (),
|
|
|
|
_ => {
|
|
|
|
// Fullnode tpu/tvu exited for some unexpected reason
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2018-07-09 19:04:49 -06:00
|
|
|
let matches = App::new("fullnode")
|
2018-08-06 20:51:12 -07:00
|
|
|
.version(crate_version!())
|
2018-07-09 19:04:49 -06:00
|
|
|
.arg(
|
2018-11-23 12:17:29 -08:00
|
|
|
Arg::with_name("nosigverify")
|
|
|
|
.short("v")
|
|
|
|
.long("nosigverify")
|
|
|
|
.help("Run without signature verification"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.arg(
|
2018-12-06 09:08:30 -08:00
|
|
|
Arg::with_name("no-leader-rotation")
|
|
|
|
.long("no-leader-rotation")
|
|
|
|
.help("Disable leader rotation"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.arg(
|
2018-07-09 19:04:49 -06:00
|
|
|
Arg::with_name("identity")
|
2018-07-12 15:45:41 -07:00
|
|
|
.short("i")
|
|
|
|
.long("identity")
|
2018-09-14 16:32:57 -06:00
|
|
|
.value_name("PATH")
|
2018-07-09 19:04:49 -06:00
|
|
|
.takes_value(true)
|
2018-09-14 16:32:57 -06:00
|
|
|
.help("Run with the identity found in FILE"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.arg(
|
2018-08-31 00:10:39 -07:00
|
|
|
Arg::with_name("network")
|
|
|
|
.short("n")
|
|
|
|
.long("network")
|
2018-07-09 19:04:49 -06:00
|
|
|
.value_name("HOST:PORT")
|
|
|
|
.takes_value(true)
|
2018-09-14 16:32:57 -06:00
|
|
|
.help("Rendezvous with the network at this gossip entry point"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
2019-01-05 12:57:52 -08:00
|
|
|
.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 20:01:28 -07: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 19:04:49 -06:00
|
|
|
.takes_value(true)
|
2018-08-03 11:06:06 -07:00
|
|
|
.required(true)
|
2018-11-05 10:50:58 -07:00
|
|
|
.help("Use DIR as persistent ledger location"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.arg(
|
2018-11-05 10:50:58 -07:00
|
|
|
Arg::with_name("rpc")
|
|
|
|
.long("rpc")
|
|
|
|
.value_name("PORT")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Custom RPC port for this node"),
|
2018-12-07 20:01:28 -07:00
|
|
|
)
|
|
|
|
.get_matches();
|
2018-04-21 21:12:57 +08:00
|
|
|
|
2018-11-25 13:11:07 -08:00
|
|
|
let nosigverify = matches.is_present("nosigverify");
|
2018-12-06 09:08:30 -08:00
|
|
|
let use_only_bootstrap_leader = matches.is_present("no-leader-rotation");
|
2018-11-23 12:17:29 -08:00
|
|
|
|
2019-01-11 16:49:18 -08:00
|
|
|
let (keypair, gossip) = parse_identity(&matches);
|
2018-07-31 22:07:53 -07:00
|
|
|
|
2018-08-03 11:06:06 -07:00
|
|
|
let ledger_path = matches.value_of("ledger").unwrap();
|
2018-07-12 14:29:36 -07:00
|
|
|
|
2018-12-06 13:52:47 -07:00
|
|
|
// socketaddr that is initial pointer into the network's gossip
|
2018-08-31 00:10:39 -07:00
|
|
|
let network = matches
|
|
|
|
.value_of("network")
|
|
|
|
.map(|network| network.parse().expect("failed to parse network address"));
|
|
|
|
|
2018-12-06 13:52:47 -07:00
|
|
|
let node = Node::new_with_external_ip(keypair.pubkey(), &gossip);
|
2018-08-31 00:10:39 -07:00
|
|
|
|
|
|
|
// save off some stuff for airdrop
|
2018-11-15 10:42:02 -08:00
|
|
|
let mut node_info = node.info.clone();
|
2018-08-25 10:24:16 -07:00
|
|
|
|
2018-10-25 16:58:40 -07:00
|
|
|
let keypair = Arc::new(keypair);
|
|
|
|
let pubkey = keypair.pubkey();
|
|
|
|
|
2018-11-02 14:32:05 -07:00
|
|
|
let mut leader_scheduler = LeaderScheduler::default();
|
2018-12-06 09:08:30 -08:00
|
|
|
leader_scheduler.use_only_bootstrap_leader = use_only_bootstrap_leader;
|
2018-11-02 14:32:05 -07:00
|
|
|
|
2018-11-05 10:50:58 -07:00
|
|
|
let rpc_port = if let Some(port) = matches.value_of("rpc") {
|
|
|
|
let port_number = port.to_string().parse().expect("integer");
|
|
|
|
if port_number == 0 {
|
|
|
|
eprintln!("Invalid RPC port requested: {:?}", port);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Some(port_number)
|
|
|
|
} else {
|
2018-12-14 17:42:46 -08:00
|
|
|
match solana_netutil::find_available_port_in_range(FULLNODE_PORT_RANGE) {
|
2018-11-15 10:42:02 -08:00
|
|
|
Ok(port) => Some(port),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let leader = match network {
|
|
|
|
Some(network) => {
|
|
|
|
poll_gossip_for_leader(network, None).expect("can't find leader on network")
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
//self = leader
|
|
|
|
if rpc_port.is_some() {
|
2018-11-15 13:42:57 -08:00
|
|
|
node_info.rpc.set_port(rpc_port.unwrap());
|
|
|
|
node_info.rpc_pubsub.set_port(rpc_port.unwrap() + 1);
|
2018-11-15 10:42:02 -08:00
|
|
|
}
|
|
|
|
node_info
|
|
|
|
}
|
2018-11-05 10:50:58 -07:00
|
|
|
};
|
|
|
|
|
2019-01-10 09:21:38 -08:00
|
|
|
let (signer_service, signer) = if let Some(signer_addr) = matches.value_of("signer") {
|
|
|
|
(
|
|
|
|
None,
|
|
|
|
signer_addr.to_string().parse().expect("Signer IP Address"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// If a remote vote-signer service is not provided, run a local instance
|
|
|
|
let (signer_service, addr) = LocalVoteSignerService::new();
|
|
|
|
(Some(signer_service), addr)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut client = mk_client(&leader);
|
2019-01-11 12:58:31 -08:00
|
|
|
let vote_signer = VoteSignerProxy::new(&keypair, Box::new(RemoteVoteSigner::new(signer)));
|
|
|
|
let vote_account = vote_signer.vote_account;
|
|
|
|
info!("New vote account ID is {:?}", vote_account);
|
2019-01-10 09:21:38 -08:00
|
|
|
|
2018-10-10 16:49:41 -07:00
|
|
|
let mut fullnode = Fullnode::new(
|
|
|
|
node,
|
|
|
|
ledger_path,
|
2018-10-25 16:58:40 -07:00
|
|
|
keypair.clone(),
|
2019-01-11 12:58:31 -08:00
|
|
|
Arc::new(vote_signer),
|
2018-10-10 16:49:41 -07:00
|
|
|
network,
|
2018-11-25 13:11:07 -08:00
|
|
|
nosigverify,
|
2018-11-02 14:32:05 -07:00
|
|
|
leader_scheduler,
|
2018-11-05 10:50:58 -07:00
|
|
|
rpc_port,
|
2018-10-10 16:49:41 -07:00
|
|
|
);
|
2018-07-31 22:07:53 -07:00
|
|
|
|
2019-01-11 16:49:18 -08:00
|
|
|
if create_and_fund_vote_account(&mut client, vote_account, &keypair).is_err() {
|
2019-01-10 09:21:38 -08:00
|
|
|
if let Some(signer_service) = signer_service {
|
|
|
|
signer_service.join().unwrap();
|
2019-01-05 12:57:52 -08:00
|
|
|
}
|
2018-11-15 17:05:31 -08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-07-31 22:07:53 -07:00
|
|
|
|
2019-01-11 16:49:18 -08:00
|
|
|
wait_for_vote_account_registeration(&mut client, &vote_account, pubkey);
|
|
|
|
run_forever_and_do_role_transition(&mut fullnode);
|
2018-07-31 22:07:53 -07:00
|
|
|
|
2019-01-11 16:49:18 -08:00
|
|
|
if let Some(signer_service) = signer_service {
|
|
|
|
signer_service.join().unwrap();
|
2018-10-25 16:58:40 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 16:49:18 -08:00
|
|
|
exit(1);
|
2018-05-23 13:03:19 -07:00
|
|
|
}
|