From 5b51bb27b65902f880155c9122b0b417a270cdaa Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Wed, 7 Aug 2019 20:06:27 -0600 Subject: [PATCH] Rpc to return software version (#5456) * Add getSoftwareVersion rpc * Add getSoftwareVersion to doc * Rename to getVersion and return object * Update jsonrpc-api.md --- book/src/jsonrpc-api.md | 44 ++++++++++++++++++++++++++++++----------- core/src/lib.rs | 1 + core/src/rpc.rs | 38 +++++++++++++++++++++++++++++++++++ core/src/version.rs | 1 + 4 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 core/src/version.rs diff --git a/book/src/jsonrpc-api.md b/book/src/jsonrpc-api.md index 8ce2c96783..2b83cf7ed8 100644 --- a/book/src/jsonrpc-api.md +++ b/book/src/jsonrpc-api.md @@ -26,6 +26,7 @@ Methods * [getBalance](#getbalance) * [getClusterNodes](#getclusternodes) * [getEpochInfo](#getepochinfo) +* [getEpochVoteAccounts](#getepochvoteaccounts) * [getLeaderSchedule](#getleaderschedule) * [getProgramAccounts](#getprogramaccounts) * [getRecentBlockhash](#getrecentblockhash) @@ -38,7 +39,7 @@ Methods * [getNumBlocksSinceSignatureConfirmation](#getnumblockssincesignatureconfirmation) * [getTransactionCount](#gettransactioncount) * [getTotalSupply](#gettotalsupply) -* [getEpochVoteAccounts](#getepochvoteaccounts) +* [getVersion](#getversion) * [requestAirdrop](#requestairdrop) * [sendTransaction](#sendtransaction) * [startSubscriptionChannel](#startsubscriptionchannel) @@ -198,6 +199,30 @@ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "m --- +### getEpochVoteAccounts +Returns the account info and associated stake for all the voting accounts in the current epoch. + +##### Parameters: +None + +##### Results: +The result field will be an array of JSON objects, each with the following sub fields: +* `votePubkey` - Vote account public key, as base-58 encoded string +* `nodePubkey` - Node public key, as base-58 encoded string +* `stake` - the stake, in lamports, delegated to this vote account +* `commission`, a 32-bit integer used as a fraction (commission/MAX_U32) for rewards payout + +##### Example: +```bash +// Request +curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getEpochVoteAccounts"}' http://localhost:8899 + +// Result +{"jsonrpc":"2.0","result":[{"commission":0,"nodePubkey":"Et2RaZJdJRTzTkodUwiHr4H6sLkVmijBFv8tkd7oSSFY","stake":42,"votePubkey":"B4CdWq3NBSoH2wYsVE1CaZSWPo2ZtopE4SJipQhZ3srF"}],"id":1} +``` + +--- + ### getLeaderSchedule Returns the leader schedule for the current epoch @@ -453,31 +478,26 @@ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "m --- -### getEpochVoteAccounts -Returns the account info and associated stake for all the voting accounts in the current epoch. +### getVersion +Returns the current solana versions running on the node ##### Parameters: None ##### Results: -The result field will be an array of JSON objects, each with the following sub fields: -* `votePubkey` - Vote account public key, as base-58 encoded string -* `nodePubkey` - Node public key, as base-58 encoded string -* `stake` - the stake, in lamports, delegated to this vote account -* `commission`, a 32-bit integer used as a fraction (commission/MAX_U32) for rewards payout +The result field will be a JSON object with the following sub fields: +* `solana-core`, software version of solana-core ##### Example: ```bash // Request -curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getEpochVoteAccounts"}' http://localhost:8899 - +curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getVersion"}' http://localhost:8899 // Result -{"jsonrpc":"2.0","result":[{"commission":0,"nodePubkey":"Et2RaZJdJRTzTkodUwiHr4H6sLkVmijBFv8tkd7oSSFY","stake":42,"votePubkey":"B4CdWq3NBSoH2wYsVE1CaZSWPo2ZtopE4SJipQhZ3srF"}],"id":1} +{"jsonrpc":"2.0","result":{"solana-core": "0.17.2"},"id":1} ``` --- - ### requestAirdrop Requests an airdrop of lamports to a Pubkey diff --git a/core/src/lib.rs b/core/src/lib.rs index 980a6b04c5..0c70839ee5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -73,6 +73,7 @@ pub mod test_tx; pub mod tpu; pub mod tvu; pub mod validator; +pub(crate) mod version; pub mod weighted_shuffle; pub mod window_service; diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 2269cad21f..d8e56292e4 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -5,6 +5,7 @@ use crate::cluster_info::ClusterInfo; use crate::contact_info::ContactInfo; use crate::packet::PACKET_DATA_SIZE; use crate::storage_stage::StorageState; +use crate::version::VERSION; use bincode::{deserialize, serialize}; use jsonrpc_core::{Error, Metadata, Result}; use jsonrpc_derive::rpc; @@ -233,6 +234,13 @@ pub struct RpcEpochInfo { pub slots_in_epoch: u64, } +#[derive(Serialize, Deserialize, Clone, Debug)] +#[serde(rename_all = "kebab-case")] +pub struct RpcVersionInfo { + /// The current version of solana-core + pub solana_core: String, +} + #[rpc(server)] pub trait RpcSol { type Metadata; @@ -317,6 +325,9 @@ pub trait RpcSol { _: Self::Metadata, _: String, ) -> Result)>>; + + #[rpc(meta, name = "getVersion")] + fn get_version(&self, _: Self::Metadata) -> Result; } pub struct RpcSolImpl; @@ -602,6 +613,12 @@ impl RpcSol for RpcSolImpl { fn fullnode_exit(&self, meta: Self::Metadata) -> Result { meta.request_processor.read().unwrap().fullnode_exit() } + + fn get_version(&self, _: Self::Metadata) -> Result { + Ok(RpcVersionInfo { + solana_core: VERSION.to_string(), + }) + } } #[cfg(test)] @@ -1088,4 +1105,25 @@ mod tests { assert_eq!(request_processor.fullnode_exit(), Ok(true)); assert_eq!(exit.load(Ordering::Relaxed), true); } + + #[test] + fn test_rpc_get_version() { + let bob_pubkey = Pubkey::new_rand(); + let (io, meta, ..) = start_rpc_handler_with_tx(&bob_pubkey); + + let req = format!(r#"{{"jsonrpc":"2.0","id":1,"method":"getVersion"}}"#); + let res = io.handle_request_sync(&req, meta); + let expected = json!({ + "jsonrpc": "2.0", + "result": { + "solana-core": VERSION + }, + "id": 1 + }); + let expected: Response = + serde_json::from_value(expected).expect("expected response deserialization"); + let result: Response = serde_json::from_str(&res.expect("actual response")) + .expect("actual response deserialization"); + assert_eq!(expected, result); + } } diff --git a/core/src/version.rs b/core/src/version.rs new file mode 100644 index 0000000000..fb62f308e4 --- /dev/null +++ b/core/src/version.rs @@ -0,0 +1 @@ +pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION");