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
//! # Moonriver specific Migrations
18

            
19
use crate::bridge_config::XcmOverPolkadotInstance;
20
use crate::{BridgePolkadotMessages, PolkadotXcm, Runtime, RuntimeOrigin};
21
use bp_messages::MessagesOperatingMode;
22
use frame_support::parameter_types;
23
use frame_support::traits::{ConstBool, OnRuntimeUpgrade};
24
use pallet_migrations::{GetMigrations, Migration};
25
use sp_std::{prelude::*, vec};
26
use sp_weights::Weight;
27
use xcm::latest::{prelude::Parachain, InteriorLocation, Location};
28

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

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

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

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

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

            
76
		weight
77
	}
78

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

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

            
90
pub struct MoonriverMigrations;
91

            
92
impl GetMigrations for MoonriverMigrations {
93
1
	fn get_migrations() -> Vec<Box<dyn Migration>> {
94
1
		vec![
95
1
			// Runtime 3000
96
1
			// Box::new(PalletStakingMultiplyRoundLenBy2)
97
1
			Box::new(SetupBridge),
98
1
		]
99
1
	}
100
}