Skip to content

Commit

Permalink
Merge branch 'master' into xcm-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak authored Jul 27, 2023
2 parents 4227f11 + 3feaf40 commit e2c4be3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 12 deletions.
2 changes: 1 addition & 1 deletion 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;
}
}
}
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.");

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

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

let mut weight = Weight::zero();

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)
}
Err(_) => {
log::info!(
target: TARGET,
"Value not migrated because it's too long: {:?}",
(session_index, old_value)
);
None
}
}
});

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

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

log::info!(target: TARGET, "Collator rewards migration: POST checks successful!");
}
}
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
10 changes: 10 additions & 0 deletions runtime/hydradx/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub struct OnRuntimeUpgradeMigration;
impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
log::info!("PreMigrate Collator Rewards Pallet start");
pallet_collator_rewards::migration::v1::pre_migrate::<Runtime>();
log::info!("PreMigrate Collator Rewards Pallet end");
log::info!("PreMigrate Genesis History Pallet start");
pallet_genesis_history::migration::v1::pre_migrate::<Runtime>();
log::info!("PreMigrate Genesis History Pallet end");
Expand All @@ -21,6 +24,9 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();

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");
log::info!("Migrate Genesis History Pallet to v1 start");
weight = weight.saturating_add(pallet_genesis_history::migration::v1::migrate::<Runtime>());
log::info!("Migrate Genesis History Pallet to v1 end");
Expand All @@ -30,6 +36,10 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {

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

log::info!("PostMigrate Genesis History Pallet start");
pallet_genesis_history::migration::v1::post_migrate::<Runtime>();
log::info!("PostMigrate Genesis History Pallet end");
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 e2c4be3

Please sign in to comment.