feat: getInflation() endpoint (#5681)

This commit is contained in:
Sunny Gleason 2019-08-27 18:17:03 -04:00 committed by GitHub
parent 8b9c3a2561
commit 34ab25a88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 0 deletions

View File

@ -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<Inflation> {
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<String> {
let response = self
.client

View File

@ -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");

View File

@ -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<Inflation> {
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<Vec<(String, Account)>>;
#[rpc(meta, name = "getInflation")]
fn get_inflation(&self, _: Self::Metadata) -> Result<Inflation>;
#[rpc(meta, name = "getBalance")]
fn get_balance(&self, _: Self::Metadata, _: String) -> Result<u64>;
@ -409,6 +417,16 @@ impl RpcSol for RpcSolImpl {
.get_program_accounts(&program_id)
}
fn get_inflation(&self, meta: Self::Metadata) -> Result<Inflation> {
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<u64> {
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();

View File

@ -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.