Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[balances] fix balance entry serde hack #146

Merged
merged 2 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions balances/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Encointer. If not, see <http://www.gnu.org/licenses/>.

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;
Expand All @@ -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<BlockHash, AccountId, BlockNumber>
where
Expand All @@ -39,7 +38,7 @@ where
&self,
account: AccountId,
at: Option<BlockHash>,
) -> Result<Vec<(CommunityIdentifier, Vec<u8>)>>;
) -> Result<Vec<(CommunityIdentifier, BalanceEntry<BlockNumber>)>>;
}

pub struct Balances<Client, Block, AccountId> {
Expand Down Expand Up @@ -70,19 +69,12 @@ where
&self,
account: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> Result<Vec<(CommunityIdentifier, Vec<u8>)>> {
) -> Result<Vec<(CommunityIdentifier, BalanceEntry<BlockNumberFor<Block>>)>> {
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)?)
}
}

Expand Down
4 changes: 4 additions & 0 deletions primitives/src/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +35,7 @@ pub type Demurrage = I64F64;
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct BalanceEntry<BlockNumber> {
/// 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,
Expand Down