-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add democracy migration and fix contracts migration #12
Changes from 7 commits
75f9fcf
e4c0323
2b9d1a1
acc62be
f851407
78e23ca
6f57659
3b5b074
bd4e44e
ff78c84
2b70362
01da290
141c11a
a98f24e
2f67ad3
0144dc4
332b668
05795c1
d3c5b72
0238e7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
|
||
use super::*; | ||
|
||
mod deprecated { | ||
use sp_std::prelude::*; | ||
|
||
use codec::{Encode, EncodeLike, Decode, Input, Output}; | ||
use frame_support::{decl_module, decl_storage, Parameter}; | ||
use sp_runtime::RuntimeDebug; | ||
use sp_std::convert::TryFrom; | ||
|
||
use crate::{Trait, BalanceOf, PropIndex, ReferendumIndex, Conviction, vote_threshold::VoteThreshold}; | ||
|
||
#[derive(Copy, Clone, Eq, PartialEq, Default, RuntimeDebug)] | ||
pub struct Vote { | ||
pub aye: bool, | ||
pub conviction: Conviction, | ||
} | ||
|
||
impl Encode for Vote { | ||
fn encode_to<T: Output>(&self, output: &mut T) { | ||
output.push_byte(u8::from(self.conviction) | if self.aye { 0b1000_0000 } else { 0 }); | ||
} | ||
} | ||
|
||
impl EncodeLike for Vote {} | ||
|
||
impl Decode for Vote { | ||
fn decode<I: Input>(input: &mut I) -> core::result::Result<Self, codec::Error> { | ||
let b = input.read_byte()?; | ||
Ok(Vote { | ||
aye: (b & 0b1000_0000) == 0b1000_0000, | ||
conviction: Conviction::try_from(b & 0b0111_1111) | ||
.map_err(|_| codec::Error::from("Invalid conviction"))?, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] | ||
pub struct ReferendumInfo<BlockNumber: Parameter, Hash: Parameter> { | ||
/// When voting on this referendum will end. | ||
end: BlockNumber, | ||
/// The hash of the proposal being voted on. | ||
proposal_hash: Hash, | ||
/// The thresholding mechanism to determine whether it passed. | ||
threshold: VoteThreshold, | ||
/// The delay (in blocks) to wait after a successful referendum before deploying. | ||
delay: BlockNumber, | ||
} | ||
|
||
decl_module! { | ||
pub struct Module<T: Trait> for enum Call where origin: T::Origin { } | ||
} | ||
decl_storage! { | ||
trait Store for Module<T: Trait> as Democracy { | ||
pub VotersFor get(fn voters_for): | ||
map hasher(opaque_blake2_256) ReferendumIndex => Vec<T::AccountId>; | ||
pub VoteOf get(fn vote_of): | ||
map hasher(opaque_blake2_256) (ReferendumIndex, T::AccountId) => Vote; | ||
pub Proxy get(fn proxy): | ||
map hasher(opaque_blake2_256) T::AccountId => Option<T::AccountId>; | ||
pub Delegations get(fn delegations): | ||
map hasher(opaque_blake2_256) T::AccountId => (T::AccountId, Conviction); | ||
|
||
// Note these actually used to be `blake2_256`, but this way we can migrate them | ||
// to then make use of them in the other migration. | ||
pub ReferendumInfoOf get(fn referendum_info): | ||
map hasher(twox_64_concat) ReferendumIndex | ||
=> Option<ReferendumInfo<T::BlockNumber, T::Hash>>; | ||
|
||
pub DepositOf get(fn deposit_of): | ||
map hasher(opaque_blake2_256) PropIndex => Option<(BalanceOf<T>, Vec<T::AccountId>)>; | ||
} | ||
} | ||
} | ||
|
||
pub fn migrate_account<T: Trait>(a: &T::AccountId) { | ||
Locks::<T>::migrate_key_from_blake(a); | ||
} | ||
|
||
pub fn migrate_all<T: Trait>() -> Weight { | ||
sp_runtime::print("🕊️ Migrating Democracy..."); | ||
let mut weight = 0; | ||
sp_runtime::print("Democracy: Hasher"); | ||
weight += migrate_hasher::<T>(); | ||
sp_runtime::print("Democracy: Remove Unused"); | ||
weight += migrate_remove_unused_storage::<T>(); | ||
sp_runtime::print("Democracy: ReferendumInfo"); | ||
weight += migrate_referendum_info::<T>(); | ||
sp_runtime::print("🕊️ Done Democracy."); | ||
weight | ||
} | ||
|
||
pub fn migrate_hasher<T: Trait>() -> Weight { | ||
// TODO: is this valid? | ||
Blacklist::<T>::remove_all(); | ||
Cancellations::<T>::remove_all(); | ||
// Note this only migrates the hasher, `ReferendumInfoOf` is fully migrated in | ||
// `migrate_referendum_info`. | ||
sp_runtime::print("Democracy: Hasher: ReferendumInfo"); | ||
for i in LowestUnbaked::get()..ReferendumCount::get() { | ||
deprecated::ReferendumInfoOf::<T>::migrate_key_from_blake(i); | ||
} | ||
sp_runtime::print("Democracy: Hasher: PublicProps"); | ||
for (p, h, _) in PublicProps::<T>::get().into_iter() { | ||
// based on [democracy weights PR](https://github.com/paritytech/substrate/pull/5828/) | ||
frame_support::runtime_print!("PublicProps key: {:?}", deprecated::DepositOf::<T>::hashed_key_for(p)); | ||
if let Some((balance, accounts)) = deprecated::DepositOf::<T>::take(p) { | ||
DepositOf::<T>::insert(p, (accounts, balance)); | ||
} | ||
Preimages::<T>::migrate_key_from_blake(h); | ||
} | ||
// TODO: figure out actual weight | ||
0 | ||
} | ||
|
||
pub fn migrate_remove_unused_storage<T: Trait>() -> Weight { | ||
// TODO: is this valid? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we delete things after we move them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where VotersFor is defined or if we have it on our substrate commit that mainnet is pegged to. What are the potential reprecussions of removing these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nope, the ones deleted here are not moved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The repercussions I can see are the following: This migration just drops some state that currently doesn't exist on edgeware. If people were to create proposals and vote on them while this migration is in process, some data might be lost. AFAICT this would not corrupt any state, people would just have to vote again. |
||
deprecated::VotersFor::<T>::remove_all(); | ||
deprecated::VoteOf::<T>::remove_all(); | ||
deprecated::Proxy::<T>::remove_all(); | ||
deprecated::Delegations::<T>::remove_all(); | ||
// TODO: figure out actual weight | ||
0 | ||
} | ||
|
||
// migration based on [substrate/#5294](https://github.com/paritytech/substrate/pull/5294) | ||
pub fn migrate_referendum_info<T: Trait>() -> Weight { | ||
use frame_support::{Twox64Concat, migration::{StorageKeyIterator}}; | ||
|
||
let range = LowestUnbaked::get()..ReferendumCount::get(); | ||
for (index, (end, proposal_hash, threshold, delay)) | ||
in StorageKeyIterator::< | ||
ReferendumIndex, | ||
(T::BlockNumber, T::Hash, VoteThreshold, T::BlockNumber), | ||
Twox64Concat, | ||
>::new(b"Democracy", b"ReferendumInfoOf").drain() | ||
{ | ||
if range.contains(&index) { | ||
let status = ReferendumStatus { | ||
end, proposal_hash, threshold, delay, tally: Tally::default() | ||
}; | ||
ReferendumInfoOf::<T>::insert(index, ReferendumInfo::Ongoing(status)) | ||
} | ||
} | ||
// TODO: figure out actual weight | ||
0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far, we haven't had an vetos/cancellations. Should be fine!