Skip to content
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

Merged
merged 20 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 4 additions & 95 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

pub mod migration;

const DEMOCRACY_ID: LockIdentifier = *b"democrac";

/// The maximum number of vetoers on a single proposal used to compute Weight.
Expand Down Expand Up @@ -569,100 +571,7 @@ mod weight_for {

impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
fn migrate_account(a: &T::AccountId) {
mod deprecated {
use sp_std::prelude::*;

use codec::{Encode, EncodeLike, Decode, Input, Output};
use frame_support::{decl_module, decl_storage};
use sp_runtime::RuntimeDebug;
use sp_std::convert::TryFrom;

use crate::{Trait, ReferendumIndex, Conviction};

#[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"))?,
})
}
}

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 VoteOf get(fn vote_of):
map hasher(opaque_blake2_256) (ReferendumIndex, T::AccountId) => Vote;
pub Delegations get(fn delegations):
map hasher(opaque_blake2_256) T::AccountId => (T::AccountId, Conviction);
}
}
}

Locks::<T>::migrate_key_from_blake(a);
// TODO: will not actually do any useful migration
deprecated::Delegations::<T>::migrate_key_from_blake(a);
for i in LowestUnbaked::get()..ReferendumCount::get() {
// TODO: will not actually do any useful migration
deprecated::VoteOf::<T>::migrate_key_from_blake((i, a));
}
}
}

mod migration {
use super::*;

pub fn migrate<T: Trait>() -> Weight {
mod deprecated {
use super::*;

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 Proxy get(fn proxy):
map hasher(opaque_blake2_256) T::AccountId => Option<T::AccountId>;
}
}
}

// proxies were removed
deprecated::Proxy::<T>::remove_all();
Blacklist::<T>::remove_all();
Cancellations::<T>::remove_all();
for i in LowestUnbaked::get()..ReferendumCount::get() {
// TODO: will not actually do any useful migration
deprecated::VotersFor::<T>::migrate_key_from_blake(i);
ReferendumInfoOf::<T>::migrate_key_from_blake(i);
}
for (p, h, _) in PublicProps::<T>::get().into_iter() {
DepositOf::<T>::migrate_key_from_blake(p);
Preimages::<T>::migrate_key_from_blake(h);
}

// TODO: figure out actual weight
0
migration::migrate_account::<T>(a)
}
}

Expand Down Expand Up @@ -701,7 +610,7 @@ decl_module! {
fn deposit_event() = default;

fn on_runtime_upgrade() -> Weight {
migration::migrate::<T>()
migration::migrate_all::<T>()
}

/// Propose a sensitive action to be taken.
Expand Down
148 changes: 148 additions & 0 deletions frame/democracy/src/migration.rs
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?
Copy link
Collaborator

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!

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?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we delete things after we move them?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VotersFor seems empty on current edgeware. (Checked on https://polkadot.js.org/apps/#/chainstate with some indices. referendumCount is 8.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we delete things after we move them?

Nope, the ones deleted here are not moved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
2 changes: 2 additions & 0 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ impl<T: Trait> MigrateAccount<T::AccountId> for Module<T> {
}
decl_storage! {
trait Store for Module<T: Trait> as PhragmenElection {
// 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 VotesOf get(fn votes_of):
map hasher(twox_64_concat) T::AccountId => Vec<T::AccountId>;
pub StakeOf get(fn stake_of):
Expand Down
4 changes: 2 additions & 2 deletions frame/elections-phragmen/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use frame_support::{migration::{StorageKeyIterator, take_storage_item}, Twox64Concat};

pub fn migrate<T: Trait>() {
sp_runtime::print("🕊️ Migrating Election Phragmen.");
sp_runtime::print("🕊️ Migrating PhragmenElection...");
for (who, votes) in StorageKeyIterator
::<T::AccountId, Vec<T::AccountId>, Twox64Concat>
::new(b"PhragmenElection", b"VotesOf")
Expand All @@ -13,5 +13,5 @@ pub fn migrate<T: Trait>() {
sp_runtime::print("Phragmen: inserted Voting.");
}
}
sp_runtime::print("🕊️ Done Election Phragmen.");
sp_runtime::print("🕊️ Done PhragmenElection.");
}
3 changes: 2 additions & 1 deletion frame/system/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn migrate_block_hash<T: Trait>() -> Weight {
// Number - 2 is therefore the most recent block's hash that needs migrating.
let db = T::DbWeight::get();
let block_num = Number::<T>::get();
frame_support::runtime_print!("BlockNumber: {}", block_num.saturated_into::<u64>());
if block_num > One::one() {
sp_runtime::print("🕊️ Migrating BlockHashes...");
BlockHash::<T>::migrate_key_from_blake(T::BlockNumber::zero());
Expand All @@ -29,7 +30,7 @@ pub fn migrate_block_hash<T: Trait>() -> Weight {
sp_runtime::print("🕊️ Done BlockHashes");
db.reads_writes(migrations + 1, migrations)
} else {
sp_runtime::print("No BlockHashes to migrate...");
sp_runtime::print("🕊️ No BlockHashes to migrate...");
db.reads(1)
}
}
Expand Down