Improve Wallet coverage (#2385)
* Add trait for RpcRequestHandler trait for RpcClient and add MockRpcClient for unit tests * Add request_airdrop integration test * Add timestamp_tx, witness_tx, and cancel_tx to wallet integration tests; add wallet integration tests to test-stable * Add test cases * Ignore plentiful sleeps in unit tests
This commit is contained in:
@@ -6,6 +6,7 @@ use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use std::{error, fmt};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RpcClient {
|
||||
pub client: reqwest::Client,
|
||||
pub addr: String,
|
||||
@@ -35,55 +36,22 @@ impl RpcClient {
|
||||
addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rpc_request_str(rpc_addr: SocketAddr) -> String {
|
||||
format!("http://{}", rpc_addr)
|
||||
}
|
||||
|
||||
pub enum RpcRequest {
|
||||
ConfirmTransaction,
|
||||
GetAccountInfo,
|
||||
GetBalance,
|
||||
GetConfirmationTime,
|
||||
GetLastId,
|
||||
GetSignatureStatus,
|
||||
GetTransactionCount,
|
||||
RequestAirdrop,
|
||||
SendTransaction,
|
||||
RegisterNode,
|
||||
SignVote,
|
||||
DeregisterNode,
|
||||
GetStorageMiningLastId,
|
||||
GetStorageMiningEntryHeight,
|
||||
GetStoragePubkeysForEntryHeight,
|
||||
}
|
||||
|
||||
impl RpcRequest {
|
||||
pub fn make_rpc_request(
|
||||
&self,
|
||||
client: &RpcClient,
|
||||
id: u64,
|
||||
params: Option<Value>,
|
||||
) -> Result<Value, Box<dyn error::Error>> {
|
||||
self.retry_make_rpc_request(client, id, params, 0)
|
||||
}
|
||||
|
||||
pub fn retry_make_rpc_request(
|
||||
&self,
|
||||
client: &RpcClient,
|
||||
id: u64,
|
||||
request: &RpcRequest,
|
||||
params: Option<Value>,
|
||||
mut retries: usize,
|
||||
) -> Result<Value, Box<dyn error::Error>> {
|
||||
let request = self.build_request_json(id, params);
|
||||
let request_json = request.build_request_json(id, params);
|
||||
|
||||
loop {
|
||||
match client
|
||||
match self
|
||||
.client
|
||||
.post(&client.addr)
|
||||
.post(&self.addr)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(request.to_string())
|
||||
.body(request_json.to_string())
|
||||
.send()
|
||||
{
|
||||
Ok(mut response) => {
|
||||
@@ -111,7 +79,52 @@ impl RpcRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rpc_request_str(rpc_addr: SocketAddr) -> String {
|
||||
format!("http://{}", rpc_addr)
|
||||
}
|
||||
|
||||
pub trait RpcRequestHandler {
|
||||
fn make_rpc_request(
|
||||
&self,
|
||||
id: u64,
|
||||
request: RpcRequest,
|
||||
params: Option<Value>,
|
||||
) -> Result<Value, Box<dyn error::Error>>;
|
||||
}
|
||||
|
||||
impl RpcRequestHandler for RpcClient {
|
||||
fn make_rpc_request(
|
||||
&self,
|
||||
id: u64,
|
||||
request: RpcRequest,
|
||||
params: Option<Value>,
|
||||
) -> Result<Value, Box<dyn error::Error>> {
|
||||
self.retry_make_rpc_request(id, &request, params, 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum RpcRequest {
|
||||
ConfirmTransaction,
|
||||
GetAccountInfo,
|
||||
GetBalance,
|
||||
GetConfirmationTime,
|
||||
GetLastId,
|
||||
GetSignatureStatus,
|
||||
GetTransactionCount,
|
||||
RequestAirdrop,
|
||||
SendTransaction,
|
||||
RegisterNode,
|
||||
SignVote,
|
||||
DeregisterNode,
|
||||
GetStorageMiningLastId,
|
||||
GetStorageMiningEntryHeight,
|
||||
GetStoragePubkeysForEntryHeight,
|
||||
}
|
||||
|
||||
impl RpcRequest {
|
||||
fn build_request_json(&self, id: u64, params: Option<Value>) -> Value {
|
||||
let jsonrpc = "2.0";
|
||||
let method = match self {
|
||||
@@ -251,15 +264,15 @@ mod tests {
|
||||
let rpc_addr = receiver.recv().unwrap();
|
||||
let rpc_client = RpcClient::new_from_socket(rpc_addr);
|
||||
|
||||
let balance = RpcRequest::GetBalance.make_rpc_request(
|
||||
&rpc_client,
|
||||
let balance = rpc_client.make_rpc_request(
|
||||
1,
|
||||
RpcRequest::GetBalance,
|
||||
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"])),
|
||||
);
|
||||
assert!(balance.is_ok());
|
||||
assert_eq!(balance.unwrap().as_u64().unwrap(), 50);
|
||||
|
||||
let last_id = RpcRequest::GetLastId.make_rpc_request(&rpc_client, 2, None);
|
||||
let last_id = rpc_client.make_rpc_request(2, RpcRequest::GetLastId, None);
|
||||
assert!(last_id.is_ok());
|
||||
assert_eq!(
|
||||
last_id.unwrap().as_str().unwrap(),
|
||||
@@ -268,7 +281,7 @@ mod tests {
|
||||
|
||||
// Send erroneous parameter
|
||||
let last_id =
|
||||
RpcRequest::GetLastId.make_rpc_request(&rpc_client, 3, Some(json!("paramter")));
|
||||
rpc_client.make_rpc_request(3, RpcRequest::GetLastId, Some(json!("paramter")));
|
||||
assert_eq!(last_id.is_err(), true);
|
||||
}
|
||||
|
||||
@@ -302,9 +315,9 @@ mod tests {
|
||||
let rpc_addr = receiver.recv().unwrap();
|
||||
let rpc_client = RpcClient::new_from_socket(rpc_addr);
|
||||
|
||||
let balance = RpcRequest::GetBalance.retry_make_rpc_request(
|
||||
&rpc_client,
|
||||
let balance = rpc_client.retry_make_rpc_request(
|
||||
1,
|
||||
&RpcRequest::GetBalance,
|
||||
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhw"])),
|
||||
10,
|
||||
);
|
||||
|
Reference in New Issue
Block a user