From fd22d80d50027c9c74acaad6c22d410f97315120 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Mon, 14 Feb 2022 16:57:36 +0100 Subject: [PATCH 1/2] [balances] fix balance entry serde hack --- balances/rpc/src/lib.rs | 18 +++++------------- primitives/src/balances.rs | 4 ++++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/balances/rpc/src/lib.rs b/balances/rpc/src/lib.rs index 19c95cbe..3d26bb92 100644 --- a/balances/rpc/src/lib.rs +++ b/balances/rpc/src/lib.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Encointer. If not, see . +use encointer_balances_rpc_runtime_api::BalancesApi as BalancesRuntimeApi; +use encointer_primitives::{balances::BalanceEntry, communities::CommunityIdentifier}; use jsonrpc_core::{Error, ErrorCode, Result}; use jsonrpc_derive::rpc; use sc_rpc::DenyUnsafe; @@ -25,9 +27,6 @@ use sp_runtime::{ }; use std::sync::Arc; -use encointer_balances_rpc_runtime_api::BalancesApi as BalancesRuntimeApi; -use encointer_primitives::communities::CommunityIdentifier; - #[rpc] pub trait BalancesApi where @@ -39,7 +38,7 @@ where &self, account: AccountId, at: Option, - ) -> Result)>>; + ) -> Result)>>; } pub struct Balances { @@ -70,19 +69,12 @@ where &self, account: AccountId, at: Option<::Hash>, - ) -> Result)>> { + ) -> Result>)>> { self.deny_unsafe.check_if_safe()?; let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - return Ok(api - .get_all_balances(&at, &account) - .map_err(runtime_error_into_rpc_err)? - .iter() - // serde can't cope with i128 and panics. So we need to hand-encode the value here - // see https://github.com/paritytech/substrate/issues/4641 - .map(|b| (b.0, b.1.encode())) - .collect()) + return Ok(api.get_all_balances(&at, &account).map_err(runtime_error_into_rpc_err)?) } } diff --git a/primitives/src/balances.rs b/primitives/src/balances.rs index c4d77795..8067204a 100644 --- a/primitives/src/balances.rs +++ b/primitives/src/balances.rs @@ -22,6 +22,9 @@ use sp_core::RuntimeDebug; #[cfg(feature = "serde_derive")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "serde_derive")] +use ep_core::serde::serialize_fixed; + // We're working with fixpoint here. pub type BalanceType = I64F64; pub type Demurrage = I64F64; @@ -32,6 +35,7 @@ pub type Demurrage = I64F64; #[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] pub struct BalanceEntry { /// The balance of the account after last manual adjustment + #[cfg_attr(feature = "serde_derive", serde(with = "serialize_fixed"))] pub principal: BalanceType, /// The time (block height) at which the balance was last adjusted pub last_update: BlockNumber, From 480d4a52c17411ab2afd436368470b0b19fc0e8e Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Tue, 15 Feb 2022 12:48:40 +0100 Subject: [PATCH 2/2] [README] add dev hint section with explanation for custom fixed point serialization shim. --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index b9c26ff7..c5508e03 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,19 @@ a registry for classifieds from community members, linking to IPFS ## personhood-oracle a digital personhood verification oracle with XCM support. See pallet sybil-gate-example for how to use this from another parachain + +## Dev Hints +* There is a know issue with serializing u-/i128 in the json-rpc crate, see (https://github.com/paritytech/substrate/issues/4641). +This affects us predominantly when serializing fixed point numbers in the custom RPCs. There is a custom serialization +shim as a workaround for that issue in [ep-core](./primitives/core), which can be used as custom serde attribute like: + +```rust +#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))] +pub struct BalanceEntry { + /// The balance of the account after last manual adjustment + #[cfg_attr(feature = "serde_derive", serde(with = "serialize_fixed"))] + pub principal: BalanceType, + /// The time (block height) at which the balance was last adjusted + pub last_update: BlockNumber, +} +``` \ No newline at end of file