Relieve the caller of having to care about the rpc request id

This commit is contained in:
Michael Vines
2019-03-15 23:49:28 -07:00
parent c2b1010f18
commit bcc34b906c
10 changed files with 51 additions and 55 deletions

View File

@ -27,20 +27,18 @@ impl MockRpcClient {
pub fn retry_get_balance(
&self,
id: u64,
pubkey: &Pubkey,
retries: usize,
) -> Result<Option<u64>, Box<dyn error::Error>> {
let params = json!([format!("{}", pubkey)]);
let res = self
.retry_make_rpc_request(id, &RpcRequest::GetBalance, Some(params), retries)?
.retry_make_rpc_request(&RpcRequest::GetBalance, Some(params), retries)?
.as_u64();
Ok(res)
}
pub fn retry_make_rpc_request(
&self,
_id: u64,
request: &RpcRequest,
params: Option<Value>,
mut _retries: usize,
@ -86,11 +84,10 @@ impl MockRpcClient {
impl RpcRequestHandler for MockRpcClient {
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)
self.retry_make_rpc_request(&request, params, 0)
}
}

View File

@ -39,25 +39,26 @@ impl RpcClient {
pub fn retry_get_balance(
&self,
id: u64,
pubkey: &Pubkey,
retries: usize,
) -> Result<Option<u64>, Box<dyn error::Error>> {
let params = json!([format!("{}", pubkey)]);
let res = self
.retry_make_rpc_request(id, &RpcRequest::GetBalance, Some(params), retries)?
.retry_make_rpc_request(&RpcRequest::GetBalance, Some(params), retries)?
.as_u64();
Ok(res)
}
pub fn retry_make_rpc_request(
&self,
id: u64,
request: &RpcRequest,
params: Option<Value>,
mut retries: usize,
) -> Result<Value, Box<dyn error::Error>> {
let request_json = request.build_request_json(id, params);
// Concurrent requests are not supported so reuse the same request id for all requests
let request_id = 1;
let request_json = request.build_request_json(request_id, params);
loop {
match self
@ -108,7 +109,6 @@ pub fn get_rpc_request_str(rpc_addr: SocketAddr, tls: bool) -> String {
pub trait RpcRequestHandler {
fn make_rpc_request(
&self,
id: u64,
request: RpcRequest,
params: Option<Value>,
) -> Result<Value, Box<dyn error::Error>>;
@ -117,11 +117,10 @@ pub trait RpcRequestHandler {
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)
self.retry_make_rpc_request(&request, params, 0)
}
}
@ -272,13 +271,12 @@ mod tests {
let rpc_client = RpcClient::new_socket(rpc_addr);
let balance = rpc_client.make_rpc_request(
1,
RpcRequest::GetBalance,
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"])),
);
assert_eq!(balance.unwrap().as_u64().unwrap(), 50);
let blockhash = rpc_client.make_rpc_request(2, RpcRequest::GetRecentBlockhash, None);
let blockhash = rpc_client.make_rpc_request(RpcRequest::GetRecentBlockhash, None);
assert_eq!(
blockhash.unwrap().as_str().unwrap(),
"deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhx"
@ -286,7 +284,7 @@ mod tests {
// Send erroneous parameter
let blockhash =
rpc_client.make_rpc_request(3, RpcRequest::GetRecentBlockhash, Some(json!("paramter")));
rpc_client.make_rpc_request(RpcRequest::GetRecentBlockhash, Some(json!("paramter")));
assert_eq!(blockhash.is_err(), true);
}
@ -321,7 +319,6 @@ mod tests {
let rpc_client = RpcClient::new_socket(rpc_addr);
let balance = rpc_client.retry_make_rpc_request(
1,
&RpcRequest::GetBalance,
Some(json!(["deadbeefXjn8o3yroDHxUtKsZZgoy4GPkPPXfouKNHhw"])),
10,

View File

@ -144,9 +144,9 @@ impl ThinClient {
pub fn get_account_data(&mut self, pubkey: &Pubkey) -> io::Result<Option<Vec<u8>>> {
let params = json!([format!("{}", pubkey)]);
let response =
self.rpc_client
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
let response = self
.rpc_client
.make_rpc_request(RpcRequest::GetAccountInfo, Some(params));
match response {
Ok(account_json) => {
let account: Account =
@ -169,9 +169,9 @@ impl ThinClient {
pub fn get_balance(&mut self, pubkey: &Pubkey) -> io::Result<u64> {
trace!("get_balance sending request to {}", self.rpc_addr);
let params = json!([format!("{}", pubkey)]);
let response =
self.rpc_client
.make_rpc_request(1, RpcRequest::GetAccountInfo, Some(params));
let response = self
.rpc_client
.make_rpc_request(RpcRequest::GetAccountInfo, Some(params));
response
.and_then(|account_json| {
@ -192,9 +192,9 @@ impl ThinClient {
pub fn transaction_count(&mut self) -> u64 {
debug!("transaction_count");
for _tries in 0..5 {
let response =
self.rpc_client
.make_rpc_request(1, RpcRequest::GetTransactionCount, None);
let response = self
.rpc_client
.make_rpc_request(RpcRequest::GetTransactionCount, None);
match response {
Ok(value) => {
@ -215,9 +215,9 @@ impl ThinClient {
pub fn try_get_recent_blockhash(&mut self, mut num_retries: u64) -> Option<Hash> {
loop {
trace!("try_get_recent_blockhash send_to {}", &self.rpc_addr);
let response =
self.rpc_client
.make_rpc_request(1, RpcRequest::GetRecentBlockhash, None);
let response = self
.rpc_client
.make_rpc_request(RpcRequest::GetRecentBlockhash, None);
match response {
Ok(value) => {
@ -326,11 +326,9 @@ impl ThinClient {
let now = Instant::now();
loop {
let response = self.rpc_client.make_rpc_request(
1,
RpcRequest::ConfirmTransaction,
Some(params.clone()),
);
let response = self
.rpc_client
.make_rpc_request(RpcRequest::ConfirmTransaction, Some(params.clone()));
match response {
Ok(confirmation) => {
@ -363,7 +361,7 @@ impl ThinClient {
trace!("fullnode_exit sending request to {}", self.rpc_addr);
let response = self
.rpc_client
.make_rpc_request(1, RpcRequest::FullnodeExit, None)
.make_rpc_request(RpcRequest::FullnodeExit, None)
.map_err(|error| {
debug!("Response from {} fullndoe_exit: {}", self.rpc_addr, error);
io::Error::new(io::ErrorKind::Other, "FullodeExit request failure")