|
| 1 | +//! Migration module for pallet-messenger |
| 2 | +#[cfg(not(feature = "std"))] |
| 3 | +extern crate alloc; |
| 4 | +use crate::{Config, Pallet}; |
| 5 | +#[cfg(not(feature = "std"))] |
| 6 | +use alloc::collections::BTreeMap; |
| 7 | +#[cfg(not(feature = "std"))] |
| 8 | +use alloc::vec::Vec; |
| 9 | +use frame_support::migrations::VersionedMigration; |
| 10 | +use frame_support::traits::UncheckedOnRuntimeUpgrade; |
| 11 | +use frame_support::weights::Weight; |
| 12 | +use sp_core::sp_std; |
| 13 | +#[cfg(feature = "std")] |
| 14 | +use std::collections::BTreeMap; |
| 15 | +#[cfg(feature = "std")] |
| 16 | +use std::vec::Vec; |
| 17 | + |
| 18 | +pub type VersionCheckedMigrateDomainsV0ToV1<T> = VersionedMigration< |
| 19 | + 0, |
| 20 | + 1, |
| 21 | + VersionUncheckedMigrateV0ToV1<T>, |
| 22 | + Pallet<T>, |
| 23 | + <T as frame_system::Config>::DbWeight, |
| 24 | +>; |
| 25 | + |
| 26 | +pub struct VersionUncheckedMigrateV0ToV1<T>(sp_std::marker::PhantomData<T>); |
| 27 | +impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV0ToV1<T> { |
| 28 | + fn on_runtime_upgrade() -> Weight { |
| 29 | + messenger_migration::migrate_messenger_storages::<T>() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +mod messenger_migration { |
| 34 | + use super::{BTreeMap, Vec}; |
| 35 | + use crate::{ |
| 36 | + BalanceOf, Config, InboxResponses as InboxResponsesNew, Outbox as OutboxNew, |
| 37 | + OutboxMessageCount, Pallet, |
| 38 | + }; |
| 39 | + use frame_support::pallet_prelude::OptionQuery; |
| 40 | + use frame_support::weights::Weight; |
| 41 | + use frame_support::{storage_alias, Identity}; |
| 42 | + use sp_core::Get; |
| 43 | + use sp_domains::{ChainId, ChannelId}; |
| 44 | + use sp_messenger::messages::{Message, Nonce}; |
| 45 | + |
| 46 | + #[storage_alias] |
| 47 | + pub(super) type InboxResponses<T: Config> = CountedStorageMap< |
| 48 | + Pallet<T>, |
| 49 | + Identity, |
| 50 | + (ChainId, ChannelId, Nonce), |
| 51 | + Message<BalanceOf<T>>, |
| 52 | + OptionQuery, |
| 53 | + >; |
| 54 | + |
| 55 | + #[storage_alias] |
| 56 | + pub(super) type Outbox<T: Config> = CountedStorageMap< |
| 57 | + Pallet<T>, |
| 58 | + Identity, |
| 59 | + (ChainId, ChannelId, Nonce), |
| 60 | + Message<BalanceOf<T>>, |
| 61 | + OptionQuery, |
| 62 | + >; |
| 63 | + |
| 64 | + pub(super) fn migrate_messenger_storages<T: Config>() -> Weight { |
| 65 | + let mut reads = 0; |
| 66 | + let mut writes = 0; |
| 67 | + let inbox_responses = InboxResponses::<T>::drain().collect::<Vec<_>>(); |
| 68 | + inbox_responses.into_iter().for_each(|(key, msg)| { |
| 69 | + // we do one read from the old storage |
| 70 | + reads += 1; |
| 71 | + |
| 72 | + // we do one write to old storage and one write to new storage |
| 73 | + writes += 2; |
| 74 | + |
| 75 | + InboxResponsesNew::<T>::insert(key, msg); |
| 76 | + }); |
| 77 | + |
| 78 | + let outbox = Outbox::<T>::drain().collect::<Vec<_>>(); |
| 79 | + let mut outbox_count = BTreeMap::new(); |
| 80 | + outbox.into_iter().for_each(|(key, msg)| { |
| 81 | + // we do one read from the old storage |
| 82 | + reads += 1; |
| 83 | + |
| 84 | + // we do one write to old storage and one write to new storage |
| 85 | + writes += 2; |
| 86 | + |
| 87 | + // total outbox count |
| 88 | + outbox_count |
| 89 | + .entry((key.0, key.1)) |
| 90 | + .and_modify(|count| *count += 1) |
| 91 | + .or_insert(1); |
| 92 | + |
| 93 | + OutboxNew::<T>::insert(key, msg); |
| 94 | + }); |
| 95 | + |
| 96 | + outbox_count.into_iter().for_each(|(key, count)| { |
| 97 | + // we do one write to the outbox message count |
| 98 | + writes += 1; |
| 99 | + OutboxMessageCount::<T>::insert(key, count); |
| 100 | + }); |
| 101 | + |
| 102 | + T::DbWeight::get().reads_writes(reads, writes) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use crate::migrations::messenger_migration::{ |
| 109 | + migrate_messenger_storages, InboxResponses, Outbox, |
| 110 | + }; |
| 111 | + use crate::mock::chain_a::{new_test_ext, Runtime, SelfChainId}; |
| 112 | + use crate::{InboxResponses as InboxResponsesNew, Outbox as OutboxNew, OutboxMessageCount}; |
| 113 | + use frame_support::weights::RuntimeDbWeight; |
| 114 | + use sp_core::Get; |
| 115 | + use sp_domains::{ChainId, ChannelId}; |
| 116 | + use sp_messenger::endpoint::{Endpoint, EndpointRequest}; |
| 117 | + use sp_messenger::messages::{Message, Nonce, Payload, RequestResponse, VersionedPayload}; |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_messenger_storage_migration() { |
| 121 | + let mut ext = new_test_ext(); |
| 122 | + let msg = Message { |
| 123 | + src_chain_id: ChainId::Consensus, |
| 124 | + dst_chain_id: SelfChainId::get(), |
| 125 | + channel_id: Default::default(), |
| 126 | + nonce: Default::default(), |
| 127 | + payload: VersionedPayload::V0(Payload::Endpoint(RequestResponse::Request( |
| 128 | + EndpointRequest { |
| 129 | + src_endpoint: Endpoint::Id(0), |
| 130 | + dst_endpoint: Endpoint::Id(0), |
| 131 | + payload: vec![], |
| 132 | + }, |
| 133 | + ))), |
| 134 | + last_delivered_message_response_nonce: None, |
| 135 | + }; |
| 136 | + ext.execute_with(|| { |
| 137 | + // one inbox response |
| 138 | + InboxResponses::<Runtime>::insert( |
| 139 | + (ChainId::Consensus, ChannelId::zero(), Nonce::zero()), |
| 140 | + msg.clone(), |
| 141 | + ); |
| 142 | + |
| 143 | + // outbox responses |
| 144 | + Outbox::<Runtime>::insert( |
| 145 | + (ChainId::Consensus, ChannelId::zero(), Nonce::zero()), |
| 146 | + msg.clone(), |
| 147 | + ); |
| 148 | + Outbox::<Runtime>::insert( |
| 149 | + (ChainId::Consensus, ChannelId::zero(), Nonce::one()), |
| 150 | + msg.clone(), |
| 151 | + ); |
| 152 | + Outbox::<Runtime>::insert( |
| 153 | + (ChainId::Consensus, ChannelId::one(), Nonce::zero()), |
| 154 | + msg.clone(), |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + ext.commit_all().unwrap(); |
| 159 | + |
| 160 | + ext.execute_with(|| { |
| 161 | + let weights = migrate_messenger_storages::<Runtime>(); |
| 162 | + // 1 read and 2 writes for inbox response |
| 163 | + // 3 reads and 6 writes for outbox |
| 164 | + // 2 writes for Outbox message count |
| 165 | + let db_weights: RuntimeDbWeight = <Runtime as frame_system::Config>::DbWeight::get(); |
| 166 | + assert_eq!(weights, db_weights.reads_writes(4, 10),); |
| 167 | + |
| 168 | + assert_eq!( |
| 169 | + InboxResponsesNew::<Runtime>::get(( |
| 170 | + ChainId::Consensus, |
| 171 | + ChannelId::zero(), |
| 172 | + Nonce::zero() |
| 173 | + )), |
| 174 | + Some(msg.clone()) |
| 175 | + ); |
| 176 | + |
| 177 | + assert_eq!( |
| 178 | + OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::zero(), Nonce::zero())), |
| 179 | + Some(msg.clone()) |
| 180 | + ); |
| 181 | + |
| 182 | + assert_eq!( |
| 183 | + OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::zero(), Nonce::one())), |
| 184 | + Some(msg.clone()) |
| 185 | + ); |
| 186 | + |
| 187 | + assert_eq!( |
| 188 | + OutboxNew::<Runtime>::get((ChainId::Consensus, ChannelId::one(), Nonce::zero())), |
| 189 | + Some(msg.clone()) |
| 190 | + ); |
| 191 | + |
| 192 | + assert_eq!( |
| 193 | + OutboxMessageCount::<Runtime>::get((ChainId::Consensus, ChannelId::zero())), |
| 194 | + 2 |
| 195 | + ); |
| 196 | + |
| 197 | + assert_eq!( |
| 198 | + OutboxMessageCount::<Runtime>::get((ChainId::Consensus, ChannelId::one())), |
| 199 | + 1 |
| 200 | + ); |
| 201 | + }); |
| 202 | + } |
| 203 | +} |
0 commit comments