From 2f0e5a61b7739ff0f41ba124653b45ce269dee7e Mon Sep 17 00:00:00 2001 From: Alin Dima Date: Tue, 9 Jul 2024 14:36:07 +0300 Subject: [PATCH 1/2] add notices to the implementer's guide docs that changed for elastic scaling (#4983) The update is tracked by: https://github.com/paritytech/polkadot-sdk/issues/3699 However, this is not worth doing at this point since it will change in the future for phase 2 of the implementation. Still, it's useful to let people know that the information is not the most up to date. --- .../implementers-guide/src/node/backing/candidate-backing.md | 4 ++++ .../src/node/backing/prospective-parachains.md | 4 ++++ .../src/node/collators/collator-protocol.md | 4 ++++ .../implementers-guide/src/node/utility/provisioner.md | 4 ++++ polkadot/roadmap/implementers-guide/src/runtime/inclusion.md | 4 ++++ .../roadmap/implementers-guide/src/runtime/parainherent.md | 4 ++++ 6 files changed, 24 insertions(+) diff --git a/polkadot/roadmap/implementers-guide/src/node/backing/candidate-backing.md b/polkadot/roadmap/implementers-guide/src/node/backing/candidate-backing.md index 31f8423fe27b2..0e483f02ec312 100644 --- a/polkadot/roadmap/implementers-guide/src/node/backing/candidate-backing.md +++ b/polkadot/roadmap/implementers-guide/src/node/backing/candidate-backing.md @@ -1,5 +1,9 @@ # Candidate Backing +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + The Candidate Backing subsystem ensures every parablock considered for relay block inclusion has been seconded by at least one validator, and approved by a quorum. Parablocks for which not enough validators will assert correctness are discarded. If the block later proves invalid, the initial backers are slashable; this gives Polkadot a rational threat diff --git a/polkadot/roadmap/implementers-guide/src/node/backing/prospective-parachains.md b/polkadot/roadmap/implementers-guide/src/node/backing/prospective-parachains.md index 701f6c87caff0..61278621cf565 100644 --- a/polkadot/roadmap/implementers-guide/src/node/backing/prospective-parachains.md +++ b/polkadot/roadmap/implementers-guide/src/node/backing/prospective-parachains.md @@ -1,5 +1,9 @@ # Prospective Parachains +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + ## Overview **Purpose:** Tracks and handles prospective parachain fragments and informs diff --git a/polkadot/roadmap/implementers-guide/src/node/collators/collator-protocol.md b/polkadot/roadmap/implementers-guide/src/node/collators/collator-protocol.md index 1fed671170c7c..432d9ab69bab9 100644 --- a/polkadot/roadmap/implementers-guide/src/node/collators/collator-protocol.md +++ b/polkadot/roadmap/implementers-guide/src/node/collators/collator-protocol.md @@ -1,5 +1,9 @@ # Collator Protocol +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + The Collator Protocol implements the network protocol by which collators and validators communicate. It is used by collators to distribute collations to validators and used by validators to accept collations by collators. diff --git a/polkadot/roadmap/implementers-guide/src/node/utility/provisioner.md b/polkadot/roadmap/implementers-guide/src/node/utility/provisioner.md index b017259da8c08..64727d39fabe0 100644 --- a/polkadot/roadmap/implementers-guide/src/node/utility/provisioner.md +++ b/polkadot/roadmap/implementers-guide/src/node/utility/provisioner.md @@ -1,5 +1,9 @@ # Provisioner +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + Relay chain block authorship authority is governed by BABE and is beyond the scope of the Overseer and the rest of the subsystems. That said, ultimately the block author needs to select a set of backable parachain candidates and other consensus data, and assemble a block from them. This subsystem is responsible for providing the necessary data to all diff --git a/polkadot/roadmap/implementers-guide/src/runtime/inclusion.md b/polkadot/roadmap/implementers-guide/src/runtime/inclusion.md index 0700a781d4263..5031433cf5a1d 100644 --- a/polkadot/roadmap/implementers-guide/src/runtime/inclusion.md +++ b/polkadot/roadmap/implementers-guide/src/runtime/inclusion.md @@ -1,5 +1,9 @@ # Inclusion Pallet +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + The inclusion module is responsible for inclusion and availability of scheduled parachains. It also manages the UMP dispatch queue of each parachain. diff --git a/polkadot/roadmap/implementers-guide/src/runtime/parainherent.md b/polkadot/roadmap/implementers-guide/src/runtime/parainherent.md index 7972c706b9ee1..f21e1a59c1a4c 100644 --- a/polkadot/roadmap/implementers-guide/src/runtime/parainherent.md +++ b/polkadot/roadmap/implementers-guide/src/runtime/parainherent.md @@ -1,5 +1,9 @@ # `ParaInherent` +> NOTE: This module has suffered changes for the elastic scaling implementation. As a result, parts of this document may +be out of date and will be updated at a later time. Issue tracking the update: +https://github.com/paritytech/polkadot-sdk/issues/3699 + This module is responsible for providing all data given to the runtime by the block author to the various parachains modules. The entry-point is mandatory, in that it must be invoked exactly once within every block, and it is also "inherent", in that it is provided with no origin by the block author. The data within it carries its own From 9403a5d40214b0d223c87c8d7b13139672edfe95 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Tue, 9 Jul 2024 15:49:01 +0200 Subject: [PATCH 2/2] Add `MAX_INSTRUCTIONS_TO_DECODE` to XCMv2 (#4978) It was added to v4 and v3 but was missing from v2 --- polkadot/xcm/src/v2/mod.rs | 67 ++++++++++++++++++++++++++++++++++++-- prdoc/pr_4978.prdoc | 18 ++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 prdoc/pr_4978.prdoc diff --git a/polkadot/xcm/src/v2/mod.rs b/polkadot/xcm/src/v2/mod.rs index fe7f05dd887cf..1afc120f500c6 100644 --- a/polkadot/xcm/src/v2/mod.rs +++ b/polkadot/xcm/src/v2/mod.rs @@ -62,7 +62,10 @@ use super::{ }; use alloc::{vec, vec::Vec}; use bounded_collections::{ConstU32, WeakBoundedVec}; -use codec::{self, Decode, Encode, MaxEncodedLen}; +use codec::{ + self, decode_vec_with_len, Compact, Decode, Encode, Error as CodecError, Input as CodecInput, + MaxEncodedLen, +}; use core::{fmt::Debug, result}; use derivative::Derivative; use scale_info::TypeInfo; @@ -278,7 +281,7 @@ pub const VERSION: super::Version = 2; pub type QueryId = u64; /// DEPRECATED. Please use XCMv3 or XCMv4 instead. -#[derive(Derivative, Default, Encode, Decode, TypeInfo)] +#[derive(Derivative, Default, Encode, TypeInfo)] #[derivative(Clone(bound = ""), Eq(bound = ""), PartialEq(bound = ""), Debug(bound = ""))] #[codec(encode_bound())] #[codec(decode_bound())] @@ -286,6 +289,31 @@ pub type QueryId = u64; #[scale_info(replace_segment("staging_xcm", "xcm"))] pub struct Xcm(pub Vec>); +environmental::environmental!(instructions_count: u8); + +impl Decode for Xcm { + fn decode(input: &mut I) -> core::result::Result { + 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)) + }) + } +} + +/// The maximal number of instructions in an XCM before decoding fails. +/// +/// This is a deliberate limit - not a technical one. +pub const MAX_INSTRUCTIONS_TO_DECODE: u8 = 100; + impl Xcm { /// Create an empty instance. pub fn new() -> Self { @@ -1157,3 +1185,38 @@ impl TryFrom> for Instruction(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize]); + 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 encoded = big_xcm.encode(); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_err()); + + let nested_xcm = Xcm::<()>(vec![ + DepositReserveAsset { + assets: All.into(), + dest: Here.into(), + xcm: max_xcm, + max_assets: 1, + }; + (MAX_INSTRUCTIONS_TO_DECODE / 2) as usize + ]); + 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(), 345730); + // This should not decode since the limit is 100 + assert_eq!(MAX_INSTRUCTIONS_TO_DECODE, 100, "precondition"); + assert!(Xcm::<()>::decode(&mut &encoded[..]).is_err()); + } +} diff --git a/prdoc/pr_4978.prdoc b/prdoc/pr_4978.prdoc new file mode 100644 index 0000000000000..1f86d512f2c78 --- /dev/null +++ b/prdoc/pr_4978.prdoc @@ -0,0 +1,18 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Add MAX_INSTRUCTIONS_TO_DECODE to XCMv2 + +doc: + - audience: Runtime User + description: | + Added a max number of instructions to XCMv2. If using XCMv2, you'll have to take this limit into account. + It was set to 100. + - audience: Runtime Dev + description: | + Added a max number of instructions to XCMv2. If using XCMv2, you'll have to take this limit into account. + It was set to 100. + +crates: + - name: staging-xcm + bump: minor