Skip to content

Commit

Permalink
fix: client rpc error types
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Oct 16, 2024
1 parent b4aaae5 commit 73af511
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 54 deletions.
68 changes: 21 additions & 47 deletions engine/src/state_chain_observer/client/base_rpc_api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use async_trait::async_trait;

use jsonrpsee::core::{
client::{ClientT, Subscription, SubscriptionClientT},
RpcResult,
};
use jsonrpsee::core::client::{ClientT, Error as ClientError, Subscription, SubscriptionClientT};
use sc_transaction_pool_api::TransactionStatus;
use sp_core::{
storage::{StorageData, StorageKey},
Expand All @@ -13,7 +10,7 @@ use sp_version::RuntimeVersion;
use state_chain_runtime::SignedBlock;

use codec::Encode;
use custom_rpc::{to_rpc_error, CustomApiClient};
use custom_rpc::CustomApiClient;
use sc_rpc_api::{
author::AuthorApiClient,
chain::ChainApiClient,
Expand All @@ -26,6 +23,8 @@ use serde_json::value::RawValue;
use std::sync::Arc;
use subxt::backend::rpc::RawRpcSubscription;

type RpcResult<T> = Result<T, ClientError>;

#[cfg(test)]
use mockall::automock;

Expand Down Expand Up @@ -183,60 +182,48 @@ fn unwrap_value<T>(list_or_value: sp_rpc::list::ListOrValue<T>) -> T {
#[async_trait]
impl<RawRpcClient: RawRpcApi + Send + Sync> BaseRpcApi for BaseRpcClient<RawRpcClient> {
async fn health(&self) -> RpcResult<Health> {
self.raw_rpc_client.system_health().await.map_err(to_rpc_error)
self.raw_rpc_client.system_health().await
}

async fn next_account_nonce(
&self,
account_id: state_chain_runtime::AccountId,
) -> RpcResult<state_chain_runtime::Nonce> {
self.raw_rpc_client.nonce(account_id).await.map_err(to_rpc_error)
self.raw_rpc_client.nonce(account_id).await
}

async fn submit_extrinsic(
&self,
extrinsic: state_chain_runtime::UncheckedExtrinsic,
) -> RpcResult<sp_core::H256> {
self.raw_rpc_client
.submit_extrinsic(Bytes::from(extrinsic.encode()))
.await
.map_err(to_rpc_error)
self.raw_rpc_client.submit_extrinsic(Bytes::from(extrinsic.encode())).await
}

async fn submit_and_watch_extrinsic(
&self,
extrinsic: state_chain_runtime::UncheckedExtrinsic,
) -> RpcResult<Subscription<TransactionStatus<sp_core::H256, sp_core::H256>>> {
self.raw_rpc_client
.watch_extrinsic(Bytes::from(extrinsic.encode()))
.await
.map_err(to_rpc_error)
self.raw_rpc_client.watch_extrinsic(Bytes::from(extrinsic.encode())).await
}

async fn storage(
&self,
block_hash: state_chain_runtime::Hash,
storage_key: StorageKey,
) -> RpcResult<Option<StorageData>> {
self.raw_rpc_client
.storage(storage_key, Some(block_hash))
.await
.map_err(to_rpc_error)
self.raw_rpc_client.storage(storage_key, Some(block_hash)).await
}

async fn storage_pairs(
&self,
block_hash: state_chain_runtime::Hash,
storage_key: StorageKey,
) -> RpcResult<Vec<(StorageKey, StorageData)>> {
self.raw_rpc_client
.storage_pairs(storage_key, Some(block_hash))
.await
.map_err(to_rpc_error)
self.raw_rpc_client.storage_pairs(storage_key, Some(block_hash)).await
}

async fn block(&self, block_hash: state_chain_runtime::Hash) -> RpcResult<Option<SignedBlock>> {
self.raw_rpc_client.block(Some(block_hash)).await.map_err(to_rpc_error)
self.raw_rpc_client.block(Some(block_hash)).await
}

async fn block_hash(
Expand All @@ -246,68 +233,58 @@ impl<RawRpcClient: RawRpcApi + Send + Sync> BaseRpcApi for BaseRpcClient<RawRpcC
Ok(unwrap_value(
self.raw_rpc_client
.block_hash(Some(sp_rpc::list::ListOrValue::Value(block_number.into())))
.await
.map_err(to_rpc_error)?,
.await?,
))
}

async fn block_header(
&self,
block_hash: state_chain_runtime::Hash,
) -> RpcResult<state_chain_runtime::Header> {
Ok(self
.raw_rpc_client
.header(Some(block_hash))
.await
.map_err(to_rpc_error)?
.expect(SUBSTRATE_BEHAVIOUR))
Ok(self.raw_rpc_client.header(Some(block_hash)).await?.expect(SUBSTRATE_BEHAVIOUR))
}

async fn latest_unfinalized_block_hash(&self) -> RpcResult<state_chain_runtime::Hash> {
Ok(unwrap_value(self.raw_rpc_client.block_hash(None).await.map_err(to_rpc_error)?)
.expect(SUBSTRATE_BEHAVIOUR))
Ok(unwrap_value(self.raw_rpc_client.block_hash(None).await?).expect(SUBSTRATE_BEHAVIOUR))
}

async fn latest_finalized_block_hash(&self) -> RpcResult<state_chain_runtime::Hash> {
self.raw_rpc_client.finalized_head().await.map_err(to_rpc_error)
self.raw_rpc_client.finalized_head().await
}

async fn subscribe_finalized_block_headers(
&self,
) -> RpcResult<Subscription<state_chain_runtime::Header>> {
self.raw_rpc_client.subscribe_finalized_heads().await.map_err(to_rpc_error)
self.raw_rpc_client.subscribe_finalized_heads().await
}

async fn subscribe_unfinalized_block_headers(
&self,
) -> RpcResult<Subscription<state_chain_runtime::Header>> {
self.raw_rpc_client.subscribe_new_heads().await.map_err(to_rpc_error)
self.raw_rpc_client.subscribe_new_heads().await
}

async fn runtime_version(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<RuntimeVersion> {
self.raw_rpc_client.runtime_version(at).await.map_err(to_rpc_error)
self.raw_rpc_client.runtime_version(at).await
}

async fn dry_run(
&self,
extrinsic: Bytes,
block_hash: state_chain_runtime::Hash,
) -> RpcResult<Bytes> {
self.raw_rpc_client
.dry_run(extrinsic, Some(block_hash))
.await
.map_err(to_rpc_error)
self.raw_rpc_client.dry_run(extrinsic, Some(block_hash)).await
}

async fn request_raw(
&self,
method: &str,
params: Option<Box<RawValue>>,
) -> RpcResult<Box<RawValue>> {
self.raw_rpc_client.request(method, Params(params)).await.map_err(to_rpc_error)
self.raw_rpc_client.request(method, Params(params)).await
}

async fn subscribe_raw(
Expand All @@ -316,10 +293,7 @@ impl<RawRpcClient: RawRpcApi + Send + Sync> BaseRpcApi for BaseRpcClient<RawRpcC
params: Option<Box<RawValue>>,
unsub: &str,
) -> RpcResult<Subscription<Box<RawValue>>> {
self.raw_rpc_client
.subscribe(sub, Params(params), unsub)
.await
.map_err(to_rpc_error)
self.raw_rpc_client.subscribe(sub, Params(params), unsub).await
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,26 @@ impl<'a, 'env, BaseRpcClient: base_rpc_api::BaseRpcApi + Send + Sync + 'static>
break Ok(Ok(tx_hash))
},
Err(rpc_err) => {
use jsonrpsee::core::ClientError;
match rpc_err {
// This occurs when a transaction with the same nonce is in the
// transaction pool (and the priority is <= priority of that
// existing tx)
obj if obj.code() == 1014 => {
ClientError::Call(obj) if obj.code() == 1014 => {
debug!(target: "state_chain_client", request_id = request.id, "Submission failed as transaction with same nonce found in transaction pool: {obj:?}");
break Ok(Err(SubmissionLogicError::NonceTooLow))
},
// This occurs when the nonce has already been *consumed* i.e a
// transaction with that nonce is in a block
obj if obj == invalid_err_obj(InvalidTransaction::Stale) => {
ClientError::Call(obj)
if obj == invalid_err_obj(InvalidTransaction::Stale) =>
{
debug!(target: "state_chain_client", request_id = request.id, "Submission failed as the transaction is stale: {obj:?}");
break Ok(Err(SubmissionLogicError::NonceTooLow))
},
obj if obj == invalid_err_obj(InvalidTransaction::BadProof) => {
ClientError::Call(obj)
if obj == invalid_err_obj(InvalidTransaction::BadProof) =>
{
warn!(target: "state_chain_client", request_id = request.id, "Submission failed due to a bad proof: {obj:?}. Refetching the runtime version.");

// TODO: Check if hash and block number should also be updated
Expand All @@ -266,14 +271,19 @@ impl<'a, 'env, BaseRpcClient: base_rpc_api::BaseRpcApi + Send + Sync + 'static>

self.runtime_version = new_runtime_version;
},
obj => {
ClientError::Call(obj) => {
warn!(
"Error while submitting extrinsic {:?} version: {}",
signed_extrinsic,
obj.message()
"Unhandled error while submitting extrinsic {:?}: {:?}",
signed_extrinsic, obj
);
break Err(obj.into())
},
err => {
error!(
"Error while submitting extrinsic {:?}: {}",
signed_extrinsic, err
);
},
}
},
}
Expand Down Expand Up @@ -647,3 +657,8 @@ impl<'a, 'env, BaseRpcClient: base_rpc_api::BaseRpcApi + Send + Sync + 'static>
}
}
}

#[test]
fn test_invalid_err_obj() {
println!("{:?}", invalid_err_obj(InvalidTransaction::Stale));
}

0 comments on commit 73af511

Please sign in to comment.