From d052de847e4b0dba5b7e4082d810df5bd998659a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 27 Aug 2019 15:56:46 -0700 Subject: [PATCH] feat: getInflation() endpoint (#5681) (#5692) (cherry picked from commit 34ab25a88b463703a12cf8395b49bf8001bb1c68) --- client/src/rpc_client.rs | 20 ++++++++++++++++++++ client/src/rpc_request.rs | 6 ++++++ core/src/rpc.rs | 40 +++++++++++++++++++++++++++++++++++++++ runtime/src/bank.rs | 5 +++++ 4 files changed, 71 insertions(+) diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index 6d3c287c9f..13b6507f8e 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -9,6 +9,7 @@ use serde_json::{json, Value}; use solana_sdk::account::Account; use solana_sdk::fee_calculator::FeeCalculator; use solana_sdk::hash::Hash; +use solana_sdk::inflation::Inflation; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{KeypairUtil, Signature}; use solana_sdk::timing::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT}; @@ -94,6 +95,25 @@ impl RpcClient { }) } + pub fn get_inflation(&self) -> io::Result { + let response = self + .client + .send(&RpcRequest::GetInflation, None, 0) + .map_err(|err| { + io::Error::new( + io::ErrorKind::Other, + format!("GetInflation request failure: {:?}", err), + ) + })?; + + serde_json::from_value(response).map_err(|err| { + io::Error::new( + io::ErrorKind::Other, + format!("GetInflation parse failure: {}", err), + ) + }) + } + pub fn get_version(&self) -> io::Result { let response = self .client diff --git a/client/src/rpc_request.rs b/client/src/rpc_request.rs index c992355b30..c0b0cdeb59 100644 --- a/client/src/rpc_request.rs +++ b/client/src/rpc_request.rs @@ -10,6 +10,7 @@ pub enum RpcRequest { GetBalance, GetClusterNodes, GetGenesisBlockhash, + GetInflation, GetNumBlocksSinceSignatureConfirmation, GetProgramAccounts, GetRecentBlockhash, @@ -40,6 +41,7 @@ impl RpcRequest { RpcRequest::GetBalance => "getBalance", RpcRequest::GetClusterNodes => "getClusterNodes", RpcRequest::GetGenesisBlockhash => "getGenesisBlockhash", + RpcRequest::GetInflation => "getInflation", RpcRequest::GetNumBlocksSinceSignatureConfirmation => { "getNumBlocksSinceSignatureConfirmation" } @@ -110,6 +112,10 @@ mod tests { let request = test_request.build_request_json(1, Some(addr)); assert_eq!(request["method"], "getBalance"); + let test_request = RpcRequest::GetInflation; + let request = test_request.build_request_json(1, None); + assert_eq!(request["method"], "getInflation"); + let test_request = RpcRequest::GetRecentBlockhash; let request = test_request.build_request_json(1, None); assert_eq!(request["method"], "getRecentBlockhash"); diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 114e0098a0..8841795e71 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -15,6 +15,7 @@ use solana_runtime::bank::Bank; use solana_sdk::account::Account; use solana_sdk::fee_calculator::FeeCalculator; use solana_sdk::hash::Hash; +use solana_sdk::inflation::Inflation; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::Signature; use solana_sdk::transaction::{self, Transaction}; @@ -81,6 +82,10 @@ impl JsonRpcRequestProcessor { .collect()) } + pub fn get_inflation(&self) -> Result { + Ok(self.bank().inflation()) + } + pub fn get_balance(&self, pubkey: &Pubkey) -> u64 { self.bank().get_balance(&pubkey) } @@ -291,6 +296,9 @@ pub trait RpcSol { #[rpc(meta, name = "getProgramAccounts")] fn get_program_accounts(&self, _: Self::Metadata, _: String) -> Result>; + #[rpc(meta, name = "getInflation")] + fn get_inflation(&self, _: Self::Metadata) -> Result; + #[rpc(meta, name = "getBalance")] fn get_balance(&self, _: Self::Metadata, _: String) -> Result; @@ -409,6 +417,16 @@ impl RpcSol for RpcSolImpl { .get_program_accounts(&program_id) } + fn get_inflation(&self, meta: Self::Metadata) -> Result { + debug!("get_inflation rpc request received"); + Ok(meta + .request_processor + .read() + .unwrap() + .get_inflation() + .unwrap()) + } + fn get_balance(&self, meta: Self::Metadata, id: String) -> Result { debug!("get_balance rpc request received: {:?}", id); let pubkey = verify_pubkey(id)?; @@ -854,6 +872,28 @@ pub mod tests { assert!(supply >= TEST_MINT_LAMPORTS); } + #[test] + fn test_rpc_get_inflation() { + let bob_pubkey = Pubkey::new_rand(); + let (io, meta, bank, _blockhash, _alice, _leader_pubkey) = + start_rpc_handler_with_tx(&bob_pubkey); + + let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getInflation"}}"#); + let rep = io.handle_request_sync(&req, meta); + let res: Response = serde_json::from_str(&rep.expect("actual response")) + .expect("actual response deserialization"); + let inflation: Inflation = if let Response::Single(res) = res { + if let Output::Success(res) = res { + serde_json::from_value(res.result).unwrap() + } else { + panic!("Expected success"); + } + } else { + panic!("Expected single response"); + }; + assert_eq!(inflation, bank.inflation()); + } + #[test] fn test_rpc_get_account_info() { let bob_pubkey = Pubkey::new_rand(); diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 804d170b8b..d7a574992e 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -1326,6 +1326,11 @@ impl Bank { self.tick_height.load(Ordering::Relaxed) as u64 } + /// Return the inflation parameters of the Bank + pub fn inflation(&self) -> Inflation { + self.inflation + } + /// Return the total capititalization of the Bank pub fn capitalization(&self) -> u64 { // capitalization is using an AtomicUSize because AtomicU64 is not yet a stable API.