Skip to content

Commit

Permalink
fix: add commitment for requests that do not support the processed …
Browse files Browse the repository at this point in the history
…level
  • Loading branch information
tiamo committed Jan 6, 2025
1 parent 73fc4fd commit 3ed64a6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ic-solana/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ impl RpcClient {
/// Method relies on the `getBlock` RPC call to get the block:
/// https://solana.com/docs/rpc/http/getBlock
pub async fn get_block(&self, slot: Slot, config: Option<RpcBlockConfig>) -> RpcResult<UiConfirmedBlock> {
if let Some(commitment) = config.as_ref().and_then(|c| c.commitment) {
ensure_at_least_confirmed(commitment.into())?;
}

self.call(
RpcRequest::GetBlock,
(slot, config.unwrap_or_default()),
Expand Down Expand Up @@ -415,6 +419,10 @@ impl RpcClient {
json!([start_slot, commitment_config])
};

if let Some(commitment_config) = commitment_config {
ensure_at_least_confirmed(commitment_config)?;
}

let end_slot = end_slot.unwrap_or(start_slot + MAX_GET_BLOCKS_RANGE);
let limit = end_slot.saturating_sub(start_slot);

Expand Down Expand Up @@ -451,6 +459,10 @@ impl RpcClient {
)));
}

if let Some(commitment_config) = commitment_config {
ensure_at_least_confirmed(commitment_config)?;
}

self.call(
RpcRequest::GetBlocksWithLimit,
(start_slot, limit, commitment_config),
Expand Down Expand Up @@ -997,6 +1009,10 @@ impl RpcClient {
signature: &Signature,
config: Option<RpcTransactionConfig>,
) -> RpcResult<Option<EncodedConfirmedTransactionWithStatusMeta>> {
if let Some(commitment) = config.as_ref().and_then(|c| c.commitment) {
ensure_at_least_confirmed(commitment.into())?;
}

self.call(
RpcRequest::GetTransaction,
(signature.to_string(), config.unwrap_or_default()),
Expand Down Expand Up @@ -1163,6 +1179,17 @@ impl RpcClient {
}
}

/// Ensures that the provided commitment configuration meets the minimum required level of
/// 'confirmed'.
fn ensure_at_least_confirmed(commitment_config: CommitmentConfig) -> RpcResult<()> {
if !commitment_config.is_at_least_confirmed() {
return Err(RpcError::ValidationError(
"This method requires a commitment level of 'confirmed' or higher.".to_string(),
));
}
Ok(())
}

// TODO:
// pub fn is_response_too_large(code: &RejectionCode, message: &str) -> bool {
// code == &RejectionCode::SysFatal && (message.contains("size limit") ||
Expand Down

0 comments on commit 3ed64a6

Please sign in to comment.