1
// Copyright 2019-2025 PureStake 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
extern crate alloc;
18

            
19
#[cfg(feature = "try-runtime")]
20
use alloc::vec::Vec;
21

            
22
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
23

            
24
use crate::*;
25

            
26
#[derive(
27
	Clone,
28
	PartialEq,
29
	Eq,
30
	parity_scale_codec::Decode,
31
	parity_scale_codec::Encode,
32
	sp_runtime::RuntimeDebug,
33
)]
34
/// Reserve information { account, percent_of_inflation }
35
pub struct OldParachainBondConfig<AccountId> {
36
	/// Account which receives funds intended for parachain bond
37
	pub account: AccountId,
38
	/// Percent of inflation set aside for parachain bond account
39
	pub percent: sp_runtime::Percent,
40
}
41

            
42
pub struct MigrateParachainBondConfig<T>(sp_std::marker::PhantomData<T>);
43
impl<T: Config> OnRuntimeUpgrade for MigrateParachainBondConfig<T> {
44
	fn on_runtime_upgrade() -> Weight {
45
		let (account, percent) = if let Some(config) =
46
			frame_support::storage::migration::get_storage_value::<
47
				OldParachainBondConfig<T::AccountId>,
48
			>(b"ParachainStaking", b"ParachainBondInfo", &[])
49
		{
50
			(config.account, config.percent)
51
		} else {
52
			return Weight::default();
53
		};
54

            
55
		let pbr = InflationDistributionAccount { account, percent };
56
		let treasury = InflationDistributionAccount::<T::AccountId>::default();
57
		let configs: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();
58

            
59
		//***** Start mutate storage *****//
60

            
61
		InflationDistributionInfo::<T>::put(configs);
62

            
63
		// Remove storage value ParachainStaking::ParachainBondInfo
64
		frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
65
			b"ParachainStaking",
66
			b"ParachainBondInfo",
67
		));
68

            
69
		Weight::default()
70
	}
71

            
72
	#[cfg(feature = "try-runtime")]
73
	fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
74
		use frame_support::ensure;
75
		use parity_scale_codec::Encode;
76

            
77
		let state = frame_support::storage::migration::get_storage_value::<
78
			OldParachainBondConfig<T::AccountId>,
79
		>(b"ParachainStaking", b"ParachainBondInfo", &[]);
80

            
81
		ensure!(state.is_some(), "State not found");
82

            
83
		Ok(state
84
			.expect("should be Some(_) due to former call to ensure!")
85
			.encode())
86
	}
87

            
88
	#[cfg(feature = "try-runtime")]
89
	fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
90
		use frame_support::ensure;
91

            
92
		let old_state: OldParachainBondConfig<T::AccountId> =
93
			parity_scale_codec::Decode::decode(&mut &state[..])
94
				.map_err(|_| sp_runtime::DispatchError::Other("Failed to decode old state"))?;
95

            
96
		let new_state = InflationDistributionInfo::<T>::get();
97

            
98
		let pbr = InflationDistributionAccount {
99
			account: old_state.account,
100
			percent: old_state.percent,
101
		};
102
		let treasury = InflationDistributionAccount::<T::AccountId>::default();
103
		let expected_new_state: InflationDistributionConfig<T::AccountId> = [pbr, treasury].into();
104

            
105
		ensure!(new_state == expected_new_state, "State migration failed");
106

            
107
		Ok(())
108
	}
109
}