referrerpolicy=no-referrer-when-downgrade

pallet_treasury/
migration.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Treasury pallet migrations.
19
20use super::*;
21use alloc::collections::BTreeSet;
22#[cfg(feature = "try-runtime")]
23use alloc::vec::Vec;
24use core::marker::PhantomData;
25use frame_support::{defensive, traits::OnRuntimeUpgrade};
26
27/// The log target for this pallet.
28const LOG_TARGET: &str = "runtime::treasury";
29
30pub mod cleanup_proposals {
31	use super::*;
32
33	/// Migration to cleanup unapproved proposals to return the bonds back to the proposers.
34	/// Proposals can no longer be created and the `Proposal` storage item will be removed in the
35	/// future.
36	///
37	/// `UnreserveWeight` returns `Weight` of `unreserve_balance` operation which is perfomed during
38	/// this migration.
39	pub struct Migration<T, I, UnreserveWeight>(PhantomData<(T, I, UnreserveWeight)>);
40
41	impl<T: Config<I>, I: 'static, UnreserveWeight: Get<Weight>> OnRuntimeUpgrade
42		for Migration<T, I, UnreserveWeight>
43	{
44		fn on_runtime_upgrade() -> frame_support::weights::Weight {
45			let mut approval_index = BTreeSet::new();
46			#[allow(deprecated)]
47			for approval in Approvals::<T, I>::get().iter() {
48				approval_index.insert(*approval);
49			}
50
51			let mut proposals_processed = 0;
52			#[allow(deprecated)]
53			for (proposal_index, p) in Proposals::<T, I>::iter() {
54				if !approval_index.contains(&proposal_index) {
55					let err_amount = T::Currency::unreserve(&p.proposer, p.bond);
56					if err_amount.is_zero() {
57						Proposals::<T, I>::remove(proposal_index);
58						log::info!(
59							target: LOG_TARGET,
60							"Released bond amount of {:?} to proposer {:?}",
61							p.bond,
62							p.proposer,
63						);
64					} else {
65						defensive!(
66							"err_amount is non zero for proposal {:?}",
67							(proposal_index, err_amount)
68						);
69						Proposals::<T, I>::mutate_extant(proposal_index, |proposal| {
70							proposal.value = err_amount;
71						});
72						log::info!(
73							target: LOG_TARGET,
74							"Released partial bond amount of {:?} to proposer {:?}",
75							p.bond - err_amount,
76							p.proposer,
77						);
78					}
79					proposals_processed += 1;
80				}
81			}
82
83			log::info!(
84				target: LOG_TARGET,
85				"Migration for pallet-treasury finished, released {} proposal bonds.",
86				proposals_processed,
87			);
88
89			// calculate and return migration weights
90			let approvals_read = 1;
91			T::DbWeight::get().reads_writes(
92				proposals_processed as u64 + approvals_read,
93				proposals_processed as u64,
94			) + UnreserveWeight::get() * proposals_processed
95		}
96
97		#[cfg(feature = "try-runtime")]
98		fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
99			let value = (
100				Proposals::<T, I>::iter_values().count() as u32,
101				Approvals::<T, I>::get().len() as u32,
102			);
103			log::info!(
104				target: LOG_TARGET,
105				"Proposals and Approvals count {:?}",
106				value,
107			);
108			Ok(value.encode())
109		}
110
111		#[cfg(feature = "try-runtime")]
112		fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
113			let (old_proposals_count, old_approvals_count) =
114				<(u32, u32)>::decode(&mut &state[..]).expect("Known good");
115			let new_proposals_count = Proposals::<T, I>::iter_values().count() as u32;
116			let new_approvals_count = Approvals::<T, I>::get().len() as u32;
117
118			log::info!(
119				target: LOG_TARGET,
120				"Proposals and Approvals count {:?}",
121				(new_proposals_count, new_approvals_count),
122			);
123
124			ensure!(
125				new_proposals_count <= old_proposals_count,
126				"Proposals after migration should be less or equal to old proposals"
127			);
128			ensure!(
129				new_approvals_count == old_approvals_count,
130				"Approvals after migration should remain the same"
131			);
132			Ok(())
133		}
134	}
135}