pallet_treasury/
migration.rs1use 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
27const LOG_TARGET: &str = "runtime::treasury";
29
30pub mod cleanup_proposals {
31 use super::*;
32
33 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 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}