Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
RpcClient::send<T> now supports client-defined RPC methods via RpcReq…
Browse files Browse the repository at this point in the history
…uest::Custom (#18882)

(cherry picked from commit f264511)

Co-authored-by: Michael Vines <[email protected]>
  • Loading branch information
mergify[bot] and mvines authored Jul 24, 2021
1 parent 548ddff commit aedcab8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
33 changes: 18 additions & 15 deletions client/src/mock_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,34 @@ impl RpcSender for MockSender {
if self.url == "fails" {
return Ok(Value::Null);
}
let val = match request {
RpcRequest::GetAccountInfo => serde_json::to_value(Response {

let method = &request.build_request_json(42, params.clone())["method"];

let val = match method.as_str().unwrap() {
"getAccountInfo" => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: Value::Null,
})?,
RpcRequest::GetBalance => serde_json::to_value(Response {
"getBalance" => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: Value::Number(Number::from(50)),
})?,
RpcRequest::GetRecentBlockhash => serde_json::to_value(Response {
"getRecentBlockhash" => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: (
Value::String(PUBKEY.to_string()),
serde_json::to_value(FeeCalculator::default()).unwrap(),
),
})?,
RpcRequest::GetEpochInfo => serde_json::to_value(EpochInfo {
"getEpochInfo" => serde_json::to_value(EpochInfo {
epoch: 1,
slot_index: 2,
slots_in_epoch: 32,
absolute_slot: 34,
block_height: 34,
transaction_count: Some(123),
})?,
RpcRequest::GetFeeCalculatorForBlockhash => {
"getFeeCalculatorForBlockhash" => {
let value = if self.url == "blockhash_expired" {
Value::Null
} else {
Expand All @@ -84,11 +87,11 @@ impl RpcSender for MockSender {
value,
})?
}
RpcRequest::GetFeeRateGovernor => serde_json::to_value(Response {
"getFeeRateGovernor" => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1 },
value: serde_json::to_value(FeeRateGovernor::default()).unwrap(),
})?,
RpcRequest::GetSignatureStatuses => {
"getSignatureStatuses" => {
let status: transaction::Result<()> = if self.url == "account_in_use" {
Err(TransactionError::AccountInUse)
} else if self.url == "instruction_error" {
Expand Down Expand Up @@ -122,11 +125,11 @@ impl RpcSender for MockSender {
value: statuses,
})?
}
RpcRequest::GetTransactionCount => Value::Number(Number::from(1234)),
RpcRequest::GetSlot => Value::Number(Number::from(0)),
RpcRequest::GetMaxShredInsertSlot => Value::Number(Number::from(0)),
RpcRequest::RequestAirdrop => Value::String(Signature::new(&[8; 64]).to_string()),
RpcRequest::SendTransaction => {
"getTransactionCount" => json![1234],
"getSlot" => json![0],
"getMaxShredInsertSlot" => json![0],
"requestAirdrop" => Value::String(Signature::new(&[8; 64]).to_string()),
"sendTransaction" => {
let signature = if self.url == "malicious" {
Signature::new(&[8; 64]).to_string()
} else {
Expand All @@ -137,8 +140,8 @@ impl RpcSender for MockSender {
};
Value::String(signature)
}
RpcRequest::GetMinimumBalanceForRentExemption => Value::Number(Number::from(20)),
RpcRequest::GetVersion => {
"getMinimumBalanceForRentExemption" => json![20],
"getVersion" => {
let version = Version::default();
json!(RpcVersionInfo {
solana_core: version.to_string(),
Expand Down
15 changes: 15 additions & 0 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@ mod tests {
let signature = rpc_client.send_transaction(&tx);
assert!(signature.is_err());
}

#[test]
fn test_get_recent_blockhash() {
let rpc_client = RpcClient::new_mock("succeeds".to_string());
Expand All @@ -2176,6 +2177,20 @@ mod tests {
assert!(rpc_client.get_recent_blockhash().is_err());
}

#[test]
fn test_custom_request() {
let rpc_client = RpcClient::new_mock("succeeds".to_string());

let slot = rpc_client.get_slot().unwrap();
assert_eq!(slot, 0);

let custom_slot = rpc_client
.send::<Slot>(RpcRequest::Custom { method: "getSlot" }, Value::Null)
.unwrap();

assert_eq!(slot, custom_slot);
}

#[test]
fn test_get_signature_status() {
let signature = Signature::default();
Expand Down
4 changes: 4 additions & 0 deletions client/src/rpc_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ pub enum RpcRequest {
SendTransaction,
SimulateTransaction,
SignVote,
Custom {
method: &'static str,
},
}

#[allow(deprecated)]
Expand Down Expand Up @@ -154,6 +157,7 @@ impl fmt::Display for RpcRequest {
RpcRequest::SendTransaction => "sendTransaction",
RpcRequest::SimulateTransaction => "simulateTransaction",
RpcRequest::SignVote => "signVote",
RpcRequest::Custom { method } => method,
};

write!(f, "{}", method)
Expand Down

0 comments on commit aedcab8

Please sign in to comment.