From 1724430489d6bf732d8ce1fffe1897ef686d3ec4 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Fri, 11 Jan 2019 16:49:18 -0800 Subject: [PATCH] Remove clippy override for cyclomatic complexity (#2392) * Remove clippy override for cyclomatic complexity * reduce cyclomatic complexity of main function * fix compilation errors --- fullnode/src/main.rs | 199 +++++++++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 83 deletions(-) diff --git a/fullnode/src/main.rs b/fullnode/src/main.rs index e8847c556a..728aa1355c 100644 --- a/fullnode/src/main.rs +++ b/fullnode/src/main.rs @@ -1,6 +1,6 @@ extern crate serde_json; -use clap::{crate_version, App, Arg}; +use clap::{crate_version, App, Arg, ArgMatches}; use log::*; use solana::client::mk_client; @@ -10,19 +10,125 @@ use solana::leader_scheduler::LeaderScheduler; use solana::local_vote_signer_service::LocalVoteSignerService; use solana::service::Service; use solana::socketaddr; -use solana::thin_client::poll_gossip_for_leader; +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}; use solana_sdk::vote_program::VoteProgram; use solana_sdk::vote_transaction::VoteTransaction; use std::fs::File; +use std::io::{Error, ErrorKind, Result}; use std::net::{Ipv4Addr, SocketAddr}; use std::process::exit; use std::sync::Arc; use std::thread::sleep; use std::time::Duration; -#[allow(clippy::cyclomatic_complexity)] +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 = + 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, +) -> 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; + } + } + } +} + fn main() { solana_logger::setup(); solana_metrics::set_panic_hook("fullnode"); @@ -84,31 +190,7 @@ fn main() { let nosigverify = matches.is_present("nosigverify"); let use_only_bootstrap_leader = matches.is_present("no-leader-rotation"); - let (keypair, gossip) = 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 = - 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)) - }; + let (keypair, gossip) = parse_identity(&matches); let ledger_path = matches.value_of("ledger").unwrap(); @@ -183,68 +265,19 @@ fn main() { rpc_port, ); - let balance = client.poll_get_balance(&pubkey).unwrap_or(0); - info!("balance is {}", balance); - if balance < 1 { - error!("insufficient tokens, one token required"); + if create_and_fund_vote_account(&mut client, vote_account, &keypair).is_err() { if let Some(signer_service) = signer_service { signer_service.join().unwrap(); } exit(1); } - // 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"); - if let Some(signer_service) = signer_service { - signer_service.join().unwrap(); - } - exit(1); - } - loop { - let last_id = client.get_last_id(); - let transaction = - VoteTransaction::vote_account_new(&keypair, vote_account, last_id, 1, 1); - if client.transfer_signed(&transaction).is_err() { - sleep(Duration::from_secs(2)); - continue; - } + wait_for_vote_account_registeration(&mut client, &vote_account, pubkey); + run_forever_and_do_role_transition(&mut fullnode); - let balance = client.poll_get_balance(&vote_account).unwrap_or(0); - if balance > 0 { - break; - } - sleep(Duration::from_secs(2)); - } + if let Some(signer_service) = signer_service { + signer_service.join().unwrap(); } - 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 == pubkey { - break; - } - } - } - panic!("Expected successful vote account registration"); - } - - 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, so exit - if let Some(signer_service) = signer_service { - signer_service.join().unwrap(); - } - exit(1); - } - } - } + exit(1); }