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 9066cc4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
19 changes: 19 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,11 @@ pub enum RpcCustomError {
UnsupportedTransactionVersion(u8),
#[error("MinContextSlotNotReached")]
MinContextSlotNotReached { context_slot: Slot },
#[error("EpochRewardsPeriodActive")]
EpochRewardsPeriodActive {
slot: Slot,
rewards_complete_block_height: u64,
},
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -79,6 +85,12 @@ pub struct MinContextSlotNotReachedErrorData {
pub context_slot: Slot,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EpochRewardsPeriodActiveErrorData {
pub rewards_complete_block_height: u64,
}

impl From<EncodeError> for RpcCustomError {
fn from(err: EncodeError) -> Self {
match err {
Expand Down Expand Up @@ -206,6 +218,13 @@ impl From<RpcCustomError> for Error {
context_slot,
})),
},
RpcCustomError::EpochRewardsPeriodActive { slot, rewards_complete_block_height } => Self {
code: ErrorCode::ServerError(JSON_RPC_SERVER_ERROR_EPOCH_REWARDS_PERIOD_ACTIVE),
message: format!("Epoch rewards period still active at slot {slot}"),
data: Some(serde_json::json!(EpochRewardsPeriodActiveErrorData {
rewards_complete_block_height,
})),
},
}
}
}
25 changes: 22 additions & 3 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,28 @@ 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.
let rewards_complete_block_height = epoch_boundary_block
.block_height
.map(|block_height| {
block_height
.saturating_add(num_partitions as u64)
.saturating_add(1)
})
.expect(
"every block after partitioned_epoch_reward_enabled should have a \
populated block_height",
);
{
RpcCustomError::EpochRewardsPeriodActive {
slot: bank.slot(),
rewards_complete_block_height,
}
}
})?;

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

0 comments on commit 9066cc4

Please sign in to comment.