Use DecodeLimit for decoding XCM messages (#605)

* Use DecodeLimit for decoding XCM messages

* Use decode_all_with_depth_limit in appropriate places

* Use decode_all while decoding byte vector

* cargo fmt
This commit is contained in:
Keith Yeung
2021-09-19 05:29:34 -07:00
committed by GitHub
parent 85bde07641
commit 1aab3349cc
34 changed files with 82 additions and 61 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", default-features
sp-application-crypto = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
# Other Dependencies
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"]}
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"]}
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
+1 -1
View File
@@ -14,7 +14,7 @@ targets = ['x86_64-unknown-linux-gnu']
[dependencies]
log = { version = "0.4.0", default-features = false }
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.0.0' }
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.3.0' }
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.119", default-features = false }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master" }
+1 -1
View File
@@ -6,7 +6,7 @@ edition = "2018"
[dependencies]
# Other dependencies
codec = { package = "parity-scale-codec", version = "2.0.0", features = [ "derive" ], default-features = false }
codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ], default-features = false }
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
+7 -4
View File
@@ -21,7 +21,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use codec::{Decode, DecodeLimit, Encode};
use cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler};
use frame_support::{
dispatch::Weight, traits::EnsureOrigin, weights::constants::WEIGHT_PER_MILLIS,
@@ -30,7 +30,7 @@ pub use pallet::*;
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
use sp_std::{convert::TryFrom, prelude::*};
use xcm::{latest::prelude::*, VersionedXcm};
use xcm::{latest::prelude::*, VersionedXcm, MAX_XCM_DECODE_DEPTH};
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct ConfigData {
@@ -225,8 +225,11 @@ pub mod pallet {
data: &[u8],
) -> Result<Weight, (MessageId, Weight)> {
let id = sp_io::hashing::blake2_256(&data[..]);
let maybe_msg =
VersionedXcm::<T::Call>::decode(&mut &data[..]).map(Xcm::<T::Call>::try_from);
let maybe_msg = VersionedXcm::<T::Call>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &data[..],
)
.map(Xcm::<T::Call>::try_from);
match maybe_msg {
Err(_) => {
Self::deposit_event(Event::InvalidFormat(id));
+1 -1
View File
@@ -30,7 +30,7 @@ sp-trie = { git = "https://github.com/paritytech/substrate", default-features =
sp-externalities = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
# Other Dependencies
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"]}
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"]}
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
+1 -1
View File
@@ -5,7 +5,7 @@ name = "cumulus-pallet-xcm"
version = "0.1.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
+12 -4
View File
@@ -20,7 +20,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use codec::{Decode, DecodeLimit, Encode};
use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler, ParaId,
};
@@ -31,7 +31,7 @@ use sp_runtime::traits::BadOrigin;
use sp_std::{convert::TryFrom, prelude::*};
use xcm::{
latest::{ExecuteXcm, Outcome, Parent, Xcm},
VersionedXcm,
VersionedXcm, MAX_XCM_DECODE_DEPTH,
};
#[frame_support::pallet]
@@ -115,7 +115,11 @@ impl<T: Config> DmpMessageHandler for UnlimitedDmpExecution<T> {
let mut used = 0;
for (_sent_at, data) in iter {
let id = sp_io::hashing::twox_64(&data[..]);
let msg = VersionedXcm::<T::Call>::decode(&mut &data[..]).map(Xcm::<T::Call>::try_from);
let msg = VersionedXcm::<T::Call>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &data[..],
)
.map(Xcm::<T::Call>::try_from);
match msg {
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
@@ -144,7 +148,11 @@ impl<T: Config> DmpMessageHandler for LimitAndDropDmpExecution<T> {
let mut used = 0;
for (_sent_at, data) in iter {
let id = sp_io::hashing::twox_64(&data[..]);
let msg = VersionedXcm::<T::Call>::decode(&mut &data[..]).map(Xcm::<T::Call>::try_from);
let msg = VersionedXcm::<T::Call>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &data[..],
)
.map(Xcm::<T::Call>::try_from);
match msg {
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
+2 -2
View File
@@ -6,10 +6,10 @@ edition = "2018"
[dependencies]
# Other dependencies
codec = { package = "parity-scale-codec", version = "2.0.0", features = [ "derive" ], default-features = false }
codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ], default-features = false }
rand_chacha = { version = "0.3.0", default-features = false }
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4.14", default-features = false }
rand_chacha = { version = "0.3.0", default-features = false }
# Substrate Dependencies
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
+16 -6
View File
@@ -31,7 +31,7 @@ mod mock;
#[cfg(test)]
mod tests;
use codec::{Decode, Encode};
use codec::{Decode, DecodeAll, DecodeLimit, Encode};
use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, ChannelStatus, GetChannelInfo, MessageSendError,
ParaId, XcmpMessageFormat, XcmpMessageHandler, XcmpMessageSource,
@@ -44,7 +44,7 @@ use rand_chacha::{
use scale_info::TypeInfo;
use sp_runtime::{traits::Hash, RuntimeDebug};
use sp_std::{convert::TryFrom, prelude::*};
use xcm::{latest::prelude::*, VersionedXcm, WrapVersion};
use xcm::{latest::prelude::*, VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH};
pub use pallet::*;
@@ -249,7 +249,11 @@ impl<T: Config> Pallet<T> {
let have_active = s[index].4 > s[index].3;
let appended = have_active &&
<OutboundXcmpMessages<T>>::mutate(recipient, s[index].4 - 1, |s| {
if XcmpMessageFormat::decode(&mut &s[..]) != Ok(format) {
if XcmpMessageFormat::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &s[..],
) != Ok(format)
{
return false
}
if s.len() + data.len() > max_message_size {
@@ -371,7 +375,10 @@ impl<T: Config> Pallet<T> {
XcmpMessageFormat::ConcatenatedVersionedXcm => {
while !remaining_fragments.is_empty() {
last_remaining_fragments = remaining_fragments;
if let Ok(xcm) = VersionedXcm::<T::Call>::decode(&mut remaining_fragments) {
if let Ok(xcm) = VersionedXcm::<T::Call>::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut remaining_fragments,
) {
let weight = max_weight - weight_used;
match Self::handle_xcm_message(sender, sent_at, xcm, weight) {
Ok(used) => weight_used = weight_used.saturating_add(used),
@@ -394,7 +401,7 @@ impl<T: Config> Pallet<T> {
XcmpMessageFormat::ConcatenatedEncodedBlob => {
while !remaining_fragments.is_empty() {
last_remaining_fragments = remaining_fragments;
if let Ok(blob) = <Vec<u8>>::decode(&mut remaining_fragments) {
if let Ok(blob) = <Vec<u8>>::decode_all(&mut remaining_fragments) {
let weight = max_weight - weight_used;
match Self::handle_blob_message(sender, sent_at, blob, weight) {
Ok(used) => weight_used = weight_used.saturating_add(used),
@@ -588,7 +595,10 @@ impl<T: Config> XcmpMessageHandler for Pallet<T> {
for (sender, sent_at, data) in iter {
// Figure out the message format.
let mut data_ref = data;
let format = match XcmpMessageFormat::decode(&mut data_ref) {
let format = match XcmpMessageFormat::decode_and_advance_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut data_ref,
) {
Ok(f) => f,
Err(_) => {
debug_assert!(false, "Unknown XCMP message format. Silently dropping message");