Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions prdoc/pr_8109.prdoc
Original file line number Diff line number Diff line change
@@ -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
15 changes: 8 additions & 7 deletions substrate/client/rpc-spec-v2/src/archive/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash> {
Expand All @@ -37,7 +38,7 @@ pub trait ArchiveApi<Hash> {
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_v1_body")]
fn archive_v1_body(&self, hash: Hash) -> RpcResult<Option<Vec<String>>>;
fn archive_v1_body(&self, hash: Hash) -> Result<Option<Vec<String>>, Infallible>;

/// Get the chain's genesis hash.
///
Expand All @@ -47,7 +48,7 @@ pub trait ArchiveApi<Hash> {
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_v1_genesisHash")]
fn archive_v1_genesis_hash(&self) -> RpcResult<String>;
fn archive_v1_genesis_hash(&self) -> Result<String, Infallible>;

/// Get the block's header.
///
Expand All @@ -58,7 +59,7 @@ pub trait ArchiveApi<Hash> {
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_v1_header")]
fn archive_v1_header(&self, hash: Hash) -> RpcResult<Option<String>>;
fn archive_v1_header(&self, hash: Hash) -> Result<Option<String>, Infallible>;

/// Get the height of the current finalized block.
///
Expand All @@ -68,7 +69,7 @@ pub trait ArchiveApi<Hash> {
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_v1_finalizedHeight")]
fn archive_v1_finalized_height(&self) -> RpcResult<u64>;
fn archive_v1_finalized_height(&self) -> Result<u64, Infallible>;

/// Get the hashes of blocks from the given height.
///
Expand All @@ -79,7 +80,7 @@ pub trait ArchiveApi<Hash> {
///
/// 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<Vec<String>>;
fn archive_v1_hash_by_height(&self, height: u64) -> Result<Vec<String>, Error>;

/// Call into the Runtime API at a specified block's state.
///
Expand All @@ -92,7 +93,7 @@ pub trait ArchiveApi<Hash> {
hash: Hash,
function: String,
call_parameters: String,
) -> RpcResult<MethodResult>;
) -> Result<MethodResult, Error>;

/// Returns storage entries at a specific block's state.
///
Expand Down
21 changes: 10 additions & 11 deletions substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
Expand Down Expand Up @@ -117,7 +116,7 @@ where
+ StorageProvider<Block, BE>
+ 'static,
{
fn archive_v1_body(&self, hash: Block::Hash) -> RpcResult<Option<Vec<String>>> {
fn archive_v1_body(&self, hash: Block::Hash) -> Result<Option<Vec<String>>, Infallible> {
let Ok(Some(signed_block)) = self.client.block(hash) else { return Ok(None) };

let extrinsics = signed_block
Expand All @@ -130,21 +129,21 @@ where
Ok(Some(extrinsics))
}

fn archive_v1_genesis_hash(&self) -> RpcResult<String> {
fn archive_v1_genesis_hash(&self) -> Result<String, Infallible> {
Ok(self.genesis_hash.clone())
}

fn archive_v1_header(&self, hash: Block::Hash) -> RpcResult<Option<String>> {
fn archive_v1_header(&self, hash: Block::Hash) -> Result<Option<String>, 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<u64> {
fn archive_v1_finalized_height(&self) -> Result<u64, Infallible> {
Ok(self.client.info().finalized_number.saturated_into())
}

fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult<Vec<String>> {
fn archive_v1_hash_by_height(&self, height: u64) -> Result<Vec<String>, ArchiveError> {
let height: NumberFor<Block> = U256::from(height)
.try_into()
.map_err(|_| ArchiveError::InvalidParam(format!("Invalid block height: {}", height)))?;
Expand Down Expand Up @@ -200,7 +199,7 @@ where
hash: Block::Hash,
function: String,
call_parameters: String,
) -> RpcResult<MethodResult> {
) -> Result<MethodResult, ArchiveError> {
let call_parameters = Bytes::from(parse_hex_param(call_parameters)?);

let result =
Expand Down
11 changes: 11 additions & 0 deletions substrate/client/rpc-spec-v2/src/archive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ impl From<Error> 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<Infallible> for ErrorObject<'static> {
fn from(e: Infallible) -> Self {
match e {}
}
}
Loading