Skip to content

Commit

Permalink
Fix u128 jsonrpc serialization/deserialization issue (paritytech#238)
Browse files Browse the repository at this point in the history
JSON serialization/deserialization of `i128`/`u128` can only be supported when the `arbitrary_precision` feature enabled.

But `arbitrary_precision` numbers don't work with `serde(tag = ..)` and `serde(flatten)` syntax (serde-rs/json#505), as a result, the deserialization of structures in `jsonrpc-core` will be error.

Fortunately, the crate `jsonrpc-core` provides a workround(paritytech/jsonrpc#521).
Although this workround will cause more extra serialization/deserialization work, but I think it's worth it.

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Sep 17, 2020
1 parent 7f4464f commit 33adc40
Show file tree
Hide file tree
Showing 37 changed files with 257 additions and 626 deletions.
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4", features = ["derive"] }
jsonrpc-core = "14.2.0"
jsonrpc-core = { version = "14.2.0", features = ["arbitrary_precision"] }
jsonrpc-pubsub = "14.2.0"

# Substrate client
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
C::Api: BlockBuilder<Block>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: xpallet_assets_rpc_runtime_api::AssetsApi<Block, AccountId, Balance>,
C::Api: xpallet_assets_rpc_runtime_api::XAssetsApi<Block, AccountId, Balance>,
C::Api:
xpallet_mining_staking_rpc_runtime_api::XStakingApi<Block, AccountId, Balance, BlockNumber>,
C::Api:
Expand All @@ -88,7 +88,7 @@ where
{
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
use substrate_frame_rpc_system::{FullSystem, SystemApi};
use xpallet_assets_rpc::{Assets, AssetsApi};
use xpallet_assets_rpc::{Assets, XAssetsApi};
use xpallet_contracts_rpc::{Contracts, ContractsApi};
use xpallet_dex_spot_rpc::{XSpot, XSpotApi};
use xpallet_gateway_common_rpc::{XGatewayCommon, XGatewayCommonApi};
Expand Down Expand Up @@ -128,7 +128,7 @@ where
),
));

io.extend_with(AssetsApi::to_delegate(Assets::new(client.clone())));
io.extend_with(XAssetsApi::to_delegate(Assets::new(client.clone())));
io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));
io.extend_with(XStakingApi::to_delegate(XStaking::new(client.clone())));
io.extend_with(XSpotApi::to_delegate(XSpot::new(client.clone())));
Expand Down
28 changes: 14 additions & 14 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;

use xpallet_contracts_rpc_runtime_api::ContractExecResult;
use xpallet_dex_spot::{Depth, FullPairInfo, RpcOrder, TradingPairId};
use xpallet_mining_asset::{MiningAssetInfo, RpcMinerLedger};
use xpallet_mining_staking::{NominatorInfo, RpcNominatorLedger, ValidatorInfo};
use xpallet_support::{traits::MultisigAddressFor, RpcBalance, RpcPrice};
use xpallet_mining_asset::{MinerLedger, MiningAssetInfo};
use xpallet_mining_staking::{NominatorInfo, NominatorLedger, ValidatorInfo};
use xpallet_support::traits::MultisigAddressFor;

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand Down Expand Up @@ -1155,7 +1155,7 @@ impl_runtime_apis! {
}
}

impl xpallet_assets_rpc_runtime_api::AssetsApi<Block, AccountId, Balance> for Runtime {
impl xpallet_assets_rpc_runtime_api::XAssetsApi<Block, AccountId, Balance> for Runtime {
fn assets_for_account(who: AccountId) -> BTreeMap<AssetId, BTreeMap<AssetType, Balance>> {
XAssets::valid_assets_of(&who)
}
Expand All @@ -1166,16 +1166,16 @@ impl_runtime_apis! {
}

impl xpallet_mining_staking_rpc_runtime_api::XStakingApi<Block, AccountId, Balance, BlockNumber> for Runtime {
fn validators() -> Vec<ValidatorInfo<AccountId, RpcBalance<Balance>, BlockNumber>> {
fn validators() -> Vec<ValidatorInfo<AccountId, Balance, BlockNumber>> {
XStaking::validators_info()
}
fn validator_info_of(who: AccountId) -> ValidatorInfo<AccountId, RpcBalance<Balance>, BlockNumber> {
fn validator_info_of(who: AccountId) -> ValidatorInfo<AccountId, Balance, BlockNumber> {
XStaking::validator_info_of(who)
}
fn staking_dividend_of(who: AccountId) -> BTreeMap<AccountId, RpcBalance<Balance>> {
fn staking_dividend_of(who: AccountId) -> BTreeMap<AccountId, Balance> {
XStaking::staking_dividend_of(who)
}
fn nomination_details_of(who: AccountId) -> BTreeMap<AccountId, RpcNominatorLedger<RpcBalance<Balance>, BlockNumber>> {
fn nomination_details_of(who: AccountId) -> BTreeMap<AccountId, NominatorLedger<Balance, BlockNumber>> {
XStaking::nomination_details_of(who)
}
fn nominator_info_of(who: AccountId) -> NominatorInfo<BlockNumber> {
Expand All @@ -1184,29 +1184,29 @@ impl_runtime_apis! {
}

impl xpallet_dex_spot_rpc_runtime_api::XSpotApi<Block, AccountId, Balance, BlockNumber, Balance> for Runtime {
fn trading_pairs() -> Vec<FullPairInfo<RpcPrice<Balance>, BlockNumber>> {
fn trading_pairs() -> Vec<FullPairInfo<Balance, BlockNumber>> {
XSpot::trading_pairs()
}

fn orders(who: AccountId, page_index: u32, page_size: u32) -> Vec<RpcOrder<TradingPairId, AccountId, RpcBalance<Balance>, RpcPrice<Balance>, BlockNumber>> {
fn orders(who: AccountId, page_index: u32, page_size: u32) -> Vec<RpcOrder<TradingPairId, AccountId, Balance, Balance, BlockNumber>> {
XSpot::orders(who, page_index, page_size)
}

fn depth(pair_id: TradingPairId, depth_size: u32) -> Option<Depth<RpcPrice<Balance>, RpcBalance<Balance>>> {
fn depth(pair_id: TradingPairId, depth_size: u32) -> Option<Depth<Balance, Balance>> {
XSpot::depth(pair_id, depth_size)
}
}

impl xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi<Block, AccountId, Balance, BlockNumber> for Runtime {
fn mining_assets() -> Vec<MiningAssetInfo<AccountId, RpcBalance<Balance>, BlockNumber>> {
fn mining_assets() -> Vec<MiningAssetInfo<AccountId, Balance, BlockNumber>> {
XMiningAsset::mining_assets()
}

fn mining_dividend(who: AccountId) -> BTreeMap<AssetId, RpcBalance<Balance>> {
fn mining_dividend(who: AccountId) -> BTreeMap<AssetId, Balance> {
XMiningAsset::mining_dividend(who)
}

fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, RpcMinerLedger<BlockNumber>> {
fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, MinerLedger<BlockNumber>> {
XMiningAsset::miner_ledger(who)
}
}
Expand Down
17 changes: 9 additions & 8 deletions xpallets/assets/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ version = "2.0.0-alpha.0"
authors = ["The ChainX Authors"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4" }
serde = { version = "1.0.101", features = ["derive"] }
jsonrpc-core = "14.2.0"
jsonrpc-core-client = "14.2.0"
jsonrpc-derive = "14.2.1"
jsonrpc-core = { version = "14.2.0", features = ["arbitrary_precision"] }
jsonrpc-core-client = { version = "14.2.0", features = ["arbitrary_precision"] }
jsonrpc-derive = "14.2.2"

sp-api = { version = "2.0.0-rc6" }
sp-runtime = { version = "2.0.0-rc6" }
sp-blockchain = { version = "2.0.0-rc6" }
sp-api = "2.0.0-rc6"
sp-runtime = "2.0.0-rc6"
sp-blockchain = "2.0.0-rc6"

chainx-primitives = { path = "../../../primitives" }
xpallet-support = { path = "../../support" }
xpallet-assets-rpc-runtime-api = { path = "./runtime-api" }
9 changes: 6 additions & 3 deletions xpallets/assets/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ version = "2.0.0-alpha.0"
authors = ["The ChainX Authors"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }

sp-api = { version = "2.0.0-rc6", default-features = false }
sp-std = { version = "2.0.0-rc6", default-features = false }
sp-runtime = { version = "2.0.0-rc6", default-features = false }

chainx-primitives = { path = "../../../../primitives", default-features = false }

xpallet-assets = { path = "../../", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
"sp-std/std",
"sp-runtime/std",

"chainx-primitives/std",

"xpallet-assets/std",
]
7 changes: 4 additions & 3 deletions xpallets/assets/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;

use sp_std::collections::btree_map::BTreeMap;

use codec::Codec;

pub use chainx_primitives::{AssetId, Decimals};
pub use xpallet_assets::{AssetInfo, AssetRestrictions, AssetType, Chain, TotalAssetInfo};

sp_api::decl_runtime_apis! {
pub trait AssetsApi<AccountId, Balance> where
pub trait XAssetsApi<AccountId, Balance>
where
AccountId: Codec,
Balance: Codec,
{
Expand Down
Loading

0 comments on commit 33adc40

Please sign in to comment.