Skip to content

Commit e7d8fd6

Browse files
committed
rpc v2 archive: more verbose error types in API
1 parent ba437dc commit e7d8fd6

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use crate::{
2323
ArchiveStorageDiffEvent, ArchiveStorageDiffItem, ArchiveStorageEvent, StorageQuery,
2424
},
2525
MethodResult,
26+
archive::error::{Error, Infallible},
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use crate::{
2222
archive::{
23-
archive_storage::ArchiveStorageDiff, error::Error as ArchiveError, ArchiveApiServer,
23+
archive_storage::ArchiveStorageDiff, error::{Error as ArchiveError, Infallible}, ArchiveApiServer,
2424
},
2525
common::{
2626
events::{
@@ -34,7 +34,7 @@ use crate::{
3434
use codec::Encode;
3535
use futures::FutureExt;
3636
use jsonrpsee::{
37-
core::{async_trait, RpcResult},
37+
core::async_trait,
3838
PendingSubscriptionSink,
3939
};
4040
use sc_client_api::{
@@ -117,7 +117,7 @@ where
117117
+ StorageProvider<Block, BE>
118118
+ 'static,
119119
{
120-
fn archive_v1_body(&self, hash: Block::Hash) -> RpcResult<Option<Vec<String>>> {
120+
fn archive_v1_body(&self, hash: Block::Hash) -> Result<Option<Vec<String>>, Infallible> {
121121
let Ok(Some(signed_block)) = self.client.block(hash) else { return Ok(None) };
122122

123123
let extrinsics = signed_block
@@ -130,21 +130,21 @@ where
130130
Ok(Some(extrinsics))
131131
}
132132

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

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

140140
Ok(Some(hex_string(&header.encode())))
141141
}
142142

143-
fn archive_v1_finalized_height(&self) -> RpcResult<u64> {
143+
fn archive_v1_finalized_height(&self) -> Result<u64, Infallible> {
144144
Ok(self.client.info().finalized_number.saturated_into())
145145
}
146146

147-
fn archive_v1_hash_by_height(&self, height: u64) -> RpcResult<Vec<String>> {
147+
fn archive_v1_hash_by_height(&self, height: u64) -> Result<Vec<String>, ArchiveError> {
148148
let height: NumberFor<Block> = U256::from(height)
149149
.try_into()
150150
.map_err(|_| ArchiveError::InvalidParam(format!("Invalid block height: {}", height)))?;
@@ -200,7 +200,7 @@ where
200200
hash: Block::Hash,
201201
function: String,
202202
call_parameters: String,
203-
) -> RpcResult<MethodResult> {
203+
) -> Result<MethodResult, ArchiveError> {
204204
let call_parameters = Bytes::from(parse_hex_param(call_parameters)?);
205205

206206
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)