From e8db23f48bb6a0663f87b2e6fc2971f22dbe2c1d Mon Sep 17 00:00:00 2001 From: Qinxuan Chen Date: Wed, 4 Nov 2020 15:02:33 +0800 Subject: [PATCH] Extract serde part of xpallet-support into xp-rpc (#325) Signed-off-by: koushiro --- Cargo.lock | 18 +-- primitives/genesis-builder/Cargo.toml | 7 +- primitives/genesis-builder/src/lib.rs | 10 +- primitives/rpc/Cargo.toml | 5 + primitives/rpc/src/lib.rs | 151 +++++++++++++++++- xpallets/assets-registrar/Cargo.toml | 2 + xpallets/assets-registrar/src/types.rs | 6 +- xpallets/assets/rpc/Cargo.toml | 3 - xpallets/assets/rpc/src/lib.rs | 4 +- xpallets/dex/spot/rpc/Cargo.toml | 3 - xpallets/dex/spot/rpc/src/lib.rs | 4 +- xpallets/gateway/common/Cargo.toml | 2 + xpallets/gateway/common/rpc/Cargo.toml | 3 - xpallets/gateway/common/rpc/src/lib.rs | 4 +- .../gateway/common/src/trustees/bitcoin.rs | 6 +- xpallets/gateway/common/src/types.rs | 2 +- xpallets/gateway/records/rpc/src/lib.rs | 2 +- xpallets/mining/asset/rpc/Cargo.toml | 3 - xpallets/mining/asset/rpc/src/lib.rs | 4 +- xpallets/mining/staking/Cargo.toml | 2 + xpallets/mining/staking/rpc/Cargo.toml | 3 - xpallets/mining/staking/rpc/src/lib.rs | 4 +- xpallets/mining/staking/src/types.rs | 2 +- xpallets/support/Cargo.toml | 7 - xpallets/support/src/lib.rs | 7 - xpallets/support/src/serde.rs | 149 ----------------- xpallets/transaction-fee/rpc/Cargo.toml | 3 +- xpallets/transaction-fee/rpc/src/lib.rs | 2 +- 28 files changed, 192 insertions(+), 226 deletions(-) delete mode 100644 xpallets/support/src/serde.rs diff --git a/Cargo.lock b/Cargo.lock index f74e830b68d3c..088d9dd3ab5bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8797,7 +8797,7 @@ version = "2.0.0-alpha.4" dependencies = [ "chainx-primitives", "serde", - "xpallet-support", + "xp-rpc", ] [[package]] @@ -8849,7 +8849,10 @@ dependencies = [ name = "xp-rpc" version = "2.0.0-alpha.4" dependencies = [ + "hex", "jsonrpc-core", + "serde", + "serde_json", ] [[package]] @@ -8905,6 +8908,7 @@ dependencies = [ "xp-assets-registrar", "xp-logging", "xp-protocol", + "xp-rpc", "xp-runtime", "xpallet-support", ] @@ -8922,7 +8926,6 @@ dependencies = [ "sp-runtime", "xp-rpc", "xpallet-assets-rpc-runtime-api", - "xpallet-support", ] [[package]] @@ -8974,7 +8977,6 @@ dependencies = [ "sp-runtime", "xp-rpc", "xpallet-dex-spot-rpc-runtime-api", - "xpallet-support", ] [[package]] @@ -9041,6 +9043,7 @@ dependencies = [ "xp-io", "xp-logging", "xp-protocol", + "xp-rpc", "xp-runtime", "xpallet-assets", "xpallet-assets-registrar", @@ -9063,7 +9066,6 @@ dependencies = [ "sp-runtime", "xp-rpc", "xpallet-gateway-common-rpc-runtime-api", - "xpallet-support", ] [[package]] @@ -9196,7 +9198,6 @@ dependencies = [ "sp-runtime", "xp-rpc", "xpallet-mining-asset-rpc-runtime-api", - "xpallet-support", ] [[package]] @@ -9235,6 +9236,7 @@ dependencies = [ "xp-mining-common", "xp-mining-staking", "xp-protocol", + "xp-rpc", "xp-runtime", "xpallet-support", ] @@ -9252,7 +9254,6 @@ dependencies = [ "sp-runtime", "xp-rpc", "xpallet-mining-staking-rpc-runtime-api", - "xpallet-support", ] [[package]] @@ -9270,9 +9271,6 @@ name = "xpallet-support" version = "2.0.0-alpha.4" dependencies = [ "hex", - "parity-scale-codec", - "serde", - "serde_json", "sp-std", ] @@ -9316,7 +9314,7 @@ dependencies = [ "sp-blockchain", "sp-core", "sp-runtime", - "xpallet-support", + "xp-rpc", "xpallet-transaction-fee-rpc-runtime-api", ] diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index 8d75dbc6e568e..e86e73b382c9e 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -9,9 +9,7 @@ serde = { version = "1.0", features = ["derive"], optional = true } # ChainX primitives chainx-primitives = { path = "../../primitives", default-features = false } - -# ChainX pallets -xpallet-support = { path = "../../xpallets/support", default-features = false } +xp-rpc = { path = "../../primitives/rpc", optional = true } [features] default = ["std"] @@ -19,6 +17,5 @@ std = [ "serde", # ChainX primitives "chainx-primitives/std", - # ChainX pallets - "xpallet-support/std", + "xp-rpc", ] diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index ab2863687af51..683eb457a0c3e 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -42,11 +42,11 @@ mod genesis_params { #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct ValidatorInfo { pub who: AccountId, - #[serde(with = "xpallet_support::serde_text")] + #[serde(with = "xp_rpc::serde_text")] pub referral_id: Vec, pub self_bonded: Balance, pub total_nomination: Balance, - #[serde(with = "xpallet_support::serde_num_str")] + #[serde(with = "xp_rpc::serde_num_str")] pub total_weight: u128, } @@ -54,7 +54,7 @@ mod genesis_params { pub struct Nomination { pub nominee: AccountId, pub nomination: Balance, - #[serde(with = "xpallet_support::serde_num_str")] + #[serde(with = "xp_rpc::serde_num_str")] pub weight: u128, } @@ -73,14 +73,14 @@ mod genesis_params { #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct XBtcInfo { pub balance: Balance, - #[serde(with = "xpallet_support::serde_num_str")] + #[serde(with = "xp_rpc::serde_num_str")] pub weight: u128, } #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct XBtcMiner { pub who: AccountId, - #[serde(with = "xpallet_support::serde_num_str")] + #[serde(with = "xp_rpc::serde_num_str")] pub weight: u128, } diff --git a/primitives/rpc/Cargo.toml b/primitives/rpc/Cargo.toml index 6efb7815c6f97..ad15e08e8a695 100644 --- a/primitives/rpc/Cargo.toml +++ b/primitives/rpc/Cargo.toml @@ -5,4 +5,9 @@ authors = ["The ChainX Authors"] edition = "2018" [dependencies] +hex = "0.4" jsonrpc-core = "15.0.0" +serde = { version = "1.0", features = ["derive"] } + +[dev-dependencies] +serde_json = "1.0" diff --git a/primitives/rpc/src/lib.rs b/primitives/rpc/src/lib.rs index 70827cda1faf7..f42522e12bcab 100644 --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -1,8 +1,13 @@ // Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0. -use std::fmt::Debug; +use std::{ + fmt::{Debug, Display}, + result::Result as StdResult, + str::FromStr, +}; pub use jsonrpc_core::{Error, ErrorCode, Result}; +use serde::{de, ser, Deserialize, Serialize}; /// The call to runtime failed. pub const RUNTIME_ERROR: i64 = 1; @@ -60,3 +65,147 @@ pub fn hex_decode_error_into_rpc_err(err: impl Debug) -> Error { data: Some(format!("{:?}", err).into()), } } + +/// Balance type when interacting with RPC. +pub type RpcBalance = RpcU128; + +/// Price type of order when interacting with RPC. +pub type RpcPrice = RpcU128; + +/// Weight type of mining when interacting with RPC. +pub type RpcMiningWeight = RpcU128; + +/// Weight type of staking when interacting with RPC. +pub type RpcVoteWeight = RpcU128; + +/// A helper struct for handling u128 serialization/deserialization of RPC. +/// See https://github.com/polkadot-js/api/issues/2464 for details (shit!). +#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)] +pub struct RpcU128(#[serde(with = "self::serde_num_str")] T); + +impl From for RpcU128 { + fn from(value: T) -> Self { + RpcU128(value) + } +} + +/// Number string serialization/deserialization +pub mod serde_num_str { + use super::*; + + /// A serializer that encodes the number as a string + pub fn serialize(value: &T, serializer: S) -> StdResult + where + S: ser::Serializer, + T: Display, + { + serializer.serialize_str(&value.to_string()) + } + + /// A deserializer that decodes a string to the number. + pub fn deserialize<'de, D, T>(deserializer: D) -> StdResult + where + D: de::Deserializer<'de>, + T: FromStr, + { + let data = String::deserialize(deserializer)?; + data.parse::() + .map_err(|_| de::Error::custom("Parse from string failed")) + } +} + +/// Hex serialization/deserialization +pub mod serde_hex { + use super::*; + + /// A serializer that encodes the bytes as a hex-string + pub fn serialize(value: &T, serializer: S) -> StdResult + where + S: ser::Serializer, + T: AsRef<[u8]>, + { + serializer.serialize_str(&format!("0x{}", hex::encode(value))) + } + + /// A deserializer that decodes the hex-string to bytes (Vec) + pub fn deserialize<'de, D>(deserializer: D) -> StdResult, D::Error> + where + D: de::Deserializer<'de>, + { + let data = String::deserialize(deserializer)?; + let data = if data.starts_with("0x") { + &data[2..] + } else { + &data[..] + }; + let hex = hex::decode(data).map_err(de::Error::custom)?; + Ok(hex) + } +} + +/// Text serialization/deserialization +pub mod serde_text { + use super::*; + + /// A serializer that encodes the bytes as a string + pub fn serialize(value: &T, serializer: S) -> StdResult + where + S: ser::Serializer, + T: AsRef<[u8]>, + { + let output = String::from_utf8_lossy(value.as_ref()); + serializer.serialize_str(&output) + } + + /// A deserializer that decodes the string to the bytes (Vec) + pub fn deserialize<'de, D>(deserializer: D) -> StdResult, D::Error> + where + D: de::Deserializer<'de>, + { + let data = String::deserialize(deserializer)?; + Ok(data.into_bytes()) + } +} + +#[cfg(test)] +mod tests { + use serde::{Deserialize, Serialize}; + + #[test] + fn test_serde_num_str_attr() { + use super::RpcU128; + + let test = RpcU128(u128::max_value()); + let ser = serde_json::to_string(&test).unwrap(); + assert_eq!(ser, "\"340282366920938463463374607431768211455\""); + let de = serde_json::from_str::>(&ser).unwrap(); + assert_eq!(de, test); + } + + #[test] + fn test_serde_hex_attr() { + #[derive(PartialEq, Debug, Serialize, Deserialize)] + struct HexTest(#[serde(with = "super::serde_hex")] Vec); + + let test = HexTest(b"0123456789".to_vec()); + let ser = serde_json::to_string(&test).unwrap(); + assert_eq!(ser, "\"0x30313233343536373839\""); + let de = serde_json::from_str::(&ser).unwrap(); + assert_eq!(de, test); + // without 0x + let de = serde_json::from_str::("\"30313233343536373839\"").unwrap(); + assert_eq!(de, test); + } + + #[test] + fn test_serde_text_attr() { + #[derive(PartialEq, Debug, Serialize, Deserialize)] + struct TextTest(#[serde(with = "super::serde_text")] Vec); + + let test = TextTest(b"0123456789".to_vec()); + let ser = serde_json::to_string(&test).unwrap(); + assert_eq!(ser, "\"0123456789\""); + let de = serde_json::from_str::(&ser).unwrap(); + assert_eq!(de, test); + } +} diff --git a/xpallets/assets-registrar/Cargo.toml b/xpallets/assets-registrar/Cargo.toml index 93ce0a6a0ad36..f8cc61eaad279 100644 --- a/xpallets/assets-registrar/Cargo.toml +++ b/xpallets/assets-registrar/Cargo.toml @@ -22,6 +22,7 @@ chainx-primitives = { path = "../../primitives", default-features = false } xp-assets-registrar = { path = "../../primitives/assets-registrar", default-features = false } xp-logging = { path = "../../primitives/logging", default-features = false } xp-protocol = { path = "../../primitives/protocol", default-features = false } +xp-rpc = { path = "../../primitives/rpc", optional = true } xp-runtime = { path = "../../primitives/runtime", default-features = false } # ChainX pallets @@ -48,6 +49,7 @@ std = [ "xp-assets-registrar/std", "xp-logging/std", "xp-protocol/std", + "xp-rpc", "xp-runtime/std", # ChainX pallets "xpallet-support/std", diff --git a/xpallets/assets-registrar/src/types.rs b/xpallets/assets-registrar/src/types.rs index c3d5a2ab07cb9..a214127b7a9e9 100644 --- a/xpallets/assets-registrar/src/types.rs +++ b/xpallets/assets-registrar/src/types.rs @@ -48,13 +48,13 @@ impl Default for Chain { #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] pub struct AssetInfo { - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] token: Token, - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] token_name: Token, chain: Chain, decimals: Decimals, - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] desc: Desc, } diff --git a/xpallets/assets/rpc/Cargo.toml b/xpallets/assets/rpc/Cargo.toml index 1a1ff44771237..6223e703a42ae 100644 --- a/xpallets/assets/rpc/Cargo.toml +++ b/xpallets/assets/rpc/Cargo.toml @@ -21,8 +21,5 @@ sp-runtime = "2.0.0" # ChainX primitives xp-rpc = { path = "../../../primitives/rpc" } -# ChainX pallets -xpallet-support = { path = "../../support" } - # ChainX pallets api xpallet-assets-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/assets/rpc/src/lib.rs b/xpallets/assets/rpc/src/lib.rs index abd0bd5bc25d8..be1adddc6bdce 100644 --- a/xpallets/assets/rpc/src/lib.rs +++ b/xpallets/assets/rpc/src/lib.rs @@ -15,9 +15,7 @@ use sp_runtime::{ traits::{Block as BlockT, Zero}, }; -use xp_rpc::{runtime_error_into_rpc_err, Result}; - -use xpallet_support::RpcBalance; +use xp_rpc::{runtime_error_into_rpc_err, Result, RpcBalance}; use xpallet_assets_rpc_runtime_api::{ AssetId, AssetType, TotalAssetInfo, XAssetsApi as XAssetsRuntimeApi, diff --git a/xpallets/dex/spot/rpc/Cargo.toml b/xpallets/dex/spot/rpc/Cargo.toml index 742933018c860..5ac5349b215e2 100644 --- a/xpallets/dex/spot/rpc/Cargo.toml +++ b/xpallets/dex/spot/rpc/Cargo.toml @@ -22,8 +22,5 @@ sp-runtime = "2.0.0" # ChainX primitives xp-rpc = { path = "../../../../primitives/rpc" } -# ChainX pallets -xpallet-support = { path = "../../../support" } - # ChainX pallets api xpallet-dex-spot-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/dex/spot/rpc/src/lib.rs b/xpallets/dex/spot/rpc/src/lib.rs index 3374e714bae85..3ff79c5f1ecd0 100644 --- a/xpallets/dex/spot/rpc/src/lib.rs +++ b/xpallets/dex/spot/rpc/src/lib.rs @@ -16,9 +16,7 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; -use xp_rpc::{runtime_error_into_rpc_err, Result}; - -use xpallet_support::{RpcBalance, RpcPrice}; +use xp_rpc::{runtime_error_into_rpc_err, Result, RpcBalance, RpcPrice}; use xpallet_dex_spot_rpc_runtime_api::{ Depth, FullPairInfo, Handicap, OrderProperty, RpcOrder, TradingPairId, TradingPairInfo, diff --git a/xpallets/gateway/common/Cargo.toml b/xpallets/gateway/common/Cargo.toml index cf76469a235b3..da3edd4b83ed6 100644 --- a/xpallets/gateway/common/Cargo.toml +++ b/xpallets/gateway/common/Cargo.toml @@ -25,6 +25,7 @@ chainx-primitives = { path = "../../../primitives", default-features = false } xp-logging = { path = "../../../primitives/logging", default-features = false } xp-io = { path = "../../../primitives/io", default-features = false, optional = true } xp-protocol = { path = "../../../primitives/protocol", default-features = false } +xp-rpc = { path = "../../../primitives/rpc", optional = true } xp-runtime = { path = "../../../primitives/runtime", default-features = false } # ChainX pallets @@ -62,6 +63,7 @@ std = [ "xp-logging/std", "xp-io/std", "xp-protocol/std", + "xp-rpc", "xp-runtime/std", # ChainX pallets "xpallet-assets/std", diff --git a/xpallets/gateway/common/rpc/Cargo.toml b/xpallets/gateway/common/rpc/Cargo.toml index 0b5dfd7e6c160..3ab1a8c7b577f 100644 --- a/xpallets/gateway/common/rpc/Cargo.toml +++ b/xpallets/gateway/common/rpc/Cargo.toml @@ -22,8 +22,5 @@ sp-runtime = "2.0.0" # ChainX primitives xp-rpc = { path = "../../../../primitives/rpc" } -# ChainX pallets -xpallet-support = { path = "../../../support" } - # ChainX pallets api xpallet-gateway-common-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/gateway/common/rpc/src/lib.rs b/xpallets/gateway/common/rpc/src/lib.rs index e7ae485ff25e9..56bee89c843c3 100644 --- a/xpallets/gateway/common/rpc/src/lib.rs +++ b/xpallets/gateway/common/rpc/src/lib.rs @@ -17,11 +17,9 @@ use sp_runtime::{generic::BlockId, traits::Block as BlockT}; use xp_rpc::{ hex_decode_error_into_rpc_err, runtime_error_into_rpc_err, trustee_decode_error_into_rpc_err, - trustee_inexistent_rpc_err, Result, + trustee_inexistent_rpc_err, Result, RpcBalance, }; -use xpallet_support::RpcBalance; - use xpallet_gateway_common_rpc_runtime_api::trustees::bitcoin::{ BtcTrusteeIntentionProps, BtcTrusteeSessionInfo, }; diff --git a/xpallets/gateway/common/src/trustees/bitcoin.rs b/xpallets/gateway/common/src/trustees/bitcoin.rs index 4808c239cfee8..954e0dd854d01 100644 --- a/xpallets/gateway/common/src/trustees/bitcoin.rs +++ b/xpallets/gateway/common/src/trustees/bitcoin.rs @@ -8,9 +8,9 @@ use sp_runtime::RuntimeDebug; #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] pub struct BtcTrusteeAddrInfo { - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] pub addr: BtcAddress, - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_hex"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_hex"))] pub redeem_script: Vec, } @@ -74,7 +74,7 @@ impl Into> for BtcTrusteeType { mod serde_impl { use super::*; use serde::{de::Error, Deserializer, Serializer}; - use xpallet_support::serde_hex; + use xp_rpc::serde_hex; impl Serialize for BtcTrusteeType { fn serialize(&self, serializer: S) -> Result diff --git a/xpallets/gateway/common/src/types.rs b/xpallets/gateway/common/src/types.rs index f3605c3627555..17e84f3acf8de 100644 --- a/xpallets/gateway/common/src/types.rs +++ b/xpallets/gateway/common/src/types.rs @@ -25,7 +25,7 @@ pub struct TrusteeInfoConfig { #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] pub struct TrusteeIntentionProps { - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] pub about: Text, pub hot_entity: TrusteeEntity, pub cold_entity: TrusteeEntity, diff --git a/xpallets/gateway/records/rpc/src/lib.rs b/xpallets/gateway/records/rpc/src/lib.rs index cbc37bfc08a2d..9649d62d4fd47 100644 --- a/xpallets/gateway/records/rpc/src/lib.rs +++ b/xpallets/gateway/records/rpc/src/lib.rs @@ -135,7 +135,7 @@ where pub struct RpcWithdrawalRecord { pub asset_id: AssetId, pub applicant: AccountId, - #[serde(with = "xpallet_support::serde_num_str")] + #[serde(with = "xp_rpc::serde_num_str")] pub balance: Balance, pub addr: String, pub ext: String, diff --git a/xpallets/mining/asset/rpc/Cargo.toml b/xpallets/mining/asset/rpc/Cargo.toml index 5849f241ee453..b52a35734966a 100644 --- a/xpallets/mining/asset/rpc/Cargo.toml +++ b/xpallets/mining/asset/rpc/Cargo.toml @@ -21,8 +21,5 @@ sp-runtime = "2.0.0" # ChainX primitives xp-rpc = { path = "../../../../primitives/rpc" } -# ChainX pallets -xpallet-support = { path = "../../../support" } - # ChainX pallets api xpallet-mining-asset-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/mining/asset/rpc/src/lib.rs b/xpallets/mining/asset/rpc/src/lib.rs index 7a695872d226f..93e298adbdfb8 100644 --- a/xpallets/mining/asset/rpc/src/lib.rs +++ b/xpallets/mining/asset/rpc/src/lib.rs @@ -14,9 +14,7 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; -use xp_rpc::{runtime_error_into_rpc_err, Result}; - -use xpallet_support::{RpcBalance, RpcMiningWeight}; +use xp_rpc::{runtime_error_into_rpc_err, Result, RpcBalance, RpcMiningWeight}; use xpallet_mining_asset_rpc_runtime_api::{ AssetId, AssetLedger, MinerLedger, MiningAssetInfo, XMiningAssetApi as XMiningAssetRuntimeApi, diff --git a/xpallets/mining/staking/Cargo.toml b/xpallets/mining/staking/Cargo.toml index 2a654f4d8ef8b..088af980bf21f 100644 --- a/xpallets/mining/staking/Cargo.toml +++ b/xpallets/mining/staking/Cargo.toml @@ -28,6 +28,7 @@ xp-genesis-builder = { path = "../../../primitives/genesis-builder", optional = xp-logging = { path = "../../../primitives/logging", default-features = false } xp-mining-common = { path = "../../../primitives/mining/common", default-features = false } xp-mining-staking = { path = "../../../primitives/mining/staking", default-features = false } +xp-rpc = { path = "../../../primitives/rpc", optional = true } xp-runtime = { path = "../../../primitives/runtime", default-features = false } # ChainX pallets @@ -63,6 +64,7 @@ std = [ "xp-logging/std", "xp-mining-common/std", "xp-mining-staking/std", + "xp-rpc", "xp-runtime/std", # ChainX pallets "xpallet-support/std", diff --git a/xpallets/mining/staking/rpc/Cargo.toml b/xpallets/mining/staking/rpc/Cargo.toml index cee62ef31dc99..3ca8068438111 100644 --- a/xpallets/mining/staking/rpc/Cargo.toml +++ b/xpallets/mining/staking/rpc/Cargo.toml @@ -21,8 +21,5 @@ sp-runtime = "2.0.0" # ChainX primitives xp-rpc = { path = "../../../../primitives/rpc" } -# ChainX pallets -xpallet-support = { path = "../../../support" } - # ChainX pallets api xpallet-mining-staking-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/mining/staking/rpc/src/lib.rs b/xpallets/mining/staking/rpc/src/lib.rs index a68d3b005a841..2df4ea0e33299 100644 --- a/xpallets/mining/staking/rpc/src/lib.rs +++ b/xpallets/mining/staking/rpc/src/lib.rs @@ -14,9 +14,7 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_runtime::{generic::BlockId, traits::Block as BlockT}; -use xp_rpc::{runtime_error_into_rpc_err, Result}; - -use xpallet_support::{RpcBalance, RpcVoteWeight}; +use xp_rpc::{runtime_error_into_rpc_err, Result, RpcBalance, RpcVoteWeight}; use xpallet_mining_staking_rpc_runtime_api::{ NominatorInfo, NominatorLedger, Unbonded, ValidatorInfo, ValidatorLedger, diff --git a/xpallets/mining/staking/src/types.rs b/xpallets/mining/staking/src/types.rs index 5377d9045b0c5..09f8e4f0ea872 100644 --- a/xpallets/mining/staking/src/types.rs +++ b/xpallets/mining/staking/src/types.rs @@ -106,7 +106,7 @@ pub struct ValidatorProfile { /// Block number of last performed `chill` operation. pub last_chilled: Option, /// Referral identity that belongs to the validator. - #[cfg_attr(feature = "std", serde(with = "xpallet_support::serde_text"))] + #[cfg_attr(feature = "std", serde(with = "xp_rpc::serde_text"))] pub referral_id: ReferralId, } diff --git a/xpallets/support/Cargo.toml b/xpallets/support/Cargo.toml index 0e469a61b839a..e63c7160f0948 100644 --- a/xpallets/support/Cargo.toml +++ b/xpallets/support/Cargo.toml @@ -5,22 +5,15 @@ authors = ["The ChainX Authors"] edition = "2018" [dependencies] -codec = { package = "parity-scale-codec", version = "1.3.4", features = ["derive"], default-features = false } hex = { version = "0.4", default-features = false } -serde = { version = "1.0", features = ["derive"], optional = true } # Substrate primitives sp-std = { version = "2.0.0", default-features = false } -[dev-dependencies] -serde_json = "1.0" - [features] default = ["std"] std = [ - "codec/std", "hex/std", - "serde", # Substrate primitives "sp-std/std", ] diff --git a/xpallets/support/src/lib.rs b/xpallets/support/src/lib.rs index 97e27d8bbe44b..28a926a436a47 100644 --- a/xpallets/support/src/lib.rs +++ b/xpallets/support/src/lib.rs @@ -4,15 +4,8 @@ #[macro_use] mod macros; -#[cfg(feature = "std")] -mod serde; pub mod traits; #[cfg(feature = "std")] pub mod x_std; pub use self::macros::*; -#[cfg(feature = "std")] -pub use self::serde::{ - serde_hex, serde_num_str, serde_text, RpcBalance, RpcMiningWeight, RpcPrice, RpcU128, - RpcVoteWeight, -}; diff --git a/xpallets/support/src/serde.rs b/xpallets/support/src/serde.rs deleted file mode 100644 index 157db8e7cf852..0000000000000 --- a/xpallets/support/src/serde.rs +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0. - -use std::{fmt::Display, str::FromStr}; - -use serde::{de, ser, Deserialize, Serialize}; - -/// Hex serialization/deserialization -pub mod serde_hex { - use super::*; - - /// A serializer that encodes the bytes as a hex-string - pub fn serialize(value: &T, serializer: S) -> Result - where - S: ser::Serializer, - T: AsRef<[u8]>, - { - serializer.serialize_str(&format!("0x{}", hex::encode(value))) - } - - /// A deserializer that decodes the hex-string to bytes (Vec) - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: de::Deserializer<'de>, - { - let data = String::deserialize(deserializer)?; - let data = if data.starts_with("0x") { - &data[2..] - } else { - &data[..] - }; - let hex = hex::decode(data).map_err(de::Error::custom)?; - Ok(hex) - } -} - -/// Text serialization/deserialization -pub mod serde_text { - use super::*; - - /// A serializer that encodes the bytes as a string - pub fn serialize(value: &T, serializer: S) -> Result - where - S: ser::Serializer, - T: AsRef<[u8]>, - { - let output = String::from_utf8_lossy(value.as_ref()); - serializer.serialize_str(&output) - } - - /// A deserializer that decodes the string to the bytes (Vec) - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: de::Deserializer<'de>, - { - let data = String::deserialize(deserializer)?; - Ok(data.into_bytes()) - } -} - -/// Number string serialization/deserialization -pub mod serde_num_str { - use super::*; - - /// A serializer that encodes the number as a string - pub fn serialize(value: &T, serializer: S) -> Result - where - S: ser::Serializer, - T: Display, - { - serializer.serialize_str(&value.to_string()) - } - - /// A deserializer that decodes a string to the number. - pub fn deserialize<'de, D, T>(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - T: FromStr, - { - let data = String::deserialize(deserializer)?; - data.parse::() - .map_err(|_| de::Error::custom("Parse from string failed")) - } -} - -/// Balance type when interacting with RPC. -pub type RpcBalance = RpcU128; - -/// Price type of order when interacting with RPC. -pub type RpcPrice = RpcU128; - -/// Weight type of mining when interacting with RPC. -pub type RpcMiningWeight = RpcU128; - -/// Weight type of staking when interacting with RPC. -pub type RpcVoteWeight = RpcU128; - -/// A helper struct for handling u128 serialization/deserialization of RPC. -/// See https://github.com/polkadot-js/api/issues/2464 for details (shit!). -#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)] -pub struct RpcU128(#[serde(with = "self::serde_num_str")] T); - -impl From for RpcU128 { - fn from(value: T) -> Self { - RpcU128(value) - } -} - -#[cfg(test)] -mod tests { - use serde::{Deserialize, Serialize}; - - #[test] - fn test_serde_hex_attr() { - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct HexTest(#[serde(with = "super::serde_hex")] Vec); - - let test = HexTest(b"0123456789".to_vec()); - let ser = serde_json::to_string(&test).unwrap(); - assert_eq!(ser, "\"0x30313233343536373839\""); - let de = serde_json::from_str::(&ser).unwrap(); - assert_eq!(de, test); - // without 0x - let de = serde_json::from_str::("\"30313233343536373839\"").unwrap(); - assert_eq!(de, test); - } - - #[test] - fn test_serde_text_attr() { - #[derive(PartialEq, Debug, Serialize, Deserialize)] - struct TextTest(#[serde(with = "super::serde_text")] Vec); - - let test = TextTest(b"0123456789".to_vec()); - let ser = serde_json::to_string(&test).unwrap(); - assert_eq!(ser, "\"0123456789\""); - let de = serde_json::from_str::(&ser).unwrap(); - assert_eq!(de, test); - } - - #[test] - fn test_serde_num_str_attr() { - use super::RpcU128; - - let test = RpcU128(u128::max_value()); - let ser = serde_json::to_string(&test).unwrap(); - assert_eq!(ser, "\"340282366920938463463374607431768211455\""); - let de = serde_json::from_str::>(&ser).unwrap(); - assert_eq!(de, test); - } -} diff --git a/xpallets/transaction-fee/rpc/Cargo.toml b/xpallets/transaction-fee/rpc/Cargo.toml index fecca32d05738..5bda5619bf2b5 100644 --- a/xpallets/transaction-fee/rpc/Cargo.toml +++ b/xpallets/transaction-fee/rpc/Cargo.toml @@ -23,8 +23,7 @@ sp-runtime = "2.0.0" # Substrate pallets pallet-transaction-payment-rpc = "2.0.0" -# ChainX pallets -xpallet-support = { path = "../../support" } +xp-rpc = { path = "../../../primitives/rpc" } # ChainX pallets api xpallet-transaction-fee-rpc-runtime-api = { path = "./runtime-api" } diff --git a/xpallets/transaction-fee/rpc/src/lib.rs b/xpallets/transaction-fee/rpc/src/lib.rs index e4e01071994b7..50445a4608941 100644 --- a/xpallets/transaction-fee/rpc/src/lib.rs +++ b/xpallets/transaction-fee/rpc/src/lib.rs @@ -19,7 +19,7 @@ use sp_runtime::{ use pallet_transaction_payment_rpc::Error; -use xpallet_support::RpcBalance; +use xp_rpc::RpcBalance; use xpallet_transaction_fee_rpc_runtime_api::{FeeDetails, InclusionFee}; pub use xpallet_transaction_fee_rpc_runtime_api::XTransactionFeeApi as XTransactionFeeRuntimeApi;