1
// Copyright 2025 Moonbeam Foundation.Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! # Moonbeam specific Migrations
18

            
19
use crate::bridge_config::XcmOverKusamaInstance;
20
use crate::{BridgeKusamaMessages, PolkadotXcm, Runtime, RuntimeOrigin};
21
use bp_messages::MessagesOperatingMode;
22
use frame_support::traits::{ConstBool, OnRuntimeUpgrade};
23
use pallet_migrations::{GetMigrations, Migration};
24
use sp_std::{prelude::*, vec};
25
use sp_weights::Weight;
26

            
27
use frame_support::parameter_types;
28

            
29
use xcm::prelude::{InteriorLocation, Location, Parachain};
30

            
31
parameter_types! {
32
	pub Lane: bp_moonbeam::LaneId = Default::default();
33
	pub RelativeLocation: Location = Location::new(
34
		1,
35
		[
36
			Parachain(bp_moonbeam::PARACHAIN_ID)
37
		],
38
	);
39
	pub BridgedUniversalLocation: InteriorLocation = bp_moonriver::GlobalConsensusLocation::get().interior;
40
}
41

            
42
pub struct SetupBridge;
43
impl Migration for SetupBridge {
44
1
	fn friendly_name(&self) -> &str {
45
1
		"MM_SetupBridge"
46
1
	}
47

            
48
	fn migrate(&self, _available_weight: Weight) -> Weight {
49
		let mut weight = <Runtime as frame_system::Config>::DbWeight::get().writes(1);
50
		let _ = PolkadotXcm::force_xcm_version(
51
			RuntimeOrigin::root(),
52
			Box::new(bp_moonriver::GlobalConsensusLocation::get()),
53
			xcm::v5::VERSION,
54
		)
55
		.map_err(|err| {
56
			log::error!("Failed to set xcm version: {:?}", err);
57
		});
58

            
59
		weight =
60
			weight.saturating_add(<Runtime as frame_system::Config>::DbWeight::get().writes(1));
61
		let _ = BridgeKusamaMessages::set_operating_mode(
62
			RuntimeOrigin::root(),
63
			MessagesOperatingMode::RejectingOutboundMessages,
64
		)
65
		.map_err(|err| {
66
			log::error!("Failed to set operating mode: {:?}", err);
67
		});
68

            
69
		weight = weight.saturating_add(pallet_xcm_bridge::migration::OpenBridgeForLane::<
70
			Runtime,
71
			XcmOverKusamaInstance,
72
			Lane,
73
			ConstBool<true>,
74
			RelativeLocation,
75
			BridgedUniversalLocation,
76
		>::on_runtime_upgrade());
77

            
78
		weight
79
	}
80

            
81
	#[cfg(feature = "try-runtime")]
82
	fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
83
		Ok(Default::default())
84
	}
85

            
86
	#[cfg(feature = "try-runtime")]
87
	fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
88
		Ok(())
89
	}
90
}
91

            
92
pub struct MoonbeamMigrations;
93

            
94
impl GetMigrations for MoonbeamMigrations {
95
1
	fn get_migrations() -> Vec<Box<dyn Migration>> {
96
1
		vec![Box::new(SetupBridge)]
97
1
	}
98
}