Avoid RpcRequest

This commit is contained in:
Michael Vines 2019-03-17 00:40:45 -07:00
parent c498775a3d
commit a35ebe1186
3 changed files with 28 additions and 34 deletions

View File

@ -47,7 +47,7 @@ impl GenericRpcClientRequest for MockRpcClientRequest {
let str = if self.url == "account_in_use" {
"AccountInUse"
} else if self.url == "bad_sig_status" {
"Nonexistent"
"SignatureNotFound"
} else {
"Confirmed"
};

View File

@ -280,24 +280,26 @@ impl RpcClient {
debug!("get_transaction_count");
let mut num_retries = 5;
loop {
while num_retries > 0 {
let response = self.client.send(&RpcRequest::GetTransactionCount, None, 0);
match response {
Ok(value) => {
debug!("transaction_count response: {:?}", value);
let transaction_count = value.as_u64().unwrap();
return Ok(transaction_count);
if let Some(transaction_count) = value.as_u64() {
return Ok(transaction_count);
}
}
Err(err) => {
debug!("transaction_count failed: {:?}", err);
num_retries -= 1;
if num_retries == 0 {
return Err(err);
}
}
}
num_retries -= 1;
}
Err(io::Error::new(
io::ErrorKind::Other,
"Unable to get transaction count, too many retries",
))?
}
pub fn get_recent_blockhash(&self) -> io::Result<Hash> {
@ -623,8 +625,8 @@ mod tests {
let rpc_client = RpcClient::new_mock("bad_sig_status".to_string());
let signature = "bad_status";
let status = rpc_client.get_signature_status(&signature);
assert!(status.is_err());
let status = dbg!(rpc_client.get_signature_status(&signature));
assert_eq!(status.unwrap(), RpcSignatureStatus::SignatureNotFound);
let rpc_client = RpcClient::new_mock("fails".to_string());
let signature = "bad_status_fmt";

View File

@ -7,7 +7,6 @@ use serde_json::json;
use solana_budget_api;
use solana_budget_api::budget_transaction::BudgetTransaction;
use solana_client::rpc_client::{get_rpc_request_str, RpcClient};
use solana_client::rpc_request::RpcRequest;
#[cfg(not(test))]
use solana_drone::drone::request_airdrop_transaction;
use solana_drone::drone::DRONE_PORT;
@ -409,21 +408,18 @@ fn process_balance(config: &WalletConfig, rpc_client: &RpcClient) -> ProcessResu
}
fn process_confirm(rpc_client: &RpcClient, signature: Signature) -> ProcessResult {
let params = json!([format!("{}", signature)]);
let confirmation = rpc_client
.retry_make_rpc_request(&RpcRequest::ConfirmTransaction, Some(params), 5)?
.as_bool();
match confirmation {
Some(b) => {
if b {
match rpc_client.get_signature_status(&signature.to_string()) {
Ok(status) => {
if status == solana_client::rpc_signature_status::RpcSignatureStatus::Confirmed {
Ok("Confirmed".to_string())
} else {
Ok("Not found".to_string())
}
}
None => Err(WalletError::RpcRequestError(
"Received result of an unexpected type".to_string(),
))?,
Err(err) => Err(WalletError::RpcRequestError(format!(
"Unable to confirm: {:?}",
err
)))?,
}
}
@ -628,15 +624,8 @@ fn process_cancel(rpc_client: &RpcClient, config: &WalletConfig, pubkey: &Pubkey
}
fn process_get_transaction_count(rpc_client: &RpcClient) -> ProcessResult {
let transaction_count = rpc_client
.retry_make_rpc_request(&RpcRequest::GetTransactionCount, None, 5)?
.as_u64();
match transaction_count {
Some(count) => Ok(count.to_string()),
None => Err(WalletError::RpcRequestError(
"Received result of an unexpected type".to_string(),
))?,
}
let transaction_count = rpc_client.get_transaction_count()?;
Ok(transaction_count.to_string())
}
fn process_time_elapsed(
@ -1300,9 +1289,6 @@ mod tests {
let good_signature = Signature::new(&bs58::decode(SIGNATURE).into_vec().unwrap());
config.command = WalletCommand::Confirm(good_signature);
assert_eq!(process_command(&config).unwrap(), "Confirmed");
let missing_signature = Signature::new(&bs58::decode("5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW").into_vec().unwrap());
config.command = WalletCommand::Confirm(missing_signature);
assert_eq!(process_command(&config).unwrap(), "Not found");
let bob_pubkey = Keypair::new().pubkey();
config.command = WalletCommand::ConfigureStakingAccount(None, Some(bob_pubkey));
@ -1387,7 +1373,13 @@ mod tests {
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
// Failture cases
// bad_sig_status cases
config.rpc_client = Some(RpcClient::new_mock("bad_sig_status".to_string()));
let missing_signature = Signature::new(&bs58::decode("5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW").into_vec().unwrap());
config.command = WalletCommand::Confirm(missing_signature);
assert_eq!(process_command(&config).unwrap(), "Not found");
// Failure cases
config.rpc_client = Some(RpcClient::new_mock("fails".to_string()));
config.command = WalletCommand::Airdrop(50);