|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2017-2022 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 | +//! Storage migrations for the referenda pallet. |
| 19 | +
|
| 20 | +use super::*; |
| 21 | +use codec::{Decode, Encode, EncodeLike, MaxEncodedLen}; |
| 22 | +use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade}; |
| 23 | +use log; |
| 24 | + |
| 25 | +/// Initial version of storage types. |
| 26 | +pub mod v0 { |
| 27 | + use super::*; |
| 28 | + // ReferendumStatus and its dependency types referenced from the latest version while staying |
| 29 | + // unchanged. [`super::test::referendum_status_v0()`] checks its immutability between v0 and |
| 30 | + // latest version. |
| 31 | + #[cfg(test)] |
| 32 | + pub(super) use super::{ReferendumStatus, ReferendumStatusOf}; |
| 33 | + |
| 34 | + pub type ReferendumInfoOf<T, I> = ReferendumInfo< |
| 35 | + TrackIdOf<T, I>, |
| 36 | + PalletsOriginOf<T>, |
| 37 | + <T as frame_system::Config>::BlockNumber, |
| 38 | + BoundedCallOf<T, I>, |
| 39 | + BalanceOf<T, I>, |
| 40 | + TallyOf<T, I>, |
| 41 | + <T as frame_system::Config>::AccountId, |
| 42 | + ScheduleAddressOf<T, I>, |
| 43 | + >; |
| 44 | + |
| 45 | + /// Info regarding a referendum, present or past. |
| 46 | + #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] |
| 47 | + pub enum ReferendumInfo< |
| 48 | + TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 49 | + RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 50 | + Moment: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike, |
| 51 | + Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 52 | + Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 53 | + Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 54 | + AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 55 | + ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone, |
| 56 | + > { |
| 57 | + /// Referendum has been submitted and is being voted on. |
| 58 | + Ongoing( |
| 59 | + ReferendumStatus< |
| 60 | + TrackId, |
| 61 | + RuntimeOrigin, |
| 62 | + Moment, |
| 63 | + Call, |
| 64 | + Balance, |
| 65 | + Tally, |
| 66 | + AccountId, |
| 67 | + ScheduleAddress, |
| 68 | + >, |
| 69 | + ), |
| 70 | + /// Referendum finished with approval. Submission deposit is held. |
| 71 | + Approved(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>), |
| 72 | + /// Referendum finished with rejection. Submission deposit is held. |
| 73 | + Rejected(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>), |
| 74 | + /// Referendum finished with cancellation. Submission deposit is held. |
| 75 | + Cancelled(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>), |
| 76 | + /// Referendum finished and was never decided. Submission deposit is held. |
| 77 | + TimedOut(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>), |
| 78 | + /// Referendum finished with a kill. |
| 79 | + Killed(Moment), |
| 80 | + } |
| 81 | + |
| 82 | + #[storage_alias] |
| 83 | + pub type ReferendumInfoFor<T: Config<I>, I: 'static> = |
| 84 | + StorageMap<Pallet<T, I>, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf<T, I>>; |
| 85 | +} |
| 86 | + |
| 87 | +pub mod v1 { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + /// The log target. |
| 91 | + const TARGET: &'static str = "runtime::democracy::migration::v1"; |
| 92 | + |
| 93 | + /// Transforms a submission deposit of ReferendumInfo(Approved|Rejected|Cancelled|TimedOut) to |
| 94 | + /// optional value, making it refundable. |
| 95 | + pub struct MigrateV0ToV1<T, I = ()>(PhantomData<(T, I)>); |
| 96 | + impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for MigrateV0ToV1<T, I> { |
| 97 | + #[cfg(feature = "try-runtime")] |
| 98 | + fn pre_upgrade() -> Result<Vec<u8>, &'static str> { |
| 99 | + let onchain_version = Pallet::<T, I>::on_chain_storage_version(); |
| 100 | + assert_eq!(onchain_version, 0, "migration from version 0 to 1."); |
| 101 | + let referendum_count = v0::ReferendumInfoFor::<T, I>::iter().count(); |
| 102 | + log::info!( |
| 103 | + target: TARGET, |
| 104 | + "pre-upgrade state contains '{}' referendums.", |
| 105 | + referendum_count |
| 106 | + ); |
| 107 | + Ok((referendum_count as u32).encode()) |
| 108 | + } |
| 109 | + |
| 110 | + fn on_runtime_upgrade() -> Weight { |
| 111 | + let current_version = Pallet::<T, I>::current_storage_version(); |
| 112 | + let onchain_version = Pallet::<T, I>::on_chain_storage_version(); |
| 113 | + let mut weight = T::DbWeight::get().reads(1); |
| 114 | + log::info!( |
| 115 | + target: TARGET, |
| 116 | + "running migration with current storage version {:?} / onchain {:?}.", |
| 117 | + current_version, |
| 118 | + onchain_version |
| 119 | + ); |
| 120 | + if onchain_version != 0 { |
| 121 | + log::warn!(target: TARGET, "skipping migration from v0 to v1."); |
| 122 | + return weight |
| 123 | + } |
| 124 | + v0::ReferendumInfoFor::<T, I>::iter().for_each(|(key, value)| { |
| 125 | + let maybe_new_value = match value { |
| 126 | + v0::ReferendumInfo::Ongoing(_) | v0::ReferendumInfo::Killed(_) => None, |
| 127 | + v0::ReferendumInfo::Approved(e, s, d) => |
| 128 | + Some(ReferendumInfo::Approved(e, Some(s), d)), |
| 129 | + v0::ReferendumInfo::Rejected(e, s, d) => |
| 130 | + Some(ReferendumInfo::Rejected(e, Some(s), d)), |
| 131 | + v0::ReferendumInfo::Cancelled(e, s, d) => |
| 132 | + Some(ReferendumInfo::Cancelled(e, Some(s), d)), |
| 133 | + v0::ReferendumInfo::TimedOut(e, s, d) => |
| 134 | + Some(ReferendumInfo::TimedOut(e, Some(s), d)), |
| 135 | + }; |
| 136 | + if let Some(new_value) = maybe_new_value { |
| 137 | + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); |
| 138 | + log::info!(target: TARGET, "migrating referendum #{:?}", &key); |
| 139 | + ReferendumInfoFor::<T, I>::insert(key, new_value); |
| 140 | + } else { |
| 141 | + weight.saturating_accrue(T::DbWeight::get().reads(1)); |
| 142 | + } |
| 143 | + }); |
| 144 | + StorageVersion::new(1).put::<Pallet<T, I>>(); |
| 145 | + weight.saturating_accrue(T::DbWeight::get().writes(1)); |
| 146 | + weight |
| 147 | + } |
| 148 | + |
| 149 | + #[cfg(feature = "try-runtime")] |
| 150 | + fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> { |
| 151 | + let onchain_version = Pallet::<T, I>::on_chain_storage_version(); |
| 152 | + assert_eq!(onchain_version, 1, "must upgrade from version 0 to 1."); |
| 153 | + let pre_referendum_count: u32 = Decode::decode(&mut &state[..]) |
| 154 | + .expect("failed to decode the state from pre-upgrade."); |
| 155 | + let post_referendum_count = ReferendumInfoFor::<T, I>::iter().count() as u32; |
| 156 | + assert_eq!( |
| 157 | + post_referendum_count, pre_referendum_count, |
| 158 | + "must migrate all referendums." |
| 159 | + ); |
| 160 | + log::info!(target: TARGET, "migrated all referendums."); |
| 161 | + Ok(()) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +#[cfg(test)] |
| 167 | +pub mod test { |
| 168 | + use super::*; |
| 169 | + use crate::mock::{Test as T, *}; |
| 170 | + use core::str::FromStr; |
| 171 | + |
| 172 | + // create referendum status v0. |
| 173 | + fn create_status_v0() -> v0::ReferendumStatusOf<T, ()> { |
| 174 | + let origin: OriginCaller = frame_system::RawOrigin::Root.into(); |
| 175 | + let track = <T as Config<()>>::Tracks::track_for(&origin).unwrap(); |
| 176 | + v0::ReferendumStatusOf::<T, ()> { |
| 177 | + track, |
| 178 | + in_queue: true, |
| 179 | + origin, |
| 180 | + proposal: set_balance_proposal_bounded(1), |
| 181 | + enactment: DispatchTime::At(1), |
| 182 | + tally: TallyOf::<T, ()>::new(track), |
| 183 | + submission_deposit: Deposit { who: 1, amount: 10 }, |
| 184 | + submitted: 1, |
| 185 | + decision_deposit: None, |
| 186 | + alarm: None, |
| 187 | + deciding: None, |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + #[test] |
| 192 | + pub fn referendum_status_v0() { |
| 193 | + // make sure the bytes of the encoded referendum v0 is decodable. |
| 194 | + let ongoing_encoded = sp_core::Bytes::from_str("0x00000000013001012a000000000000000400000100000000000000010000000000000001000000000000000a00000000000000000000000000000000000100").unwrap(); |
| 195 | + let ongoing_dec = v0::ReferendumInfoOf::<T, ()>::decode(&mut &*ongoing_encoded).unwrap(); |
| 196 | + let ongoing = v0::ReferendumInfoOf::<T, ()>::Ongoing(create_status_v0()); |
| 197 | + assert_eq!(ongoing, ongoing_dec); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn migration_v0_to_v1_works() { |
| 202 | + new_test_ext().execute_with(|| { |
| 203 | + // create and insert into the storage an ongoing referendum v0. |
| 204 | + let status_v0 = create_status_v0(); |
| 205 | + let ongoing_v0 = v0::ReferendumInfoOf::<T, ()>::Ongoing(status_v0.clone()); |
| 206 | + v0::ReferendumInfoFor::<T, ()>::insert(2, ongoing_v0); |
| 207 | + // create and insert into the storage an approved referendum v0. |
| 208 | + let approved_v0 = v0::ReferendumInfoOf::<T, ()>::Approved( |
| 209 | + 123, |
| 210 | + Deposit { who: 1, amount: 10 }, |
| 211 | + Some(Deposit { who: 2, amount: 20 }), |
| 212 | + ); |
| 213 | + v0::ReferendumInfoFor::<T, ()>::insert(5, approved_v0); |
| 214 | + // run migration from v0 to v1. |
| 215 | + v1::MigrateV0ToV1::<T, ()>::on_runtime_upgrade(); |
| 216 | + // fetch and assert migrated into v1 the ongoing referendum. |
| 217 | + let ongoing_v1 = ReferendumInfoFor::<T, ()>::get(2).unwrap(); |
| 218 | + // referendum status schema is the same for v0 and v1. |
| 219 | + assert_eq!(ReferendumInfoOf::<T, ()>::Ongoing(status_v0), ongoing_v1); |
| 220 | + // fetch and assert migrated into v1 the approved referendum. |
| 221 | + let approved_v1 = ReferendumInfoFor::<T, ()>::get(5).unwrap(); |
| 222 | + assert_eq!( |
| 223 | + approved_v1, |
| 224 | + ReferendumInfoOf::<T, ()>::Approved( |
| 225 | + 123, |
| 226 | + Some(Deposit { who: 1, amount: 10 }), |
| 227 | + Some(Deposit { who: 2, amount: 20 }) |
| 228 | + ) |
| 229 | + ); |
| 230 | + }); |
| 231 | + } |
| 232 | +} |
0 commit comments