Skip to content

Commit 4c0198f

Browse files
authored
Merge pull request #3364 from autonomys/evm-migrations
add missed xdm migrations
2 parents 56d135e + adf8114 commit 4c0198f

File tree

5 files changed

+216
-1
lines changed

5 files changed

+216
-1
lines changed

crates/subspace-runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,8 @@ pub type Executive = frame_executive::Executive<
967967
frame_system::ChainContext<Runtime>,
968968
Runtime,
969969
AllPalletsWithSystem,
970+
// TODO: remove once migrations are done
971+
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
970972
>;
971973

972974
fn extract_segment_headers(ext: &UncheckedExtrinsic) -> Option<Vec<SegmentHeader>> {

domains/pallets/messenger/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
mod benchmarking;
2525
mod fees;
2626
mod messages;
27+
pub mod migrations;
2728
#[cfg(test)]
2829
mod mock;
2930
#[cfg(test)]
@@ -34,6 +35,7 @@ pub mod weights;
3435
extern crate alloc;
3536

3637
use codec::{Decode, Encode};
38+
use frame_support::pallet_prelude::StorageVersion;
3739
use frame_support::traits::fungible::{Inspect, InspectHold};
3840
use frame_system::pallet_prelude::BlockNumberFor;
3941
pub use pallet::*;
@@ -107,13 +109,16 @@ pub trait HoldIdentifier<T: Config> {
107109
fn messenger_channel() -> FungibleHoldId<T>;
108110
}
109111

112+
/// The current storage version.
113+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
114+
110115
#[frame_support::pallet]
111116
mod pallet {
112117
use crate::weights::WeightInfo;
113118
use crate::{
114119
BalanceOf, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, CloseChannelBy,
115120
FeeModel, HoldIdentifier, Nonce, OutboxMessageResult, StateRootOf, ValidatedRelayMessage,
116-
U256,
121+
STORAGE_VERSION, U256,
117122
};
118123
#[cfg(not(feature = "std"))]
119124
use alloc::boxed::Box;
@@ -196,6 +201,7 @@ mod pallet {
196201
/// Pallet messenger used to communicate between chains and other blockchains.
197202
#[pallet::pallet]
198203
#[pallet::without_storage_info]
204+
#[pallet::storage_version(STORAGE_VERSION)]
199205
pub struct Pallet<T>(_);
200206

201207
/// Stores the next channel id for a foreign chain.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
}

domains/runtime/auto-id/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ pub type Executive = domain_pallet_executive::Executive<
106106
frame_system::ChainContext<Runtime>,
107107
Runtime,
108108
AllPalletsWithSystem,
109+
// TODO: remove once migrations are done
110+
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
109111
>;
110112

111113
impl_opaque_keys! {

domains/runtime/evm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub type Executive = domain_pallet_executive::Executive<
143143
frame_system::ChainContext<Runtime>,
144144
Runtime,
145145
AllPalletsWithSystem,
146+
// TODO: remove once migrations are done
147+
pallet_messenger::migrations::VersionCheckedMigrateDomainsV0ToV1<Runtime>,
146148
>;
147149

148150
impl fp_self_contained::SelfContainedCall for RuntimeCall {

0 commit comments

Comments
 (0)