diff --git a/prdoc/pr_8109.prdoc b/prdoc/pr_8109.prdoc new file mode 100644 index 0000000000000..b5f031f92a221 --- /dev/null +++ b/prdoc/pr_8109.prdoc @@ -0,0 +1,7 @@ +title: 'rpc v2 archive: more verbose error types in API' +doc: +- audience: Node Dev + description: 'This PR changes the error types to more precise rather than arbitrary JSON-RPC error' +crates: +- name: sc-rpc-spec-v2 + bump: major diff --git a/substrate/client/rpc-spec-v2/src/archive/api.rs b/substrate/client/rpc-spec-v2/src/archive/api.rs index 4ce0a9a4f5d64..9f8ffe0db235b 100644 --- a/substrate/client/rpc-spec-v2/src/archive/api.rs +++ b/substrate/client/rpc-spec-v2/src/archive/api.rs @@ -19,12 +19,13 @@ //! API trait of the archive methods. use crate::{ + archive::error::{Error, Infallible}, common::events::{ ArchiveStorageDiffEvent, ArchiveStorageDiffItem, ArchiveStorageEvent, StorageQuery, }, MethodResult, }; -use jsonrpsee::{core::RpcResult, proc_macros::rpc}; +use jsonrpsee::proc_macros::rpc; #[rpc(client, server)] pub trait ArchiveApi { @@ -37,7 +38,7 @@ pub trait ArchiveApi { /// /// This method is unstable and subject to change in the future. #[method(name = "archive_v1_body")] - fn archive_v1_body(&self, hash: Hash) -> RpcResult>>; + fn archive_v1_body(&self, hash: Hash) -> Result>, Infallible>; /// Get the chain's genesis hash. /// @@ -47,7 +48,7 @@ pub trait ArchiveApi { /// /// This method is unstable and subject to change in the future. #[method(name = "archive_v1_genesisHash")] - fn archive_v1_genesis_hash(&self) -> RpcResult; + fn archive_v1_genesis_hash(&self) -> Result; /// Get the block's header. /// @@ -58,7 +59,7 @@ pub trait ArchiveApi { /// /// This method is unstable and subject to change in the future. #[method(name = "archive_v1_header")] - fn archive_v1_header(&self, hash: Hash) -> RpcResult>; + fn archive_v1_header(&self, hash: Hash) -> Result, Infallible>; /// Get the height of the current finalized block. /// @@ -68,7 +69,7 @@ pub trait ArchiveApi { /// /// This method is unstable and subject to change in the future. #[method(name = "archive_v1_finalizedHeight")] - fn archive_v1_finalized_height(&self) -> RpcResult; + fn archive_v1_finalized_height(&self) -> Result; /// Get the hashes of blocks from the given height. /// @@ -79,7 +80,7 @@ pub trait ArchiveApi { /// /// This method is unstable and subject to change in the future. #[method(name = "archive_v1_hashByHeight")] - fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult>; + fn archive_v1_hash_by_height(&self, height: u64) -> Result, Error>; /// Call into the Runtime API at a specified block's state. /// @@ -92,7 +93,7 @@ pub trait ArchiveApi { hash: Hash, function: String, call_parameters: String, - ) -> RpcResult; + ) -> Result; /// Returns storage entries at a specific block's state. /// diff --git a/substrate/client/rpc-spec-v2/src/archive/archive.rs b/substrate/client/rpc-spec-v2/src/archive/archive.rs index ca89dbe3a7c77..9842655085a33 100644 --- a/substrate/client/rpc-spec-v2/src/archive/archive.rs +++ b/substrate/client/rpc-spec-v2/src/archive/archive.rs @@ -20,7 +20,9 @@ use crate::{ archive::{ - archive_storage::ArchiveStorageDiff, error::Error as ArchiveError, ArchiveApiServer, + archive_storage::ArchiveStorageDiff, + error::{Error as ArchiveError, Infallible}, + ArchiveApiServer, }, common::{ events::{ @@ -33,10 +35,7 @@ use crate::{ use codec::Encode; use futures::FutureExt; -use jsonrpsee::{ - core::{async_trait, RpcResult}, - PendingSubscriptionSink, -}; +use jsonrpsee::{core::async_trait, PendingSubscriptionSink}; use sc_client_api::{ Backend, BlockBackend, BlockchainEvents, CallExecutor, ChildInfo, ExecutorProvider, StorageKey, StorageProvider, @@ -117,7 +116,7 @@ where + StorageProvider + 'static, { - fn archive_v1_body(&self, hash: Block::Hash) -> RpcResult>> { + fn archive_v1_body(&self, hash: Block::Hash) -> Result>, Infallible> { let Ok(Some(signed_block)) = self.client.block(hash) else { return Ok(None) }; let extrinsics = signed_block @@ -130,21 +129,21 @@ where Ok(Some(extrinsics)) } - fn archive_v1_genesis_hash(&self) -> RpcResult { + fn archive_v1_genesis_hash(&self) -> Result { Ok(self.genesis_hash.clone()) } - fn archive_v1_header(&self, hash: Block::Hash) -> RpcResult> { + fn archive_v1_header(&self, hash: Block::Hash) -> Result, Infallible> { let Ok(Some(header)) = self.client.header(hash) else { return Ok(None) }; Ok(Some(hex_string(&header.encode()))) } - fn archive_v1_finalized_height(&self) -> RpcResult { + fn archive_v1_finalized_height(&self) -> Result { Ok(self.client.info().finalized_number.saturated_into()) } - fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult> { + fn archive_v1_hash_by_height(&self, height: u64) -> Result, ArchiveError> { let height: NumberFor = U256::from(height) .try_into() .map_err(|_| ArchiveError::InvalidParam(format!("Invalid block height: {}", height)))?; @@ -200,7 +199,7 @@ where hash: Block::Hash, function: String, call_parameters: String, - ) -> RpcResult { + ) -> Result { let call_parameters = Bytes::from(parse_hex_param(call_parameters)?); let result = diff --git a/substrate/client/rpc-spec-v2/src/archive/error.rs b/substrate/client/rpc-spec-v2/src/archive/error.rs index d631c3fb8e890..93e1769dc19f3 100644 --- a/substrate/client/rpc-spec-v2/src/archive/error.rs +++ b/substrate/client/rpc-spec-v2/src/archive/error.rs @@ -55,3 +55,14 @@ impl From for ErrorObject<'static> { .into() } } + +/// The error type for errors that can never happen. +// +// NOTE: Can't use std::convert::Infallible because of the orphan-rule +pub enum Infallible {} + +impl From for ErrorObject<'static> { + fn from(e: Infallible) -> Self { + match e {} + } +}