Skip to content

Commit

Permalink
rpc: add rollback to getLatestBlockhash (agave#2023)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Aug 8, 2024
1 parent 25281b2 commit fb8ebcf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
9 changes: 9 additions & 0 deletions rpc-client-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,12 @@ pub struct RpcContextConfig {
pub struct RpcRecentPrioritizationFeesConfig {
pub percentile: Option<u16>,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcLatestBlockhashConfig {
#[serde(flatten)]
pub context: RpcContextConfig,
#[serde(default)]
pub rollback: usize,
}
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 @@ -5290,6 +5290,26 @@ impl RpcClient {
Ok(blockhash)
}

pub async fn get_latest_blockhash_with_config(
&self,
config: RpcLatestBlockhashConfig,
) -> ClientResult<(Hash, u64)> {
let RpcBlockhash {
blockhash,
last_valid_block_height,
} = self
.send::<Response<RpcBlockhash>>(RpcRequest::GetLatestBlockhash, json!([config]))
.await?
.value;
let blockhash = blockhash.parse().map_err(|_| {
ClientError::new_with_request(
RpcError::ParseError("Hash".to_string()).into(),
RpcRequest::GetLatestBlockhash,
)
})?;
Ok((blockhash, last_valid_block_height))
}

#[allow(deprecated)]
pub async fn get_latest_blockhash_with_commitment(
&self,
Expand Down
20 changes: 16 additions & 4 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,8 +2135,20 @@ impl JsonRpcRequestProcessor {
}
}

fn get_latest_blockhash(&self, config: RpcContextConfig) -> Result<RpcResponse<RpcBlockhash>> {
let bank = self.get_bank_with_config(config)?;
fn get_latest_blockhash(
&self,
config: RpcLatestBlockhashConfig,
) -> Result<RpcResponse<RpcBlockhash>> {
let mut bank = self.get_bank_with_config(config.context)?;
if config.rollback > 300 {
return Err(Error::invalid_params("rollback exceeds 300"));
}
for _ in 0..config.rollback {
bank = match bank.parent() {
Some(bank) => bank,
None => return Err(Error::invalid_params("failed to rollback block")),
};
}
let blockhash = bank.last_blockhash();
let last_valid_block_height = bank
.get_blockhash_last_valid_block_height(&blockhash)
Expand Down Expand Up @@ -3383,7 +3395,7 @@ pub mod rpc_full {
fn get_latest_blockhash(
&self,
meta: Self::Metadata,
config: Option<RpcContextConfig>,
config: Option<RpcLatestBlockhashConfig>,
) -> Result<RpcResponse<RpcBlockhash>>;

#[rpc(meta, name = "isBlockhashValid")]
Expand Down Expand Up @@ -4045,7 +4057,7 @@ pub mod rpc_full {
fn get_latest_blockhash(
&self,
meta: Self::Metadata,
config: Option<RpcContextConfig>,
config: Option<RpcLatestBlockhashConfig>,
) -> Result<RpcResponse<RpcBlockhash>> {
debug!("get_latest_blockhash rpc request received");
meta.get_latest_blockhash(config.unwrap_or_default())
Expand Down

0 comments on commit fb8ebcf

Please sign in to comment.