pallet_domains/migrations/
v5_to_v6.rs1use crate::{Config, Pallet};
4use core::marker::PhantomData;
5use frame_support::migrations::VersionedMigration;
6use frame_support::traits::UncheckedOnRuntimeUpgrade;
7use frame_support::weights::Weight;
8
9pub type VersionCheckedMigrateDomainsV5ToV6<T> = VersionedMigration<
10 5,
11 6,
12 VersionUncheckedMigrateV5ToV6<T>,
13 Pallet<T>,
14 <T as frame_system::Config>::DbWeight,
15>;
16
17pub struct VersionUncheckedMigrateV5ToV6<T>(PhantomData<T>);
18impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV5ToV6<T> {
19 fn on_runtime_upgrade() -> Weight {
20 migrate_evm_chain_id::migrate_evm_chain_ids::<T>()
21 }
22}
23
24mod migrate_evm_chain_id {
25 use crate::pallet::DomainRegistry;
26 use crate::{Config, EvmChainIds, Pallet};
27 use domain_runtime_primitives::EVMChainId;
28 use frame_support::pallet_prelude::ValueQuery;
29 use frame_support::storage_alias;
30 use sp_core::Get;
31 use sp_domains::DomainRuntimeInfo;
32 use sp_runtime::Weight;
33
34 #[storage_alias]
35 pub(super) type NextEVMChainId<T: Config> = StorageValue<Pallet<T>, EVMChainId, ValueQuery>;
36
37 pub(super) fn migrate_evm_chain_ids<T: Config>() -> Weight {
38 let (mut read, mut write) = (0, 0);
39 NextEVMChainId::<T>::kill();
41 write += 1;
42
43 DomainRegistry::<T>::iter().for_each(|(domain_id, domain_obj)| {
44 read += 1;
45 if let DomainRuntimeInfo::Evm { chain_id, .. } = domain_obj.domain_runtime_info {
48 EvmChainIds::<T>::insert(chain_id, domain_id);
49 write += 1;
50 }
51 });
52
53 T::DbWeight::get().reads_writes(read, write)
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use crate::EvmChainIds;
60 use crate::domain_registry::{DomainConfigParams, DomainObject, into_domain_config};
61 use crate::migrations::v5_to_v6::migrate_evm_chain_id::{
62 NextEVMChainId, migrate_evm_chain_ids,
63 };
64 use crate::pallet::DomainRegistry;
65 use crate::tests::{AccountId, Test, new_test_ext};
66 use domain_runtime_primitives::{Balance, DEFAULT_EVM_CHAIN_ID};
67 use frame_support::weights::RuntimeDbWeight;
68 use sp_domains::{DomainId, EvmDomainRuntimeConfig, OperatorAllowList};
69
70 #[test]
71 fn test_migrate_evm_chain_ids() {
72 let mut ext = new_test_ext();
73
74 let creator = 1u128;
76 let created_at = 0u32;
77 let domain_config_params = DomainConfigParams::<AccountId, Balance> {
78 domain_name: "evm-domain".to_owned(),
79 runtime_id: 0,
80 maybe_bundle_limit: None,
81 bundle_slot_probability: (1, 1),
82 operator_allow_list: OperatorAllowList::Anyone,
83 initial_balances: vec![],
84 domain_runtime_info: (DEFAULT_EVM_CHAIN_ID, EvmDomainRuntimeConfig::default()).into(),
85 };
86
87 let domain_obj = DomainObject {
88 owner_account_id: creator,
89 created_at,
90 genesis_receipt_hash: Default::default(),
91 domain_config: into_domain_config::<Test>(domain_config_params.clone()).unwrap(),
92 domain_runtime_info: domain_config_params.domain_runtime_info,
93 domain_instantiation_deposit: Default::default(),
94 };
95 ext.execute_with(|| {
96 DomainRegistry::<Test>::insert(DomainId::new(0), domain_obj);
97 NextEVMChainId::<Test>::set(871);
98 });
99 ext.commit_all().unwrap();
100
101 ext.execute_with(|| {
103 let weight = migrate_evm_chain_ids::<Test>();
104 let db_weights: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get();
105 assert_eq!(weight, db_weights.reads_writes(1, 2));
108 });
109
110 ext.commit_all().unwrap();
111
112 ext.execute_with(|| {
114 assert!(!NextEVMChainId::<Test>::exists());
115 assert_eq!(
116 EvmChainIds::<Test>::get(DEFAULT_EVM_CHAIN_ID),
117 Some(DomainId::new(0))
118 );
119 })
120 }
121}