From 3d9d7557c882444fb4c2600b97e16c389bd886d0 Mon Sep 17 00:00:00 2001 From: Ryo Onodera Date: Tue, 8 Dec 2020 01:43:03 +0900 Subject: [PATCH] core/validator: Wrap std::process:exit(1) for easier testing (#13990) --- core/src/validator.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/core/src/validator.rs b/core/src/validator.rs index c5e2cd8e3..464f99bea 100644 --- a/core/src/validator.rs +++ b/core/src/validator.rs @@ -62,7 +62,6 @@ use std::{ collections::HashSet, net::SocketAddr, path::{Path, PathBuf}, - process, sync::atomic::{AtomicBool, Ordering}, sync::mpsc::Receiver, sync::{mpsc::channel, Arc, Mutex, RwLock}, @@ -199,6 +198,15 @@ pub struct Validator { ip_echo_server: solana_net_utils::IpEchoServer, } +// in the distant future, get rid of ::new()/exit() and use Result properly... +fn abort() -> ! { + #[cfg(not(test))] + std::process::exit(1); + + #[cfg(test)] + panic!("process::exit(1) is intercepted for friendly test failure..."); +} + impl Validator { pub fn new( mut node: Node, @@ -240,7 +248,7 @@ impl Validator { "ledger directory does not exist or is not accessible: {:?}", ledger_path ); - process::exit(1); + abort(); } if let Some(shred_version) = config.expected_shred_version { @@ -331,7 +339,7 @@ impl Validator { "shred version mismatch: expected {} found: {}", expected_shred_version, node.info.shred_version, ); - process::exit(1); + abort(); } } @@ -495,7 +503,7 @@ impl Validator { }; if wait_for_supermajority(config, &bank, &cluster_info, rpc_override_health_check) { - std::process::exit(1); + abort(); } let poh_service = PohService::new(poh_recorder.clone(), &poh_config, &exit); @@ -777,7 +785,7 @@ fn post_process_restored_tower( "And there is an existing vote_account containing actual votes. \ Aborting due to possible conflicting duplicate votes", ); - process::exit(1); + abort(); } if err.is_file_missing() && !voting_has_been_active { // Currently, don't protect against spoofed snapshots with no tower at all @@ -837,7 +845,7 @@ fn new_banks_from_ledger( if genesis_hash != expected_genesis_hash { error!("genesis hash mismatch: expected {}", expected_genesis_hash); error!("Delete the ledger directory to continue: {:?}", ledger_path); - process::exit(1); + abort(); } } @@ -854,7 +862,7 @@ fn new_banks_from_ledger( if let Ok(tower) = &restored_tower { reconcile_blockstore_roots_with_tower(&tower, &blockstore).unwrap_or_else(|err| { error!("Failed to reconcile blockstore with tower: {:?}", err); - std::process::exit(1); + abort() }); } @@ -888,7 +896,7 @@ fn new_banks_from_ledger( ) .unwrap_or_else(|err| { error!("Failed to load ledger: {:?}", err); - process::exit(1); + abort() }); let tower = post_process_restored_tower( @@ -1110,7 +1118,7 @@ unsafe fn check_avx() { error!( "Your machine does not have AVX support, please rebuild from source on your machine" ); - process::exit(1); + abort(); } }