use super::*;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade};
use log;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
pub mod v0 {
use super::*;
#[cfg(test)]
pub(super) use super::{ReferendumStatus, ReferendumStatusOf};
pub type ReferendumInfoOf<T, I> = ReferendumInfo<
TrackIdOf<T, I>,
PalletsOriginOf<T>,
frame_system::pallet_prelude::BlockNumberFor<T>,
BoundedCallOf<T, I>,
BalanceOf<T, I>,
TallyOf<T, I>,
<T as frame_system::Config>::AccountId,
ScheduleAddressOf<T, I>,
>;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum ReferendumInfo<
TrackId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
RuntimeOrigin: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Moment: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone + EncodeLike,
Call: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Balance: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
Tally: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
AccountId: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
ScheduleAddress: Eq + PartialEq + Debug + Encode + Decode + TypeInfo + Clone,
> {
Ongoing(
ReferendumStatus<
TrackId,
RuntimeOrigin,
Moment,
Call,
Balance,
Tally,
AccountId,
ScheduleAddress,
>,
),
Approved(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
Rejected(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
Cancelled(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
TimedOut(Moment, Deposit<AccountId, Balance>, Option<Deposit<AccountId, Balance>>),
Killed(Moment),
}
#[storage_alias]
pub type ReferendumInfoFor<T: Config<I>, I: 'static> =
StorageMap<Pallet<T, I>, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf<T, I>>;
}
pub mod v1 {
use super::*;
const TARGET: &'static str = "runtime::referenda::migration::v1";
pub struct MigrateV0ToV1<T, I = ()>(PhantomData<(T, I)>);
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for MigrateV0ToV1<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
ensure!(onchain_version == 0, "migration from version 0 to 1.");
let referendum_count = v0::ReferendumInfoFor::<T, I>::iter().count();
log::info!(
target: TARGET,
"pre-upgrade state contains '{}' referendums.",
referendum_count
);
Ok((referendum_count as u32).encode())
}
fn on_runtime_upgrade() -> Weight {
let current_version = Pallet::<T, I>::current_storage_version();
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
let mut weight = T::DbWeight::get().reads(1);
log::info!(
target: TARGET,
"running migration with current storage version {:?} / onchain {:?}.",
current_version,
onchain_version
);
if onchain_version != 0 {
log::warn!(target: TARGET, "skipping migration from v0 to v1.");
return weight
}
v0::ReferendumInfoFor::<T, I>::iter().for_each(|(key, value)| {
let maybe_new_value = match value {
v0::ReferendumInfo::Ongoing(_) | v0::ReferendumInfo::Killed(_) => None,
v0::ReferendumInfo::Approved(e, s, d) =>
Some(ReferendumInfo::Approved(e, Some(s), d)),
v0::ReferendumInfo::Rejected(e, s, d) =>
Some(ReferendumInfo::Rejected(e, Some(s), d)),
v0::ReferendumInfo::Cancelled(e, s, d) =>
Some(ReferendumInfo::Cancelled(e, Some(s), d)),
v0::ReferendumInfo::TimedOut(e, s, d) =>
Some(ReferendumInfo::TimedOut(e, Some(s), d)),
};
if let Some(new_value) = maybe_new_value {
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
log::info!(target: TARGET, "migrating referendum #{:?}", &key);
ReferendumInfoFor::<T, I>::insert(key, new_value);
} else {
weight.saturating_accrue(T::DbWeight::get().reads(1));
}
});
StorageVersion::new(1).put::<Pallet<T, I>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
weight
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
ensure!(onchain_version == 1, "must upgrade from version 0 to 1.");
let pre_referendum_count: u32 = Decode::decode(&mut &state[..])
.expect("failed to decode the state from pre-upgrade.");
let post_referendum_count = ReferendumInfoFor::<T, I>::iter().count() as u32;
ensure!(post_referendum_count == pre_referendum_count, "must migrate all referendums.");
log::info!(target: TARGET, "migrated all referendums.");
Ok(())
}
}
}
#[cfg(test)]
pub mod test {
use super::*;
use crate::mock::{Test as T, *};
use core::str::FromStr;
fn create_status_v0() -> v0::ReferendumStatusOf<T, ()> {
let origin: OriginCaller = frame_system::RawOrigin::Root.into();
let track = <T as Config<()>>::Tracks::track_for(&origin).unwrap();
v0::ReferendumStatusOf::<T, ()> {
track,
in_queue: true,
origin,
proposal: set_balance_proposal_bounded(1),
enactment: DispatchTime::At(1),
tally: TallyOf::<T, ()>::new(track),
submission_deposit: Deposit { who: 1, amount: 10 },
submitted: 1,
decision_deposit: None,
alarm: None,
deciding: None,
}
}
#[test]
pub fn referendum_status_v0() {
let ongoing_encoded = sp_core::Bytes::from_str("0x00000000012c01082a0000000000000004000100000000000000010000000000000001000000000000000a00000000000000000000000000000000000100").unwrap();
let ongoing_dec = v0::ReferendumInfoOf::<T, ()>::decode(&mut &*ongoing_encoded).unwrap();
let ongoing = v0::ReferendumInfoOf::<T, ()>::Ongoing(create_status_v0());
assert_eq!(ongoing, ongoing_dec);
}
#[test]
fn migration_v0_to_v1_works() {
ExtBuilder::default().build_and_execute(|| {
let status_v0 = create_status_v0();
let ongoing_v0 = v0::ReferendumInfoOf::<T, ()>::Ongoing(status_v0.clone());
ReferendumCount::<T, ()>::mutate(|x| x.saturating_inc());
v0::ReferendumInfoFor::<T, ()>::insert(2, ongoing_v0);
let approved_v0 = v0::ReferendumInfoOf::<T, ()>::Approved(
123,
Deposit { who: 1, amount: 10 },
Some(Deposit { who: 2, amount: 20 }),
);
ReferendumCount::<T, ()>::mutate(|x| x.saturating_inc());
v0::ReferendumInfoFor::<T, ()>::insert(5, approved_v0);
v1::MigrateV0ToV1::<T, ()>::on_runtime_upgrade();
let ongoing_v1 = ReferendumInfoFor::<T, ()>::get(2).unwrap();
assert_eq!(ReferendumInfoOf::<T, ()>::Ongoing(status_v0), ongoing_v1);
let approved_v1 = ReferendumInfoFor::<T, ()>::get(5).unwrap();
assert_eq!(
approved_v1,
ReferendumInfoOf::<T, ()>::Approved(
123,
Some(Deposit { who: 1, amount: 10 }),
Some(Deposit { who: 2, amount: 20 })
)
);
});
}
}