From 3ceb001f0174260155b5b1a5ecc1ada72c46ec9e Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 30 Aug 2023 16:25:12 -0300 Subject: [PATCH 1/6] Add environmental variable to track decoded instructions --- Cargo.lock | 1 + polkadot/xcm/Cargo.toml | 2 ++ polkadot/xcm/src/lib.rs | 3 ++- polkadot/xcm/src/v3/mod.rs | 39 +++++++++++++++++++++++++++++--------- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74034bab663c..244c7dc6721d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17897,6 +17897,7 @@ version = "1.0.0" dependencies = [ "bounded-collections", "derivative", + "environmental", "hex", "hex-literal 0.4.1", "impl-trait-for-tuples", diff --git a/polkadot/xcm/Cargo.toml b/polkadot/xcm/Cargo.toml index 3c9fd0267173..d113848adaf4 100644 --- a/polkadot/xcm/Cargo.toml +++ b/polkadot/xcm/Cargo.toml @@ -16,6 +16,7 @@ scale-info = { version = "2.5.0", default-features = false, features = ["derive" sp-weights = { path = "../../substrate/primitives/weights", default-features = false, features = ["serde"] } serde = { version = "1.0.188", default-features = false, features = ["alloc", "derive"] } xcm-procedural = { path = "procedural" } +environmental = { version = "1.1.4", default-features = false } [dev-dependencies] sp-io = { path = "../../substrate/primitives/io" } @@ -28,6 +29,7 @@ wasm-api = [] std = [ "bounded-collections/std", "parity-scale-codec/std", + "environmental/std", "scale-info/std", "serde/std", "sp-weights/std", diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index a012c5f53fbf..f8039311bdba 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -20,7 +20,8 @@ // necessarily related to FRAME or even Substrate. // // Hence, `no_std` rather than sp-runtime. -#![no_std] +#![cfg_attr(not(feature = "std"), no_std)] + extern crate alloc; use derivative::Derivative; diff --git a/polkadot/xcm/src/v3/mod.rs b/polkadot/xcm/src/v3/mod.rs index ef3306d276fc..5d0a8efbdc0e 100644 --- a/polkadot/xcm/src/v3/mod.rs +++ b/polkadot/xcm/src/v3/mod.rs @@ -22,7 +22,7 @@ use super::v2::{ }; use crate::{DoubleEncoded, GetWeight}; use alloc::{vec, vec::Vec}; -use bounded_collections::{parameter_types, BoundedVec, ConstU32}; +use bounded_collections::{parameter_types, BoundedVec}; use core::{ convert::{TryFrom, TryInto}, fmt::Debug, @@ -31,6 +31,7 @@ use core::{ use derivative::Derivative; use parity_scale_codec::{ self, Decode, Encode, Error as CodecError, Input as CodecInput, MaxEncodedLen, + Compact, decode_vec_with_len, }; use scale_info::TypeInfo; @@ -69,13 +70,24 @@ pub type QueryId = u64; #[scale_info(bounds(), skip_type_params(Call))] pub struct Xcm(pub Vec>); -const MAX_INSTRUCTIONS_TO_DECODE: u32 = 100; +const MAX_INSTRUCTIONS_TO_DECODE: u8 = 100; + +environmental::environmental!(instructions_count: u8); impl Decode for Xcm { fn decode(input: &mut I) -> core::result::Result { - let bounded_instructions = - BoundedVec::, ConstU32>::decode(input)?; - Ok(Self(bounded_instructions.into_inner())) + instructions_count::using_once(&mut 0, || { + let number_of_instructions: u32 = >::decode(input)?.into(); + instructions_count::with(|count| { + *count = count.saturating_add(number_of_instructions as u8); + if *count > MAX_INSTRUCTIONS_TO_DECODE { + return Err(CodecError::from("Max instructions exceeded")); + } + Ok(()) + }).unwrap_or(Ok(()))?; + let decoded_instructions = decode_vec_with_len(input, number_of_instructions as usize)?; + Ok(Self(decoded_instructions)) + }) } } @@ -1440,15 +1452,24 @@ mod tests { } #[test] - fn decoding_fails_when_too_many_instructions() { - let small_xcm = Xcm::<()>(vec![ClearOrigin; 20]); - let bytes = small_xcm.encode(); + fn decoding_limits() { + let max_xcm = Xcm::<()>(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize]); + let bytes = max_xcm.encode(); let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); assert!(matches!(decoded_xcm, Ok(_))); - let big_xcm = Xcm::<()>(vec![ClearOrigin; 64_000]); + let big_xcm = Xcm::<()>(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize + 1]); let bytes = big_xcm.encode(); let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); assert!(matches!(decoded_xcm, Err(CodecError { .. }))); + + let nested_xcm = Xcm::<()>(vec![DepositReserveAsset { + assets: All.into(), + dest: Here.into(), + xcm: max_xcm, + }; (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize]); + let bytes = nested_xcm.encode(); + let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); + assert!(matches!(decoded_xcm, Err(CodecError { .. }))); } } From b1a89ca5646bb380a07f2c8915b8f6a90bbd7af0 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 30 Aug 2023 16:47:35 -0300 Subject: [PATCH 2/6] Fix doc tests --- polkadot/xcm/src/v2/multilocation.rs | 12 ++++++------ polkadot/xcm/src/v2/traits.rs | 2 +- polkadot/xcm/src/v3/junctions.rs | 4 ++-- polkadot/xcm/src/v3/multilocation.rs | 10 +++++----- polkadot/xcm/src/v3/traits.rs | 4 ++-- polkadot/xcm/xcm-builder/src/currency_adapter.rs | 2 +- polkadot/xcm/xcm-builder/src/matcher.rs | 2 +- polkadot/xcm/xcm-builder/src/matches_token.rs | 4 ++-- polkadot/xcm/xcm-executor/src/assets.rs | 2 +- polkadot/xcm/xcm-executor/src/traits/conversion.rs | 2 +- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/polkadot/xcm/src/v2/multilocation.rs b/polkadot/xcm/src/v2/multilocation.rs index 9fb74e8afb35..4a65a419045f 100644 --- a/polkadot/xcm/src/v2/multilocation.rs +++ b/polkadot/xcm/src/v2/multilocation.rs @@ -238,7 +238,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*, MultiLocation}; /// # fn main() { /// let mut m = MultiLocation::new(1, X2(PalletInstance(3), OnlyChild)); /// assert_eq!( @@ -260,7 +260,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*, MultiLocation}; /// let m = MultiLocation::new(1, X3(PalletInstance(3), OnlyChild, OnlyChild)); /// assert!(m.starts_with(&MultiLocation::new(1, X1(PalletInstance(3))))); /// assert!(!m.starts_with(&MultiLocation::new(1, X1(GeneralIndex(99))))); @@ -279,7 +279,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*, MultiLocation}; /// # fn main() { /// let mut m = MultiLocation::new(1, X1(Parachain(21))); /// assert_eq!(m.append_with(X1(PalletInstance(3))), Ok(())); @@ -302,7 +302,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*, MultiLocation}; /// # fn main() { /// let mut m = MultiLocation::new(2, X1(PalletInstance(3))); /// assert_eq!(m.prepend_with(MultiLocation::new(1, X2(Parachain(21), OnlyChild))), Ok(())); @@ -839,7 +839,7 @@ impl Junctions { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*}; /// # fn main() { /// let mut m = X3(Parachain(2), PalletInstance(3), OnlyChild); /// assert_eq!(m.match_and_split(&X2(Parachain(2), PalletInstance(3))), Some(&OnlyChild)); @@ -857,7 +857,7 @@ impl Junctions { /// /// # Example /// ```rust - /// # use xcm::v2::{Junctions::*, Junction::*}; + /// # use staging_xcm::v2::{Junctions::*, Junction::*}; /// let mut j = X3(Parachain(2), PalletInstance(3), OnlyChild); /// assert!(j.starts_with(&X2(Parachain(2), PalletInstance(3)))); /// assert!(j.starts_with(&j)); diff --git a/polkadot/xcm/src/v2/traits.rs b/polkadot/xcm/src/v2/traits.rs index ae03cf5547ba..10a63828e689 100644 --- a/polkadot/xcm/src/v2/traits.rs +++ b/polkadot/xcm/src/v2/traits.rs @@ -273,7 +273,7 @@ pub type SendResult = result::Result<(), SendError>; /// /// # Example /// ```rust -/// # use xcm::v2::prelude::*; +/// # use staging_xcm::v2::prelude::*; /// # use parity_scale_codec::Encode; /// /// /// A sender that only passes the message through and does nothing. diff --git a/polkadot/xcm/src/v3/junctions.rs b/polkadot/xcm/src/v3/junctions.rs index 201a80fb7658..c2826c4969e1 100644 --- a/polkadot/xcm/src/v3/junctions.rs +++ b/polkadot/xcm/src/v3/junctions.rs @@ -437,7 +437,7 @@ impl Junctions { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation}; /// # fn main() { /// let mut m = X1(Parachain(21)); /// assert_eq!(m.append_with(X1(PalletInstance(3))), Ok(())); @@ -568,7 +568,7 @@ impl Junctions { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*}; /// # fn main() { /// let mut m = X3(Parachain(2), PalletInstance(3), OnlyChild); /// assert_eq!(m.match_and_split(&X2(Parachain(2), PalletInstance(3))), Some(&OnlyChild)); diff --git a/polkadot/xcm/src/v3/multilocation.rs b/polkadot/xcm/src/v3/multilocation.rs index 07f829d014c0..7ac377d5c064 100644 --- a/polkadot/xcm/src/v3/multilocation.rs +++ b/polkadot/xcm/src/v3/multilocation.rs @@ -265,7 +265,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation}; /// # fn main() { /// let mut m = MultiLocation::new(1, X2(PalletInstance(3), OnlyChild)); /// assert_eq!( @@ -292,7 +292,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; /// # fn main() { /// let mut m: MultiLocation = (Parent, Parachain(21), 69u64).into(); /// assert_eq!(m.append_with((Parent, PalletInstance(3))), Ok(())); @@ -313,7 +313,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; /// # fn main() { /// let mut m: MultiLocation = (Parent, Parachain(21), 69u64).into(); /// let r = m.appended_with((Parent, PalletInstance(3))).unwrap(); @@ -333,7 +333,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; /// # fn main() { /// let mut m: MultiLocation = (Parent, Parent, PalletInstance(3)).into(); /// assert_eq!(m.prepend_with((Parent, Parachain(21), OnlyChild)), Ok(())); @@ -382,7 +382,7 @@ impl MultiLocation { /// /// # Example /// ```rust - /// # use xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; + /// # use staging_xcm::v3::{Junctions::*, Junction::*, MultiLocation, Parent}; /// # fn main() { /// let m: MultiLocation = (Parent, Parent, PalletInstance(3)).into(); /// let r = m.prepended_with((Parent, Parachain(21), OnlyChild)).unwrap(); diff --git a/polkadot/xcm/src/v3/traits.rs b/polkadot/xcm/src/v3/traits.rs index 128be42c2a2b..a3cbeada91bd 100644 --- a/polkadot/xcm/src/v3/traits.rs +++ b/polkadot/xcm/src/v3/traits.rs @@ -449,8 +449,8 @@ pub type SendResult = result::Result<(T, MultiAssets), SendError>; /// # Example /// ```rust /// # use parity_scale_codec::Encode; -/// # use xcm::v3::{prelude::*, Weight}; -/// # use xcm::VersionedXcm; +/// # use staging_xcm::v3::{prelude::*, Weight}; +/// # use staging_xcm::VersionedXcm; /// # use std::convert::Infallible; /// /// /// A sender that only passes the message through and does nothing. diff --git a/polkadot/xcm/xcm-builder/src/currency_adapter.rs b/polkadot/xcm/xcm-builder/src/currency_adapter.rs index 4dbd4fe8bcd0..23dc7958fe15 100644 --- a/polkadot/xcm/xcm-builder/src/currency_adapter.rs +++ b/polkadot/xcm/xcm-builder/src/currency_adapter.rs @@ -53,7 +53,7 @@ impl From for XcmError { /// use frame_support::{parameter_types, PalletId}; /// use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput}; /// use xcm::latest::prelude::*; -/// use xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete}; +/// use staging_xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete}; /// /// /// Our chain's account id. /// type AccountId = sp_runtime::AccountId32; diff --git a/polkadot/xcm/xcm-builder/src/matcher.rs b/polkadot/xcm/xcm-builder/src/matcher.rs index 4a3c896a159f..9da135dae31e 100644 --- a/polkadot/xcm/xcm-builder/src/matcher.rs +++ b/polkadot/xcm/xcm-builder/src/matcher.rs @@ -50,7 +50,7 @@ impl<'a, Call> CreateMatcher for &'a mut [Instruction] { /// ```rust /// use frame_support::traits::ProcessMessageError; /// use xcm::latest::Instruction; -/// use xcm_builder::{CreateMatcher, MatchXcm}; +/// use staging_xcm_builder::{CreateMatcher, MatchXcm}; /// /// let mut msg = [Instruction::<()>::ClearOrigin]; /// let res = msg diff --git a/polkadot/xcm/xcm-builder/src/matches_token.rs b/polkadot/xcm/xcm-builder/src/matches_token.rs index ddb25799be62..b6a320d89316 100644 --- a/polkadot/xcm/xcm-builder/src/matches_token.rs +++ b/polkadot/xcm/xcm-builder/src/matches_token.rs @@ -33,7 +33,7 @@ use xcm_executor::traits::{MatchesFungible, MatchesNonFungible}; /// /// ``` /// use xcm::latest::{MultiLocation, Parent}; -/// use xcm_builder::IsConcrete; +/// use staging_xcm_builder::IsConcrete; /// use xcm_executor::traits::MatchesFungible; /// /// frame_support::parameter_types! { @@ -71,7 +71,7 @@ impl, I: TryFrom> MatchesNonFungible for /// /// ``` /// use xcm::latest::prelude::*; -/// use xcm_builder::IsAbstract; +/// use staging_xcm_builder::IsAbstract; /// use xcm_executor::traits::{MatchesFungible, MatchesNonFungible}; /// /// frame_support::parameter_types! { diff --git a/polkadot/xcm/xcm-executor/src/assets.rs b/polkadot/xcm/xcm-executor/src/assets.rs index d8d8936df331..33f2ff218c73 100644 --- a/polkadot/xcm/xcm-executor/src/assets.rs +++ b/polkadot/xcm/xcm-executor/src/assets.rs @@ -446,7 +446,7 @@ impl Assets { /// Example: /// /// ``` - /// use xcm_executor::Assets; + /// use staging_xcm_executor::Assets; /// use xcm::latest::prelude::*; /// let assets_i_have: Assets = vec![ (Here, 100).into(), ([0; 32], 100).into() ].into(); /// let assets_they_want: MultiAssetFilter = vec![ (Here, 200).into(), ([0; 32], 50).into() ].into(); diff --git a/polkadot/xcm/xcm-executor/src/traits/conversion.rs b/polkadot/xcm/xcm-executor/src/traits/conversion.rs index dac099ffaf8e..1fcdf2140578 100644 --- a/polkadot/xcm/xcm-executor/src/traits/conversion.rs +++ b/polkadot/xcm/xcm-executor/src/traits/conversion.rs @@ -46,7 +46,7 @@ impl ConvertLocation for Tuple { /// /// ```rust /// # use xcm::latest::{MultiLocation, Junctions, Junction, OriginKind}; -/// # use xcm_executor::traits::ConvertOrigin; +/// # use staging_xcm_executor::traits::ConvertOrigin; /// // A convertor that will bump the para id and pass it to the next one. /// struct BumpParaId; /// impl ConvertOrigin for BumpParaId { From 10349f31739f7a0a576a351431ac5c79e8bd39d2 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 30 Aug 2023 16:56:27 -0300 Subject: [PATCH 3/6] Fix manifest formatting --- polkadot/xcm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/xcm/Cargo.toml b/polkadot/xcm/Cargo.toml index d113848adaf4..f04f31c3ec10 100644 --- a/polkadot/xcm/Cargo.toml +++ b/polkadot/xcm/Cargo.toml @@ -28,8 +28,8 @@ default = [ "std" ] wasm-api = [] std = [ "bounded-collections/std", - "parity-scale-codec/std", "environmental/std", + "parity-scale-codec/std", "scale-info/std", "serde/std", "sp-weights/std", From 12d1415ddd59bb2b1a1fab88e37aaf7e826d6494 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Wed, 30 Aug 2023 20:02:05 +0000 Subject: [PATCH 4/6] ".git/.scripts/commands/fmt/fmt.sh" --- polkadot/xcm/src/v3/mod.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/polkadot/xcm/src/v3/mod.rs b/polkadot/xcm/src/v3/mod.rs index 5d0a8efbdc0e..dc5de96a93e1 100644 --- a/polkadot/xcm/src/v3/mod.rs +++ b/polkadot/xcm/src/v3/mod.rs @@ -30,8 +30,8 @@ use core::{ }; use derivative::Derivative; use parity_scale_codec::{ - self, Decode, Encode, Error as CodecError, Input as CodecInput, MaxEncodedLen, - Compact, decode_vec_with_len, + self, decode_vec_with_len, Compact, Decode, Encode, Error as CodecError, Input as CodecInput, + MaxEncodedLen, }; use scale_info::TypeInfo; @@ -81,10 +81,11 @@ impl Decode for Xcm { instructions_count::with(|count| { *count = count.saturating_add(number_of_instructions as u8); if *count > MAX_INSTRUCTIONS_TO_DECODE { - return Err(CodecError::from("Max instructions exceeded")); + return Err(CodecError::from("Max instructions exceeded")) } Ok(()) - }).unwrap_or(Ok(()))?; + }) + .unwrap_or(Ok(()))?; let decoded_instructions = decode_vec_with_len(input, number_of_instructions as usize)?; Ok(Self(decoded_instructions)) }) @@ -1463,11 +1464,14 @@ mod tests { let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); assert!(matches!(decoded_xcm, Err(CodecError { .. }))); - let nested_xcm = Xcm::<()>(vec![DepositReserveAsset { - assets: All.into(), - dest: Here.into(), - xcm: max_xcm, - }; (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize]); + let nested_xcm = Xcm::<()>(vec![ + DepositReserveAsset { + assets: All.into(), + dest: Here.into(), + xcm: max_xcm, + }; + (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize + ]); let bytes = nested_xcm.encode(); let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); assert!(matches!(decoded_xcm, Err(CodecError { .. }))); From 0a008634cfa83bb7b8a635c88fab9e55d375fa5d Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Wed, 30 Aug 2023 18:06:47 -0300 Subject: [PATCH 5/6] Add one more test --- polkadot/xcm/src/v3/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/polkadot/xcm/src/v3/mod.rs b/polkadot/xcm/src/v3/mod.rs index dc5de96a93e1..2049c8ed2a79 100644 --- a/polkadot/xcm/src/v3/mod.rs +++ b/polkadot/xcm/src/v3/mod.rs @@ -1475,5 +1475,17 @@ mod tests { let bytes = nested_xcm.encode(); let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); assert!(matches!(decoded_xcm, Err(CodecError { .. }))); + + let even_more_nested_xcm = Xcm::<()>(vec![ + DepositReserveAsset { + assets: All.into(), + dest: Here.into(), + xcm: nested_xcm, + }; + (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize + ]); + let bytes = even_more_nested_xcm.encode(); + let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); + assert!(matches!(decoded_xcm, Err(CodecError { .. }))); } } From 5dc15fd6e7c4f5b2084af47531a5414f45140982 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Thu, 31 Aug 2023 12:00:12 -0300 Subject: [PATCH 6/6] Add SetAppendix in test --- polkadot/xcm/src/v3/mod.rs | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/polkadot/xcm/src/v3/mod.rs b/polkadot/xcm/src/v3/mod.rs index 2049c8ed2a79..4ce1c83ab3dc 100644 --- a/polkadot/xcm/src/v3/mod.rs +++ b/polkadot/xcm/src/v3/mod.rs @@ -1453,16 +1453,14 @@ mod tests { } #[test] - fn decoding_limits() { + fn decoding_respects_limit() { let max_xcm = Xcm::<()>(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize]); - let bytes = max_xcm.encode(); - let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); - assert!(matches!(decoded_xcm, Ok(_))); + let encoded = max_xcm.encode(); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_ok()); let big_xcm = Xcm::<()>(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize + 1]); - let bytes = big_xcm.encode(); - let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); - assert!(matches!(decoded_xcm, Err(CodecError { .. }))); + let encoded = big_xcm.encode(); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_err()); let nested_xcm = Xcm::<()>(vec![ DepositReserveAsset { @@ -1472,20 +1470,14 @@ mod tests { }; (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize ]); - let bytes = nested_xcm.encode(); - let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); - assert!(matches!(decoded_xcm, Err(CodecError { .. }))); - - let even_more_nested_xcm = Xcm::<()>(vec![ - DepositReserveAsset { - assets: All.into(), - dest: Here.into(), - xcm: nested_xcm, - }; - (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize - ]); - let bytes = even_more_nested_xcm.encode(); - let decoded_xcm = Xcm::<()>::decode(&mut &bytes[..]); - assert!(matches!(decoded_xcm, Err(CodecError { .. }))); + let encoded = nested_xcm.encode(); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_err()); + + let even_more_nested_xcm = Xcm::<()>(vec![SetAppendix(nested_xcm); 64]); + let encoded = even_more_nested_xcm.encode(); + assert_eq!(encoded.len(), 342530); + // This should not decode since the limit is 100 + assert_eq!(MAX_INSTRUCTIONS_TO_DECODE, 100, "precondition"); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_err()); } }