Skip to content
Snippets Groups Projects
Commit 45d26e74 authored by thiolliere's avatar thiolliere Committed by GitHub
Browse files

Make decoding of `compact<perthing>` saturating instead of invalid (#7062)


* make decoding of cmopact<perthing> saturating

* fix stable build

* Update primitives/arithmetic/src/per_things.rs

Co-authored-by: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/arithmetic/src/per_things.rs

Co-authored-by: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>

Co-authored-by: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>
parent 380cf822
No related merge requests found
......@@ -323,9 +323,28 @@ macro_rules! implement_per_thing {
///
#[doc = $title]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, CompactAs)]
#[derive(Encode, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug)]
pub struct $name($type);
/// Implementation makes any compact encoding of `PerThing::Inner` valid,
/// when decoding it will saturate up to `PerThing::ACCURACY`.
impl CompactAs for $name {
type As = $type;
fn encode_as(&self) -> &Self::As {
&self.0
}
fn decode_from(x: Self::As) -> Self {
// Saturates if `x` is more than `$max` internally.
Self::from_parts(x)
}
}
impl From<codec::Compact<$name>> for $name {
fn from(x: codec::Compact<$name>) -> $name {
x.0
}
}
impl PerThing for $name {
type Inner = $type;
type Upper = $upper_type;
......@@ -1166,6 +1185,17 @@ macro_rules! implement_per_thing {
// deconstruct is also const, hence it can be called in const rhs.
const C5: bool = C1.deconstruct() == 0;
}
#[test]
fn compact_decoding_saturate_when_beyond_accuracy() {
use num_traits::Bounded;
use codec::Compact;
let p = Compact::<$name>::decode(&mut &Compact(<$type>::max_value()).encode()[..])
.unwrap();
assert_eq!((p.0).0, $max);
assert_eq!($name::from(p), $name::max_value());
}
}
};
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment