serialize partial_fee into string (#4898)

* serialize partial_fee into string

* implement deserialize

* bump version
This commit is contained in:
Xiliang Chen
2020-02-13 01:16:00 +13:00
committed by GitHub
parent b3cf97940c
commit 35b5cdd29b
4 changed files with 52 additions and 50 deletions
@@ -22,12 +22,13 @@ use sp_std::prelude::*;
use frame_support::weights::{Weight, DispatchClass};
use codec::{Encode, Codec, Decode};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use sp_runtime::traits::{UniqueSaturatedInto, SaturatedConversion};
use serde::{Serialize, Deserialize, Serializer, Deserializer};
use sp_runtime::traits::{MaybeDisplay, MaybeFromStr};
/// Some information related to a dispatchable that can be queried from the runtime.
#[derive(Eq, PartialEq, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Debug))]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct RuntimeDispatchInfo<Balance> {
/// Weight of this dispatch.
pub weight: Weight,
@@ -35,47 +36,27 @@ pub struct RuntimeDispatchInfo<Balance> {
pub class: DispatchClass,
/// The partial inclusion fee of this dispatch. This does not include tip or anything else which
/// is dependent on the signature (aka. depends on a `SignedExtension`).
#[cfg_attr(feature = "std", serde(bound(serialize = "Balance: std::fmt::Display")))]
#[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))]
#[cfg_attr(feature = "std", serde(bound(deserialize = "Balance: std::str::FromStr")))]
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))]
pub partial_fee: Balance,
}
/// A capped version of `RuntimeDispatchInfo`.
///
/// The `Balance` is capped (or expanded) to `u64` to avoid serde issues with `u128`.
#[derive(Eq, PartialEq, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct CappedDispatchInfo {
/// Weight of this dispatch.
pub weight: Weight,
/// Class of this dispatch.
pub class: DispatchClass,
/// The partial inclusion fee of this dispatch. This does not include tip or anything else which
/// is dependent on the signature (aka. depends on a `SignedExtension`).
pub partial_fee: u64,
#[cfg(feature = "std")]
fn serialize_as_string<S: Serializer, T: std::fmt::Display>(t: &T, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&t.to_string())
}
impl CappedDispatchInfo {
/// Create a new `CappedDispatchInfo` from `RuntimeDispatchInfo`.
pub fn new<Balance: UniqueSaturatedInto<u64>>(
dispatch: RuntimeDispatchInfo<Balance>,
) -> Self {
let RuntimeDispatchInfo {
weight,
class,
partial_fee,
} = dispatch;
Self {
weight,
class,
partial_fee: partial_fee.saturated_into(),
}
}
#[cfg(feature = "std")]
fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(deserializer: D) -> Result<T, D::Error> {
let s = String::deserialize(deserializer)?;
s.parse::<T>().map_err(|_| serde::de::Error::custom("Parse from string failed"))
}
sp_api::decl_runtime_apis! {
pub trait TransactionPaymentApi<Balance, Extrinsic> where
Balance: Codec,
Balance: Codec + MaybeDisplay + MaybeFromStr,
Extrinsic: Codec,
{
fn query_info(uxt: Extrinsic, len: u32) -> RuntimeDispatchInfo<Balance>;
@@ -87,18 +68,34 @@ mod tests {
use super::*;
#[test]
fn should_serialize_properly_with_u64() {
fn should_serialize_and_deserialize_properly_with_string() {
let info = RuntimeDispatchInfo {
weight: 5,
class: DispatchClass::Normal,
partial_fee: 1_000_000_u64,
};
let info = CappedDispatchInfo::new(info);
assert_eq!(
serde_json::to_string(&info).unwrap(),
r#"{"weight":5,"class":"normal","partialFee":1000000}"#,
);
let json_str = r#"{"weight":5,"class":"normal","partialFee":"1000000"}"#;
assert_eq!(serde_json::to_string(&info).unwrap(), json_str);
assert_eq!(serde_json::from_str::<RuntimeDispatchInfo<u64>>(json_str).unwrap(), info);
// should not panic
serde_json::to_value(&info).unwrap();
}
#[test]
fn should_serialize_and_deserialize_properly_large_value() {
let info = RuntimeDispatchInfo {
weight: 5,
class: DispatchClass::Normal,
partial_fee: u128::max_value(),
};
let json_str = r#"{"weight":5,"class":"normal","partialFee":"340282366920938463463374607431768211455"}"#;
assert_eq!(serde_json::to_string(&info).unwrap(), json_str);
assert_eq!(serde_json::from_str::<RuntimeDispatchInfo<u128>>(json_str).unwrap(), info);
// should not panic
serde_json::to_value(&info).unwrap();