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::mock::{AccountId, Test};
65 use crate::pallet::DomainRegistry;
66 use crate::tests::new_test_ext;
67 use domain_runtime_primitives::{Balance, DEFAULT_EVM_CHAIN_ID};
68 use frame_support::weights::RuntimeDbWeight;
69 use sp_domains::{DomainId, EvmDomainRuntimeConfig, OperatorAllowList};
70
71 #[test]
72 fn test_migrate_evm_chain_ids() {
73 let mut ext = new_test_ext();
74
75 let creator = 1u128;
77 let created_at = 0u32;
78 let domain_config_params = DomainConfigParams::<AccountId, Balance> {
79 domain_name: "evm-domain".to_owned(),
80 runtime_id: 0,
81 maybe_bundle_limit: None,
82 bundle_slot_probability: (1, 1),
83 operator_allow_list: OperatorAllowList::Anyone,
84 initial_balances: vec![],
85 domain_runtime_info: (DEFAULT_EVM_CHAIN_ID, EvmDomainRuntimeConfig::default()).into(),
86 };
87
88 let domain_obj = DomainObject {
89 owner_account_id: creator,
90 created_at,
91 genesis_receipt_hash: Default::default(),
92 domain_config: into_domain_config::<Test>(domain_config_params.clone()).unwrap(),
93 domain_runtime_info: domain_config_params.domain_runtime_info,
94 domain_instantiation_deposit: Default::default(),
95 };
96 ext.execute_with(|| {
97 DomainRegistry::<Test>::insert(DomainId::new(0), domain_obj);
98 NextEVMChainId::<Test>::set(871);
99 });
100 ext.commit_all().unwrap();
101
102 ext.execute_with(|| {
104 let weight = migrate_evm_chain_ids::<Test>();
105 let db_weights: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get();
106 assert_eq!(weight, db_weights.reads_writes(1, 2));
109 });
110
111 ext.commit_all().unwrap();
112
113 ext.execute_with(|| {
115 assert!(!NextEVMChainId::<Test>::exists());
116 assert_eq!(
117 EvmChainIds::<Test>::get(DEFAULT_EVM_CHAIN_ID),
118 Some(DomainId::new(0))
119 );
120 })
121 }
122}