Skip to content

Commit

Permalink
Bump jsonrpsee (paritytech#1530)
Browse files Browse the repository at this point in the history
* switch to jsonrpsee 0.15

* use client_bounds marker
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent fe312ba commit 32a374c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 210 deletions.
2 changes: 1 addition & 1 deletion bridges/relays/client-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async-std = { version = "1.6.5", features = ["attributes"] }
async-trait = "0.1"
codec = { package = "parity-scale-codec", version = "3.0.0" }
futures = "0.3.7"
jsonrpsee = { version = "0.8", features = ["macros", "ws-client"] }
jsonrpsee = { version = "0.15", features = ["macros", "ws-client"] }
log = "0.4.11"
num-traits = "0.2"
rand = "0.7"
Expand Down
242 changes: 46 additions & 196 deletions bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
use crate::{
chain::{Chain, ChainWithBalances, TransactionStatusOf},
rpc::SubstrateClient,
AccountIdOf, BlockNumberOf, ConnectionParams, Error, HashOf, HeaderIdOf, HeaderOf, IndexOf,
Result,
ConnectionParams, Error, HashOf, HeaderIdOf, Result,
};

use async_std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -154,15 +153,8 @@ impl<C: Chain> Client<C> {
let genesis_hash_client = client.clone();
let genesis_hash = tokio
.spawn(async move {
SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_block_hash(&*genesis_hash_client, Some(number))
.await
SubstrateClient::<C>::chain_get_block_hash(&*genesis_hash_client, Some(number))
.await
})
.await??;

Expand Down Expand Up @@ -220,15 +212,7 @@ impl<C: Chain> Client<C> {
/// Returns true if client is connected to at least one peer and is in synced state.
pub async fn ensure_synced(&self) -> Result<()> {
self.jsonrpsee_execute(|client| async move {
let health = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::system_health(&*client)
.await?;
let health = SubstrateClient::<C>::system_health(&*client).await?;
let is_synced = !health.is_syncing && (!health.should_have_peers || health.peers > 0);
if is_synced {
Ok(())
Expand All @@ -247,15 +231,7 @@ impl<C: Chain> Client<C> {
/// Return hash of the best finalized block.
pub async fn best_finalized_header_hash(&self) -> Result<C::Hash> {
self.jsonrpsee_execute(|client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_finalized_head(&*client)
.await?)
Ok(SubstrateClient::<C>::chain_get_finalized_head(&*client).await?)
})
.await
}
Expand All @@ -276,31 +252,15 @@ impl<C: Chain> Client<C> {
C::Header: DeserializeOwned,
{
self.jsonrpsee_execute(|client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_header(&*client, None)
.await?)
Ok(SubstrateClient::<C>::chain_get_header(&*client, None).await?)
})
.await
}

/// Get a Substrate block from its hash.
pub async fn get_block(&self, block_hash: Option<C::Hash>) -> Result<C::SignedBlock> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_block(&*client, block_hash)
.await?)
Ok(SubstrateClient::<C>::chain_get_block(&*client, block_hash).await?)
})
.await
}
Expand All @@ -311,31 +271,15 @@ impl<C: Chain> Client<C> {
C::Header: DeserializeOwned,
{
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_header(&*client, Some(block_hash))
.await?)
Ok(SubstrateClient::<C>::chain_get_header(&*client, Some(block_hash)).await?)
})
.await
}

/// Get a Substrate block hash by its number.
pub async fn block_hash_by_number(&self, number: C::BlockNumber) -> Result<C::Hash> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::chain_get_block_hash(&*client, Some(number))
.await?)
Ok(SubstrateClient::<C>::chain_get_block_hash(&*client, Some(number)).await?)
})
.await
}
Expand All @@ -353,15 +297,7 @@ impl<C: Chain> Client<C> {
/// Return runtime version.
pub async fn runtime_version(&self) -> Result<RuntimeVersion> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_runtime_version(&*client)
.await?)
Ok(SubstrateClient::<C>::state_runtime_version(&*client).await?)
})
.await
}
Expand Down Expand Up @@ -405,15 +341,7 @@ impl<C: Chain> Client<C> {
block_hash: Option<C::Hash>,
) -> Result<Option<StorageData>> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_get_storage(&*client, storage_key, block_hash)
.await?)
Ok(SubstrateClient::<C>::state_get_storage(&*client, storage_key, block_hash).await?)
})
.await
}
Expand All @@ -425,16 +353,10 @@ impl<C: Chain> Client<C> {
{
self.jsonrpsee_execute(move |client| async move {
let storage_key = C::account_info_storage_key(&account);
let encoded_account_data = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_get_storage(&*client, storage_key, None)
.await?
.ok_or(Error::AccountDoesNotExist)?;
let encoded_account_data =
SubstrateClient::<C>::state_get_storage(&*client, storage_key, None)
.await?
.ok_or(Error::AccountDoesNotExist)?;
let decoded_account_data = AccountInfo::<C::Index, AccountData<C::Balance>>::decode(
&mut &encoded_account_data.0[..],
)
Expand All @@ -449,15 +371,7 @@ impl<C: Chain> Client<C> {
/// Note: It's the caller's responsibility to make sure `account` is a valid SS58 address.
pub async fn next_account_index(&self, account: C::AccountId) -> Result<C::Index> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::system_account_next_index(&*client, account)
.await?)
Ok(SubstrateClient::<C>::system_account_next_index(&*client, account).await?)
})
.await
}
Expand All @@ -467,19 +381,12 @@ impl<C: Chain> Client<C> {
/// Note: The given transaction needs to be SCALE encoded beforehand.
pub async fn submit_unsigned_extrinsic(&self, transaction: Bytes) -> Result<C::Hash> {
self.jsonrpsee_execute(move |client| async move {
let tx_hash = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::author_submit_extrinsic(&*client, transaction)
.await
.map_err(|e| {
log::error!(target: "bridge", "Failed to send transaction to {} node: {:?}", C::NAME, e);
e
})?;
let tx_hash = SubstrateClient::<C>::author_submit_extrinsic(&*client, transaction)
.await
.map_err(|e| {
log::error!(target: "bridge", "Failed to send transaction to {} node: {:?}", C::NAME, e);
e
})?;
log::trace!(target: "bridge", "Sent transaction to {} node: {:?}", C::NAME, tx_hash);
Ok(tx_hash)
})
Expand Down Expand Up @@ -511,19 +418,12 @@ impl<C: Chain> Client<C> {

self.jsonrpsee_execute(move |client| async move {
let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let tx_hash = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::author_submit_extrinsic(&*client, extrinsic)
.await
.map_err(|e| {
log::error!(target: "bridge", "Failed to send transaction to {} node: {:?}", C::NAME, e);
e
})?;
let tx_hash = SubstrateClient::<C>::author_submit_extrinsic(&*client, extrinsic)
.await
.map_err(|e| {
log::error!(target: "bridge", "Failed to send transaction to {} node: {:?}", C::NAME, e);
e
})?;
log::trace!(target: "bridge", "Sent transaction to {} node: {:?}", C::NAME, tx_hash);
Ok(tx_hash)
})
Expand Down Expand Up @@ -574,15 +474,7 @@ impl<C: Chain> Client<C> {
/// Returns pending extrinsics from transaction pool.
pub async fn pending_extrinsics(&self) -> Result<Vec<Bytes>> {
self.jsonrpsee_execute(move |client| async move {
Ok(SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::author_pending_extrinsics(&*client)
.await?)
Ok(SubstrateClient::<C>::author_pending_extrinsics(&*client).await?)
})
.await
}
Expand All @@ -597,15 +489,8 @@ impl<C: Chain> Client<C> {
let call = SUB_API_TXPOOL_VALIDATE_TRANSACTION.to_string();
let data = Bytes((TransactionSource::External, transaction, at_block).encode());

let encoded_response = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_call(&*client, call, data, Some(at_block))
.await?;
let encoded_response =
SubstrateClient::<C>::state_call(&*client, call, data, Some(at_block)).await?;
let validity = TransactionValidity::decode(&mut &encoded_response.0[..])
.map_err(Error::ResponseParseFailed)?;

Expand All @@ -620,15 +505,9 @@ impl<C: Chain> Client<C> {
transaction: Bytes,
) -> Result<InclusionFee<C::Balance>> {
self.jsonrpsee_execute(move |client| async move {
let fee_details = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::payment_query_fee_details(&*client, transaction, None)
.await?;
let fee_details =
SubstrateClient::<C>::payment_query_fee_details(&*client, transaction, None)
.await?;
let inclusion_fee = fee_details
.inclusion_fee
.map(|inclusion_fee| InclusionFee {
Expand Down Expand Up @@ -660,15 +539,8 @@ impl<C: Chain> Client<C> {
let call = SUB_API_GRANDPA_AUTHORITIES.to_string();
let data = Bytes(Vec::new());

let encoded_response = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_call(&*client, call, data, Some(block))
.await?;
let encoded_response =
SubstrateClient::<C>::state_call(&*client, call, data, Some(block)).await?;
let authority_list = encoded_response.0;

Ok(authority_list)
Expand All @@ -684,16 +556,9 @@ impl<C: Chain> Client<C> {
at_block: Option<C::Hash>,
) -> Result<Bytes> {
self.jsonrpsee_execute(move |client| async move {
SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_call(&*client, method, data, at_block)
.await
.map_err(Into::into)
SubstrateClient::<C>::state_call(&*client, method, data, at_block)
.await
.map_err(Into::into)
})
.await
}
Expand All @@ -705,35 +570,20 @@ impl<C: Chain> Client<C> {
at_block: C::Hash,
) -> Result<StorageProof> {
self.jsonrpsee_execute(move |client| async move {
SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::state_prove_storage(&*client, keys, Some(at_block))
.await
.map(|proof| {
StorageProof::new(proof.proof.into_iter().map(|b| b.0).collect::<Vec<_>>())
})
.map_err(Into::into)
SubstrateClient::<C>::state_prove_storage(&*client, keys, Some(at_block))
.await
.map(|proof| {
StorageProof::new(proof.proof.into_iter().map(|b| b.0).collect::<Vec<_>>())
})
.map_err(Into::into)
})
.await
}

/// Return `tokenDecimals` property from the set of chain properties.
pub async fn token_decimals(&self) -> Result<Option<u64>> {
self.jsonrpsee_execute(move |client| async move {
let system_properties = SubstrateClient::<
AccountIdOf<C>,
BlockNumberOf<C>,
HashOf<C>,
HeaderOf<C>,
IndexOf<C>,
C::SignedBlock,
>::system_properties(&*client)
.await?;
let system_properties = SubstrateClient::<C>::system_properties(&*client).await?;
Ok(system_properties.get("tokenDecimals").and_then(|v| v.as_u64()))
})
.await
Expand Down
Loading

0 comments on commit 32a374c

Please sign in to comment.