Skip to content

Commit

Permalink
remove without_storage_info macro and add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Jul 17, 2023
1 parent f99f258 commit 352ad0b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pallets/collator-rewards/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-collator-rewards"
version = "1.0.4"
version = "1.0.5"
description = "Pallet for collator rewards"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
20 changes: 16 additions & 4 deletions pallets/collator-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ mod mock;
#[cfg(test)]
mod tests;

use frame_support::traits::Get;
pub mod migration;

use frame_support::{traits::Get, BoundedVec};

use orml_traits::MultiCurrency;
use pallet_session::SessionManager;
Expand All @@ -42,7 +44,6 @@ pub mod pallet {
use frame_system::pallet_prelude::BlockNumberFor;

#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
Expand Down Expand Up @@ -80,6 +81,9 @@ pub mod pallet {
/// The session manager this pallet will wrap that provides the collator account list on
/// `new_session`.
type SessionManager: SessionManager<Self::AccountId>;

/// Max candidates
type MaxCandidates: Get<u32>;
}

#[pallet::error]
Expand All @@ -99,14 +103,22 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn collators)]
/// Stores the collators per session (index).
pub type Collators<T: Config> = StorageMap<_, Twox64Concat, SessionIndex, Vec<T::AccountId>, ValueQuery>;
pub type Collators<T: Config> =
StorageMap<_, Twox64Concat, SessionIndex, BoundedVec<T::AccountId, T::MaxCandidates>, ValueQuery>;
}

impl<T: Config> SessionManager<T::AccountId> for Pallet<T> {
fn new_session(index: SessionIndex) -> Option<Vec<T::AccountId>> {
let maybe_collators = T::SessionManager::new_session(index);
if let Some(ref collators) = maybe_collators {
Collators::<T>::insert(index, collators)
let maybe_collators_b = BoundedVec::<T::AccountId, T::MaxCandidates>::try_from(collators.clone());
match maybe_collators_b {
Ok(collators_b) => Collators::<T>::insert(index, collators_b),
Err(_) => {
log::warn!(target: "runtime::collator-rewards", "Error reward collators: too many collators {:?}", collators);
return None;

Check warning on line 119 in pallets/collator-rewards/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/lib.rs#L117-L119

Added lines #L117 - L119 were not covered by tests
}
}
}
maybe_collators
}
Expand Down
78 changes: 78 additions & 0 deletions pallets/collator-rewards/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This file is part of pallet-collator-rewards

// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// 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..

use super::*;
use frame_support::{
log, storage_alias,
traits::{Get, StorageVersion},
weights::Weight,
};

/// The log target.
const TARGET: &str = "runtime::collator-rewards::migration::v1";

pub mod v0 {
use super::*;
use frame_support::{pallet_prelude::ValueQuery, Twox64Concat};

#[storage_alias]
pub type Collators<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, SessionIndex, Vec<<T as frame_system::Config>::AccountId>, ValueQuery>;
}
pub mod v1 {
use super::*;

pub fn pre_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "Storage version too high.");

Check warning on line 40 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

log::info!(target: TARGET, "Collator rewards migration: PRE checks successful!");

Check warning on line 42 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L42

Added line #L42 was not covered by tests
}

pub fn migrate<T: Config>() -> Weight {
log::info!(target: TARGET, "Collator rewards to v1 for Transaction pause");

Check warning on line 46 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L45-L46

Added lines #L45 - L46 were not covered by tests

let mut weight = Weight::zero();

Check warning on line 48 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L48

Added line #L48 was not covered by tests

Collators::<T>::translate::<Vec<<T as frame_system::Config>::AccountId>, _>(|session_index, old_value| {
let maybe_collators_b =
BoundedVec::<<T as frame_system::Config>::AccountId, T::MaxCandidates>::try_from(old_value.clone());
match maybe_collators_b {
Ok(collators_b) => {
weight.saturating_accrue(T::DbWeight::get().writes(1));
Some(collators_b)

Check warning on line 56 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L50-L56

Added lines #L50 - L56 were not covered by tests
}
Err(_) => {
log::info!(
target: TARGET,
"Value not migrated because it's too long: {:?}",
(session_index, old_value)

Check warning on line 62 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L58-L62

Added lines #L58 - L62 were not covered by tests
);
None

Check warning on line 64 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L64

Added line #L64 was not covered by tests
}
}
});

StorageVersion::new(1).put::<Pallet<T>>();
weight.saturating_add(T::DbWeight::get().writes(1))

Check warning on line 70 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L69-L70

Added lines #L69 - L70 were not covered by tests
}

pub fn post_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "Unexpected storage version.");

Check warning on line 74 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L73-L74

Added lines #L73 - L74 were not covered by tests

log::info!(target: TARGET, "Collator rewards migration: POST checks successful!");

Check warning on line 76 in pallets/collator-rewards/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/collator-rewards/src/migration.rs#L76

Added line #L76 was not covered by tests
}
}
7 changes: 2 additions & 5 deletions pallets/collator-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,11 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = ();
}

parameter_types! {
pub const Offset: u64 = 0;
pub const Period: u64 = 10;
}

parameter_types! {
pub const RewardPerCollator: Balance = COLLATOR_REWARD;
pub const RewardCurrencyId: AssetId = NATIVE_TOKEN;
pub GcCollators: Vec<AccountId> = vec![GC_COLL_1, GC_COLL_2, GC_COLL_3];
pub const MaxCandidates: u32 = 50;
}

thread_local! {
Expand All @@ -155,6 +151,7 @@ impl Config for Test {
type RewardCurrencyId = RewardCurrencyId;
type ExcludedCollators = GcCollators;
type SessionManager = MockSessionManager;
type MaxCandidates = MaxCandidates;
}

#[derive(Default)]
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydradx-runtime"
version = "167.0.0"
version = "168.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
license = "Apache 2.0"
Expand Down
14 changes: 2 additions & 12 deletions runtime/hydradx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("hydradx"),
impl_name: create_runtime_str!("hydradx"),
authoring_version: 1,
spec_version: 167,
spec_version: 168,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -232,17 +232,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsReversedWithSystemFirst,
(
pallet_preimage::migration::v1::Migration<Runtime>,
pallet_democracy::migrations::v1::Migration<Runtime>,
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
DmpQueue,
XcmpQueue,
ParachainSystem,
migrations::OnRuntimeUpgradeMigration,
migrations::MigrateRegistryLocationToV3<Runtime>,
migrations::XcmRateLimitMigration,
),
(migrations::OnRuntimeUpgradeMigration,),
>;

impl_runtime_apis! {
Expand Down
26 changes: 9 additions & 17 deletions runtime/hydradx/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,28 @@ pub struct OnRuntimeUpgradeMigration;
impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
frame_support::log::info!("PreMigrate Duster Pallet start");
pallet_duster::migration::v1::pre_migrate::<Runtime, Duster>();
frame_support::log::info!("PreMigrate Duster Pallet end");
log::info!("PreMigrate Collator Rewards Pallet start");
pallet_collator_rewards::migration::v1::pre_migrate::<Runtime>();
log::info!("PreMigrate Collator Rewards Pallet end");

Check warning on line 16 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L14-L16

Added lines #L14 - L16 were not covered by tests

Ok(vec![])
}

fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();

frame_support::log::info!("Migrate Scheduler Pallet to v4 start");
weight = weight.saturating_add(Scheduler::migrate_v1_to_v4());
frame_support::log::info!("Migrate Scheduler Pallet to v4 end");

frame_support::log::info!("Migrate Duster Pallet to v1 start");
weight = weight.saturating_add(pallet_duster::migration::v1::migrate::<Runtime, Duster>(
get_all_module_accounts(),
TreasuryAccount::get(),
TreasuryAccount::get(),
));
frame_support::log::info!("Migrate Duster Pallet to v1 end");
log::info!("Migrate Collator Rewards Pallet to v1 start");
weight = weight.saturating_add(pallet_collator_rewards::migration::v1::migrate::<Runtime>());
log::info!("Migrate Collator Rewards Pallet to v1 end");

Check warning on line 26 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L24-L26

Added lines #L24 - L26 were not covered by tests

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
frame_support::log::info!("PostMigrate Duster Pallet start");
pallet_duster::migration::v1::post_migrate::<Runtime, Duster>();
frame_support::log::info!("PostMigrate Duster Pallet end");
log::info!("PostMigrate Collator Rewards Pallet start");
pallet_collator_rewards::migration::v1::post_migrate::<Runtime>();
log::info!("PostMigrate Collator Rewards Pallet end");

Check warning on line 35 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L33-L35

Added lines #L33 - L35 were not covered by tests
Ok(())
}
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/hydradx/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,10 @@ impl pallet_collator_rewards::Config for Runtime {
type RewardPerCollator = RewardPerCollator;
type RewardCurrencyId = NativeAssetId;
type ExcludedCollators = ExcludedCollators;
// We wrap the ` SessionManager` implementation of `CollatorSelection` to get the collatrs that
// We wrap the ` SessionManager` implementation of `CollatorSelection` to get the collators that
// we hand out rewards to.
type SessionManager = CollatorSelection;
type MaxCandidates = MaxInvulnerables;
}

impl pallet_transaction_pause::Config for Runtime {
Expand Down

0 comments on commit 352ad0b

Please sign in to comment.