Skip to content

Commit c6b1a8f

Browse files
niklasad1jsdwgithub-actions[bot]
authored andcommitted
rpc v2 archive: more verbose error types in API (#8109)
Builds on #8104 --------- Co-authored-by: James Wilson <[email protected]> Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3510e12 commit c6b1a8f

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

prdoc/pr_8109.prdoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: 'rpc v2 archive: more verbose error types in API'
2+
doc:
3+
- audience: Node Dev
4+
description: 'This PR changes the error types to more precise rather than arbitrary JSON-RPC error'
5+
crates:
6+
- name: sc-rpc-spec-v2
7+
bump: major

substrate/client/rpc-spec-v2/src/archive/api.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
//! API trait of the archive methods.
2020
2121
use crate::{
22+
archive::error::{Error, Infallible},
2223
common::events::{
2324
ArchiveStorageDiffEvent, ArchiveStorageDiffItem, ArchiveStorageEvent, StorageQuery,
2425
},
2526
MethodResult,
2627
};
27-
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
28+
use jsonrpsee::proc_macros::rpc;
2829

2930
#[rpc(client, server)]
3031
pub trait ArchiveApi<Hash> {
@@ -37,7 +38,7 @@ pub trait ArchiveApi<Hash> {
3738
///
3839
/// This method is unstable and subject to change in the future.
3940
#[method(name = "archive_v1_body")]
40-
fn archive_v1_body(&self, hash: Hash) -> RpcResult<Option<Vec<String>>>;
41+
fn archive_v1_body(&self, hash: Hash) -> Result<Option<Vec<String>>, Infallible>;
4142

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

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

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

7374
/// Get the hashes of blocks from the given height.
7475
///
@@ -79,7 +80,7 @@ pub trait ArchiveApi<Hash> {
7980
///
8081
/// This method is unstable and subject to change in the future.
8182
#[method(name = "archive_v1_hashByHeight")]
82-
fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult<Vec<String>>;
83+
fn archive_v1_hash_by_height(&self, height: u64) -> Result<Vec<String>, Error>;
8384

8485
/// Call into the Runtime API at a specified block's state.
8586
///
@@ -92,7 +93,7 @@ pub trait ArchiveApi<Hash> {
9293
hash: Hash,
9394
function: String,
9495
call_parameters: String,
95-
) -> RpcResult<MethodResult>;
96+
) -> Result<MethodResult, Error>;
9697

9798
/// Returns storage entries at a specific block's state.
9899
///

substrate/client/rpc-spec-v2/src/archive/archive.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
2121
use crate::{
2222
archive::{
23-
archive_storage::ArchiveStorageDiff, error::Error as ArchiveError, ArchiveApiServer,
23+
archive_storage::ArchiveStorageDiff,
24+
error::{Error as ArchiveError, Infallible},
25+
ArchiveApiServer,
2426
},
2527
common::{
2628
events::{
@@ -33,10 +35,7 @@ use crate::{
3335

3436
use codec::Encode;
3537
use futures::FutureExt;
36-
use jsonrpsee::{
37-
core::{async_trait, RpcResult},
38-
PendingSubscriptionSink,
39-
};
38+
use jsonrpsee::{core::async_trait, PendingSubscriptionSink};
4039
use sc_client_api::{
4140
Backend, BlockBackend, BlockchainEvents, CallExecutor, ChildInfo, ExecutorProvider, StorageKey,
4241
StorageProvider,
@@ -117,7 +116,7 @@ where
117116
+ StorageProvider<Block, BE>
118117
+ 'static,
119118
{
120-
fn archive_v1_body(&self, hash: Block::Hash) -> RpcResult<Option<Vec<String>>> {
119+
fn archive_v1_body(&self, hash: Block::Hash) -> Result<Option<Vec<String>>, Infallible> {
121120
let Ok(Some(signed_block)) = self.client.block(hash) else { return Ok(None) };
122121

123122
let extrinsics = signed_block
@@ -130,21 +129,21 @@ where
130129
Ok(Some(extrinsics))
131130
}
132131

133-
fn archive_v1_genesis_hash(&self) -> RpcResult<String> {
132+
fn archive_v1_genesis_hash(&self) -> Result<String, Infallible> {
134133
Ok(self.genesis_hash.clone())
135134
}
136135

137-
fn archive_v1_header(&self, hash: Block::Hash) -> RpcResult<Option<String>> {
136+
fn archive_v1_header(&self, hash: Block::Hash) -> Result<Option<String>, Infallible> {
138137
let Ok(Some(header)) = self.client.header(hash) else { return Ok(None) };
139138

140139
Ok(Some(hex_string(&header.encode())))
141140
}
142141

143-
fn archive_v1_finalized_height(&self) -> RpcResult<u64> {
142+
fn archive_v1_finalized_height(&self) -> Result<u64, Infallible> {
144143
Ok(self.client.info().finalized_number.saturated_into())
145144
}
146145

147-
fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult<Vec<String>> {
146+
fn archive_v1_hash_by_height(&self, height: u64) -> Result<Vec<String>, ArchiveError> {
148147
let height: NumberFor<Block> = U256::from(height)
149148
.try_into()
150149
.map_err(|_| ArchiveError::InvalidParam(format!("Invalid block height: {}", height)))?;
@@ -200,7 +199,7 @@ where
200199
hash: Block::Hash,
201200
function: String,
202201
call_parameters: String,
203-
) -> RpcResult<MethodResult> {
202+
) -> Result<MethodResult, ArchiveError> {
204203
let call_parameters = Bytes::from(parse_hex_param(call_parameters)?);
205204

206205
let result =

substrate/client/rpc-spec-v2/src/archive/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ impl From<Error> for ErrorObject<'static> {
5555
.into()
5656
}
5757
}
58+
59+
/// The error type for errors that can never happen.
60+
//
61+
// NOTE: Can't use std::convert::Infallible because of the orphan-rule
62+
pub enum Infallible {}
63+
64+
impl From<Infallible> for ErrorObject<'static> {
65+
fn from(e: Infallible) -> Self {
66+
match e {}
67+
}
68+
}

0 commit comments

Comments
 (0)