1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2025 Moonbeam Foundation.Inc.
// This file is part of Moonbeam.

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

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

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

//! # Moonbeam specific Migrations

use crate::bridge_config::XcmOverKusamaInstance;
use crate::{BridgeKusamaMessages, PolkadotXcm, Runtime, RuntimeOrigin};
use bp_messages::MessagesOperatingMode;
use frame_support::traits::{ConstBool, OnRuntimeUpgrade};
use pallet_migrations::{GetMigrations, Migration};
use sp_std::{prelude::*, vec};
use sp_weights::Weight;

use frame_support::parameter_types;

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

parameter_types! {
	pub Lane: bp_moonbeam::LaneId = Default::default();
	pub RelativeLocation: Location = Location::new(
		1,
		[
			Parachain(bp_moonbeam::PARACHAIN_ID)
		],
	);
	pub BridgedUniversalLocation: InteriorLocation = bp_moonriver::GlobalConsensusLocation::get().interior;
}

pub struct SetupBridge;
impl Migration for SetupBridge {
	fn friendly_name(&self) -> &str {
		"MM_SetupBridge"
	}

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

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

		weight = weight.saturating_add(pallet_xcm_bridge::migration::OpenBridgeForLane::<
			Runtime,
			XcmOverKusamaInstance,
			Lane,
			ConstBool<true>,
			RelativeLocation,
			BridgedUniversalLocation,
		>::on_runtime_upgrade());

		weight
	}

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

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

pub struct MoonbeamMigrations;

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