Skip to content

Commit

Permalink
Fix u128 RPC serialization/deserialization (paritytech#307)
Browse files Browse the repository at this point in the history
Previously, the `Balance`, `Price` and `Weight` were serialized directly into u128 numbers
in chainx-org/ChainX#238, but polkadot.js could not parse them successfully.

Therefore, it is necessary to serialize `Balance`, `Price`, and `Weight` back to String.
Unlike before, the serialization conversion of `Balance` and `Price` only occurs at the RPC layer 
and does not involve the types in the pallet (because the `Weight` is an alias of u128,
so we use serde derive directly on the type to complete the serialization conversion).

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro authored Oct 23, 2020
1 parent b39c5fb commit 43a33fb
Show file tree
Hide file tree
Showing 42 changed files with 682 additions and 254 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ xpallet-support = { path = "../xpallets/support", default-features = false }
default = ["std"]
std = [
"codec/std",
"serde/std",

"serde",
# Substrate primitives
"sp-application-crypto/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",

# Substrate pallets
"frame-system/std",

# ChainX pallets
"xpallet-support/std",
]
4 changes: 4 additions & 0 deletions primitives/assets-registrar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ edition = "2018"
[dependencies]
impl-trait-for-tuples = "0.1.3"

# Substrate pallets
frame-support = { version = "2.0.0", default-features = false }

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

[features]
default = ["std"]
std = [
# Substrate pallets
"frame-support/std",
# ChainX primitives
"chainx-primitives/std",
]
2 changes: 1 addition & 1 deletion primitives/genesis-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ xpallet-support = { path = "../../xpallets/support", default-features = false }
[features]
default = ["std"]
std = [
"serde/std",
"serde",
# ChainX primitives
"chainx-primitives/std",
# ChainX pallets
Expand Down
16 changes: 4 additions & 12 deletions primitives/genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ mod genesis_params {
use chainx_primitives::Balance;
use serde::{Deserialize, Serialize};

fn deserialize_u128<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse::<u128>().map_err(serde::de::Error::custom)
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct AllParams<AccountId, TBalance, AssetBalanceOf, StakingBalanceOf> {
pub balances: BalancesParams<AccountId, TBalance>,
Expand Down Expand Up @@ -54,15 +46,15 @@ mod genesis_params {
pub referral_id: Vec<u8>,
pub self_bonded: Balance,
pub total_nomination: Balance,
#[serde(deserialize_with = "deserialize_u128")]
#[serde(with = "xpallet_support::serde_num_str")]
pub total_weight: u128,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Nomination<AccountId, Balance> {
pub nominee: AccountId,
pub nomination: Balance,
#[serde(deserialize_with = "deserialize_u128")]
#[serde(with = "xpallet_support::serde_num_str")]
pub weight: u128,
}

Expand All @@ -81,14 +73,14 @@ mod genesis_params {
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct XBtcInfo {
pub balance: Balance,
#[serde(deserialize_with = "deserialize_u128")]
#[serde(with = "xpallet_support::serde_num_str")]
pub weight: u128,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct XBtcMiner<AccountId> {
pub who: AccountId,
#[serde(deserialize_with = "deserialize_u128")]
#[serde(with = "xpallet_support::serde_num_str")]
pub weight: u128,
}

Expand Down
3 changes: 2 additions & 1 deletion primitives/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false }

# Substrate primitives
sp-core = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-runtime-interface = { version = "2.0.0", default-features = false }
Expand All @@ -18,7 +19,7 @@ hex = "0.4"
default = ["std"]
std = [
"codec/std",

# Substrate primitives
"sp-core/std",
"sp-runtime/std",
"sp-runtime-interface/std",
Expand Down
10 changes: 6 additions & 4 deletions primitives/mining/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ edition = "2018"

[dependencies]
# Substrate primitives
sp-std = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-arithmetic = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-std = { version = "2.0.0", default-features = false }

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

[features]
default = ["std"]
std = [
"sp-std/std",
# Substrate primitives
"sp-arithmetic/std",
"sp-runtime/std",

"sp-std/std",
# ChainX primitives
"chainx-primitives/std",
]
7 changes: 4 additions & 3 deletions primitives/mining/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2018"

[dependencies]
# Substrate primitives
sp-std = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-std = { version = "2.0.0", default-features = false }

# ChainX primitives
chainx-primitives = { path = "../../../primitives", default-features = false }
Expand All @@ -16,9 +16,10 @@ xp-mining-common = { path = "../common", default-features = false }
[features]
default = ["std"]
std = [
"sp-std/std",
# Substrate primitives
"sp-runtime/std",

"sp-std/std",
# ChainX primitives
"chainx-primitives/std",
"xp-mining-common/std",
]
2 changes: 1 addition & 1 deletion primitives/protocol/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"], default-features = false }
serde = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

# Substrate primitives
sp-runtime = { version = "2.0.0", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion primitives/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false }
serde = { version = "1.0.101", optional = true, features = ["derive"] }

# Substrate primitives
sp-core = { version = "2.0.0", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false }
sp-std = { version = "2.0.0", default-features = false }
Expand All @@ -20,7 +21,7 @@ default = ["std"]
std = [
"codec/std",
"serde/std",

# Substrate primitives
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
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 = { version = "15.0.0", features = ["arbitrary_precision"] }
jsonrpc-core = "15.0.0"
jsonrpc-pubsub = "15.0.0"

# Substrate client
Expand Down
19 changes: 14 additions & 5 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use sp_transaction_pool::TransactionPool;
use chainx_primitives::Block;
use chainx_runtime::{AccountId, Balance, BlockNumber, Hash, Index};

use xpallet_mining_asset_rpc_runtime_api::MiningWeight;
use xpallet_mining_staking_rpc_runtime_api::VoteWeight;

/// Light client extra dependencies.
pub struct LightDeps<C, F, P> {
/// The client instance to use.
Expand Down Expand Up @@ -89,23 +92,29 @@ where
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::XAssetsApi<Block, AccountId, Balance>,
C::Api:
xpallet_mining_staking_rpc_runtime_api::XStakingApi<Block, AccountId, Balance, BlockNumber>,
C::Api:
xpallet_dex_spot_rpc_runtime_api::XSpotApi<Block, AccountId, Balance, BlockNumber, Balance>,
C::Api: xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi<
C::Api: xpallet_gateway_common_rpc_runtime_api::XGatewayCommonApi<Block, AccountId, Balance>,
C::Api: xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<
Block,
AccountId,
Balance,
BlockNumber,
>,
C::Api: xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<
C::Api: xpallet_mining_staking_rpc_runtime_api::XStakingApi<
Block,
AccountId,
Balance,
VoteWeight,
BlockNumber,
>,
C::Api: xpallet_mining_asset_rpc_runtime_api::XMiningAssetApi<
Block,
AccountId,
Balance,
MiningWeight,
BlockNumber,
>,
C::Api: xpallet_gateway_common_rpc_runtime_api::XGatewayCommonApi<Block, AccountId, Balance>,
<C::Api as sp_api::ApiErrorExt>::Error: fmt::Debug,
P: TransactionPool + 'static,
SC: SelectChain<Block> + 'static,
Expand Down
16 changes: 9 additions & 7 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub use xpallet_gateway_common::{
types::{GenericTrusteeIntentionProps, GenericTrusteeSessionInfo, TrusteeInfoConfig},
};
pub use xpallet_gateway_records::Withdrawal;
pub use xpallet_mining_asset::MiningWeight;
pub use xpallet_mining_staking::VoteWeight;

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -1224,17 +1226,17 @@ impl_runtime_apis! {
}
}

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

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

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

fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, MinerLedger<BlockNumber>> {
fn miner_ledger(who: AccountId) -> BTreeMap<AssetId, MinerLedger<MiningWeight, BlockNumber>> {
XMiningAsset::miner_ledger(who)
}
}
Expand Down
17 changes: 10 additions & 7 deletions scripts/chainx-js/chainx_types_manual.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"is_chilled": "bool",
"last_chilled": "Option<BlockNumber>",
"total": "Balance",
"last_total_vote_weight": "WeightType",
"last_total_vote_weight": "VoteWeight",
"last_total_vote_weight_update": "BlockNumber",
"is_validating": "bool",
"self_bonded": "Balance",
Expand All @@ -213,7 +213,7 @@
},
"NominatorLedger": {
"nomination": "Balance",
"last_vote_weight": "WeightType",
"last_vote_weight": "VoteWeight",
"last_vote_weight_update": "BlockNumber",
"unbonded_chunks": "Vec<Unbonded>"
},
Expand All @@ -222,7 +222,7 @@
"mining_power": "FixedAssetPower",
"reward_pot": "AccountId",
"reward_pot_balance": "Balance",
"last_total_mining_weight": "WeightType",
"last_total_mining_weight": "MiningWeight",
"last_total_mining_weight_update": "BlockNumber"
},
"Unbonded": {
Expand All @@ -239,17 +239,20 @@
"data": "Vec<RpcOrder>"
},
"String": "Text",
"Price": "u128",
"Balance": "u128",
"WeightType": "u128",
"MiningPower": "u128",
"MiningWeight": "u128",
"VoteWeight": "u128",
"RpcPrice": "String",
"RpcBalance": "String",
"RpcMiningWeight": "String",
"RpcVoteWeight": "String",
"AssetRestrictions": {
"mask": "u32"
"bits": "u32"
},
"OrderInfo": "Order",
"HandicapInfo": "Handicap",
"FullIdentification": "ValidatorId",
"Price": "Balance",
"WithdrawalRecordOf": "WithdrawalRecord",
"RpcWithdrawalRecord": {
"asset_id": "AssetId",
Expand Down
Loading

0 comments on commit 43a33fb

Please sign in to comment.