From 65553c5e10b28a8463aa2b2c36b404efe77c9049 Mon Sep 17 00:00:00 2001 From: raychu86 <14917648+raychu86@users.noreply.github.com> Date: Wed, 1 Dec 2021 11:55:33 -0800 Subject: [PATCH 1/3] Update serialization of nodestate --- Cargo.toml | 5 +++-- src/rpc/rpc.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/rpc/rpc_impl.rs | 4 ++-- storage/Cargo.toml | 4 ++-- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49966f9ac4..da41c44784 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,10 +95,11 @@ version = "1" version = "0.27" [dependencies.serde] -version = "1" +version = "1.0.130" [dependencies.serde_json] -version = "1" +version = "1.0.72" +features = [ "arbitrary_precision" ] [dependencies.structopt] version = "0.3" diff --git a/src/rpc/rpc.rs b/src/rpc/rpc.rs index 54cbd8e5f4..eb546d88e9 100644 --- a/src/rpc/rpc.rs +++ b/src/rpc/rpc.rs @@ -1041,6 +1041,49 @@ mod tests { assert_eq!(expected, actual); } + #[tokio::test] + async fn test_get_node_state() { + // Initialize a new RPC. + let rpc = new_rpc::, RocksDB, PathBuf>(None).await; + + // Declare the expected node state. + let expected = serde_json::json!({ + "candidate_peers": Vec::::new(), + "connected_peers": Vec::::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::::NODE_TYPE, + "version": Client::::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 diff --git a/src/rpc/rpc_impl.rs b/src/rpc/rpc_impl.rs index e5ec4257ed..8da04f4710 100644 --- a/src/rpc/rpc_impl.rs +++ b/src/rpc/rpc_impl.rs @@ -63,7 +63,7 @@ impl From for std::io::Error { #[doc(hidden)] pub struct RpcInner { - status: Status, + pub(crate) status: Status, peers: Arc>, ledger: LedgerReader, prover_router: ProverRouter, @@ -219,7 +219,7 @@ impl RpcFunctions for RpcImpl { 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, diff --git a/storage/Cargo.toml b/storage/Cargo.toml index 98ffbdb413..d500878170 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -52,10 +52,10 @@ version = "0.17" optional = true [dependencies.serde] -version = "1" +version = "1.0.130" [dependencies.serde_json] -version = "1" +version = "1.0.72" [dependencies.tracing] version = "0.1" From 10f0989bf80135e8c35095c2456139d3dfbd9ffb Mon Sep 17 00:00:00 2001 From: raychu86 <14917648+raychu86@users.noreply.github.com> Date: Wed, 1 Dec 2021 12:29:34 -0800 Subject: [PATCH 2/3] Update serde versions --- Cargo.toml | 4 ++-- src/rpc/rpc.rs | 40 ++++++++++++++++------------------------ storage/Cargo.toml | 4 ++-- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da41c44784..e10b4f5b6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,10 +95,10 @@ version = "1" version = "0.27" [dependencies.serde] -version = "1.0.130" +version = "1" [dependencies.serde_json] -version = "1.0.72" +version = "1" features = [ "arbitrary_precision" ] [dependencies.structopt] diff --git a/src/rpc/rpc.rs b/src/rpc/rpc.rs index eb546d88e9..2fcb5a2290 100644 --- a/src/rpc/rpc.rs +++ b/src/rpc/rpc.rs @@ -19,10 +19,7 @@ use crate::{ helpers::Status, rpc::{rpc_impl::RpcImpl, rpc_trait::RpcFunctions}, - Environment, - LedgerReader, - Peers, - ProverRouter, + Environment, LedgerReader, Peers, ProverRouter, }; use snarkvm::dpc::Network; @@ -35,7 +32,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. @@ -409,8 +406,7 @@ fn result_to_response( ) -> jrt::Response { 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()) } @@ -968,14 +964,12 @@ mod tests { let actual: ::RecordCiphertext = process_response(response).await; // Check the ciphertext. - assert!( - Testnet2::genesis_block() - .transactions() - .first() - .unwrap() - .ciphertexts() - .any(|expected| *expected == actual) - ); + assert!(Testnet2::genesis_block() + .transactions() + .first() + .unwrap() + .ciphertexts() + .any(|expected| *expected == actual)); } #[tokio::test] @@ -1163,15 +1157,13 @@ mod tests { let actual: Transition = process_response(response).await; // Check the transition. - assert!( - Testnet2::genesis_block() - .transactions() - .first() - .unwrap() - .transitions() - .iter() - .any(|expected| *expected == actual) - ); + assert!(Testnet2::genesis_block() + .transactions() + .first() + .unwrap() + .transitions() + .iter() + .any(|expected| *expected == actual)); } #[tokio::test] diff --git a/storage/Cargo.toml b/storage/Cargo.toml index d500878170..98ffbdb413 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -52,10 +52,10 @@ version = "0.17" optional = true [dependencies.serde] -version = "1.0.130" +version = "1" [dependencies.serde_json] -version = "1.0.72" +version = "1" [dependencies.tracing] version = "0.1" From d7285a12841b72da0749891a9112153245cc61c3 Mon Sep 17 00:00:00 2001 From: raychu86 <14917648+raychu86@users.noreply.github.com> Date: Wed, 1 Dec 2021 12:37:08 -0800 Subject: [PATCH 3/3] cargo fmt --- src/rpc/rpc.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/rpc/rpc.rs b/src/rpc/rpc.rs index 2fcb5a2290..8fa6b519f1 100644 --- a/src/rpc/rpc.rs +++ b/src/rpc/rpc.rs @@ -19,7 +19,10 @@ use crate::{ helpers::Status, rpc::{rpc_impl::RpcImpl, rpc_trait::RpcFunctions}, - Environment, LedgerReader, Peers, ProverRouter, + Environment, + LedgerReader, + Peers, + ProverRouter, }; use snarkvm::dpc::Network; @@ -964,12 +967,14 @@ mod tests { let actual: ::RecordCiphertext = process_response(response).await; // Check the ciphertext. - assert!(Testnet2::genesis_block() - .transactions() - .first() - .unwrap() - .ciphertexts() - .any(|expected| *expected == actual)); + assert!( + Testnet2::genesis_block() + .transactions() + .first() + .unwrap() + .ciphertexts() + .any(|expected| *expected == actual) + ); } #[tokio::test] @@ -1157,13 +1162,15 @@ mod tests { let actual: Transition = process_response(response).await; // Check the transition. - assert!(Testnet2::genesis_block() - .transactions() - .first() - .unwrap() - .transitions() - .iter() - .any(|expected| *expected == actual)); + assert!( + Testnet2::genesis_block() + .transactions() + .first() + .unwrap() + .transitions() + .iter() + .any(|expected| *expected == actual) + ); } #[tokio::test]