mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 05:05:41 +00:00
Check that PerThing valid on decode (#5475)
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
use sp_std::{ops, fmt, prelude::*, convert::TryInto};
|
use sp_std::{ops, fmt, prelude::*, convert::TryInto};
|
||||||
use codec::{Encode, Decode, CompactAs};
|
use codec::{Encode, CompactAs};
|
||||||
use crate::traits::{
|
use crate::traits::{
|
||||||
SaturatedConversion, UniqueSaturatedInto, Saturating, BaseArithmetic, Bounded, Zero,
|
SaturatedConversion, UniqueSaturatedInto, Saturating, BaseArithmetic, Bounded, Zero,
|
||||||
};
|
};
|
||||||
@@ -311,7 +311,7 @@ macro_rules! implement_per_thing {
|
|||||||
///
|
///
|
||||||
#[doc = $title]
|
#[doc = $title]
|
||||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||||
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord,
|
#[derive(Encode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord,
|
||||||
RuntimeDebug, CompactAs)]
|
RuntimeDebug, CompactAs)]
|
||||||
pub struct $name($type);
|
pub struct $name($type);
|
||||||
|
|
||||||
@@ -534,6 +534,18 @@ macro_rules! implement_per_thing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl codec::Decode for $name {
|
||||||
|
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||||
|
let inner = <$type as codec::Decode>::decode(input)?;
|
||||||
|
|
||||||
|
if inner <= <Self as PerThing>::ACCURACY {
|
||||||
|
Ok(Self(inner))
|
||||||
|
} else {
|
||||||
|
Err("Value is greater than allowed maximum!".into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl crate::traits::Bounded for $name {
|
impl crate::traits::Bounded for $name {
|
||||||
fn min_value() -> Self {
|
fn min_value() -> Self {
|
||||||
<Self as PerThing>::zero()
|
<Self as PerThing>::zero()
|
||||||
@@ -629,6 +641,21 @@ macro_rules! implement_per_thing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fail_on_invalid_encoded_value() {
|
||||||
|
let value = <$upper_type>::from($max) * 2;
|
||||||
|
let casted = value as $type;
|
||||||
|
let encoded = casted.encode();
|
||||||
|
|
||||||
|
// For types where `$max == $type::maximum()` we can not
|
||||||
|
if <$upper_type>::from(casted) == value {
|
||||||
|
assert_eq!(
|
||||||
|
$name::decode(&mut &encoded[..]),
|
||||||
|
Err("Value is greater than allowed maximum!".into()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn per_thing_api_works() {
|
fn per_thing_api_works() {
|
||||||
// some really basic stuff
|
// some really basic stuff
|
||||||
|
|||||||
Reference in New Issue
Block a user