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,53 +2969,83 @@ 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) =
.send::<Response<RpcBlockhash>>( if self.get_node_version()? < semver::Version::new(1, 8, 0) {
RpcRequest::GetLatestBlockhash, let Fees {
json!([self.maybe_map_commitment(commitment)?]), blockhash,
)? last_valid_block_height,
.value; ..
} = self.get_fees_with_commitment(commitment)?.value;
let blockhash = latest_blockhash.blockhash.parse().map_err(|_| { (blockhash, last_valid_block_height)
ClientError::new_with_request( } else {
RpcError::ParseError("Hash".to_string()).into(), let RpcBlockhash {
RpcRequest::GetLatestBlockhash, blockhash,
) last_valid_block_height,
})?; } = self
Ok((blockhash, latest_blockhash.last_valid_block_height)) .send::<Response<RpcBlockhash>>(
RpcRequest::GetLatestBlockhash,
json!([self.maybe_map_commitment(commitment)?]),
)?
.value;
let blockhash = blockhash.parse().map_err(|_| {
ClientError::new_with_request(
RpcError::ParseError("Hash".to_string()).into(),
RpcRequest::GetLatestBlockhash,
)
})?;
(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) {
RpcRequest::IsBlockhashValid, self.get_fee_calculator_for_blockhash_with_commitment(blockhash, commitment)?
json!([blockhash.to_string(), commitment,]), .value
)?; .is_some()
Ok(result.value) } else {
self.send::<Response<bool>>(
RpcRequest::IsBlockhashValid,
json!([blockhash.to_string(), commitment,]),
)?
.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> {
let serialized_encoded = if self.get_node_version()? < semver::Version::new(1, 8, 0) {
serialize_and_encode::<Message>(message, UiTransactionEncoding::Base64)?; let Fees { fee_calculator, .. } = self.get_fees()?;
let result = self.send::<Response<Option<u64>>>( Ok(fee_calculator
RpcRequest::GetFeeForMessage, .lamports_per_signature
json!([ .saturating_mul(message.header.num_required_signatures as u64))
blockhash.to_string(), } else {
serialized_encoded, let serialized_encoded =
UiTransactionEncoding::Base64, serialize_and_encode::<Message>(message, UiTransactionEncoding::Base64)?;
self.commitment(), let result = self.send::<Response<Option<u64>>>(
]), RpcRequest::GetFeeForMessage,
)?; json!([
result blockhash.to_string(),
.value serialized_encoded,
.ok_or_else(|| ClientErrorKind::Custom("Invalid blockhash".to_string()).into()) UiTransactionEncoding::Base64,
self.commitment(),
]),
)?;
result
.value
.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> {