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

feat: add block rewards to altair and centrifuge runtimes #1342

Merged
merged 30 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0328dd0
feat: add ED funding to rewards pallet
wischli May 10, 2023
aad3cd7
feat: apply reward genesis config to dev runtime
wischli May 10, 2023
793797e
tests: apply rewards ED to block rewards
wischli May 11, 2023
e320b01
tests: add sanity intgr block rewards check
wischli May 11, 2023
cd36e63
feat: add block rewards to altair runtime
wischli May 11, 2023
1c64212
feat: add block rewards to cfg runtime
wischli May 11, 2023
7a4b92f
docs: improve blk rw migraton com
wischli May 11, 2023
afc4d54
feat: reduce collator kick threshold to 2 hours
wischli May 10, 2023
0301952
Revert "feat: reduce collator kick threshold to 2 hours"
wischli May 11, 2023
9b4dc2a
fix: re-enable transfer keep alive
wischli May 11, 2023
d2aeaa2
fix: chain_spec
wischli May 12, 2023
d0aa485
refactor: move rewards migration
wischli May 12, 2023
315bb78
feat: add session key migration
wischli May 12, 2023
e913b6f
Merge branch 'main' into feat/block-rewards-to-prod
wischli May 12, 2023
87446db
fix: mixed up tokens in spec
wischli May 12, 2023
12f08c1
refactor: rm dev migrations
wischli May 12, 2023
b87cd00
bench: fix block rewards
wischli May 15, 2023
a995225
Merge remote-tracking branch 'origin/main' into feat/block-rewards-to…
wischli May 15, 2023
2b43d2b
refactor: move migrations to sep file
wischli May 15, 2023
310f2b5
refactor: block rewards pallet id
wischli May 15, 2023
635a6af
refactor: improve pallet genesis build err log
wischli May 15, 2023
e3047d2
refactor: remove rewards domain concept (#1344)
wischli May 16, 2023
24616cf
refactor: rename LiquidityRewardsBase
wischli May 16, 2023
7d322e4
tests: add block rewards RAPI int
wischli May 16, 2023
9957c73
clippy: remove unused
wischli May 16, 2023
238ddc9
fmt: taplo
wischli May 16, 2023
4be7404
fix: dev chainspec after renaming
wischli May 16, 2023
1fb344e
refactor: apply suggestion from code review
wischli May 16, 2023
7bf4a40
Update runtime/centrifuge/src/lib.rs
wischli May 16, 2023
0fcf91c
Merge branch 'main' into feat/block-rewards-to-prod
wischli May 16, 2023
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
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions libs/types/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const CROWDLOAN_CLAIM_PALLET_ID: PalletId = PalletId(*b"cc/claim");
pub const TREASURY_PALLET_ID: PalletId = PalletId(*b"py/trsry");
pub const NFT_SALES_PALLET_ID: PalletId = PalletId(*b"pal/nfts");
pub const STAKE_POT_PALLET_ID: PalletId = PalletId(*b"PotStake");
pub const BLOCK_REWARDS_PALLET_ID: PalletId = PalletId(*b"cfg/blrw");

// Other ids
pub const CHAIN_BRIDGE_HASH_ID: [u8; 13] = *b"cent_nft_hash";
Expand Down
10 changes: 5 additions & 5 deletions pallets/block-rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ benchmarks! {
assert!(T::Rewards::is_ready(T::StakeGroupId::get()));
assert!(
!T::Rewards::compute_reward(
(
T::Domain::get(),
T::StakeCurrencyId::get(),
),
T::StakeCurrencyId::get(),
&beneficiary,
).unwrap().is_zero()
);
let before = <T as Config>::Currency::balance(CurrencyId::Native.into(), &beneficiary);

}: _(RawOrigin::Signed(caller), beneficiary.clone())
verify {
let num_collators: u128 = BlockRewards::<T>::next_session_changes().collator_count.unwrap_or(
BlockRewards::<T>::active_session_data().collator_count
).into();
// Does not get entire reward since another collator is auto-staked via genesis config
assert_eq!(<T as Config>::Currency::balance(CurrencyId::Native.into(), &beneficiary).saturating_sub(before), (REWARD / 2).into());
assert_eq!(<T as Config>::Currency::balance(CurrencyId::Native.into(), &beneficiary).saturating_sub(before), (REWARD / (num_collators + 1)).into());
}

set_collator_reward {
Expand Down
33 changes: 9 additions & 24 deletions pallets/block-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ pub struct SessionChanges<T: Config> {
total_reward: Option<T::Balance>,
}

pub(crate) type DomainIdOf<T> = <<T as Config>::Domain as TypedGet>::Type;
pub(crate) type NegativeImbalanceOf<T> = <<T as Config>::Currency as CurrencyT<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
Expand All @@ -131,9 +130,6 @@ pub mod pallet {
+ Into<<<Self as Config>::Currency as CurrencyT<Self::AccountId>>::Balance>
+ MaybeSerializeDeserialize;

/// Domain identification used by this pallet
type Domain: TypedGet;

/// Type used to handle group weights.
type Weight: Parameter + MaxEncodedLen + EnsureAdd + Unsigned + FixedPointOperand + Default;

Expand All @@ -142,11 +138,8 @@ pub mod pallet {
+ AccountRewards<
Self::AccountId,
Balance = Self::Balance,
CurrencyId = (DomainIdOf<Self>, <Self as Config>::CurrencyId),
> + CurrencyGroupChange<
GroupId = u32,
CurrencyId = (DomainIdOf<Self>, <Self as Config>::CurrencyId),
>;
CurrencyId = <Self as Config>::CurrencyId,
> + CurrencyGroupChange<GroupId = u32, CurrencyId = <Self as Config>::CurrencyId>;

/// The type used to handle currency minting and burning for collators.
type Currency: Mutate<Self::AccountId, AssetId = <Self as Config>::CurrencyId, Balance = Self::Balance>
Expand Down Expand Up @@ -258,12 +251,9 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
T::Rewards::attach_currency(
(T::Domain::get(), T::StakeCurrencyId::get()),
T::StakeGroupId::get(),
)
.map_err(|e| log::error!("Failed to attach currency to collator group: {:?}", e))
.ok();
T::Rewards::attach_currency(T::StakeCurrencyId::get(), T::StakeGroupId::get())
.map_err(|e| log::error!("Failed to attach currency to collator group: {:?}", e))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Genesis builds usually panic!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.ok();

ActiveSessionData::<T>::mutate(|session_data| {
session_data.collator_count = self.collators.len().saturated_into();
Expand Down Expand Up @@ -291,8 +281,7 @@ pub mod pallet {
pub fn claim_reward(origin: OriginFor<T>, account_id: T::AccountId) -> DispatchResult {
ensure_signed(origin)?;

T::Rewards::claim_reward((T::Domain::get(), T::StakeCurrencyId::get()), &account_id)
.map(|_| ())
T::Rewards::claim_reward(T::StakeCurrencyId::get(), &account_id).map(|_| ())
}

/// Admin method to set the reward amount for a collator used for the
Expand Down Expand Up @@ -369,18 +358,14 @@ impl<T: Config> Pallet<T> {
/// Account
pub(crate) fn do_init_collator(who: &T::AccountId) -> DispatchResult {
T::Currency::mint_into(T::StakeCurrencyId::get(), who, T::StakeAmount::get())?;
T::Rewards::deposit_stake(
(T::Domain::get(), T::StakeCurrencyId::get()),
who,
T::StakeAmount::get(),
)
T::Rewards::deposit_stake(T::StakeCurrencyId::get(), who, T::StakeAmount::get())
}

/// Withdraw currently staked amount for target address and immediately burn
/// it. Disables receiving rewards onwards.
pub(crate) fn do_exit_collator(who: &T::AccountId) -> DispatchResult {
let amount = T::Rewards::account_stake((T::Domain::get(), T::StakeCurrencyId::get()), who);
T::Rewards::withdraw_stake((T::Domain::get(), T::StakeCurrencyId::get()), who, amount)?;
let amount = T::Rewards::account_stake(T::StakeCurrencyId::get(), who);
T::Rewards::withdraw_stake(T::StakeCurrencyId::get(), who, amount)?;
T::Currency::burn_from(T::StakeCurrencyId::get(), who, amount).map(|_| ())
}

Expand Down
15 changes: 4 additions & 11 deletions pallets/block-rewards/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use frame_support::{
pallet_prelude::{StorageVersion, Weight},
traits::{Get, OnRuntimeUpgrade},
};
use sp_runtime::{traits::TypedGet, BoundedVec, SaturatedConversion};
use sp_runtime::{BoundedVec, SaturatedConversion};
use sp_std::marker::PhantomData;
#[cfg(feature = "try-runtime")]
use {
Expand Down Expand Up @@ -84,10 +84,7 @@ where
weight.saturating_accrue(T::DbWeight::get().reads(2));

<T as Config>::Rewards::attach_currency(
(
<T as Config>::Domain::get(),
<T as Config>::StakeCurrencyId::get(),
),
<T as Config>::StakeCurrencyId::get(),
<T as Config>::StakeGroupId::get(),
)
.map_err(|e| log::error!("Failed to attach currency to collator group: {:?}", e))
Expand All @@ -101,8 +98,7 @@ where
weight.saturating_accrue(T::DbWeight::get().writes(1));

for collator in collators.iter() {
// TODO: Benching preferred to be precise.
// However, not necessarily needed as num of collators <= 10.
// NOTE: Benching not required as num of collators <= 10.
lemunozm marked this conversation as resolved.
Show resolved Hide resolved
Pallet::<T>::do_init_collator(collator)
.map_err(|e| {
log::error!("Failed to init genesis collators for rewards: {:?}", e);
Expand Down Expand Up @@ -142,10 +138,7 @@ where

for collator in collators.iter() {
assert!(!<T as Config>::Rewards::account_stake(
(
<T as Config>::Domain::get(),
<T as Config>::StakeCurrencyId::get(),
),
<T as Config>::StakeCurrencyId::get(),
collator,
)
.is_zero())
Expand Down
28 changes: 10 additions & 18 deletions pallets/block-rewards/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use cfg_traits::rewards::AccountRewards;
use cfg_types::tokens::{CurrencyId, StakingCurrency::BlockRewards as BlockRewardsCurrency};
use codec::MaxEncodedLen;
use frame_support::{
parameter_types,
traits::{
Expand Down Expand Up @@ -115,7 +114,7 @@ impl pallet_session::Config for Test {
}

parameter_types! {
pub const ExistentialDeposit: Balance = 0;
pub const ExistentialDeposit: Balance = 1;
}
impl pallet_balances::Config for Test {
type AccountStore = System;
Expand Down Expand Up @@ -182,16 +181,8 @@ impl pallet_restricted_tokens::Config for Test {
type WeightInfo = ();
}

#[derive(
scale_info::TypeInfo, Debug, Copy, codec::Encode, codec::Decode, PartialEq, Clone, MaxEncodedLen,
)]
pub enum RewardDomain {
Liquidity,
Block,
}

frame_support::parameter_types! {
pub const RewardsPalletId: PalletId = PalletId(*b"d/reward");
pub const RewardsPalletId: PalletId = cfg_types::ids::BLOCK_REWARDS_PALLET_ID;
pub const NativeToken: CurrencyId = CurrencyId::Native;

#[derive(scale_info::TypeInfo)]
Expand All @@ -201,7 +192,6 @@ frame_support::parameter_types! {
impl pallet_rewards::Config<pallet_rewards::Instance1> for Test {
type Currency = Tokens;
type CurrencyId = CurrencyId;
type DomainId = RewardDomain;
type GroupId = u32;
type PalletId = RewardsPalletId;
type RewardCurrency = NativeToken;
Expand All @@ -227,7 +217,6 @@ frame_support::parameter_types! {
pub const MaxChangesPerSession: u32 = 50;
#[derive(scale_info::TypeInfo, Debug, PartialEq, Clone)]
pub const MaxCollators: u32 = MAX_COLLATORS;
pub const BlockRewardsDomain: RewardDomain = RewardDomain::Block;
pub const BlockRewardCurrency: CurrencyId = CurrencyId::Staking(BlockRewardsCurrency);
pub const StakeAmount: Balance = cfg_types::consts::rewards::DEFAULT_COLLATOR_STAKE;
pub const CollatorGroupId: u32 = cfg_types::ids::COLLATOR_GROUP_ID;
Expand All @@ -240,7 +229,6 @@ impl pallet_block_rewards::Config for Test {
type Beneficiary = RewardRemainderMock;
type Currency = Tokens;
type CurrencyId = CurrencyId;
type Domain = BlockRewardsDomain;
type MaxChangesPerSession = MaxChangesPerSession;
type MaxCollators = MaxCollators;
type Rewards = Rewards;
Expand All @@ -265,10 +253,7 @@ pub(crate) fn assert_staked(who: &AccountId) {

pub(crate) fn assert_not_staked(who: &AccountId) {
assert!(<Test as Config>::Rewards::account_stake(
(
<Test as Config>::Domain::get(),
<Test as Config>::StakeCurrencyId::get()
),
<Test as Config>::StakeCurrencyId::get(),
who
)
.is_zero());
Expand Down Expand Up @@ -378,6 +363,13 @@ impl ExtBuilder {
.assimilate_storage(&mut storage)
.expect("Session pallet's storage can be assimilated");

pallet_rewards::GenesisConfig::<Test, pallet_rewards::Instance1> {
currency_id: CurrencyId::Native,
amount: ExistentialDeposit::get(),
}
.assimilate_storage(&mut storage)
.expect("Rewards pallet's storage can be assimilated");

let mut ext = sp_io::TestExternalities::new(storage);

ext.execute_with(|| {
Expand Down
Loading