Skip to content

Commit

Permalink
Merge pull request AleoNet#1366 from AleoHQ/update/getnodestate
Browse files Browse the repository at this point in the history
Update serialization of `getnodestate`
  • Loading branch information
howardwu authored Dec 2, 2021
2 parents 5382000 + b93cfa1 commit bc518c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ version = "1"

[dependencies.serde_json]
version = "1"
features = [ "arbitrary_precision" ]

[dependencies.structopt]
version = "0.3"
Expand Down
48 changes: 45 additions & 3 deletions src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use hyper::{
use json_rpc_types as jrt;
use jsonrpc_core::{Metadata, Params};
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, net::SocketAddr, str::FromStr, sync::Arc};
use std::{convert::Infallible, net::SocketAddr, sync::Arc};
use tokio::sync::oneshot;

/// Defines the authentication format for accessing private endpoints on the RPC server.
Expand Down Expand Up @@ -409,8 +409,7 @@ fn result_to_response<T: Serialize>(
) -> jrt::Response<serde_json::Value, String> {
match result {
Ok(res) => {
let candidate_string = serde_json::to_string(&res).unwrap_or_default();
let result = serde_json::Value::from_str(&candidate_string).unwrap_or_default();
let result = serde_json::to_value(&res).unwrap_or_default();

jrt::Response::result(jrt::Version::V2, result, request.id.clone())
}
Expand Down Expand Up @@ -1041,6 +1040,49 @@ mod tests {
assert_eq!(expected, actual);
}

#[tokio::test]
async fn test_get_node_state() {
// Initialize a new RPC.
let rpc = new_rpc::<Testnet2, Client<Testnet2>, RocksDB, PathBuf>(None).await;

// Declare the expected node state.
let expected = serde_json::json!({
"candidate_peers": Vec::<SocketAddr>::new(),
"connected_peers": Vec::<SocketAddr>::new(),
"latest_block_height": 0,
"latest_cumulative_weight": 0,
"number_of_candidate_peers": 0,
"number_of_connected_peers": 0,
"number_of_connected_sync_nodes": 0,
"software": format!("snarkOS {}", env!("CARGO_PKG_VERSION")),
"status": rpc.status.to_string(),
"type": Client::<Testnet2>::NODE_TYPE,
"version": Client::<Testnet2>::MESSAGE_VERSION,
});

// Initialize a new request that calls the `getnodestate` endpoint.
let request = Request::new(Body::from(
r#"{
"jsonrpc":"2.0",
"id": "1",
"method": "getnodestate"
}"#,
));

// Send the request to the RPC.
let response = handle_rpc(caller(), rpc, request)
.await
.expect("Test RPC failed to process request");

// Process the response into a ledger root.
let actual: serde_json::Value = process_response(response).await;

println!("get_node_state: {:?}", actual);

// Check the ledger root.
assert_eq!(expected, actual);
}

#[tokio::test]
async fn test_get_transaction() {
/// Additional metadata included with a transaction response
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl From<RpcError> for std::io::Error {

#[doc(hidden)]
pub struct RpcInner<N: Network, E: Environment> {
status: Status,
pub(crate) status: Status,
peers: Arc<Peers<N, E>>,
ledger: LedgerReader<N>,
prover_router: ProverRouter<N>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<N: Network, E: Environment> RpcFunctions<N> for RpcImpl<N, E> {
let number_of_connected_sync_nodes = self.peers.number_of_connected_sync_nodes().await;

let latest_block_height = self.ledger.latest_block_height();
let latest_cumulative_weight = self.ledger.latest_cumulative_weight().to_string();
let latest_cumulative_weight = self.ledger.latest_cumulative_weight();

Ok(serde_json::json!({
"candidate_peers": candidate_peers,
Expand Down

0 comments on commit bc518c3

Please sign in to comment.