mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-06 04:27:28 +00:00
93e8ffed55
* Introduce preimages module in traits * Multisize Preimages * Len not actually necessary * Tweaks to the preimage API * Fixes * Get Scheduler building with new API * Scheduler tests pass * Bounded Scheduler 🎉 * Use Agenda holes and introduce IncompleteSince to avoid need to reschedule * Tests pass with new weight system * New benchmarks * Add missing file * Drop preimage when permenantly overeight * Drop preimage when permenantly overeight * Referenda uses latest preimage API * Testing ok * Adding tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fmt Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add preimage migration Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Docs * Remove dbg * Refactor Democracy * Refactor Democracy * Add final MEL * Remove silly maps * Fixes * Minor refactor * Formatting * Fixes * Fixes * Fixes * Update frame/preimage/src/lib.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Add migrations to Democracy * WIP Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Resolve conflicts Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Revert "Resolve conflicts" This reverts commit 734d66d69e54553471ffa54fa52e3e304dc8f106. * Undo wrong resolves... Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * WIP Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Make compile Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * massage clippy Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More clippy Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * clippy annoyance Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * clippy annoyance Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix benchmarks Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * add missing file * Test <Preimage as QueryPreimage> Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Clippy harassment Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * clippy Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fixup tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove old stuff Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fmt Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Test <Scheduler as Anon> trait functions Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update pallet-ui tests Why is this needed? Should not be the case unless master is broken... Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More scheduler trait test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Apply review suggestion Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Beauty fixes Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add Scheduler test migration_v3_to_v4_works Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Merge fixup Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Keep referenda benchmarks instantiatable Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update weights Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use new scheduler weight functions Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use new democracy weight functions Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use weight compare functions Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update pallet-ui tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More renaming… Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * More renaming… Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add comment Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Implement OnRuntimeUpgrade for scheduler::v3_to_v4 migration Put the migration into a proper `MigrateToV4` struct and implement the OnRuntimeUpgrade hooks for it. Also move the test to use that instead. This should make it easier for adding it to Polkadot. Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Clippy Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Handle undecodable Agendas Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove trash Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use new OnRuntimeUpgrade functions Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix BoundedSlice::truncate_from Co-authored-by: jakoblell Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix pre_upgrade hook return values Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Add more error logging Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Find too large preimages in the pre_upgrade hook Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Test that too large Calls in agendas are ignored Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Use new OnRuntimeUpgrade hooks Why did the CI not catch this?! Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * works fine - just more logs Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix staking migration Causing issues on Kusama... Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix UI tests No idea why this is needed. This is actually undoing an earlier change. Maybe the CI has different rustc versions!? Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove multisig's Calls (#12072) * Remove multisig's Calls * Multisig: Fix tests and re-introduce reserve logic (#12241) * Fix tests and re-introduce reserve logic * fix benches * add todo * remove irrelevant bench * [Feature] Add a migration that drains and refunds stored calls (#12313) * [Feature] Add a migration that drains and refunds stored calls * migration fixes * fixes * address review comments * consume the whole block weight * fix assertions * license header * fix interface Co-authored-by: parity-processbot <> Co-authored-by: parity-processbot <> Co-authored-by: Roman Useinov <roman.useinov@gmail.com> * Fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix multisig benchmarks Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * ".git/.scripts/bench-bot.sh" pallet dev pallet_democracy * ".git/.scripts/bench-bot.sh" pallet dev pallet_scheduler * ".git/.scripts/bench-bot.sh" pallet dev pallet_preimage Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: parity-processbot <> Co-authored-by: Roman Useinov <roman.useinov@gmail.com>
237 lines
7.4 KiB
Rust
237 lines
7.4 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Storage migrations for the preimage pallet.
|
|
|
|
use super::*;
|
|
use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade, BoundedVec};
|
|
use sp_core::H256;
|
|
|
|
/// The log target.
|
|
const TARGET: &'static str = "runtime::democracy::migration::v1";
|
|
|
|
/// The original data layout of the democracy pallet without a specific version number.
|
|
mod v0 {
|
|
use super::*;
|
|
|
|
#[storage_alias]
|
|
pub type PublicProps<T: Config> = StorageValue<
|
|
Pallet<T>,
|
|
Vec<(PropIndex, <T as frame_system::Config>::Hash, <T as frame_system::Config>::AccountId)>,
|
|
ValueQuery,
|
|
>;
|
|
|
|
#[storage_alias]
|
|
pub type NextExternal<T: Config> =
|
|
StorageValue<Pallet<T>, (<T as frame_system::Config>::Hash, VoteThreshold)>;
|
|
|
|
#[cfg(feature = "try-runtime")]
|
|
#[storage_alias]
|
|
pub type ReferendumInfoOf<T: Config> = StorageMap<
|
|
Pallet<T>,
|
|
frame_support::Twox64Concat,
|
|
ReferendumIndex,
|
|
ReferendumInfo<
|
|
<T as frame_system::Config>::BlockNumber,
|
|
<T as frame_system::Config>::Hash,
|
|
BalanceOf<T>,
|
|
>,
|
|
>;
|
|
}
|
|
|
|
pub mod v1 {
|
|
use super::*;
|
|
|
|
/// Migration for translating bare `Hash`es into `Bounded<Call>`s.
|
|
pub struct Migration<T>(sp_std::marker::PhantomData<T>);
|
|
|
|
impl<T: Config + frame_system::Config<Hash = H256>> OnRuntimeUpgrade for Migration<T> {
|
|
#[cfg(feature = "try-runtime")]
|
|
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
|
|
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "can only upgrade from version 0");
|
|
|
|
let props_count = v0::PublicProps::<T>::get().len();
|
|
log::info!(target: TARGET, "{} public proposals will be migrated.", props_count,);
|
|
ensure!(props_count <= T::MaxProposals::get() as usize, "too many proposals");
|
|
|
|
let referenda_count = v0::ReferendumInfoOf::<T>::iter().count();
|
|
log::info!(target: TARGET, "{} referenda will be migrated.", referenda_count);
|
|
|
|
Ok((props_count as u32, referenda_count as u32).encode())
|
|
}
|
|
|
|
#[allow(deprecated)]
|
|
fn on_runtime_upgrade() -> Weight {
|
|
let mut weight = T::DbWeight::get().reads(1);
|
|
if StorageVersion::get::<Pallet<T>>() != 0 {
|
|
log::warn!(
|
|
target: TARGET,
|
|
"skipping on_runtime_upgrade: executed on wrong storage version.\
|
|
Expected version 0"
|
|
);
|
|
return weight
|
|
}
|
|
|
|
ReferendumInfoOf::<T>::translate(
|
|
|index, old: ReferendumInfo<T::BlockNumber, T::Hash, BalanceOf<T>>| {
|
|
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
|
|
log::info!(target: TARGET, "migrating referendum #{:?}", &index);
|
|
Some(match old {
|
|
ReferendumInfo::Ongoing(status) =>
|
|
ReferendumInfo::Ongoing(ReferendumStatus {
|
|
end: status.end,
|
|
proposal: Bounded::from_legacy_hash(status.proposal),
|
|
threshold: status.threshold,
|
|
delay: status.delay,
|
|
tally: status.tally,
|
|
}),
|
|
ReferendumInfo::Finished { approved, end } =>
|
|
ReferendumInfo::Finished { approved, end },
|
|
})
|
|
},
|
|
);
|
|
|
|
let props = v0::PublicProps::<T>::take()
|
|
.into_iter()
|
|
.map(|(i, hash, a)| (i, Bounded::from_legacy_hash(hash), a))
|
|
.collect::<Vec<_>>();
|
|
let bounded = BoundedVec::<_, T::MaxProposals>::truncate_from(props.clone());
|
|
PublicProps::<T>::put(bounded);
|
|
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
|
|
|
|
if props.len() as u32 > T::MaxProposals::get() {
|
|
log::error!(
|
|
target: TARGET,
|
|
"truncated {} public proposals to {}; continuing",
|
|
props.len(),
|
|
T::MaxProposals::get()
|
|
);
|
|
}
|
|
|
|
if let Some((hash, threshold)) = v0::NextExternal::<T>::take() {
|
|
log::info!(target: TARGET, "migrating next external proposal");
|
|
NextExternal::<T>::put((Bounded::from_legacy_hash(hash), threshold));
|
|
}
|
|
|
|
StorageVersion::new(1).put::<Pallet<T>>();
|
|
|
|
weight.saturating_add(T::DbWeight::get().reads_writes(1, 2))
|
|
}
|
|
|
|
#[cfg(feature = "try-runtime")]
|
|
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
|
|
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "must upgrade");
|
|
|
|
let (old_props_count, old_ref_count): (u32, u32) =
|
|
Decode::decode(&mut &state[..]).expect("pre_upgrade provides a valid state; qed");
|
|
let new_props_count = crate::PublicProps::<T>::get().len() as u32;
|
|
assert_eq!(new_props_count, old_props_count, "must migrate all public proposals");
|
|
let new_ref_count = crate::ReferendumInfoOf::<T>::iter().count() as u32;
|
|
assert_eq!(new_ref_count, old_ref_count, "must migrate all referenda");
|
|
|
|
log::info!(
|
|
target: TARGET,
|
|
"{} public proposals migrated, {} referenda migrated",
|
|
new_props_count,
|
|
new_ref_count,
|
|
);
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[cfg(feature = "try-runtime")]
|
|
mod test {
|
|
use super::*;
|
|
use crate::{
|
|
tests::{Test as T, *},
|
|
types::*,
|
|
};
|
|
use frame_support::bounded_vec;
|
|
|
|
#[allow(deprecated)]
|
|
#[test]
|
|
fn migration_works() {
|
|
new_test_ext().execute_with(|| {
|
|
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0);
|
|
// Insert some values into the v0 storage:
|
|
|
|
// Case 1: Ongoing referendum
|
|
let hash = H256::repeat_byte(1);
|
|
let status = ReferendumStatus {
|
|
end: 1u32.into(),
|
|
proposal: hash.clone(),
|
|
threshold: VoteThreshold::SuperMajorityApprove,
|
|
delay: 1u32.into(),
|
|
tally: Tally { ayes: 1u32.into(), nays: 1u32.into(), turnout: 1u32.into() },
|
|
};
|
|
v0::ReferendumInfoOf::<T>::insert(1u32, ReferendumInfo::Ongoing(status));
|
|
|
|
// Case 2: Finished referendum
|
|
v0::ReferendumInfoOf::<T>::insert(
|
|
2u32,
|
|
ReferendumInfo::Finished { approved: true, end: 123u32.into() },
|
|
);
|
|
|
|
// Case 3: Public proposals
|
|
let hash2 = H256::repeat_byte(2);
|
|
v0::PublicProps::<T>::put(vec![
|
|
(3u32, hash.clone(), 123u64),
|
|
(4u32, hash2.clone(), 123u64),
|
|
]);
|
|
|
|
// Case 4: Next external
|
|
v0::NextExternal::<T>::put((hash.clone(), VoteThreshold::SuperMajorityApprove));
|
|
|
|
// Migrate.
|
|
let state = v1::Migration::<T>::pre_upgrade().unwrap();
|
|
let _weight = v1::Migration::<T>::on_runtime_upgrade();
|
|
v1::Migration::<T>::post_upgrade(state).unwrap();
|
|
// Check that all values got migrated.
|
|
|
|
// Case 1: Ongoing referendum
|
|
assert_eq!(
|
|
ReferendumInfoOf::<T>::get(1u32),
|
|
Some(ReferendumInfo::Ongoing(ReferendumStatus {
|
|
end: 1u32.into(),
|
|
proposal: Bounded::from_legacy_hash(hash),
|
|
threshold: VoteThreshold::SuperMajorityApprove,
|
|
delay: 1u32.into(),
|
|
tally: Tally { ayes: 1u32.into(), nays: 1u32.into(), turnout: 1u32.into() },
|
|
}))
|
|
);
|
|
// Case 2: Finished referendum
|
|
assert_eq!(
|
|
ReferendumInfoOf::<T>::get(2u32),
|
|
Some(ReferendumInfo::Finished { approved: true, end: 123u32.into() })
|
|
);
|
|
// Case 3: Public proposals
|
|
let props: BoundedVec<_, <Test as Config>::MaxProposals> = bounded_vec![
|
|
(3u32, Bounded::from_legacy_hash(hash), 123u64),
|
|
(4u32, Bounded::from_legacy_hash(hash2), 123u64)
|
|
];
|
|
assert_eq!(PublicProps::<T>::get(), props);
|
|
// Case 4: Next external
|
|
assert_eq!(
|
|
NextExternal::<T>::get(),
|
|
Some((Bounded::from_legacy_hash(hash), VoteThreshold::SuperMajorityApprove))
|
|
);
|
|
});
|
|
}
|
|
}
|