Skip to content

Commit

Permalink
Merge testnet2
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Dec 2, 2021
2 parents d9e4313 + bc518c3 commit cd009a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 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
84 changes: 59 additions & 25 deletions src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{MemoryPool, Network};

Expand All @@ -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, RwLock};

/// Defines the authentication format for accessing private endpoints on the RPC server.
Expand Down Expand Up @@ -414,9 +411,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())
}
Err(err) => jrt::Response::error(jrt::Version::V2, err, request.id.clone()),
Expand Down Expand Up @@ -1003,14 +998,12 @@ mod tests {
let actual: <Testnet2 as Network>::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]
Expand Down Expand Up @@ -1076,6 +1069,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 Expand Up @@ -1155,15 +1191,13 @@ mod tests {
let actual: Transition<Testnet2> = 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]
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 @@ -64,7 +64,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 @@ -292,7 +292,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 cd009a0

Please sign in to comment.