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 Jul 16, 2024
1 parent 2f055bb commit 1cfde0f
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 @@ -360,3 +360,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 @@ -4578,6 +4578,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))
}

pub async fn get_latest_blockhash_with_commitment(
&self,
commitment: CommitmentConfig,
Expand Down
20 changes: 16 additions & 4 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2184,8 +2184,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 @@ -3422,7 +3434,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 @@ -4098,7 +4110,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 1cfde0f

Please sign in to comment.