From 900933bbcc5821b70a1d057dd8474da38d3824d2 Mon Sep 17 00:00:00 2001 From: sakridge Date: Thu, 9 Apr 2020 18:05:56 -0700 Subject: [PATCH] Reduce rpc test code (#9413) automerge --- cli/src/lib.rs | 1 + cli/src/nonce.rs | 4 +- cli/src/test_utils.rs | 16 +++++ cli/tests/nonce.rs | 66 ++++++-------------- cli/tests/pay.rs | 16 +---- cli/tests/stake.rs | 16 +---- cli/tests/transfer.rs | 16 +---- cli/tests/vote.rs | 16 +---- core/tests/rpc.rs | 137 +++++++++++++----------------------------- 9 files changed, 88 insertions(+), 200 deletions(-) create mode 100644 cli/src/test_utils.rs diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 00a23001be..64b87e63c7 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -25,5 +25,6 @@ pub mod nonce; pub mod offline; pub mod stake; pub mod storage; +pub mod test_utils; pub mod validator_info; pub mod vote; diff --git a/cli/src/nonce.rs b/cli/src/nonce.rs index 93479ef7a8..3fb70ed0ae 100644 --- a/cli/src/nonce.rs +++ b/cli/src/nonce.rs @@ -456,8 +456,8 @@ pub fn process_create_nonce_account( lamports: u64, ) -> ProcessResult { let nonce_account_pubkey = config.signers[nonce_account].pubkey(); - let nonce_account_address = if let Some(seed) = seed.clone() { - Pubkey::create_with_seed(&nonce_account_pubkey, &seed, &system_program::id())? + let nonce_account_address = if let Some(ref seed) = seed { + Pubkey::create_with_seed(&nonce_account_pubkey, seed, &system_program::id())? } else { nonce_account_pubkey }; diff --git a/cli/src/test_utils.rs b/cli/src/test_utils.rs new file mode 100644 index 0000000000..abc57db998 --- /dev/null +++ b/cli/src/test_utils.rs @@ -0,0 +1,16 @@ +use solana_client::rpc_client::RpcClient; +use solana_sdk::pubkey::Pubkey; +use std::{thread::sleep, time::Duration}; + +pub fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { + (0..5).for_each(|tries| { + let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); + if balance == expected_balance { + return; + } + if tries == 4 { + assert_eq!(balance, expected_balance); + } + sleep(Duration::from_millis(500)); + }); +} diff --git a/cli/tests/nonce.rs b/cli/tests/nonce.rs index 94e569fd10..216a94db13 100644 --- a/cli/tests/nonce.rs +++ b/cli/tests/nonce.rs @@ -1,3 +1,4 @@ +use solana_cli::test_utils::check_balance; use solana_cli::{ cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig}, nonce, @@ -7,6 +8,7 @@ use solana_cli::{ }, }; use solana_client::rpc_client::RpcClient; +use solana_core::contact_info::ContactInfo; use solana_core::validator::{TestValidator, TestValidatorOptions}; use solana_faucet::faucet::run_local_faucet; use solana_sdk::{ @@ -15,23 +17,11 @@ use solana_sdk::{ signature::{keypair_from_seed, Keypair, Signer}, system_program, }; -use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration}; - -fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { - (0..5).for_each(|tries| { - let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); - if balance == expected_balance { - return; - } - if tries == 4 { - assert_eq!(balance, expected_balance); - } - sleep(Duration::from_millis(500)); - }); -} +use std::{fs::remove_dir_all, sync::mpsc::channel}; #[test] fn test_nonce() { + solana_logger::setup(); let TestValidator { server, leader_data, @@ -39,14 +29,8 @@ fn test_nonce() { ledger_path, .. } = TestValidator::run(); - let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); - let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); - let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); - - full_battery_tests(&rpc_client, &faucet_addr, json_rpc_url, None, false); + full_battery_tests(leader_data, alice, None, false); server.close().unwrap(); remove_dir_all(ledger_path).unwrap(); @@ -61,20 +45,8 @@ fn test_nonce_with_seed() { ledger_path, .. } = TestValidator::run(); - let (sender, receiver) = channel(); - run_local_faucet(alice, sender, None); - let faucet_addr = receiver.recv().unwrap(); - let rpc_client = RpcClient::new_socket(leader_data.rpc); - let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); - - full_battery_tests( - &rpc_client, - &faucet_addr, - json_rpc_url, - Some(String::from("seed")), - false, - ); + full_battery_tests(leader_data, alice, Some(String::from("seed")), false); server.close().unwrap(); remove_dir_all(ledger_path).unwrap(); @@ -89,6 +61,19 @@ fn test_nonce_with_authority() { ledger_path, .. } = TestValidator::run(); + + full_battery_tests(leader_data, alice, None, true); + + server.close().unwrap(); + remove_dir_all(ledger_path).unwrap(); +} + +fn full_battery_tests( + leader_data: ContactInfo, + alice: Keypair, + seed: Option, + use_nonce_authority: bool, +) { let (sender, receiver) = channel(); run_local_faucet(alice, sender, None); let faucet_addr = receiver.recv().unwrap(); @@ -96,19 +81,6 @@ fn test_nonce_with_authority() { let rpc_client = RpcClient::new_socket(leader_data.rpc); let json_rpc_url = format!("http://{}:{}", leader_data.rpc.ip(), leader_data.rpc.port()); - full_battery_tests(&rpc_client, &faucet_addr, json_rpc_url, None, true); - - server.close().unwrap(); - remove_dir_all(ledger_path).unwrap(); -} - -fn full_battery_tests( - rpc_client: &RpcClient, - faucet_addr: &std::net::SocketAddr, - json_rpc_url: String, - seed: Option, - use_nonce_authority: bool, -) { let mut config_payer = CliConfig::default(); config_payer.json_rpc_url = json_rpc_url.clone(); let payer = Keypair::new(); diff --git a/cli/tests/pay.rs b/cli/tests/pay.rs index 8001e00f7d..38148e24b4 100644 --- a/cli/tests/pay.rs +++ b/cli/tests/pay.rs @@ -1,5 +1,6 @@ use chrono::prelude::*; use serde_json::Value; +use solana_cli::test_utils::check_balance; use solana_cli::{ cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig, PayCommand}, nonce, @@ -16,20 +17,7 @@ use solana_sdk::{ pubkey::Pubkey, signature::{Keypair, Signer}, }; -use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration}; - -fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { - (0..5).for_each(|tries| { - let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); - if balance == expected_balance { - return; - } - if tries == 4 { - assert_eq!(balance, expected_balance); - } - sleep(Duration::from_millis(500)); - }); -} +use std::{fs::remove_dir_all, sync::mpsc::channel}; #[test] fn test_cli_timestamp_tx() { diff --git a/cli/tests/stake.rs b/cli/tests/stake.rs index 601d44a9fa..92cfdc6d45 100644 --- a/cli/tests/stake.rs +++ b/cli/tests/stake.rs @@ -1,3 +1,4 @@ +use solana_cli::test_utils::check_balance; use solana_cli::{ cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig}, nonce, @@ -19,20 +20,7 @@ use solana_stake_program::{ stake_instruction::LockupArgs, stake_state::{Lockup, StakeAuthorize, StakeState}, }; -use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration}; - -fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { - (0..5).for_each(|tries| { - let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); - if balance == expected_balance { - return; - } - if tries == 4 { - assert_eq!(balance, expected_balance); - } - sleep(Duration::from_millis(500)); - }); -} +use std::{fs::remove_dir_all, sync::mpsc::channel}; #[test] fn test_stake_delegation_force() { diff --git a/cli/tests/transfer.rs b/cli/tests/transfer.rs index 4ab944551f..c7fd9272ad 100644 --- a/cli/tests/transfer.rs +++ b/cli/tests/transfer.rs @@ -1,3 +1,4 @@ +use solana_cli::test_utils::check_balance; use solana_cli::{ cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig}, nonce, @@ -14,20 +15,7 @@ use solana_sdk::{ pubkey::Pubkey, signature::{keypair_from_seed, Keypair, NullSigner, Signer}, }; -use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration}; - -fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { - (0..5).for_each(|tries| { - let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); - if balance == expected_balance { - return; - } - if tries == 4 { - assert_eq!(balance, expected_balance); - } - sleep(Duration::from_millis(500)); - }); -} +use std::{fs::remove_dir_all, sync::mpsc::channel}; #[test] fn test_transfer() { diff --git a/cli/tests/vote.rs b/cli/tests/vote.rs index 42f87b35f3..094fc2ee84 100644 --- a/cli/tests/vote.rs +++ b/cli/tests/vote.rs @@ -1,4 +1,5 @@ use solana_cli::cli::{process_command, request_and_confirm_airdrop, CliCommand, CliConfig}; +use solana_cli::test_utils::check_balance; use solana_client::rpc_client::RpcClient; use solana_core::validator::TestValidator; use solana_faucet::faucet::run_local_faucet; @@ -8,20 +9,7 @@ use solana_sdk::{ signature::{Keypair, Signer}, }; use solana_vote_program::vote_state::{VoteAuthorize, VoteState, VoteStateVersions}; -use std::{fs::remove_dir_all, sync::mpsc::channel, thread::sleep, time::Duration}; - -fn check_balance(expected_balance: u64, client: &RpcClient, pubkey: &Pubkey) { - (0..5).for_each(|tries| { - let balance = client.retry_get_balance(pubkey, 1).unwrap().unwrap(); - if balance == expected_balance { - return; - } - if tries == 4 { - assert_eq!(balance, expected_balance); - } - sleep(Duration::from_millis(500)); - }); -} +use std::{fs::remove_dir_all, sync::mpsc::channel}; #[test] fn test_vote_authorize_and_withdraw() { diff --git a/core/tests/rpc.rs b/core/tests/rpc.rs index d1e3baf5ba..1d12fb8c18 100644 --- a/core/tests/rpc.rs +++ b/core/tests/rpc.rs @@ -11,6 +11,7 @@ use solana_client::{ rpc_client::{get_rpc_request_str, RpcClient}, rpc_response::{Response, RpcSignatureResult}, }; +use solana_core::contact_info::ContactInfo; use solana_core::{rpc_pubsub::gen_client::Client as PubsubClient, validator::TestValidator}; use solana_sdk::{ commitment_config::CommitmentConfig, hash::Hash, pubkey::Pubkey, signature::Signer, @@ -26,6 +27,30 @@ use std::{ }; use tokio::runtime::Runtime; +macro_rules! json_req { + ($method: expr, $params: expr) => {{ + json!({ + "jsonrpc": "2.0", + "id": 1, + "method": $method, + "params": $params, + }) + }} +} + +fn post_rpc(request: Value, data: &ContactInfo) -> Value { + let client = reqwest::blocking::Client::new(); + let rpc_addr = data.rpc; + let rpc_string = get_rpc_request_str(rpc_addr, false); + let response = client + .post(&rpc_string) + .header(CONTENT_TYPE, "application/json") + .body(request.to_string()) + .send() + .unwrap(); + serde_json::from_str(&response.text().unwrap()).unwrap() +} + #[test] fn test_rpc_send_tx() { solana_logger::setup(); @@ -39,22 +64,9 @@ fn test_rpc_send_tx() { } = TestValidator::run(); let bob_pubkey = Pubkey::new_rand(); - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "getRecentBlockhash", - "params": json!([]) - }); - let rpc_addr = leader_data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + let req = json_req!("getRecentBlockhash", json!([])); + let json = post_rpc(req, &leader_data); + let blockhash: Hash = json["result"]["value"]["blockhash"] .as_str() .unwrap() @@ -65,43 +77,17 @@ fn test_rpc_send_tx() { let tx = system_transaction::transfer(&alice, &bob_pubkey, 20, blockhash); let serialized_encoded_tx = bs58::encode(serialize(&tx).unwrap()).into_string(); - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "sendTransaction", - "params": json!([serialized_encoded_tx]) - }); - let rpc_addr = leader_data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + let req = json_req!("sendTransaction", json!([serialized_encoded_tx])); + let json: Value = post_rpc(req, &leader_data); + let signature = &json["result"]; let mut confirmed_tx = false; - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "confirmTransaction", - "params": [signature], - }); + let request = json_req!("confirmTransaction", [signature]); for _ in 0..solana_sdk::clock::DEFAULT_TICKS_PER_SLOT { - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let response_json_text = response.text().unwrap(); - let json: Value = serde_json::from_str(&response_json_text).unwrap(); + let json = post_rpc(request.clone(), &leader_data); if true == json["result"]["value"] { confirmed_tx = true; @@ -130,62 +116,23 @@ fn test_rpc_invalid_requests() { let bob_pubkey = Pubkey::new_rand(); // test invalid get_balance request - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "getBalance", - "params": json!(["invalid9999"]) - }); - let rpc_addr = leader_data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + let req = json_req!("getBalance", json!(["invalid9999"])); + let json = post_rpc(req, &leader_data); + let the_error = json["error"]["message"].as_str().unwrap(); assert_eq!(the_error, "Invalid request"); // test invalid get_account_info request - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "getAccountInfo", - "params": json!(["invalid9999"]) - }); - let rpc_addr = leader_data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + let req = json_req!("getAccountInfo", json!(["invalid9999"])); + let json = post_rpc(req, &leader_data); + let the_error = json["error"]["message"].as_str().unwrap(); assert_eq!(the_error, "Invalid request"); // test invalid get_account_info request - let client = reqwest::blocking::Client::new(); - let request = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "getAccountInfo", - "params": json!([bob_pubkey.to_string()]) - }); - let rpc_addr = leader_data.rpc; - let rpc_string = get_rpc_request_str(rpc_addr, false); - let response = client - .post(&rpc_string) - .header(CONTENT_TYPE, "application/json") - .body(request.to_string()) - .send() - .unwrap(); - let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + let req = json_req!("getAccountInfo", json!([bob_pubkey.to_string()])); + let json = post_rpc(req, &leader_data); + let the_value = &json["result"]["value"]; assert!(the_value.is_null());