Skip to content

Commit c38ef4c

Browse files
authored
Introduce preliminary rpc primitives for unified RPC error (paritytech#315)
* Introduce rpc primitives for unified RPC error * Clean up * . * Trustee rpc error kind * Nits * . * Update chainx_rpc.json
1 parent 186c3f9 commit c38ef4c

File tree

17 files changed

+144
-170
lines changed

17 files changed

+144
-170
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ members = [
2626
"primitives/mining/common",
2727
"primitives/mining/staking",
2828
"primitives/protocol",
29+
"primitives/rpc",
2930
"primitives/runtime",
3031
"rpc",
3132
"runtime",

primitives/rpc/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "xp-rpc"
3+
version = "2.0.0-alpha.3"
4+
authors = ["The ChainX Authors"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
jsonrpc-core = "15.0.0"

primitives/rpc/src/lib.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0.
2+
3+
use std::fmt::Debug;
4+
5+
pub use jsonrpc_core::{Error, ErrorCode, Result};
6+
7+
/// The call to runtime failed.
8+
pub const RUNTIME_ERROR: i64 = 1;
9+
10+
/// The call related to trustee to runtime failed.
11+
const RUNTIME_TRUSTEE_ERROR: i64 = RUNTIME_ERROR + 100;
12+
13+
/// Decode the generic trustee info failed.
14+
///
15+
/// TODO: these pallet-specific errors should be moved to its own rpc module
16+
/// when there are many of them.
17+
pub const RUNTIME_TRUSTEE_DECODE_ERROR: i64 = RUNTIME_TRUSTEE_ERROR + 1;
18+
19+
/// The trustees are inexistent.
20+
pub const RUNTIME_TRUSTEE_INEXISTENT_ERROR: i64 = RUNTIME_TRUSTEE_ERROR + 2;
21+
22+
/// The transaction was not decodable.
23+
pub const DECODE_ERROR: i64 = 10000;
24+
25+
/// The bytes failed to be decoded as hex.
26+
pub const DECODE_HEX_ERROR: i64 = DECODE_ERROR + 1;
27+
28+
/// Converts a runtime trap into an RPC error.
29+
pub fn runtime_error_into_rpc_err(err: impl Debug) -> Error {
30+
Error {
31+
code: ErrorCode::ServerError(RUNTIME_ERROR),
32+
message: "Runtime trapped".into(),
33+
data: Some(format!("{:?}", err).into()),
34+
}
35+
}
36+
37+
/// Converts a trustee runtime trap into an RPC error.
38+
pub fn trustee_decode_error_into_rpc_err(err: impl Debug) -> Error {
39+
Error {
40+
code: ErrorCode::ServerError(RUNTIME_TRUSTEE_DECODE_ERROR),
41+
message: "Can not decode generic trustee session info".into(),
42+
data: Some(format!("{:?}", err).into()),
43+
}
44+
}
45+
46+
/// Converts a trustee runtime trap into an RPC error.
47+
pub fn trustee_inexistent_rpc_err() -> Error {
48+
Error {
49+
code: ErrorCode::ServerError(RUNTIME_TRUSTEE_INEXISTENT_ERROR),
50+
message: "Trustee does not exist".into(),
51+
data: None,
52+
}
53+
}
54+
55+
/// Converts a hex decode error into an RPC error.
56+
pub fn hex_decode_error_into_rpc_err(err: impl Debug) -> Error {
57+
Error {
58+
code: ErrorCode::ServerError(DECODE_HEX_ERROR),
59+
message: "Failed to decode hex".into(),
60+
data: Some(format!("{:?}", err).into()),
61+
}
62+
}

scripts/chainx-js/res/chainx_rpc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"isOptional": true
139139
}
140140
],
141-
"type": "()"
141+
"type": "bool"
142142
},
143143
"trusteeMultisigs": {
144144
"description": "Return the trustee multisig address for all chain.",

xpallets/assets/rpc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ sp-api = "2.0.0"
1818
sp-blockchain = "2.0.0"
1919
sp-runtime = "2.0.0"
2020

21+
# ChainX primitives
22+
xp-rpc = { path = "../../../primitives/rpc" }
23+
2124
# ChainX pallets
2225
xpallet-support = { path = "../../support" }
2326

xpallets/assets/rpc/src/lib.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
// Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0.
22

33
use std::collections::BTreeMap;
4-
use std::fmt::{Debug, Display};
4+
use std::fmt::Display;
55
use std::str::FromStr;
66
use std::sync::Arc;
77

88
use codec::Codec;
9-
use jsonrpc_core::{Error, ErrorCode, Result};
109
use jsonrpc_derive::rpc;
1110

11+
use sp_api::ProvideRuntimeApi;
1212
use sp_blockchain::HeaderBackend;
1313
use sp_runtime::{
1414
generic::BlockId,
1515
traits::{Block as BlockT, Zero},
1616
};
1717

18+
use xp_rpc::{runtime_error_into_rpc_err, Result};
19+
1820
use xpallet_support::RpcBalance;
1921

2022
use xpallet_assets_rpc_runtime_api::{
@@ -29,7 +31,7 @@ pub struct Assets<C, B> {
2931
impl<C, B> Assets<C, B> {
3032
/// Create new `Contracts` with the given reference to the client.
3133
pub fn new(client: Arc<C>) -> Self {
32-
Assets {
34+
Self {
3335
client,
3436
_marker: Default::default(),
3537
}
@@ -60,9 +62,7 @@ where
6062
impl<C, Block, AccountId, Balance> XAssetsApi<<Block as BlockT>::Hash, AccountId, Balance>
6163
for Assets<C, Block>
6264
where
63-
C: sp_api::ProvideRuntimeApi<Block>,
64-
C: HeaderBackend<Block>,
65-
C: Send + Sync + 'static,
65+
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
6666
C::Api: XAssetsRuntimeApi<Block, AccountId, Balance>,
6767
Block: BlockT,
6868
AccountId: Clone + Display + Codec,
@@ -129,13 +129,3 @@ where
129129
.map_err(runtime_error_into_rpc_err)
130130
}
131131
}
132-
133-
const RUNTIME_ERROR: i64 = 1;
134-
/// Converts a runtime trap into an RPC error.
135-
fn runtime_error_into_rpc_err(err: impl Debug) -> Error {
136-
Error {
137-
code: ErrorCode::ServerError(RUNTIME_ERROR),
138-
message: "Runtime trapped".into(),
139-
data: Some(format!("{:?}", err).into()),
140-
}
141-
}

xpallets/dex/spot/rpc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ sp-api = "2.0.0"
1919
sp-blockchain = "2.0.0"
2020
sp-runtime = "2.0.0"
2121

22+
# ChainX primitives
23+
xp-rpc = { path = "../../../../primitives/rpc" }
24+
2225
# ChainX pallets
2326
xpallet-support = { path = "../../../support" }
2427

xpallets/dex/spot/rpc/src/lib.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ use std::str::FromStr;
99
use std::sync::Arc;
1010

1111
use codec::Codec;
12-
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
1312
use jsonrpc_derive::rpc;
1413
use serde::{Deserialize, Serialize};
1514

1615
use sp_api::ProvideRuntimeApi;
1716
use sp_blockchain::HeaderBackend;
1817
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
1918

19+
use xp_rpc::{runtime_error_into_rpc_err, Result};
20+
2021
use xpallet_support::{RpcBalance, RpcPrice};
2122

2223
use xpallet_dex_spot_rpc_runtime_api::{
@@ -217,30 +218,3 @@ pub struct Page<T> {
217218
pub page_size: u32,
218219
pub data: T,
219220
}
220-
221-
/// Error type of this RPC api.
222-
pub enum Error {
223-
/// The transaction was not decodable.
224-
DecodeError,
225-
/// The call to runtime failed.
226-
RuntimeError,
227-
}
228-
229-
impl From<Error> for i64 {
230-
fn from(e: Error) -> i64 {
231-
match e {
232-
Error::RuntimeError => 1,
233-
Error::DecodeError => 2,
234-
}
235-
}
236-
}
237-
238-
const RUNTIME_ERROR: i64 = 1;
239-
/// Converts a runtime trap into an RPC error.
240-
fn runtime_error_into_rpc_err(err: impl Debug) -> RpcError {
241-
RpcError {
242-
code: ErrorCode::ServerError(RUNTIME_ERROR),
243-
message: "Runtime trapped".into(),
244-
data: Some(format!("{:?}", err).into()),
245-
}
246-
}

xpallets/gateway/common/rpc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ sp-api = "2.0.0"
1919
sp-blockchain = "2.0.0"
2020
sp-runtime = "2.0.0"
2121

22+
# ChainX primitives
23+
xp-rpc = { path = "../../../../primitives/rpc" }
24+
2225
# ChainX pallets
2326
xpallet-support = { path = "../../../support" }
2427

0 commit comments

Comments
 (0)