Reduce rpc test code (#9413)

automerge
This commit is contained in:
sakridge 2020-04-09 18:05:56 -07:00 committed by GitHub
parent aeddd8c95a
commit 900933bbcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 88 additions and 200 deletions

View File

@ -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;

View File

@ -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
};

16
cli/src/test_utils.rs Normal file
View File

@ -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));
});
}

View File

@ -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<String>,
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<String>,
use_nonce_authority: bool,
) {
let mut config_payer = CliConfig::default();
config_payer.json_rpc_url = json_rpc_url.clone();
let payer = Keypair::new();

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -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());