Skip to content

Commit

Permalink
v2.0: RPC: rewards, return error if epoch_boundary_block is a lie (ba…
Browse files Browse the repository at this point in the history
…ckport of solana-labs#2758) (solana-labs#2767)

RPC: rewards, return error if epoch_boundary_block is a lie (solana-labs#2758)

* Return error if epoch_boundary_block is not actually the epoch boundary block

* Update rpc-client-api/src/custom_error.rs

Co-authored-by: Trent Nelson <[email protected]>

---------

Co-authored-by: Trent Nelson <[email protected]>
(cherry picked from commit 9a4b094)

Co-authored-by: Tyera <[email protected]>
  • Loading branch information
mergify[bot] and CriesofCarrots authored Aug 30, 2024
1 parent 3853b5d commit 3e7563c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rpc-client-api/src/custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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;
pub const JSON_RPC_SERVER_ERROR_SLOT_NOT_EPOCH_BOUNDARY: i64 = -32018;

#[derive(Error, Debug)]
pub enum RpcCustomError {
Expand Down Expand Up @@ -72,6 +73,8 @@ pub enum RpcCustomError {
current_block_height: u64,
rewards_complete_block_height: u64,
},
#[error("SlotNotEpochBoundary")]
SlotNotEpochBoundary { slot: Slot },
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -228,6 +231,14 @@ impl From<RpcCustomError> for Error {
rewards_complete_block_height,
})),
},
RpcCustomError::SlotNotEpochBoundary { slot } => Self {
code: ErrorCode::ServerError(JSON_RPC_SERVER_ERROR_SLOT_NOT_EPOCH_BOUNDARY),
message: format!(
"Rewards cannot be found because slot {slot} is not the epoch boundary. This \
may be due to gap in the queried node's local ledger or long-term storage"
),
data: None,
},
}
}
}
14 changes: 14 additions & 0 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,20 @@ impl JsonRpcRequestProcessor {
.into());
};

// If there is a gap in blockstore or long-term historical storage that
// includes the epoch boundary, the `get_blocks_with_limit()` call above
// will return the slot of the block at the end of that gap, not a
// legitimate epoch-boundary block. Therefore, verify that the parent of
// `epoch_boundary_block` occurred before the `first_slot_in_epoch`. If
// it didn't, return an error; it will be impossible to locate
// rewards properly.
if epoch_boundary_block.parent_slot >= first_slot_in_epoch {
return Err(RpcCustomError::SlotNotEpochBoundary {
slot: first_confirmed_block_in_epoch,
}
.into());
}

// Collect rewards from first block in the epoch if partitioned epoch
// rewards not enabled, or address is a vote account
let mut reward_map: HashMap<String, (Reward, Slot)> = {
Expand Down

0 comments on commit 3e7563c

Please sign in to comment.