Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive DecodeWithMemTracking for bridge and xcm pallets #7620

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bridges/modules/messages/src/lanes_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ use bp_messages::{
MessageNonce, OutboundLaneData,
};
use bp_runtime::AccountIdOf;
use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::{ensure, sp_runtime::RuntimeDebug, PalletError};
use scale_info::TypeInfo;
use sp_std::marker::PhantomData;

/// Lanes manager errors.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo)]
#[derive(
Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo,
)]
pub enum LanesManagerError {
/// Inbound lane already exists.
InboundLaneAlreadyExists,
Expand Down
6 changes: 4 additions & 2 deletions bridges/modules/messages/src/outbound_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bp_messages::{
ChainWithMessages, DeliveredMessages, LaneState, MessageNonce, OutboundLaneData,
UnrewardedRelayer,
};
use codec::{Decode, Encode};
use codec::{Decode, DecodeWithMemTracking, Encode};
use frame_support::{traits::Get, BoundedVec, PalletError};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
Expand Down Expand Up @@ -65,7 +65,9 @@ impl<T: Config<I>, I: 'static> Get<u32> for StoredMessagePayloadLimit<T, I> {
pub type StoredMessagePayload<T, I> = BoundedVec<u8, StoredMessagePayloadLimit<T, I>>;

/// Result of messages receival confirmation.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo)]
#[derive(
Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo,
)]
pub enum ReceptionConfirmationError {
/// Bridged chain is trying to confirm more messages than we have generated. May be a result
/// of invalid bridged chain storage.
Expand Down
13 changes: 11 additions & 2 deletions bridges/modules/xcm-bridge-hub/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{Config, Pallet, LOG_TARGET};
use bp_messages::target_chain::{DispatchMessage, MessageDispatch};
use bp_runtime::messages::MessageDispatchResult;
use bp_xcm_bridge_hub::{LocalXcmChannelManager, XcmAsPlainPayload};
use codec::{Decode, Encode};
use codec::{Decode, DecodeWithMemTracking, Encode};
use frame_support::{weights::Weight, CloneNoBound, EqNoBound, PartialEqNoBound};
use pallet_bridge_messages::{Config as BridgeMessagesConfig, WeightInfoExt};
use scale_info::TypeInfo;
Expand All @@ -35,7 +35,16 @@ use xcm::prelude::*;
use xcm_builder::{DispatchBlob, DispatchBlobError};

/// Message dispatch result type for single message.
#[derive(CloneNoBound, EqNoBound, PartialEqNoBound, Encode, Decode, Debug, TypeInfo)]
#[derive(
CloneNoBound,
EqNoBound,
PartialEqNoBound,
Encode,
Decode,
DecodeWithMemTracking,
Debug,
TypeInfo,
)]
pub enum XcmBlobMessageDispatchResult {
/// We've been unable to decode message payload.
InvalidPayload,
Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/header-chain/src/justification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use verification::{
};

use bp_runtime::{BlockNumberOf, Chain, HashOf, HeaderId};
use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_consensus_grandpa::{AuthorityId, AuthoritySignature};
use sp_runtime::{traits::Header as HeaderT, RuntimeDebug, SaturatedConversion};
Expand All @@ -42,7 +42,7 @@ use sp_std::prelude::*;
///
/// This particular proof is used to prove that headers on a bridged chain
/// (so not our chain) have been finalized correctly.
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
#[derive(Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct GrandpaJustification<Header: HeaderT> {
/// The round (voting period) this justification is valid for.
Expand Down
24 changes: 19 additions & 5 deletions bridges/primitives/header-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use bp_runtime::{
BasicOperatingMode, BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf, RawStorageProof,
StorageProofChecker, StorageProofError, UnderlyingChainProvider,
};
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
use codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug};
use frame_support::PalletError;
use scale_info::TypeInfo;
Expand All @@ -46,7 +46,9 @@ pub mod justification;
pub mod storage_keys;

/// Header chain error.
#[derive(Clone, Decode, Encode, Eq, PartialEq, PalletError, Debug, TypeInfo)]
#[derive(
Clone, Decode, DecodeWithMemTracking, Encode, Eq, PartialEq, PalletError, Debug, TypeInfo,
)]
pub enum HeaderChainError {
/// Header with given hash is missing from the chain.
UnknownHeader,
Expand Down Expand Up @@ -101,7 +103,9 @@ pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug + TypeInfo {}
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug + TypeInfo {}

/// A GRANDPA Authority List and ID.
#[derive(Default, Encode, Eq, Decode, RuntimeDebug, PartialEq, Clone, TypeInfo)]
#[derive(
Default, Encode, Eq, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Clone, TypeInfo,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct AuthoritySet {
/// List of GRANDPA authorities for the current round.
Expand All @@ -121,7 +125,17 @@ impl AuthoritySet {
///
/// The bridge needs to know where to start its sync from, and this provides that initial context.
#[derive(
Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, Clone, TypeInfo, Serialize, Deserialize,
Default,
Encode,
Decode,
DecodeWithMemTracking,
RuntimeDebug,
PartialEq,
Eq,
Clone,
TypeInfo,
Serialize,
Deserialize,
)]
pub struct InitializationData<H: HeaderT> {
/// The header from which we should start syncing.
Expand Down Expand Up @@ -189,7 +203,7 @@ impl<Number: Codec> ConsensusLogReader for GrandpaConsensusLogReader<Number> {
}

/// The finality-related info associated to a header.
#[derive(Encode, Decode, Debug, PartialEq, Clone, TypeInfo)]
#[derive(Encode, Decode, DecodeWithMemTracking, Debug, PartialEq, Clone, TypeInfo)]
pub struct HeaderFinalityInfo<FinalityProof, FinalityVerificationContext> {
/// The header finality proof.
pub finality_proof: FinalityProof,
Expand Down
4 changes: 3 additions & 1 deletion bridges/primitives/messages/src/lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Primitives of messages module, that represents lane id.

use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
use codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp_core::{RuntimeDebug, TypeId, H256};
Expand Down Expand Up @@ -50,6 +50,7 @@ pub trait LaneIdType:
Clone,
Copy,
Decode,
DecodeWithMemTracking,
Default,
Encode,
Eq,
Expand Down Expand Up @@ -121,6 +122,7 @@ impl TypeId for LegacyLaneId {
Clone,
Copy,
Decode,
DecodeWithMemTracking,
Default,
Encode,
Eq,
Expand Down
28 changes: 22 additions & 6 deletions bridges/primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use bp_runtime::{
messages::MessageDispatchResult, BasicOperatingMode, Chain, OperatingMode, RangeInclusiveExt,
StorageProofError, UnderlyingChainOf, UnderlyingChainProvider,
};
use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::PalletError;
// Weight is reexported to avoid additional frame-support dependencies in related crates.
pub use frame_support::weights::Weight;
Expand Down Expand Up @@ -135,6 +135,7 @@ where
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Clone,
Copy,
PartialEq,
Expand Down Expand Up @@ -336,7 +337,7 @@ pub struct UnrewardedRelayer<RelayerId> {
}

/// Received messages with their dispatch result.
#[derive(Clone, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)]
#[derive(Clone, Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct ReceivedMessages<DispatchLevelResult, LaneId> {
/// Id of the lane which is receiving messages.
pub lane: LaneId,
Expand All @@ -360,7 +361,7 @@ impl<DispatchLevelResult, LaneId> ReceivedMessages<DispatchLevelResult, LaneId>
}

/// Result of single message receival.
#[derive(RuntimeDebug, Encode, Decode, PartialEq, Eq, Clone, TypeInfo)]
#[derive(RuntimeDebug, Encode, Decode, DecodeWithMemTracking, PartialEq, Eq, Clone, TypeInfo)]
pub enum ReceptionResult<DispatchLevelResult> {
/// Message has been received and dispatched. Note that we don't care whether dispatch has
/// been successful or not - in both case message falls into this category.
Expand All @@ -376,7 +377,18 @@ pub enum ReceptionResult<DispatchLevelResult> {
}

/// Delivered messages with their dispatch result.
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
#[derive(
Clone,
Default,
Encode,
Decode,
DecodeWithMemTracking,
RuntimeDebug,
PartialEq,
Eq,
TypeInfo,
MaxEncodedLen,
)]
pub struct DeliveredMessages {
/// Nonce of the first message that has been delivered (inclusive).
pub begin: MessageNonce,
Expand Down Expand Up @@ -408,7 +420,9 @@ impl DeliveredMessages {
}

/// Gist of `InboundLaneData::relayers` field used by runtime APIs.
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)]
#[derive(
Clone, Default, Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Eq, TypeInfo,
)]
pub struct UnrewardedRelayersState {
/// Number of entries in the `InboundLaneData::relayers` set.
pub unrewarded_relayer_entries: MessageNonce,
Expand Down Expand Up @@ -512,7 +526,9 @@ where
}

/// Error that happens during message verification.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo)]
#[derive(
Encode, Decode, DecodeWithMemTracking, RuntimeDebug, PartialEq, Eq, PalletError, TypeInfo,
)]
pub enum VerificationError {
/// The message proof is empty.
EmptyMessageProof,
Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/messages/src/source_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{MessageNonce, UnrewardedRelayer};

use bp_runtime::{raw_storage_proof_size, RawStorageProof, Size};
use codec::{Decode, Encode};
use codec::{Decode, DecodeWithMemTracking, Encode};
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;
use sp_std::{
Expand All @@ -38,7 +38,7 @@ use sp_std::{
/// - storage proof of the inbound lane state;
///
/// - lane id.
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[derive(Clone, Decode, DecodeWithMemTracking, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct FromBridgedChainMessagesDeliveryProof<BridgedHeaderHash, LaneId> {
/// Hash of the bridge header the proof is for.
pub bridged_header_hash: BridgedHeaderHash,
Expand Down
4 changes: 2 additions & 2 deletions bridges/primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{Message, MessageKey, MessageNonce, MessagePayload, OutboundLaneData};

use bp_runtime::{messages::MessageDispatchResult, raw_storage_proof_size, RawStorageProof, Size};
use codec::{Decode, Encode, Error as CodecError};
use codec::{Decode, DecodeWithMemTracking, Encode, Error as CodecError};
use frame_support::weights::Weight;
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;
Expand All @@ -37,7 +37,7 @@ use sp_std::{fmt::Debug, marker::PhantomData, prelude::*};
/// - lane id;
///
/// - nonces (inclusive range) of messages which are included in this proof.
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[derive(Clone, Decode, DecodeWithMemTracking, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct FromBridgedChainMessagesProof<BridgedHeaderHash, Lane> {
/// Hash of the finalized bridged header the proof is for.
pub bridged_header_hash: BridgedHeaderHash,
Expand Down
5 changes: 3 additions & 2 deletions bridges/primitives/polkadot-core/src/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! be two versions of polkadot crates included in the runtime. Which is bad.

use bp_runtime::{raw_storage_proof_size, RawStorageProof, Size};
use codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use codec::{CompactAs, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::Hasher;
use sp_runtime::RuntimeDebug;
Expand All @@ -41,6 +41,7 @@ use serde::{Deserialize, Serialize};
CompactAs,
Copy,
Decode,
DecodeWithMemTracking,
Default,
Encode,
Eq,
Expand Down Expand Up @@ -85,7 +86,7 @@ pub type ParaHash = crate::Hash;
pub type ParaHasher = crate::Hasher;

/// Raw storage proof of parachain heads, stored in polkadot-like chain runtime.
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[derive(Clone, Decode, DecodeWithMemTracking, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct ParaHeadsProof {
/// Unverified storage proof of finalized parachain heads.
pub storage_proof: RawStorageProof,
Expand Down
28 changes: 25 additions & 3 deletions bridges/primitives/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bp_runtime::{ChainId, StorageDoubleMapKeyProvider};
use frame_support::{traits::tokens::Preservation, Blake2_128Concat, Identity};
use scale_info::TypeInfo;
use sp_runtime::{
codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen},
codec::{Codec, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen},
traits::AccountIdConversion,
TypeId,
};
Expand All @@ -42,7 +42,18 @@ mod registration;
///
/// Each of the 2 final points connected by a bridge owns a sovereign account at each end of the
/// bridge. So here, at this end of the bridge there can be 2 sovereign accounts that pay rewards.
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[derive(
Copy,
Clone,
Debug,
Decode,
DecodeWithMemTracking,
Encode,
Eq,
PartialEq,
TypeInfo,
MaxEncodedLen,
)]
pub enum RewardsAccountOwner {
/// The sovereign account of the final chain on this end of the bridge.
ThisChain,
Expand All @@ -59,7 +70,18 @@ pub enum RewardsAccountOwner {
/// destinations of a bridge lane must have a sovereign account at each end of the bridge and each
/// of the sovereign accounts will pay rewards for different operations. So we need multiple
/// parameters to identify the account that pays a reward to the relayer.
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[derive(
Copy,
Clone,
Debug,
Decode,
DecodeWithMemTracking,
Encode,
Eq,
PartialEq,
TypeInfo,
MaxEncodedLen,
)]
pub struct RewardsAccountParams<LaneId> {
// **IMPORTANT NOTE**: the order of fields here matters - we are using
// `into_account_truncating` and lane id is already `32` byte, so if other fields are encoded
Expand Down
15 changes: 13 additions & 2 deletions bridges/primitives/relayers/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

use crate::RewardsAccountParams;

use codec::{Decode, Encode, MaxEncodedLen};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Get, Zero},
Expand All @@ -64,7 +64,18 @@ impl<AccountId, LaneId: Decode + Encode> From<RewardsAccountParams<LaneId>>
}

/// Relayer registration.
#[derive(Copy, Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[derive(
Copy,
Clone,
Debug,
Decode,
DecodeWithMemTracking,
Encode,
Eq,
PartialEq,
TypeInfo,
MaxEncodedLen,
)]
pub struct Registration<BlockNumber, Balance> {
/// The last block number, where this registration is considered active.
///
Expand Down
Loading
Loading