Add backward compatibility for new RpcClient methods (#19383)

This commit is contained in:
Tyera Eulberg 2021-08-23 18:09:00 -06:00 committed by GitHub
parent 0b3fad19f4
commit cf3fc61821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 63 additions and 33 deletions

View File

@ -2969,39 +2969,68 @@ impl RpcClient {
Ok(blockhash) Ok(blockhash)
} }
#[allow(deprecated)]
pub fn get_latest_blockhash_with_commitment( pub fn get_latest_blockhash_with_commitment(
&self, &self,
commitment: CommitmentConfig, commitment: CommitmentConfig,
) -> ClientResult<(Hash, u64)> { ) -> ClientResult<(Hash, u64)> {
let latest_blockhash = self let (blockhash, last_valid_block_height) =
if self.get_node_version()? < semver::Version::new(1, 8, 0) {
let Fees {
blockhash,
last_valid_block_height,
..
} = self.get_fees_with_commitment(commitment)?.value;
(blockhash, last_valid_block_height)
} else {
let RpcBlockhash {
blockhash,
last_valid_block_height,
} = self
.send::<Response<RpcBlockhash>>( .send::<Response<RpcBlockhash>>(
RpcRequest::GetLatestBlockhash, RpcRequest::GetLatestBlockhash,
json!([self.maybe_map_commitment(commitment)?]), json!([self.maybe_map_commitment(commitment)?]),
)? )?
.value; .value;
let blockhash = blockhash.parse().map_err(|_| {
let blockhash = latest_blockhash.blockhash.parse().map_err(|_| {
ClientError::new_with_request( ClientError::new_with_request(
RpcError::ParseError("Hash".to_string()).into(), RpcError::ParseError("Hash".to_string()).into(),
RpcRequest::GetLatestBlockhash, RpcRequest::GetLatestBlockhash,
) )
})?; })?;
Ok((blockhash, latest_blockhash.last_valid_block_height)) (blockhash, last_valid_block_height)
};
Ok((blockhash, last_valid_block_height))
} }
#[allow(deprecated)]
pub fn is_blockhash_valid( pub fn is_blockhash_valid(
&self, &self,
blockhash: &Hash, blockhash: &Hash,
commitment: CommitmentConfig, commitment: CommitmentConfig,
) -> ClientResult<bool> { ) -> ClientResult<bool> {
let result = self.send::<Response<bool>>( let result = if self.get_node_version()? < semver::Version::new(1, 8, 0) {
self.get_fee_calculator_for_blockhash_with_commitment(blockhash, commitment)?
.value
.is_some()
} else {
self.send::<Response<bool>>(
RpcRequest::IsBlockhashValid, RpcRequest::IsBlockhashValid,
json!([blockhash.to_string(), commitment,]), json!([blockhash.to_string(), commitment,]),
)?; )?
Ok(result.value) .value
};
Ok(result)
} }
#[allow(deprecated)]
pub fn get_fee_for_message(&self, blockhash: &Hash, message: &Message) -> ClientResult<u64> { pub fn get_fee_for_message(&self, blockhash: &Hash, message: &Message) -> ClientResult<u64> {
if self.get_node_version()? < semver::Version::new(1, 8, 0) {
let Fees { fee_calculator, .. } = self.get_fees()?;
Ok(fee_calculator
.lamports_per_signature
.saturating_mul(message.header.num_required_signatures as u64))
} else {
let serialized_encoded = let serialized_encoded =
serialize_and_encode::<Message>(message, UiTransactionEncoding::Base64)?; serialize_and_encode::<Message>(message, UiTransactionEncoding::Base64)?;
let result = self.send::<Response<Option<u64>>>( let result = self.send::<Response<Option<u64>>>(
@ -3017,6 +3046,7 @@ impl RpcClient {
.value .value
.ok_or_else(|| ClientErrorKind::Custom("Invalid blockhash".to_string()).into()) .ok_or_else(|| ClientErrorKind::Custom("Invalid blockhash".to_string()).into())
} }
}
pub fn get_new_latest_blockhash(&self, blockhash: &Hash) -> ClientResult<Hash> { pub fn get_new_latest_blockhash(&self, blockhash: &Hash) -> ClientResult<Hash> {
let mut num_retries = 0; let mut num_retries = 0;