From 5dd40d7d884d0f9da72e7e48d0fe95281c22474e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 11 Jun 2020 17:19:26 -0700 Subject: [PATCH] Enable jsonrpc client (#10522) (#10525) automerge --- core/src/rpc.rs | 96 ++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 9349b99c86..d4a754c50d 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -19,6 +19,7 @@ use solana_client::{ DELINQUENT_VALIDATOR_SLOT_DISTANCE, MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS_SLOT_RANGE, MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS, NUM_LARGEST_ACCOUNTS, }, + rpc_response::Response as RpcResponse, rpc_response::*, }; use solana_faucet::faucet::request_airdrop_transaction; @@ -50,9 +51,7 @@ use std::{ sync::{Arc, RwLock}, }; -type RpcResponse = Result>; - -fn new_response(bank: &Bank, value: T) -> RpcResponse { +fn new_response(bank: &Bank, value: T) -> Result> { let context = RpcResponseContext { slot: bank.slot() }; Ok(Response { context, value }) } @@ -155,7 +154,7 @@ impl JsonRpcRequestProcessor { &self, pubkey: Result, commitment: Option, - ) -> RpcResponse> { + ) -> Result>> { let bank = &*self.bank(commitment)?; pubkey.and_then(|key| new_response(bank, bank.get_account(&key).map(RpcAccount::encode))) } @@ -219,7 +218,7 @@ impl JsonRpcRequestProcessor { &self, pubkey: Result, commitment: Option, - ) -> RpcResponse { + ) -> Result> { let bank = &*self.bank(commitment)?; pubkey.and_then(|key| new_response(bank, bank.get_balance(&key))) } @@ -227,7 +226,7 @@ impl JsonRpcRequestProcessor { fn get_recent_blockhash( &self, commitment: Option, - ) -> RpcResponse { + ) -> Result> { let bank = &*self.bank(commitment)?; let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); new_response( @@ -239,7 +238,7 @@ impl JsonRpcRequestProcessor { ) } - fn get_fees(&self, commitment: Option) -> RpcResponse { + fn get_fees(&self, commitment: Option) -> Result> { let bank = &*self.bank(commitment)?; let (blockhash, fee_calculator) = bank.confirmed_last_blockhash(); let last_valid_slot = bank @@ -259,7 +258,7 @@ impl JsonRpcRequestProcessor { &self, blockhash: &Hash, commitment: Option, - ) -> RpcResponse> { + ) -> Result>> { let bank = &*self.bank(commitment)?; let fee_calculator = bank.get_fee_calculator(blockhash); new_response( @@ -268,7 +267,7 @@ impl JsonRpcRequestProcessor { ) } - fn get_fee_rate_governor(&self) -> RpcResponse { + fn get_fee_rate_governor(&self) -> Result> { let bank = &*self.bank(None)?; let fee_rate_governor = bank.get_fee_rate_governor(); new_response( @@ -283,7 +282,7 @@ impl JsonRpcRequestProcessor { &self, signature: Result, commitment: Option, - ) -> RpcResponse { + ) -> Result> { let bank = &*self.bank(commitment)?; match signature { Err(e) => Err(e), @@ -339,7 +338,7 @@ impl JsonRpcRequestProcessor { fn get_largest_accounts( &self, config: Option, - ) -> RpcResponse> { + ) -> Result>> { let config = config.unwrap_or_default(); let bank = self.bank(config.commitment)?; let (addresses, address_filter) = if let Some(filter) = config.filter { @@ -365,7 +364,7 @@ impl JsonRpcRequestProcessor { ) } - fn get_supply(&self, commitment: Option) -> RpcResponse { + 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(); @@ -586,7 +585,7 @@ impl JsonRpcRequestProcessor { &self, signatures: Vec, config: Option, - ) -> RpcResponse>> { + ) -> Result>>> { let mut statuses: Vec> = vec![]; let search_transaction_history = config @@ -741,7 +740,7 @@ fn run_transaction_simulation( (executed[0].0.clone().map(|_| ()), log_collector.output()) } -#[rpc(server)] +#[rpc] pub trait RpcSol { type Metadata; @@ -752,7 +751,7 @@ pub trait RpcSol { meta: Self::Metadata, signature_str: String, commitment: Option, - ) -> RpcResponse; + ) -> Result>; // DEPRECATED #[rpc(meta, name = "getSignatureStatus")] @@ -778,7 +777,7 @@ pub trait RpcSol { meta: Self::Metadata, pubkey_str: String, commitment: Option, - ) -> RpcResponse>; + ) -> Result>>; #[rpc(meta, name = "getProgramAccounts")] fn get_program_accounts( @@ -819,7 +818,7 @@ pub trait RpcSol { meta: Self::Metadata, pubkey_str: String, commitment: Option, - ) -> RpcResponse; + ) -> Result>; #[rpc(meta, name = "getClusterNodes")] fn get_cluster_nodes(&self, meta: Self::Metadata) -> Result>; @@ -854,14 +853,14 @@ pub trait RpcSol { &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse; + ) -> Result>; #[rpc(meta, name = "getFees")] fn get_fees( &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse; + ) -> Result>; #[rpc(meta, name = "getFeeCalculatorForBlockhash")] fn get_fee_calculator_for_blockhash( @@ -869,10 +868,13 @@ pub trait RpcSol { meta: Self::Metadata, blockhash: String, commitment: Option, - ) -> RpcResponse>; + ) -> Result>>; #[rpc(meta, name = "getFeeRateGovernor")] - fn get_fee_rate_governor(&self, meta: Self::Metadata) -> RpcResponse; + fn get_fee_rate_governor( + &self, + meta: Self::Metadata, + ) -> Result>; #[rpc(meta, name = "getSignatureStatuses")] fn get_signature_statuses( @@ -880,7 +882,7 @@ pub trait RpcSol { meta: Self::Metadata, signature_strs: Vec, config: Option, - ) -> RpcResponse>>; + ) -> Result>>>; #[rpc(meta, name = "getSlot")] fn get_slot(&self, meta: Self::Metadata, commitment: Option) -> Result; @@ -905,14 +907,14 @@ pub trait RpcSol { &self, meta: Self::Metadata, config: Option, - ) -> RpcResponse>; + ) -> Result>>; #[rpc(meta, name = "getSupply")] fn get_supply( &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse; + ) -> Result>; #[rpc(meta, name = "requestAirdrop")] fn request_airdrop( @@ -937,7 +939,7 @@ pub trait RpcSol { meta: Self::Metadata, data: String, config: Option, - ) -> RpcResponse; + ) -> Result>; #[rpc(meta, name = "getSlotLeader")] fn get_slot_leader( @@ -1017,7 +1019,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, id: String, commitment: Option, - ) -> RpcResponse { + ) -> Result> { debug!("confirm_transaction rpc request received: {:?}", id); let signature = verify_signature(&id); meta.confirm_transaction(signature, commitment) @@ -1028,7 +1030,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, pubkey_str: String, commitment: Option, - ) -> RpcResponse> { + ) -> Result>> { debug!("get_account_info rpc request received: {:?}", pubkey_str); let pubkey = verify_pubkey(pubkey_str); meta.get_account_info(pubkey, commitment) @@ -1089,7 +1091,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, pubkey_str: String, commitment: Option, - ) -> RpcResponse { + ) -> Result> { debug!("get_balance rpc request received: {:?}", pubkey_str); let pubkey = verify_pubkey(pubkey_str); meta.get_balance(pubkey, commitment) @@ -1181,7 +1183,7 @@ impl RpcSol for RpcSolImpl { &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse { + ) -> Result> { debug!("get_recent_blockhash rpc request received"); meta.get_recent_blockhash(commitment) } @@ -1190,7 +1192,7 @@ impl RpcSol for RpcSolImpl { &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse { + ) -> Result> { debug!("get_fees rpc request received"); meta.get_fees(commitment) } @@ -1200,14 +1202,17 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, blockhash: String, commitment: Option, - ) -> RpcResponse> { + ) -> Result>> { 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) } - fn get_fee_rate_governor(&self, meta: Self::Metadata) -> RpcResponse { + fn get_fee_rate_governor( + &self, + meta: Self::Metadata, + ) -> Result> { debug!("get_fee_rate_governor rpc request received"); meta.get_fee_rate_governor() } @@ -1241,7 +1246,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, signature_strs: Vec, config: Option, - ) -> RpcResponse>> { + ) -> Result>>> { if signature_strs.len() > MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS { return Err(Error::invalid_params(format!( "Too many inputs provided; max {}", @@ -1281,7 +1286,7 @@ impl RpcSol for RpcSolImpl { &self, meta: Self::Metadata, config: Option, - ) -> RpcResponse> { + ) -> Result>> { debug!("get_largest_accounts rpc request received"); meta.get_largest_accounts(config) } @@ -1290,7 +1295,7 @@ impl RpcSol for RpcSolImpl { &self, meta: Self::Metadata, commitment: Option, - ) -> RpcResponse { + ) -> Result> { debug!("get_supply rpc request received"); meta.get_supply(commitment) } @@ -1392,7 +1397,7 @@ impl RpcSol for RpcSolImpl { meta: Self::Metadata, data: String, config: Option, - ) -> RpcResponse { + ) -> Result> { let (_, transaction) = deserialize_bs58_transaction(data)?; let config = config.unwrap_or_default(); @@ -1558,7 +1563,10 @@ pub mod tests { replay_stage::tests::create_test_transactions_and_populate_blockstore, }; use bincode::deserialize; - use jsonrpc_core::{ErrorCode, MetaIoHandler, Output, Response, Value}; + use jsonrpc_core::{ + futures::future::Future, ErrorCode, MetaIoHandler, Output, Response, Value, + }; + use jsonrpc_core_client::transports::local; use solana_ledger::{ blockstore::entries_to_test_shreds, blockstore_processor::fill_blockstore_slot_with_ticks, @@ -1822,6 +1830,20 @@ pub mod tests { assert_eq!(expected, result); } + #[test] + fn test_rpc_get_balance_via_client() { + let bob_pubkey = Pubkey::new_rand(); + let handler = start_rpc_handler_with_tx(&bob_pubkey); + let fut = { + let (client, server) = + local::connect_with_metadata::(&handler.io, handler.meta); + client + .get_balance(bob_pubkey.to_string(), None) + .join(server) + }; + assert_eq!(20, fut.wait().unwrap().0.value); + } + #[test] fn test_rpc_get_cluster_nodes() { let bob_pubkey = Pubkey::new_rand();