From af3bea51ec97ad8331f89944e0208584a62b955c Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 3 Jul 2020 11:31:54 -0600 Subject: [PATCH 1/3] Remove unreachable error case --- core/src/rpc.rs | 85 ++++++++++++++++++++----------------------- core/src/rpc_error.rs | 16 -------- 2 files changed, 40 insertions(+), 61 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 0a55e0ee0493e3..ebebacd682fe4d 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -99,7 +99,7 @@ pub struct JsonRpcRequestProcessor { impl Metadata for JsonRpcRequestProcessor {} impl JsonRpcRequestProcessor { - fn bank(&self, commitment: Option) -> Result> { + fn bank(&self, commitment: Option) -> Arc { debug!("RPC commitment_config: {:?}", commitment); let r_bank_forks = self.bank_forks.read().unwrap(); @@ -108,16 +108,16 @@ impl JsonRpcRequestProcessor { Some(config) => config.commitment, }; - match commitment_level { + let slot = match commitment_level { CommitmentLevel::Recent => { - let bank = r_bank_forks.working_bank(); - debug!("RPC using working_bank: {:?}", bank.slot()); - Ok(bank) + let slot = r_bank_forks.highest_slot(); + debug!("RPC using working_bank: {:?}", slot); + slot } CommitmentLevel::Root => { let slot = r_bank_forks.root(); debug!("RPC using node root: {:?}", slot); - Ok(r_bank_forks.get(slot).cloned().unwrap()) + slot } CommitmentLevel::Single | CommitmentLevel::SingleGossip => { let slot = self @@ -126,24 +126,19 @@ impl JsonRpcRequestProcessor { .unwrap() .highest_confirmed_slot(); debug!("RPC using confirmed slot: {:?}", slot); - Ok(r_bank_forks.get(slot).cloned().unwrap()) + slot } CommitmentLevel::Max => { - let cluster_root = self + let slot = self .block_commitment_cache .read() .unwrap() .largest_confirmed_root(); - debug!("RPC using block: {:?}", cluster_root); - r_bank_forks.get(cluster_root).cloned().ok_or_else(|| { - RpcCustomError::NonexistentClusterRoot { - cluster_root, - node_root: r_bank_forks.root(), - } - .into() - }) + debug!("RPC using block: {:?}", slot); + slot } - } + }; + r_bank_forks.get(slot).cloned().unwrap() } pub fn new( @@ -210,7 +205,7 @@ impl JsonRpcRequestProcessor { config: Option, ) -> Result>> { let config = config.unwrap_or_default(); - let bank = self.bank(config.commitment)?; + let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary); new_response( &bank, @@ -225,7 +220,7 @@ impl JsonRpcRequestProcessor { commitment: Option, ) -> Result { Ok(self - .bank(commitment)? + .bank(commitment) .get_minimum_balance_for_rent_exemption(data_len)) } @@ -236,7 +231,7 @@ impl JsonRpcRequestProcessor { filters: Vec, ) -> Result> { let config = config.unwrap_or_default(); - let bank = self.bank(config.commitment)?; + let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary); Ok(bank .get_program_accounts(Some(&program_id)) @@ -258,11 +253,11 @@ impl JsonRpcRequestProcessor { &self, commitment: Option, ) -> Result { - Ok(self.bank(commitment)?.inflation().into()) + Ok(self.bank(commitment).inflation().into()) } pub fn get_inflation_rate(&self) -> Result { - let bank = self.bank(None)?; + let bank = self.bank(None); let epoch = bank.epoch(); let inflation = bank.inflation(); let year = @@ -279,7 +274,7 @@ impl JsonRpcRequestProcessor { pub fn get_epoch_schedule(&self) -> Result { // Since epoch schedule data comes from the genesis config, any commitment level should be // fine - let bank = self.bank(Some(CommitmentConfig::root()))?; + let bank = self.bank(Some(CommitmentConfig::root())); Ok(*bank.epoch_schedule()) } @@ -288,7 +283,7 @@ impl JsonRpcRequestProcessor { pubkey: &Pubkey, commitment: Option, ) -> Result> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); new_response(&bank, bank.get_balance(pubkey)) } @@ -296,7 +291,7 @@ impl JsonRpcRequestProcessor { &self, commitment: Option, ) -> Result> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); new_response( &bank, @@ -308,7 +303,7 @@ impl JsonRpcRequestProcessor { } fn get_fees(&self, commitment: Option) -> Result> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); let last_valid_slot = bank .get_blockhash_last_valid_slot(&blockhash) @@ -328,7 +323,7 @@ impl JsonRpcRequestProcessor { blockhash: &Hash, commitment: Option, ) -> Result>> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let fee_calculator = bank.get_fee_calculator(blockhash); new_response( &bank, @@ -337,7 +332,7 @@ impl JsonRpcRequestProcessor { } fn get_fee_rate_governor(&self) -> Result> { - let bank = self.bank(None)?; + let bank = self.bank(None); let fee_rate_governor = bank.get_fee_rate_governor(); new_response( &bank, @@ -352,7 +347,7 @@ impl JsonRpcRequestProcessor { signature: &Signature, commitment: Option, ) -> Result> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let status = bank.get_signature_status(signature); match status { Some(status) => new_response(&bank, status.is_ok()), @@ -371,11 +366,11 @@ impl JsonRpcRequestProcessor { } fn get_slot(&self, commitment: Option) -> Result { - Ok(self.bank(commitment)?.slot()) + Ok(self.bank(commitment).slot()) } fn get_slot_leader(&self, commitment: Option) -> Result { - Ok(self.bank(commitment)?.collector_id().to_string()) + Ok(self.bank(commitment).collector_id().to_string()) } fn minimum_ledger_slot(&self) -> Result { @@ -392,11 +387,11 @@ impl JsonRpcRequestProcessor { } fn get_transaction_count(&self, commitment: Option) -> Result { - Ok(self.bank(commitment)?.transaction_count() as u64) + Ok(self.bank(commitment).transaction_count() as u64) } fn get_total_supply(&self, commitment: Option) -> Result { - Ok(self.bank(commitment)?.capitalization()) + Ok(self.bank(commitment).capitalization()) } fn get_largest_accounts( @@ -404,7 +399,7 @@ impl JsonRpcRequestProcessor { config: Option, ) -> Result>> { let config = config.unwrap_or_default(); - let bank = self.bank(config.commitment)?; + let bank = self.bank(config.commitment); let (addresses, address_filter) = if let Some(filter) = config.filter { let non_circulating_supply = calculate_non_circulating_supply(&bank); let addresses = non_circulating_supply.accounts.into_iter().collect(); @@ -429,7 +424,7 @@ impl JsonRpcRequestProcessor { } fn get_supply(&self, commitment: Option) -> Result> { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let non_circulating_supply = calculate_non_circulating_supply(&bank); let total_supply = bank.capitalization(); new_response( @@ -451,7 +446,7 @@ impl JsonRpcRequestProcessor { &self, commitment: Option, ) -> Result { - let bank = self.bank(commitment)?; + let bank = self.bank(commitment); let vote_accounts = bank.vote_accounts(); let epoch_vote_accounts = bank .epoch_vote_accounts(bank.get_epoch_and_slot_index(bank.slot()).0) @@ -602,7 +597,7 @@ impl JsonRpcRequestProcessor { // queried). If these values will be variable in the future, those timing parameters will // need to be stored persistently, and the slot_duration calculation will likely need to be // moved upstream into blockstore. Also, an explicit commitment level will need to be set. - let bank = self.bank(None)?; + let bank = self.bank(None); let slot_duration = slot_duration_from_slots_per_year(bank.slots_per_year()); let epoch = bank.epoch_schedule().get_epoch(slot); let stakes = HashMap::new(); @@ -621,7 +616,7 @@ impl JsonRpcRequestProcessor { signature: Signature, commitment: Option, ) -> Option { - let bank = self.bank(commitment).ok()?; + let bank = self.bank(commitment); let transaction_status = self.get_transaction_status(signature, &bank)?; let confirmations = transaction_status .confirmations @@ -637,7 +632,7 @@ impl JsonRpcRequestProcessor { signature: Signature, commitment: Option, ) -> Option> { - let bank = self.bank(commitment).ok()?; + let bank = self.bank(commitment); let (_, status) = bank.get_signature_status_slot(&signature)?; Some(status) } @@ -652,7 +647,7 @@ impl JsonRpcRequestProcessor { let search_transaction_history = config .map(|x| x.search_transaction_history) .unwrap_or(false); - let bank = self.bank(Some(CommitmentConfig::recent()))?; + let bank = self.bank(Some(CommitmentConfig::recent())); for signature in signatures { let status = if let Some(status) = self.get_transaction_status(signature, &bank) { @@ -1207,7 +1202,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, commitment: Option, ) -> Result { - let bank = meta.bank(commitment)?; + let bank = meta.bank(commitment); Ok(bank.get_epoch_info()) } @@ -1230,7 +1225,7 @@ impl RpcSol for RpcSolImpl { slot: Option, commitment: Option, ) -> Result> { - let bank = meta.bank(commitment)?; + let bank = meta.bank(commitment); let slot = slot.unwrap_or_else(|| bank.slot()); let epoch = bank.epoch_schedule().get_epoch(slot); @@ -1390,7 +1385,7 @@ impl RpcSol for RpcSolImpl { let pubkey = verify_pubkey(pubkey_str)?; let (blockhash, last_valid_slot) = { - let bank = meta.bank(commitment)?; + let bank = meta.bank(commitment); let blockhash = bank.confirmed_last_blockhash().0; ( @@ -1426,7 +1421,7 @@ impl RpcSol for RpcSolImpl { let config = config.unwrap_or_default(); let (wire_transaction, transaction) = deserialize_bs58_transaction(data)?; let signature = transaction.signatures[0]; - let bank = &*meta.bank(None)?; + let bank = &*meta.bank(None); let last_valid_slot = bank .get_blockhash_last_valid_slot(&transaction.message.recent_blockhash) .unwrap_or(0); @@ -1479,7 +1474,7 @@ impl RpcSol for RpcSolImpl { Ok(()) }; - let bank = &*meta.bank(None)?; + let bank = &*meta.bank(None); let logs = if result.is_ok() { let sim_result = run_transaction_simulation(&bank, transaction); result = sim_result.0; diff --git a/core/src/rpc_error.rs b/core/src/rpc_error.rs index 276ad96c5f63d5..9125b65c7fd0a8 100644 --- a/core/src/rpc_error.rs +++ b/core/src/rpc_error.rs @@ -1,15 +1,10 @@ use jsonrpc_core::{Error, ErrorCode}; use solana_sdk::clock::Slot; -const JSON_RPC_SERVER_ERROR_0: i64 = -32000; const JSON_RPC_SERVER_ERROR_1: i64 = -32001; const JSON_RPC_SERVER_ERROR_2: i64 = -32002; pub enum RpcCustomError { - NonexistentClusterRoot { - cluster_root: Slot, - node_root: Slot, - }, BlockCleanedUp { slot: Slot, first_available_block: Slot, @@ -22,17 +17,6 @@ pub enum RpcCustomError { impl From for Error { fn from(e: RpcCustomError) -> Self { match e { - RpcCustomError::NonexistentClusterRoot { - cluster_root, - node_root, - } => Self { - code: ErrorCode::ServerError(JSON_RPC_SERVER_ERROR_0), - message: format!( - "Cluster largest_confirmed_root {} does not exist on node. Node root: {}", - cluster_root, node_root, - ), - data: None, - }, RpcCustomError::BlockCleanedUp { slot, first_available_block, From 46938aa5b3f16092a4cdbc6bba9ff747acf9c44b Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 3 Jul 2020 11:54:04 -0600 Subject: [PATCH 2/3] Inline Ok --- core/src/rpc.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index ebebacd682fe4d..b621c1ca7ae235 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -56,9 +56,9 @@ use std::{ }, }; -fn new_response(bank: &Bank, value: T) -> Result> { +fn new_response(bank: &Bank, value: T) -> RpcResponse { let context = RpcResponseContext { slot: bank.slot() }; - Ok(Response { context, value }) + Response { context, value } } pub fn is_confirmed_rooted( @@ -207,11 +207,11 @@ impl JsonRpcRequestProcessor { let config = config.unwrap_or_default(); let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary); - new_response( + Ok(new_response( &bank, bank.get_account(pubkey) .map(|account| UiAccount::encode(account, encoding)), - ) + )) } pub fn get_minimum_balance_for_rent_exemption( @@ -284,7 +284,7 @@ impl JsonRpcRequestProcessor { commitment: Option, ) -> Result> { let bank = self.bank(commitment); - new_response(&bank, bank.get_balance(pubkey)) + Ok(new_response(&bank, bank.get_balance(pubkey))) } fn get_recent_blockhash( @@ -293,13 +293,13 @@ impl JsonRpcRequestProcessor { ) -> Result> { let bank = self.bank(commitment); let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); - new_response( + Ok(new_response( &bank, RpcBlockhashFeeCalculator { blockhash: blockhash.to_string(), fee_calculator, }, - ) + )) } fn get_fees(&self, commitment: Option) -> Result> { @@ -308,14 +308,14 @@ impl JsonRpcRequestProcessor { let last_valid_slot = bank .get_blockhash_last_valid_slot(&blockhash) .expect("bank blockhash queue should contain blockhash"); - new_response( + Ok(new_response( &bank, RpcFees { blockhash: blockhash.to_string(), fee_calculator, last_valid_slot, }, - ) + )) } fn get_fee_calculator_for_blockhash( @@ -325,21 +325,21 @@ impl JsonRpcRequestProcessor { ) -> Result>> { let bank = self.bank(commitment); let fee_calculator = bank.get_fee_calculator(blockhash); - new_response( + Ok(new_response( &bank, fee_calculator.map(|fee_calculator| RpcFeeCalculator { fee_calculator }), - ) + )) } fn get_fee_rate_governor(&self) -> Result> { let bank = self.bank(None); let fee_rate_governor = bank.get_fee_rate_governor(); - new_response( + Ok(new_response( &bank, RpcFeeRateGovernor { fee_rate_governor: fee_rate_governor.clone(), }, - ) + )) } pub fn confirm_transaction( @@ -350,8 +350,8 @@ impl JsonRpcRequestProcessor { let bank = self.bank(commitment); let status = bank.get_signature_status(signature); match status { - Some(status) => new_response(&bank, status.is_ok()), - None => new_response(&bank, false), + Some(status) => Ok(new_response(&bank, status.is_ok())), + None => Ok(new_response(&bank, false)), } } @@ -411,7 +411,7 @@ impl JsonRpcRequestProcessor { } else { (HashSet::new(), AccountAddressFilter::Exclude) }; - new_response( + Ok(new_response( &bank, bank.get_largest_accounts(NUM_LARGEST_ACCOUNTS, &addresses, address_filter) .into_iter() @@ -420,14 +420,14 @@ impl JsonRpcRequestProcessor { lamports, }) .collect(), - ) + )) } fn get_supply(&self, commitment: Option) -> Result> { let bank = self.bank(commitment); let non_circulating_supply = calculate_non_circulating_supply(&bank); let total_supply = bank.capitalization(); - new_response( + Ok(new_response( &bank, RpcSupply { total: total_supply, @@ -439,7 +439,7 @@ impl JsonRpcRequestProcessor { .map(|pubkey| pubkey.to_string()) .collect(), }, - ) + )) } fn get_vote_accounts( @@ -1483,13 +1483,13 @@ impl RpcSol for RpcSolImpl { None }; - new_response( + Ok(new_response( &bank, RpcSimulateTransactionResult { err: result.err(), logs, }, - ) + )) } fn get_slot_leader( From 8f4f743bc6b239bfc5d3df09a31b6bea41bfb59c Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 3 Jul 2020 12:21:58 -0600 Subject: [PATCH 3/3] Hoist Ok --- core/src/rpc.rs | 193 +++++++++++++++++++--------------------- core/src/rpc_service.rs | 1 - 2 files changed, 93 insertions(+), 101 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index b621c1ca7ae235..f417e15772b455 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -203,25 +203,24 @@ impl JsonRpcRequestProcessor { &self, pubkey: &Pubkey, config: Option, - ) -> Result>> { + ) -> RpcResponse> { let config = config.unwrap_or_default(); let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary); - Ok(new_response( + new_response( &bank, bank.get_account(pubkey) .map(|account| UiAccount::encode(account, encoding)), - )) + ) } pub fn get_minimum_balance_for_rent_exemption( &self, data_len: usize, commitment: Option, - ) -> Result { - Ok(self - .bank(commitment) - .get_minimum_balance_for_rent_exemption(data_len)) + ) -> u64 { + self.bank(commitment) + .get_minimum_balance_for_rent_exemption(data_len) } pub fn get_program_accounts( @@ -229,12 +228,11 @@ impl JsonRpcRequestProcessor { program_id: &Pubkey, config: Option, filters: Vec, - ) -> Result> { + ) -> Vec { let config = config.unwrap_or_default(); let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Binary); - Ok(bank - .get_program_accounts(Some(&program_id)) + bank.get_program_accounts(Some(&program_id)) .into_iter() .filter(|(_, account)| { filters.iter().all(|filter_type| match filter_type { @@ -246,112 +244,112 @@ impl JsonRpcRequestProcessor { pubkey: pubkey.to_string(), account: UiAccount::encode(account, encoding.clone()), }) - .collect()) + .collect() } pub fn get_inflation_governor( &self, commitment: Option, - ) -> Result { - Ok(self.bank(commitment).inflation().into()) + ) -> RpcInflationGovernor { + self.bank(commitment).inflation().into() } - pub fn get_inflation_rate(&self) -> Result { + pub fn get_inflation_rate(&self) -> RpcInflationRate { let bank = self.bank(None); let epoch = bank.epoch(); let inflation = bank.inflation(); let year = (bank.epoch_schedule().get_last_slot_in_epoch(epoch)) as f64 / bank.slots_per_year(); - Ok(RpcInflationRate { + RpcInflationRate { total: inflation.total(year), validator: inflation.validator(year), foundation: inflation.foundation(year), epoch, - }) + } } - pub fn get_epoch_schedule(&self) -> Result { + pub fn get_epoch_schedule(&self) -> EpochSchedule { // Since epoch schedule data comes from the genesis config, any commitment level should be // fine let bank = self.bank(Some(CommitmentConfig::root())); - Ok(*bank.epoch_schedule()) + *bank.epoch_schedule() } pub fn get_balance( &self, pubkey: &Pubkey, commitment: Option, - ) -> Result> { + ) -> RpcResponse { let bank = self.bank(commitment); - Ok(new_response(&bank, bank.get_balance(pubkey))) + new_response(&bank, bank.get_balance(pubkey)) } fn get_recent_blockhash( &self, commitment: Option, - ) -> Result> { + ) -> RpcResponse { let bank = self.bank(commitment); let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); - Ok(new_response( + new_response( &bank, RpcBlockhashFeeCalculator { blockhash: blockhash.to_string(), fee_calculator, }, - )) + ) } - fn get_fees(&self, commitment: Option) -> Result> { + fn get_fees(&self, commitment: Option) -> RpcResponse { let bank = self.bank(commitment); let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); let last_valid_slot = bank .get_blockhash_last_valid_slot(&blockhash) .expect("bank blockhash queue should contain blockhash"); - Ok(new_response( + new_response( &bank, RpcFees { blockhash: blockhash.to_string(), fee_calculator, last_valid_slot, }, - )) + ) } fn get_fee_calculator_for_blockhash( &self, blockhash: &Hash, commitment: Option, - ) -> Result>> { + ) -> RpcResponse> { let bank = self.bank(commitment); let fee_calculator = bank.get_fee_calculator(blockhash); - Ok(new_response( + new_response( &bank, fee_calculator.map(|fee_calculator| RpcFeeCalculator { fee_calculator }), - )) + ) } - fn get_fee_rate_governor(&self) -> Result> { + fn get_fee_rate_governor(&self) -> RpcResponse { let bank = self.bank(None); let fee_rate_governor = bank.get_fee_rate_governor(); - Ok(new_response( + new_response( &bank, RpcFeeRateGovernor { fee_rate_governor: fee_rate_governor.clone(), }, - )) + ) } pub fn confirm_transaction( &self, signature: &Signature, commitment: Option, - ) -> Result> { + ) -> RpcResponse { let bank = self.bank(commitment); let status = bank.get_signature_status(signature); match status { - Some(status) => Ok(new_response(&bank, status.is_ok())), - None => Ok(new_response(&bank, false)), + Some(status) => new_response(&bank, status.is_ok()), + None => new_response(&bank, false), } } @@ -365,12 +363,12 @@ impl JsonRpcRequestProcessor { } } - fn get_slot(&self, commitment: Option) -> Result { - Ok(self.bank(commitment).slot()) + fn get_slot(&self, commitment: Option) -> u64 { + self.bank(commitment).slot() } - fn get_slot_leader(&self, commitment: Option) -> Result { - Ok(self.bank(commitment).collector_id().to_string()) + fn get_slot_leader(&self, commitment: Option) -> String { + self.bank(commitment).collector_id().to_string() } fn minimum_ledger_slot(&self) -> Result { @@ -386,18 +384,18 @@ impl JsonRpcRequestProcessor { } } - fn get_transaction_count(&self, commitment: Option) -> Result { - Ok(self.bank(commitment).transaction_count() as u64) + fn get_transaction_count(&self, commitment: Option) -> u64 { + self.bank(commitment).transaction_count() as u64 } - fn get_total_supply(&self, commitment: Option) -> Result { - Ok(self.bank(commitment).capitalization()) + fn get_total_supply(&self, commitment: Option) -> u64 { + self.bank(commitment).capitalization() } fn get_largest_accounts( &self, config: Option, - ) -> Result>> { + ) -> RpcResponse> { let config = config.unwrap_or_default(); let bank = self.bank(config.commitment); let (addresses, address_filter) = if let Some(filter) = config.filter { @@ -411,7 +409,7 @@ impl JsonRpcRequestProcessor { } else { (HashSet::new(), AccountAddressFilter::Exclude) }; - Ok(new_response( + new_response( &bank, bank.get_largest_accounts(NUM_LARGEST_ACCOUNTS, &addresses, address_filter) .into_iter() @@ -420,14 +418,14 @@ impl JsonRpcRequestProcessor { lamports, }) .collect(), - )) + ) } - fn get_supply(&self, commitment: Option) -> Result> { + fn get_supply(&self, commitment: Option) -> RpcResponse { let bank = self.bank(commitment); let non_circulating_supply = calculate_non_circulating_supply(&bank); let total_supply = bank.capitalization(); - Ok(new_response( + new_response( &bank, RpcSupply { total: total_supply, @@ -439,7 +437,7 @@ impl JsonRpcRequestProcessor { .map(|pubkey| pubkey.to_string()) .collect(), }, - )) + ) } fn get_vote_accounts( @@ -497,23 +495,22 @@ impl JsonRpcRequestProcessor { }) } - pub fn set_log_filter(&self, filter: String) -> Result<()> { + pub fn set_log_filter(&self, filter: String) { if self.config.enable_set_log_filter { solana_logger::setup_with(&filter); } - Ok(()) } - pub fn validator_exit(&self) -> Result { + pub fn validator_exit(&self) -> bool { if self.config.enable_validator_exit { warn!("validator_exit request..."); if let Some(x) = self.validator_exit.write().unwrap().take() { x.exit() } - Ok(true) + true } else { debug!("validator_exit ignored"); - Ok(false) + false } } @@ -713,10 +710,9 @@ impl JsonRpcRequestProcessor { &self, signature: Signature, encoding: Option, - ) -> Result> { + ) -> Option { if self.config.enable_rpc_transaction_history { - Ok(self - .blockstore + self.blockstore .get_confirmed_transaction(signature, encoding) .unwrap_or(None) .filter(|confirmed_transaction| { @@ -726,9 +722,9 @@ impl JsonRpcRequestProcessor { .read() .unwrap() .largest_confirmed_root() - })) + }) } else { - Ok(None) + None } } @@ -737,7 +733,7 @@ impl JsonRpcRequestProcessor { pubkey: Pubkey, start_slot: Slot, end_slot: Slot, - ) -> Result> { + ) -> Vec { if self.config.enable_rpc_transaction_history { let end_slot = min( end_slot, @@ -746,20 +742,18 @@ impl JsonRpcRequestProcessor { .unwrap() .largest_confirmed_root(), ); - Ok(self - .blockstore + self.blockstore .get_confirmed_signatures_for_address(pubkey, start_slot, end_slot) - .unwrap_or_else(|_| vec![])) + .unwrap_or_else(|_| vec![]) } else { - Ok(vec![]) + vec![] } } - pub fn get_first_available_block(&self) -> Result { - Ok(self - .blockstore + pub fn get_first_available_block(&self) -> Slot { + self.blockstore .get_first_available_block() - .unwrap_or_default()) + .unwrap_or_default() } } @@ -1082,7 +1076,7 @@ impl RpcSol for RpcSolImpl { ) -> Result> { debug!("confirm_transaction rpc request received: {:?}", id); let signature = verify_signature(&id)?; - meta.confirm_transaction(&signature, commitment) + Ok(meta.confirm_transaction(&signature, commitment)) } fn get_account_info( @@ -1093,7 +1087,7 @@ impl RpcSol for RpcSolImpl { ) -> Result>> { debug!("get_account_info rpc request received: {:?}", pubkey_str); let pubkey = verify_pubkey(pubkey_str)?; - meta.get_account_info(&pubkey, config) + Ok(meta.get_account_info(&pubkey, config)) } fn get_minimum_balance_for_rent_exemption( @@ -1106,7 +1100,7 @@ impl RpcSol for RpcSolImpl { "get_minimum_balance_for_rent_exemption rpc request received: {:?}", data_len ); - meta.get_minimum_balance_for_rent_exemption(data_len, commitment) + Ok(meta.get_minimum_balance_for_rent_exemption(data_len, commitment)) } fn get_program_accounts( @@ -1131,7 +1125,7 @@ impl RpcSol for RpcSolImpl { for filter in &filters { verify_filter(filter)?; } - meta.get_program_accounts(&program_id, config, filters) + Ok(meta.get_program_accounts(&program_id, config, filters)) } fn get_inflation_governor( @@ -1140,17 +1134,17 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result { debug!("get_inflation_governor rpc request received"); - meta.get_inflation_governor(commitment) + Ok(meta.get_inflation_governor(commitment)) } fn get_inflation_rate(&self, meta: Self::Metadata) -> Result { debug!("get_inflation_rate rpc request received"); - meta.get_inflation_rate() + Ok(meta.get_inflation_rate()) } fn get_epoch_schedule(&self, meta: Self::Metadata) -> Result { debug!("get_epoch_schedule rpc request received"); - meta.get_epoch_schedule() + Ok(meta.get_epoch_schedule()) } fn get_balance( @@ -1161,7 +1155,7 @@ impl RpcSol for RpcSolImpl { ) -> Result> { debug!("get_balance rpc request received: {:?}", pubkey_str); let pubkey = verify_pubkey(pubkey_str)?; - meta.get_balance(&pubkey, commitment) + Ok(meta.get_balance(&pubkey, commitment)) } fn get_cluster_nodes(&self, meta: Self::Metadata) -> Result> { @@ -1252,7 +1246,7 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result> { debug!("get_recent_blockhash rpc request received"); - meta.get_recent_blockhash(commitment) + Ok(meta.get_recent_blockhash(commitment)) } fn get_fees( @@ -1261,7 +1255,7 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result> { debug!("get_fees rpc request received"); - meta.get_fees(commitment) + Ok(meta.get_fees(commitment)) } fn get_fee_calculator_for_blockhash( @@ -1273,7 +1267,7 @@ impl RpcSol for RpcSolImpl { debug!("get_fee_calculator_for_blockhash rpc request received"); let blockhash = Hash::from_str(&blockhash).map_err(|e| Error::invalid_params(format!("{:?}", e)))?; - meta.get_fee_calculator_for_blockhash(&blockhash, commitment) + Ok(meta.get_fee_calculator_for_blockhash(&blockhash, commitment)) } fn get_fee_rate_governor( @@ -1281,7 +1275,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, ) -> Result> { debug!("get_fee_rate_governor rpc request received"); - meta.get_fee_rate_governor() + Ok(meta.get_fee_rate_governor()) } fn get_signature_confirmation( @@ -1328,7 +1322,7 @@ impl RpcSol for RpcSolImpl { } fn get_slot(&self, meta: Self::Metadata, commitment: Option) -> Result { - meta.get_slot(commitment) + Ok(meta.get_slot(commitment)) } fn get_transaction_count( @@ -1337,7 +1331,7 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result { debug!("get_transaction_count rpc request received"); - meta.get_transaction_count(commitment) + Ok(meta.get_transaction_count(commitment)) } fn get_total_supply( @@ -1346,7 +1340,7 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result { debug!("get_total_supply rpc request received"); - meta.get_total_supply(commitment) + Ok(meta.get_total_supply(commitment)) } fn get_largest_accounts( @@ -1355,7 +1349,7 @@ impl RpcSol for RpcSolImpl { config: Option, ) -> Result>> { debug!("get_largest_accounts rpc request received"); - meta.get_largest_accounts(config) + Ok(meta.get_largest_accounts(config)) } fn get_supply( @@ -1364,7 +1358,7 @@ impl RpcSol for RpcSolImpl { commitment: Option, ) -> Result> { debug!("get_supply rpc request received"); - meta.get_supply(commitment) + Ok(meta.get_supply(commitment)) } fn request_airdrop( @@ -1497,7 +1491,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, commitment: Option, ) -> Result { - meta.get_slot_leader(commitment) + Ok(meta.get_slot_leader(commitment)) } fn minimum_ledger_slot(&self, meta: Self::Metadata) -> Result { @@ -1513,7 +1507,7 @@ impl RpcSol for RpcSolImpl { } fn validator_exit(&self, meta: Self::Metadata) -> Result { - meta.validator_exit() + Ok(meta.validator_exit()) } fn get_identity(&self, meta: Self::Metadata) -> Result { @@ -1529,7 +1523,8 @@ impl RpcSol for RpcSolImpl { } fn set_log_filter(&self, meta: Self::Metadata, filter: String) -> Result<()> { - meta.set_log_filter(filter) + meta.set_log_filter(filter); + Ok(()) } fn get_confirmed_block( @@ -1561,7 +1556,7 @@ impl RpcSol for RpcSolImpl { encoding: Option, ) -> Result> { let signature = verify_signature(&signature_str)?; - meta.get_confirmed_transaction(signature, encoding) + Ok(meta.get_confirmed_transaction(signature, encoding)) } fn get_confirmed_signatures_for_address( @@ -1584,17 +1579,15 @@ impl RpcSol for RpcSolImpl { MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS_SLOT_RANGE ))); } - meta.get_confirmed_signatures_for_address(pubkey, start_slot, end_slot) - .map(|signatures| { - signatures - .iter() - .map(|signature| signature.to_string()) - .collect() - }) + Ok(meta + .get_confirmed_signatures_for_address(pubkey, start_slot, end_slot) + .iter() + .map(|signature| signature.to_string()) + .collect()) } fn get_first_available_block(&self, meta: Self::Metadata) -> Result { - meta.get_first_available_block() + Ok(meta.get_first_available_block()) } } @@ -1844,7 +1837,7 @@ pub mod tests { bank.transfer(20, &genesis.mint_keypair, &bob_pubkey) .unwrap(); let request_processor = JsonRpcRequestProcessor::new_from_bank(&bank); - assert_eq!(request_processor.get_transaction_count(None).unwrap(), 1); + assert_eq!(request_processor.get_transaction_count(None), 1); } #[test] @@ -3150,7 +3143,7 @@ pub mod tests { &exit, )), ); - assert_eq!(request_processor.validator_exit(), Ok(false)); + assert_eq!(request_processor.validator_exit(), false); assert_eq!(exit.load(Ordering::Relaxed), false); } @@ -3180,7 +3173,7 @@ pub mod tests { &exit, )), ); - assert_eq!(request_processor.validator_exit(), Ok(true)); + assert_eq!(request_processor.validator_exit(), true); assert_eq!(exit.load(Ordering::Relaxed), true); } diff --git a/core/src/rpc_service.rs b/core/src/rpc_service.rs index fe1b6a023db7a3..f9bc93db35f986 100644 --- a/core/src/rpc_service.rs +++ b/core/src/rpc_service.rs @@ -402,7 +402,6 @@ mod tests { rpc_service .request_processor .get_balance(&mint_keypair.pubkey(), None) - .unwrap() .value ); rpc_service.exit();