Skip to content

Commit

Permalink
derive DecodeWithMemTracking for RuntimeCall (#7634)
Browse files Browse the repository at this point in the history
Related to #7360
  • Loading branch information
serban300 authored Feb 20, 2025
1 parent f241439 commit e8d17cb
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use super::{weights, AccountId, Balance, Balances, BlockNumber, Runtime, RuntimeEvent};
use bp_messages::LegacyLaneId;
use bp_relayers::RewardsAccountParams;
use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::parameter_types;
use scale_info::TypeInfo;
use xcm::VersionedLocation;
Expand All @@ -36,7 +36,18 @@ parameter_types! {
}

/// Showcasing that we can handle multiple different rewards with the same pallet.
#[derive(Clone, Copy, Debug, Decode, Encode, Eq, MaxEncodedLen, PartialEq, TypeInfo)]
#[derive(
Clone,
Copy,
Debug,
Decode,
DecodeWithMemTracking,
Encode,
Eq,
MaxEncodedLen,
PartialEq,
TypeInfo,
)]
pub enum BridgeReward {
/// Rewards for the R/W bridge—distinguished by the `RewardsAccountParams` key.
RococoWestend(RewardsAccountParams<LegacyLaneId>),
Expand All @@ -51,7 +62,9 @@ impl From<RewardsAccountParams<LegacyLaneId>> for BridgeReward {
}

/// An enum representing the different types of supported beneficiaries.
#[derive(Clone, Debug, Decode, Encode, Eq, MaxEncodedLen, PartialEq, TypeInfo)]
#[derive(
Clone, Debug, Decode, DecodeWithMemTracking, Encode, Eq, MaxEncodedLen, PartialEq, TypeInfo,
)]
pub enum BridgeRewardBeneficiaries {
/// A local chain account.
LocalAccount(AccountId),
Expand Down
16 changes: 16 additions & 0 deletions prdoc/pr_7634.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: derive `DecodeWithMemTracking` for `RuntimeCall`

doc:
- audience: Runtime Dev
description: |
This PR derives `DecodeWithMemTracking` for `RuntimeCall`.
All the types used in the `RuntimeCall` should implement `DecodeWithMemTracking` as well.

crates:
- name: frame-support-procedural
bump: major
- name: bridge-hub-westend-runtime
bump: none
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn expand_outer_dispatch(
Clone, PartialEq, Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ pub fn expand_outer_freeze_reason(pallet_decls: &[Pallet], scrate: &TokenStream)
/// A reason for placing a freeze on funds.
#[derive(
Copy, Clone, Eq, PartialEq,
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ pub fn expand_outer_hold_reason(pallet_decls: &[Pallet], scrate: &TokenStream) -
/// A reason for placing a hold on funds.
#[derive(
Copy, Clone, Eq, PartialEq,
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ pub fn expand_outer_lock_id(pallet_decls: &[Pallet], scrate: &TokenStream) -> To
/// An identifier for each lock placed on funds.
#[derive(
Copy, Clone, Eq, PartialEq,
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ pub fn expand_outer_origin(
}

#[derive(
Clone, PartialEq, Eq, #scrate::__private::RuntimeDebug, #scrate::__private::codec::Encode,
#scrate::__private::codec::Decode, #scrate::__private::scale_info::TypeInfo, #scrate::__private::codec::MaxEncodedLen,
Clone, PartialEq, Eq,
#scrate::__private::RuntimeDebug,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::codec::MaxEncodedLen,
)]
#[allow(non_camel_case_types)]
pub enum OriginCaller {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub fn expand_outer_enum(
#event_custom_derives
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::Debug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ pub fn expand_outer_slash_reason(pallet_decls: &[Pallet], scrate: &TokenStream)
/// A reason for slashing funds.
#[derive(
Copy, Clone, Eq, PartialEq,
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn expand_outer_task(
Clone, Eq, PartialEq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn expand_outer_query(
Clone, PartialEq, Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
7 changes: 7 additions & 0 deletions substrate/frame/support/procedural/src/dynamic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ impl ToTokens for DynamicPalletParamAttr {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand All @@ -284,6 +285,7 @@ impl ToTokens for DynamicPalletParamAttr {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand All @@ -302,6 +304,7 @@ impl ToTokens for DynamicPalletParamAttr {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand Down Expand Up @@ -336,6 +339,7 @@ impl ToTokens for DynamicPalletParamAttr {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand Down Expand Up @@ -473,6 +477,7 @@ impl ToTokens for DynamicParamAggregatedEnum {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::sp_runtime::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand All @@ -492,6 +497,7 @@ impl ToTokens for DynamicParamAggregatedEnum {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::sp_runtime::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand All @@ -510,6 +516,7 @@ impl ToTokens for DynamicParamAggregatedEnum {
Eq,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::sp_runtime::RuntimeDebug,
#scrate::__private::scale_info::TypeInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
#frame_support::PartialEqNoBound,
#frame_support::__private::codec::Encode,
#frame_support::__private::codec::Decode,
#frame_support::__private::codec::DecodeWithMemTracking,
#frame_support::__private::scale_info::TypeInfo,
)]
#[codec(encode_bound())]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub fn expand_error(def: &mut Def) -> proc_macro2::TokenStream {
#[derive(
#frame_support::__private::codec::Encode,
#frame_support::__private::codec::Decode,
#frame_support::__private::codec::DecodeWithMemTracking,
#frame_support::__private::scale_info::TypeInfo,
#frame_support::PalletError,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub fn expand_event(def: &mut Def) -> proc_macro2::TokenStream {
#frame_support::DebugNoBound,
#frame_support::__private::codec::Encode,
#frame_support::__private::codec::Decode,
#frame_support::__private::codec::DecodeWithMemTracking,
#frame_support::__private::scale_info::TypeInfo,
)]
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl TaskEnumDef {
#frame_support::PartialEqNoBound,
#frame_support::pallet_prelude::Encode,
#frame_support::pallet_prelude::Decode,
#frame_support::pallet_prelude::DecodeWithMemTracking,
#frame_support::pallet_prelude::TypeInfo,
)]
#[codec(encode_bound())]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn expand_view_function(
#frame_support::PartialEqNoBound,
#frame_support::__private::codec::Encode,
#frame_support::__private::codec::Decode,
#frame_support::__private::codec::DecodeWithMemTracking,
#frame_support::__private::scale_info::TypeInfo,
)]
#[codec(encode_bound())]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ impl CompositeDef {
let derive_attr: syn::Attribute = syn::parse_quote! {
#[derive(
Copy, Clone, Eq, PartialEq,
#scrate::__private::codec::Encode, #scrate::__private::codec::Decode, #scrate::__private::codec::MaxEncodedLen,
#scrate::__private::codec::Encode,
#scrate::__private::codec::Decode,
#scrate::__private::codec::DecodeWithMemTracking,
#scrate::__private::codec::MaxEncodedLen,
#scrate::__private::scale_info::TypeInfo,
#scrate::__private::RuntimeDebug,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,46 @@ mod pallet {
}
}

#[derive(scale_info::TypeInfo, frame_support::PalletError, codec::Encode, codec::Decode)]
#[derive(
scale_info::TypeInfo,
frame_support::PalletError,
codec::Encode,
codec::Decode,
codec::DecodeWithMemTracking,
)]
pub enum Nested1 {
Nested2(Nested2),
}

#[derive(scale_info::TypeInfo, frame_support::PalletError, codec::Encode, codec::Decode)]
#[derive(
scale_info::TypeInfo,
frame_support::PalletError,
codec::Encode,
codec::Decode,
codec::DecodeWithMemTracking,
)]
pub enum Nested2 {
Nested3(Nested3),
}

#[derive(scale_info::TypeInfo, frame_support::PalletError, codec::Encode, codec::Decode)]
#[derive(
scale_info::TypeInfo,
frame_support::PalletError,
codec::Encode,
codec::Decode,
codec::DecodeWithMemTracking,
)]
pub enum Nested3 {
Nested4(Nested4),
}

#[derive(scale_info::TypeInfo, frame_support::PalletError, codec::Encode, codec::Decode)]
#[derive(
scale_info::TypeInfo,
frame_support::PalletError,
codec::Encode,
codec::Decode,
codec::DecodeWithMemTracking,
)]
pub enum Nested4 {
Num(u8),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0080]: evaluation of constant value failed
--> tests/construct_runtime_ui/pallet_error_too_large.rs:91:1
|
91 | / construct_runtime! {
92 | | pub struct Runtime
93 | | {
94 | | System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>},
95 | | Pallet: pallet::{Pallet},
96 | | }
97 | | }
| |_^ the evaluated program panicked at 'The maximum encoded size of the error type in the `Pallet` pallet exceeds `MAX_MODULE_ERROR_ENCODED_SIZE`', $DIR/tests/construct_runtime_ui/pallet_error_too_large.rs:91:1
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `frame_support::construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
--> tests/construct_runtime_ui/pallet_error_too_large.rs:115:1
|
115 | / construct_runtime! {
116 | | pub struct Runtime
117 | | {
118 | | System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>},
119 | | Pallet: pallet::{Pallet},
120 | | }
121 | | }
| |_^ the evaluated program panicked at 'The maximum encoded size of the error type in the `Pallet` pallet exceeds `MAX_MODULE_ERROR_ENCODED_SIZE`', $DIR/tests/construct_runtime_ui/pallet_error_too_large.rs:115:1
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `frame_support::construct_runtime` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#[frame_support::pallet]
mod pallet {
use codec::{Decode, Encode};
use codec::{Decode, DecodeWithMemTracking, Encode};
use frame_support::pallet_prelude::{DispatchResultWithPostInfo, Hooks};
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor};

Expand All @@ -30,7 +30,7 @@ mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[derive(Encode, Decode, scale_info::TypeInfo, PartialEq, Clone)]
#[derive(Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Clone)]
struct Bar;

#[pallet::call]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod pallet {
}
}

#[derive(scale_info::TypeInfo, codec::Encode, codec::Decode)]
#[derive(scale_info::TypeInfo, codec::Encode, codec::Decode, codec::DecodeWithMemTracking)]
enum MyError {}

fn main() {}
Loading

0 comments on commit e8d17cb

Please sign in to comment.