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

add missed xdm migrations #3364

Merged
merged 1 commit into from
Feb 3, 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
2 changes: 2 additions & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,8 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
// TODO: remove once migrations are done
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
>;

fn extract_segment_headers(ext: &UncheckedExtrinsic) -> Option<Vec<SegmentHeader>> {
Expand Down
8 changes: 7 additions & 1 deletion domains/pallets/messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
mod benchmarking;
mod fees;
mod messages;
pub mod migrations;
#[cfg(test)]
mod mock;
#[cfg(test)]
Expand All @@ -34,6 +35,7 @@ pub mod weights;
extern crate alloc;

use codec::{Decode, Encode};
use frame_support::pallet_prelude::StorageVersion;
use frame_support::traits::fungible::{Inspect, InspectHold};
use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
Expand Down Expand Up @@ -107,13 +109,16 @@ pub trait HoldIdentifier<T: Config> {
fn messenger_channel() -> FungibleHoldId<T>;
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
mod pallet {
use crate::weights::WeightInfo;
use crate::{
BalanceOf, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, CloseChannelBy,
FeeModel, HoldIdentifier, Nonce, OutboxMessageResult, StateRootOf, ValidatedRelayMessage,
U256,
STORAGE_VERSION, U256,
};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
Expand Down Expand Up @@ -196,6 +201,7 @@ mod pallet {
/// Pallet messenger used to communicate between chains and other blockchains.
#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

/// Stores the next channel id for a foreign chain.
Expand Down
203 changes: 203 additions & 0 deletions domains/pallets/messenger/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
//! Migration module for pallet-messenger
#[cfg(not(feature = "std"))]
extern crate alloc;
use crate::{Config, Pallet};
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use frame_support::migrations::VersionedMigration;
use frame_support::traits::UncheckedOnRuntimeUpgrade;
use frame_support::weights::Weight;
use sp_core::sp_std;
#[cfg(feature = "std")]
use std::collections::BTreeMap;
#[cfg(feature = "std")]
use std::vec::Vec;

pub type VersionCheckedMigrateDomainsV0ToV1<T> = VersionedMigration<
0,
1,
VersionUncheckedMigrateV0ToV1<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

pub struct VersionUncheckedMigrateV0ToV1<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV0ToV1<T> {
fn on_runtime_upgrade() -> Weight {
messenger_migration::migrate_messenger_storages::<T>()
}
}

mod messenger_migration {
use super::{BTreeMap, Vec};
use crate::{
BalanceOf, Config, InboxResponses as InboxResponsesNew, Outbox as OutboxNew,
OutboxMessageCount, Pallet,
};
use frame_support::pallet_prelude::OptionQuery;
use frame_support::weights::Weight;
use frame_support::{storage_alias, Identity};
use sp_core::Get;
use sp_domains::{ChainId, ChannelId};
use sp_messenger::messages::{Message, Nonce};

#[storage_alias]
pub(super) type InboxResponses<T: Config> = CountedStorageMap<
Pallet<T>,
Identity,
(ChainId, ChannelId, Nonce),
Message<BalanceOf<T>>,
OptionQuery,
>;

#[storage_alias]
pub(super) type Outbox<T: Config> = CountedStorageMap<
Pallet<T>,
Identity,
(ChainId, ChannelId, Nonce),
Message<BalanceOf<T>>,
OptionQuery,
>;

pub(super) fn migrate_messenger_storages<T: Config>() -> Weight {
let mut reads = 0;
let mut writes = 0;
let inbox_responses = InboxResponses::<T>::drain().collect::<Vec<_>>();
inbox_responses.into_iter().for_each(|(key, msg)| {
// we do one read from the old storage
reads += 1;

// we do one write to old storage and one write to new storage
writes += 2;

InboxResponsesNew::<T>::insert(key, msg);
});

let outbox = Outbox::<T>::drain().collect::<Vec<_>>();
let mut outbox_count = BTreeMap::new();
outbox.into_iter().for_each(|(key, msg)| {
// we do one read from the old storage
reads += 1;

// we do one write to old storage and one write to new storage
writes += 2;

// total outbox count
outbox_count
.entry((key.0, key.1))
.and_modify(|count| *count += 1)
.or_insert(1);

OutboxNew::<T>::insert(key, msg);
});

outbox_count.into_iter().for_each(|(key, count)| {
// we do one write to the outbox message count
writes += 1;
OutboxMessageCount::<T>::insert(key, count);
});

T::DbWeight::get().reads_writes(reads, writes)
}
}

#[cfg(test)]
mod tests {
use crate::migrations::messenger_migration::{
migrate_messenger_storages, InboxResponses, Outbox,
};
use crate::mock::chain_a::{new_test_ext, Runtime, SelfChainId};
use crate::{InboxResponses as InboxResponsesNew, Outbox as OutboxNew, OutboxMessageCount};
use frame_support::weights::RuntimeDbWeight;
use sp_core::Get;
use sp_domains::{ChainId, ChannelId};
use sp_messenger::endpoint::{Endpoint, EndpointRequest};
use sp_messenger::messages::{Message, Nonce, Payload, RequestResponse, VersionedPayload};

#[test]
fn test_messenger_storage_migration() {
let mut ext = new_test_ext();
let msg = Message {
src_chain_id: ChainId::Consensus,
dst_chain_id: SelfChainId::get(),
channel_id: Default::default(),
nonce: Default::default(),
payload: VersionedPayload::V0(Payload::Endpoint(RequestResponse::Request(
EndpointRequest {
src_endpoint: Endpoint::Id(0),
dst_endpoint: Endpoint::Id(0),
payload: vec![],
},
))),
last_delivered_message_response_nonce: None,
};
ext.execute_with(|| {
// one inbox response
InboxResponses::<Runtime>::insert(
(ChainId::Consensus, ChannelId::zero(), Nonce::zero()),
msg.clone(),
);

// outbox responses
Outbox::<Runtime>::insert(
(ChainId::Consensus, ChannelId::zero(), Nonce::zero()),
msg.clone(),
);
Outbox::<Runtime>::insert(
(ChainId::Consensus, ChannelId::zero(), Nonce::one()),
msg.clone(),
);
Outbox::<Runtime>::insert(
(ChainId::Consensus, ChannelId::one(), Nonce::zero()),
msg.clone(),
);
});

ext.commit_all().unwrap();

ext.execute_with(|| {
let weights = migrate_messenger_storages::<Runtime>();
// 1 read and 2 writes for inbox response
// 3 reads and 6 writes for outbox
// 2 writes for Outbox message count
let db_weights: RuntimeDbWeight = <Runtime as frame_system::Config>::DbWeight::get();
assert_eq!(weights, db_weights.reads_writes(4, 10),);

assert_eq!(
InboxResponsesNew::<Runtime>::get((
ChainId::Consensus,
ChannelId::zero(),
Nonce::zero()
)),
Some(msg.clone())
);

assert_eq!(
OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::zero(), Nonce::zero())),
Some(msg.clone())
);

assert_eq!(
OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::zero(), Nonce::one())),
Some(msg.clone())
);

assert_eq!(
OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::one(), Nonce::zero())),
Some(msg.clone())
);

assert_eq!(
OutboxMessageCount::<Runtime>::get((ChainId::Consensus, ChannelId::zero())),
2
);

assert_eq!(
OutboxMessageCount::<Runtime>::get((ChainId::Consensus, ChannelId::one())),
1
);
});
}
}
2 changes: 2 additions & 0 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub type Executive = domain_pallet_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
// TODO: remove once migrations are done
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
>;

impl_opaque_keys! {
Expand Down
2 changes: 2 additions & 0 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ pub type Executive = domain_pallet_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
// TODO: remove once migrations are done
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
>;

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down
Loading