solana/client/src/mock_rpc_client_request.rs

101 lines
3.5 KiB
Rust
Raw Normal View History

use crate::{
client_error::ClientError,
generic_rpc_client_request::GenericRpcClientRequest,
rpc_request::RpcRequest,
rpc_response::{Response, RpcResponseContext},
};
use serde_json::{Number, Value};
use solana_sdk::{
fee_calculator::FeeCalculator,
instruction::InstructionError,
transaction::{self, TransactionError},
};
use std::{collections::HashMap, sync::RwLock};
2019-03-12 18:26:07 -06:00
pub const PUBKEY: &str = "7RoSF9fUmdphVCpabEoefH81WwrW7orsWonXWqTXkKV8";
pub const SIGNATURE: &str =
"43yNSFC6fYTuPgTNFFhF4axw7AfWxB2BPdurme8yrsWEYwm8299xh8n6TAHjGymiSub1XtyxTNyd9GBfY2hxoBw8";
pub type Mocks = HashMap<RpcRequest, Value>;
pub struct MockRpcClientRequest {
mocks: RwLock<Mocks>,
url: String,
2019-03-12 18:26:07 -06:00
}
impl MockRpcClientRequest {
2019-03-15 22:42:36 -07:00
pub fn new(url: String) -> Self {
Self::new_with_mocks(url, Mocks::default())
}
pub fn new_with_mocks(url: String, mocks: Mocks) -> Self {
Self {
url,
mocks: RwLock::new(mocks),
}
2019-03-12 18:26:07 -06:00
}
}
2019-03-12 18:26:07 -06:00
impl GenericRpcClientRequest for MockRpcClientRequest {
fn send(
2019-03-12 18:26:07 -06:00
&self,
request: &RpcRequest,
2019-12-18 22:26:11 -07:00
params: serde_json::Value,
_retries: usize,
2019-04-25 11:29:44 -06:00
) -> Result<serde_json::Value, ClientError> {
if let Some(value) = self.mocks.write().unwrap().remove(request) {
return Ok(value);
}
2019-03-15 22:42:36 -07:00
if self.url == "fails" {
2019-03-12 18:26:07 -06:00
return Ok(Value::Null);
}
let val = match request {
RpcRequest::ConfirmTransaction => {
2019-12-18 22:26:11 -07:00
if let Some(params_array) = params.as_array() {
if let Value::String(param_string) = &params_array[0] {
2019-03-12 18:26:07 -06:00
Value::Bool(param_string == SIGNATURE)
} else {
Value::Null
}
} else {
Value::Null
}
}
RpcRequest::GetBalance => {
2019-03-15 22:42:36 -07:00
let n = if self.url == "airdrop" { 0 } else { 50 };
serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: Value::Number(Number::from(n)),
})?
2019-03-12 18:26:07 -06:00
}
RpcRequest::GetRecentBlockhash => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: (
Value::String(PUBKEY.to_string()),
serde_json::to_value(FeeCalculator::default()).unwrap(),
),
})?,
2019-03-12 18:26:07 -06:00
RpcRequest::GetSignatureStatus => {
2019-04-05 20:56:17 -06:00
let response: Option<transaction::Result<()>> = if self.url == "account_in_use" {
Some(Err(TransactionError::AccountInUse))
} else if self.url == "instruction_error" {
Some(Err(TransactionError::InstructionError(
0,
InstructionError::UninitializedAccount,
)))
2019-04-05 20:56:17 -06:00
} else if self.url == "sig_not_found" {
None
2019-03-12 18:26:07 -06:00
} else {
2019-04-05 20:56:17 -06:00
Some(Ok(()))
2019-03-12 18:26:07 -06:00
};
2019-04-05 20:56:17 -06:00
serde_json::to_value(response).unwrap()
2019-03-12 18:26:07 -06:00
}
RpcRequest::GetTransactionCount => Value::Number(Number::from(1234)),
RpcRequest::GetSlot => Value::Number(Number::from(0)),
2019-03-12 18:26:07 -06:00
RpcRequest::SendTransaction => Value::String(SIGNATURE.to_string()),
RpcRequest::GetMinimumBalanceForRentExemption => Value::Number(Number::from(1234)),
2019-03-12 18:26:07 -06:00
_ => Value::Null,
};
Ok(val)
}
}