Skip to content

Commit

Permalink
Refactor away from opaque hashes (paritytech#5226)
Browse files Browse the repository at this point in the history
* System.BlockHash

* Fix hash

* Introduce K/V iteration in all _concat maps

Also move across:
- System.Account (blake2_128_concat)
- Balances.Locks (twox_64_concat)
- ElectionsPhragmen.VotesOf (twox_64_concat)
- ElectionsPhragmen.StakeOf (twox_64_concat)
- Identity.IdentityOf (twox_64_concat)
- Identity.SubsOf (twox_64_concat)
- Society.Payouts (twox_64_concat)
- Session.NextKeys (twox_64_concat)
- Identity.SuperOf (blake2_128_concat)
- Session.KeyOwner (blake2_128_concat)
- Society.SuspendedCandidates (twox_64_concat)
- Society.SuspendedMembers (twox_64_concat)
- Society.Vouching (twox_64_concat)
- Society.Strikes (twox_64_concat)
- System.EventTopics
- Balances.Account

* Build fixes

* Ensure migration happens in correct order

* Staking.*

* Vesting.* Offences.*

* Democracy.*

* Babe.* Collective.*

* Grandpa.*

* Assets.* Benchmark.* Contracts.* Elections.* Asset.* Nicks.*

Also introduce real account list

* ImOnline.*

* Treasury.*

* Recovery.*

* Final bits.

* Docs

* Fix one test

* Fix test

* All passing except the UI tests

* Remove linked_map part 1

* Remove linked_map

* Some iterator utils for double maps.

* Remove old migrations

* Introduce tombstone for LinkedMap type

* Migration for genesis hash

* Fix build

* Fix hash

* Rename Map is_linked -> unused, keeping backwards compat (paritytech#5256)

* Update frame/balances/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/elections/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Remove old migration code.

* Update frame/system/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update bin/node/runtime/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Fix hash

* fix session migration

* Fix watning

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
  • Loading branch information
4 people authored and General-Beck committed Mar 17, 2020
1 parent 1dfcc88 commit a85de98
Show file tree
Hide file tree
Showing 94 changed files with 1,112 additions and 2,021 deletions.
2 changes: 1 addition & 1 deletion bin/node-template/pallets/template/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl system::Trait for Runtime {
/// This type is being generated by `construct_runtime!`.
type ModuleToIndex = ModuleToIndex;
/// What to do if a new account is created.
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
/// What to do if an account is fully reaped from the system.
type OnKilledAccount = ();
/// The data to be stored in an account.
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl frame_system::Trait for Runtime {
type Version = Version;
type ModuleToIndex = ModuleToIndex;
type AccountData = pallet_balances::AccountData<Balance>;
type MigrateAccount = (Balances, Identity, Democracy, Elections, ImOnline, Recovery, Session, Society, Staking, Vesting);
type OnNewAccount = ();
type OnKilledAccount = ();
}
Expand Down
6 changes: 3 additions & 3 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ decl_error! {
decl_storage! {
trait Store for Module<T: Trait> as Assets {
/// The number of units of assets held by any given account.
Balances: map hasher(blake2_256) (T::AssetId, T::AccountId) => T::Balance;
Balances: map hasher(blake2_128_concat) (T::AssetId, T::AccountId) => T::Balance;
/// The next asset identifier up for grabs.
NextAssetId get(fn next_asset_id): T::AssetId;
/// The total unit supply of an asset.
TotalSupply: map hasher(blake2_256) T::AssetId => T::Balance;
TotalSupply: map hasher(twox_64_concat) T::AssetId => T::Balance;
}
}

Expand Down Expand Up @@ -294,7 +294,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
2 changes: 1 addition & 1 deletion frame/authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
8 changes: 7 additions & 1 deletion frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ decl_storage! {
/// We reset all segments and return to `0` at the beginning of every
/// epoch.
SegmentIndex build(|_| 0): u32;
UnderConstruction: map hasher(blake2_256) u32 => Vec<[u8; 32 /* VRF_OUTPUT_LENGTH */]>;
UnderConstruction: map hasher(twox_64_concat) u32 => Vec<[u8; 32 /* VRF_OUTPUT_LENGTH */]>;

/// Temporary value (cleared at block finalization) which is `Some`
/// if per-block initialization has already been called for current block.
Expand Down Expand Up @@ -188,6 +188,12 @@ decl_module! {
Self::deposit_vrf_output(&vrf_output);
}
}

fn on_runtime_upgrade() {
for i in 0..=SegmentIndex::get() {
UnderConstruction::migrate_key_from_blake(i);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl frame_system::Trait for Test {
type MaximumBlockLength = MaximumBlockLength;
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
14 changes: 10 additions & 4 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ use frame_support::{
Currency, OnKilledAccount, OnUnbalanced, TryDrop, StoredMap,
WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status, MigrateAccount,
}
};
use sp_runtime::{
Expand Down Expand Up @@ -372,11 +372,11 @@ decl_storage! {
/// is ever zero, then the entry *MUST* be removed.
///
/// NOTE: This is only used in the case that this module is used to store balances.
pub Account: map hasher(blake2_256) T::AccountId => AccountData<T::Balance>;
pub Account: map hasher(blake2_128_concat) T::AccountId => AccountData<T::Balance>;

/// Any liquidity locks on some account balances.
/// NOTE: Should only be accessed when setting, changing and freeing a lock.
pub Locks get(fn locks): map hasher(blake2_256) T::AccountId => Vec<BalanceLock<T::Balance>>;
pub Locks get(fn locks): map hasher(blake2_128_concat) T::AccountId => Vec<BalanceLock<T::Balance>>;

/// Storage version of the pallet.
///
Expand Down Expand Up @@ -531,6 +531,12 @@ decl_module! {
}
}

impl<T: Trait<I>, I: Instance> MigrateAccount<T::AccountId> for Module<T, I> {
fn migrate_account(account: &T::AccountId) {
Locks::<T, I>::migrate_key_from_blake(account);
}
}

impl<T: Trait<I>, I: Instance> Module<T, I> {
// PRIVATE MUTABLES

Expand Down Expand Up @@ -850,7 +856,7 @@ impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
type AvailableBlockRatio = T::AvailableBlockRatio;
type Version = T::Version;
type ModuleToIndex = T::ModuleToIndex;
type OnNewAccount = T::OnNewAccount;
type MigrateAccount = (); type OnNewAccount = T::OnNewAccount;
type OnKilledAccount = T::OnKilledAccount;
type AccountData = T::AccountData;
}
Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = super::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = Module<Test>;
}
parameter_types! {
Expand Down
6 changes: 3 additions & 3 deletions frame/benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub trait Trait: system::Trait {
decl_storage! {
trait Store for Module<T: Trait> as Benchmark {
MyMemberList: Vec<T::AccountId>;
MyMemberMap: map hasher(blake2_256) T::AccountId => bool;
MyMemberMap: map hasher(blake2_128_concat) T::AccountId => bool;
MyValue: u32;
MyMap: map hasher(blake2_256) u32 => u32;
MyDoubleMap: double_map hasher(blake2_256) u32, hasher(blake2_256) u32 => u32;
MyMap: map hasher(twox_64_concat) u32 => u32;
MyDoubleMap: double_map hasher(twox_64_concat) u32, hasher(identity) u32 => u32;
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}

Expand Down
21 changes: 18 additions & 3 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ decl_storage! {
pub Proposals get(fn proposals): Vec<T::Hash>;
/// Actual proposal for a given hash, if it's current.
pub ProposalOf get(fn proposal_of):
map hasher(blake2_256) T::Hash => Option<<T as Trait<I>>::Proposal>;
map hasher(identity) T::Hash => Option<<T as Trait<I>>::Proposal>;
/// Votes on a given proposal, if it is ongoing.
pub Voting get(fn voting):
map hasher(blake2_256) T::Hash => Option<Votes<T::AccountId, T::BlockNumber>>;
map hasher(identity) T::Hash => Option<Votes<T::AccountId, T::BlockNumber>>;
/// Proposals so far.
pub ProposalCount get(fn proposal_count): u32;
/// The current members of the collective. This is stored sorted (just by value).
Expand Down Expand Up @@ -168,6 +168,17 @@ decl_error! {
}
}

mod migration {
use super::*;

pub fn migrate<T: Trait<I>, I: Instance>() {
for p in Proposals::<T, I>::get().into_iter() {
ProposalOf::<T, I>::migrate_key_from_blake(&p);
Voting::<T, I>::migrate_key_from_blake(&p);
}
}
}

// Note: this module is not benchmarked. The weights are obtained based on the similarity of the
// executed logic with other democracy function. Note that councillor operations are assigned to the
// operational class.
Expand All @@ -177,6 +188,10 @@ decl_module! {

fn deposit_event() = default;

fn on_runtime_upgrade() {
migration::migrate::<T, I>();
}

/// Set the collective's membership.
///
/// - `new_members`: The new member list. Be nice to the chain and
Expand Down Expand Up @@ -535,7 +550,7 @@ mod tests {
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = ();
}
impl Trait<Instance1> for Test {
Expand Down
11 changes: 3 additions & 8 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ mod rent;

#[cfg(test)]
mod tests;
mod migration;

use crate::exec::ExecutionContext;
use crate::account_db::{AccountDb, DirectAccountDb};
Expand Down Expand Up @@ -667,10 +666,6 @@ decl_module! {
fn on_finalize() {
GasSpent::kill();
}

fn on_runtime_upgrade() {
migration::on_runtime_upgrade::<T>()
}
}
}

Expand Down Expand Up @@ -934,13 +929,13 @@ decl_storage! {
/// Current cost schedule for contracts.
CurrentSchedule get(fn current_schedule) config(): Schedule = Schedule::default();
/// A mapping from an original code hash to the original code, untouched by instrumentation.
pub PristineCode: map hasher(blake2_256) CodeHash<T> => Option<Vec<u8>>;
pub PristineCode: map hasher(identity) CodeHash<T> => Option<Vec<u8>>;
/// A mapping between an original code hash and instrumented wasm code, ready for execution.
pub CodeStorage: map hasher(blake2_256) CodeHash<T> => Option<wasm::PrefabWasmModule>;
pub CodeStorage: map hasher(identity) CodeHash<T> => Option<wasm::PrefabWasmModule>;
/// The subtrie counter.
pub AccountCounter: u64 = 0;
/// The code associated with a given account.
pub ContractInfoOf: map hasher(blake2_256) T::AccountId => Option<ContractInfo<T>>;
pub ContractInfoOf: map hasher(twox_64_concat) T::AccountId => Option<ContractInfo<T>>;
/// The price of one unit of gas.
GasPrice get(fn gas_price) config(): BalanceOf<T> = 1.into();
}
Expand Down
62 changes: 0 additions & 62 deletions frame/contracts/src/migration.rs

This file was deleted.

2 changes: 1 addition & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl frame_system::Trait for Test {
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type MigrateAccount = (); type OnNewAccount = ();
type OnKilledAccount = Contracts;
}
impl pallet_balances::Trait for Test {
Expand Down
Loading

0 comments on commit a85de98

Please sign in to comment.