Skip to content

Commit

Permalink
Add custom error and use in getInflationReward
Browse files Browse the repository at this point in the history
  • Loading branch information
CriesofCarrots committed Jun 25, 2024
1 parent f2ecfb9 commit fa40c26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions rpc-client-api/src/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH: i64 = -32013
pub const JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: i64 = -32014;
pub const JSON_RPC_SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION: i64 = -32015;
pub const JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: i64 = -32016;
pub const JSON_RPC_SERVER_ERROR_EPOCH_REWARDS_PERIOD_ACTIVE: i64 = -32017;

#[derive(Error, Debug)]
pub enum RpcCustomError {
Expand Down Expand Up @@ -65,6 +66,8 @@ pub enum RpcCustomError {
UnsupportedTransactionVersion(u8),
#[error("MinContextSlotNotReached")]
MinContextSlotNotReached { context_slot: Slot },
#[error("EpochRewardsPeriodActive")]
EpochRewardsPeriodActive { slot: Slot },
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -206,6 +209,11 @@ impl From<RpcCustomError> for Error {
context_slot,
})),
},
RpcCustomError::EpochRewardsPeriodActive { slot } => Self {
code: ErrorCode::ServerError(JSON_RPC_SERVER_ERROR_EPOCH_REWARDS_PERIOD_ACTIVE),
message: format!("Epoch rewards period still active at slot {slot}"),
data: None,
},
}
}
}
9 changes: 6 additions & 3 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,12 @@ impl JsonRpcRequestProcessor {
.await?;

for (partition_index, addresses) in partition_index_addresses.iter() {
let slot = *block_list
.get(*partition_index)
.ok_or_else(Error::internal_error)?;
let slot = *block_list.get(*partition_index).ok_or_else(||
// If block_list.len() too short to contain
// partition_index, the epoch rewards period must be
// currently active.
{ RpcCustomError::EpochRewardsPeriodActive { slot: bank.slot() }
})?;

let Ok(Some(block)) = self
.get_block(
Expand Down

0 comments on commit fa40c26

Please sign in to comment.