Skip to content

Commit

Permalink
rpc: add method sanitize_transaction (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Aug 8, 2024
1 parent 55e7b00 commit 1d3a02d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rpc-client-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ pub struct RpcSendTransactionConfig {
pub min_context_slot: Option<Slot>,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RcpSanitizeTransactionConfig {
pub sig_verify: bool,
#[serde(flatten)]
pub commitment: Option<CommitmentConfig>,
pub encoding: Option<UiTransactionEncoding>,
pub min_context_slot: Option<Slot>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcSimulateTransactionAccountsConfig {
Expand Down
2 changes: 2 additions & 0 deletions rpc-client-api/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub enum RpcRequest {
RegisterNode,
RequestAirdrop,
SendTransaction,
SanitizeTransaction,
SimulateTransaction,
SignVote,
}
Expand Down Expand Up @@ -130,6 +131,7 @@ impl fmt::Display for RpcRequest {
RpcRequest::RegisterNode => "registerNode",
RpcRequest::RequestAirdrop => "requestAirdrop",
RpcRequest::SendTransaction => "sendTransaction",
RpcRequest::SanitizeTransaction => "sanitizeTransaction",
RpcRequest::SimulateTransaction => "simulateTransaction",
RpcRequest::SignVote => "signVote",
};
Expand Down
20 changes: 20 additions & 0 deletions rpc-client/src/nonblocking/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,26 @@ impl RpcClient {
}
}

pub async fn sanitize_transaction(
&self,
transaction: &impl SerializableTransaction,
config: RcpSanitizeTransactionConfig,
) -> ClientResult<()> {
let encoding = config.encoding.unwrap_or(UiTransactionEncoding::Base64);
let commitment = config.commitment.unwrap_or_default();
let config = RcpSanitizeTransactionConfig {
encoding: Some(encoding),
commitment: Some(commitment),
..config
};
let serialized_encoded = serialize_and_encode(transaction, encoding)?;
self.send(
RpcRequest::SanitizeTransaction,
json!([serialized_encoded, config]),
)
.await
}

/// Simulates sending a transaction.
///
/// If the transaction fails, then the [`err`] field of the returned
Expand Down
42 changes: 42 additions & 0 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3359,6 +3359,14 @@ pub mod rpc_full {
config: Option<RpcSendTransactionConfig>,
) -> Result<String>;

#[rpc(meta, name = "sanitizeTransaction")]
fn sanitize_transaction(
&self,
meta: Self::Metadata,
data: String,
config: Option<RcpSanitizeTransactionConfig>,
) -> Result<()>;

#[rpc(meta, name = "simulateTransaction")]
fn simulate_transaction(
&self,
Expand Down Expand Up @@ -3799,6 +3807,40 @@ pub mod rpc_full {
)
}

fn sanitize_transaction(
&self,
meta: Self::Metadata,
data: String,
config: Option<RcpSanitizeTransactionConfig>,
) -> Result<()> {
let RcpSanitizeTransactionConfig {
sig_verify,
commitment,
encoding,
min_context_slot,
} = config.unwrap_or_default();
let tx_encoding = encoding.unwrap_or(UiTransactionEncoding::Base58);
let binary_encoding = tx_encoding.into_binary_encoding().ok_or_else(|| {
Error::invalid_params(format!(
"unsupported encoding: {tx_encoding}. Supported encodings: base58, base64"
))
})?;
let (_wire_transaction, unsanitized_tx) =
decode_and_deserialize::<VersionedTransaction>(data, binary_encoding)?;

let bank = &*meta.get_bank_with_config(RpcContextConfig {
commitment,
min_context_slot,
})?;
let transaction =
sanitize_transaction(unsanitized_tx, bank, bank.get_reserved_account_keys())?;
if sig_verify {
verify_transaction(&transaction, &bank.feature_set)?;
}

Ok(())
}

fn simulate_transaction(
&self,
meta: Self::Metadata,
Expand Down

0 comments on commit 1d3a02d

Please sign in to comment.