Bump jsonrpsee (#1530)

* switch to jsonrpsee 0.15

* use client_bounds marker
This commit is contained in:
Svyatoslav Nikolsky
2022-08-01 14:30:09 +03:00
committed by Bastian Köcher
parent 86b5a0ee6a
commit dd9debed3c
3 changed files with 65 additions and 210 deletions
+1 -1
View File
@@ -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"
+46 -196
View File
@@ -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};
@@ -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??;
@@ -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(())
@@ -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
}
@@ -276,15 +252,7 @@ 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
}
@@ -292,15 +260,7 @@ impl<C: Chain> Client<C> {
/// 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
}
@@ -311,15 +271,7 @@ 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
}
@@ -327,15 +279,7 @@ impl<C: Chain> Client<C> {
/// 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
}
@@ -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
}
@@ -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
}
@@ -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[..],
)
@@ -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
}
@@ -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)
})
@@ -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)
})
@@ -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
}
@@ -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)?;
@@ -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 {
@@ -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)
@@ -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
}
@@ -705,19 +570,12 @@ 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
}
@@ -725,15 +583,7 @@ impl<C: Chain> Client<C> {
/// 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
+18 -13
View File
@@ -16,6 +16,8 @@
//! The most generic Substrate node RPC interface.
use crate::Chain;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use pallet_transaction_payment_rpc_runtime_api::FeeDetails;
use sc_rpc_api::{state::ReadProof, system::Health};
@@ -26,24 +28,27 @@ use sp_core::{
use sp_rpc::number::NumberOrHex;
use sp_version::RuntimeVersion;
#[rpc(client)]
pub(crate) trait Substrate<AccountId, BlockNumber, Hash, Header, Index, SignedBlock> {
#[rpc(client, client_bounds(C: Chain))]
pub(crate) trait Substrate<C> {
#[method(name = "system_health", param_kind = array)]
async fn system_health(&self) -> RpcResult<Health>;
#[method(name = "system_properties", param_kind = array)]
async fn system_properties(&self) -> RpcResult<sc_chain_spec::Properties>;
#[method(name = "chain_getHeader", param_kind = array)]
async fn chain_get_header(&self, block_hash: Option<Hash>) -> RpcResult<Header>;
async fn chain_get_header(&self, block_hash: Option<C::Hash>) -> RpcResult<C::Header>;
#[method(name = "chain_getFinalizedHead", param_kind = array)]
async fn chain_get_finalized_head(&self) -> RpcResult<Hash>;
async fn chain_get_finalized_head(&self) -> RpcResult<C::Hash>;
#[method(name = "chain_getBlock", param_kind = array)]
async fn chain_get_block(&self, block_hash: Option<Hash>) -> RpcResult<SignedBlock>;
async fn chain_get_block(&self, block_hash: Option<C::Hash>) -> RpcResult<C::SignedBlock>;
#[method(name = "chain_getBlockHash", param_kind = array)]
async fn chain_get_block_hash(&self, block_number: Option<BlockNumber>) -> RpcResult<Hash>;
async fn chain_get_block_hash(
&self,
block_number: Option<C::BlockNumber>,
) -> RpcResult<C::Hash>;
#[method(name = "system_accountNextIndex", param_kind = array)]
async fn system_account_next_index(&self, account_id: AccountId) -> RpcResult<Index>;
async fn system_account_next_index(&self, account_id: C::AccountId) -> RpcResult<C::Index>;
#[method(name = "author_submitExtrinsic", param_kind = array)]
async fn author_submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<Hash>;
async fn author_submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<C::Hash>;
#[method(name = "author_pendingExtrinsics", param_kind = array)]
async fn author_pending_extrinsics(&self) -> RpcResult<Vec<Bytes>>;
#[method(name = "state_call", param_kind = array)]
@@ -51,26 +56,26 @@ pub(crate) trait Substrate<AccountId, BlockNumber, Hash, Header, Index, SignedBl
&self,
method: String,
data: Bytes,
at_block: Option<Hash>,
at_block: Option<C::Hash>,
) -> RpcResult<Bytes>;
#[method(name = "state_getStorage", param_kind = array)]
async fn state_get_storage(
&self,
key: StorageKey,
at_block: Option<Hash>,
at_block: Option<C::Hash>,
) -> RpcResult<Option<StorageData>>;
#[method(name = "state_getReadProof", param_kind = array)]
async fn state_prove_storage(
&self,
keys: Vec<StorageKey>,
hash: Option<Hash>,
) -> RpcResult<ReadProof<Hash>>;
hash: Option<C::Hash>,
) -> RpcResult<ReadProof<C::Hash>>;
#[method(name = "state_getRuntimeVersion", param_kind = array)]
async fn state_runtime_version(&self) -> RpcResult<RuntimeVersion>;
#[method(name = "payment_queryFeeDetails", param_kind = array)]
async fn payment_query_fee_details(
&self,
extrinsic: Bytes,
at_block: Option<Hash>,
at_block: Option<C::Hash>,
) -> RpcResult<FeeDetails<NumberOrHex>>;
}