Skip to content

Commit

Permalink
Merge pull request #5086 from ignazio-bovo/luxor/reward-curve-proposal
Browse files Browse the repository at this point in the history
Luxor/reward-curve-proposal
  • Loading branch information
mnaamani committed Apr 4, 2024
2 parents 4268f95 + 8b3f6a9 commit 3580787
Show file tree
Hide file tree
Showing 29 changed files with 554 additions and 398 deletions.
2 changes: 2 additions & 0 deletions bin/node/src/chain_spec/council_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use node_runtime::{
constants::currency, council::CouncilStageUpdate, days, dollars, hours,
monthly_dollars_to_per_block, Balance, CouncilConfig, ExpectedBlockTime,
};
use sp_runtime::Percent;

pub fn create_council_config() -> CouncilConfig {
CouncilConfig {
Expand All @@ -12,5 +13,6 @@ pub fn create_council_config() -> CouncilConfig {
next_budget_refill: 1,
budget_increment: dollars!(22_000),
councilor_reward: monthly_dollars_to_per_block!(10_000),
era_payout_damping_factor: Percent::from_percent(100),
}
}
2 changes: 1 addition & 1 deletion chain-metadata.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion query-node/chain-metadata/2003.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions runtime-modules/council/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,16 @@ benchmarks! {
);
}

set_era_payout_damping_factor {
let new_value = Percent::from_percent(80);

}: _ (RawOrigin::Root, new_value)
verify {
assert_eq!(Council::<T>::era_payout_damping_factor(), Percent::from_percent(80), "Budget not updated");
assert_last_event::<T>(
RawEvent::EraPayoutDampingFactorSet(new_value).into()
);
}

candidate_remark {
let msg = b"test".to_vec();
Expand Down Expand Up @@ -900,4 +910,12 @@ mod tests {
assert_ok!(Council::<Runtime>::test_benchmark_candidate_remark());
})
}

#[test]
fn test_set_era_payout_damping_factor() {
let config = default_genesis_config();
build_test_externalities(config).execute_with(|| {
assert_ok!(Council::<Runtime>::test_benchmark_set_era_payout_damping_factor());
})
}
}
45 changes: 45 additions & 0 deletions runtime-modules/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::traits::{Hash, One, SaturatedConversion, Saturating, Zero};
use sp_runtime::Percent;
use sp_std::convert::TryInto;
use sp_std::{vec, vec::Vec};
use staking_handler::StakingHandler;
Expand Down Expand Up @@ -367,6 +368,10 @@ decl_storage! { generate_storage_info

/// Councilor reward per block
pub CouncilorReward get(fn councilor_reward) config(): Balance<T>;

/// Era payou damping factor: a parameter in [0,1] that can be used to reduce the era
/// payout without changing the reward curve directly
pub EraPayoutDampingFactor get(fn era_payout_damping_factor) config(): Percent = Percent::from_percent(100);
}
}

Expand Down Expand Up @@ -443,6 +448,9 @@ decl_event! {

/// Candidate remark message
CandidateRemarked(MemberId, Vec<u8>),

/// Era payou damping factor set
EraPayoutDampingFactorSet(Percent),
}
}

Expand Down Expand Up @@ -851,6 +859,33 @@ decl_module! {
Ok(())
}

/// Set the era payout damping factor
///
/// # <weight>
///
/// ## weight
/// `O (1)`
/// - db:
/// - `O(1)` doesn't depend on the state or parameters
/// # </weight>
#[weight = CouncilWeightInfo::<T>::set_era_payout_damping_factor()] // TODO: adjust
pub fn set_era_payout_damping_factor(origin, new_parameter: Percent) -> Result<(), Error<T>> {
// ensure action can be started
EnsureChecks::<T>::can_set_era_payout_damping_factor(origin)?;

//
// == MUTATION SAFE ==
//

// update state
Mutations::<T>::set_era_payout_damping_factor(new_parameter);

// emit event
Self::deposit_event(RawEvent::EraPayoutDampingFactorSet(new_parameter));

Ok(())
}

/// Decrease the council total budget
///
/// # <weight>
Expand Down Expand Up @@ -1653,6 +1688,10 @@ impl<T: Config> Mutations<T> {
let next_reward_block = now + T::ElectedMemberRewardPeriod::get();
NextRewardPayments::<T>::put(next_reward_block);
}

fn set_era_payout_damping_factor(new_parameter: Percent) {
EraPayoutDampingFactor::put(new_parameter);
}
}

/////////////////// Ensure checks //////////////////////////////////////////////
Expand Down Expand Up @@ -1835,6 +1874,12 @@ impl<T: Config> EnsureChecks<T> {

Ok(())
}

fn can_set_era_payout_damping_factor(origin: T::RuntimeOrigin) -> Result<(), Error<T>> {
ensure_root(origin)?;

Ok(())
}
}

impl<T: Config + common::membership::MembershipTypes>
Expand Down
2 changes: 2 additions & 0 deletions runtime-modules/council/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use sp_runtime::traits::Hash;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup, Zero},
Percent,
};
use staking_handler::{LockComparator, StakingHandler, StakingManager};
use std::boxed::Box;
Expand Down Expand Up @@ -538,6 +539,7 @@ pub fn default_genesis_config() -> council::GenesisConfig<Runtime> {
next_budget_refill: <Runtime as Config>::BudgetRefillPeriod::get(),
budget_increment: 1,
councilor_reward: 100,
era_payout_damping_factor: Percent::from_percent(100),
}
}

Expand Down
9 changes: 9 additions & 0 deletions runtime-modules/council/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub trait WeightInfo {
fn decrease_council_budget() -> Weight;
fn candidate_remark() -> Weight;
fn councilor_remark() -> Weight;
fn set_era_payout_damping_factor() -> Weight;
}

/// Weights for council using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -348,6 +349,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(Weight::from_parts(0, 5400))
.saturating_add(T::DbWeight::get().reads(2_u64))
}

fn set_era_payout_damping_factor() -> Weight {
Weight::from_parts(0, 0u64)
}

}

// Default implementation for tests
Expand Down Expand Up @@ -406,5 +412,8 @@ impl WeightInfo for () {
fn decrease_council_budget() -> Weight {
Weight::from_parts(0, 0u64)
}
fn set_era_payout_damping_factor() -> Weight {
Weight::from_parts(0, 0u64)
}

}
38 changes: 30 additions & 8 deletions runtime-modules/proposals/codex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ pub trait Config:
ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
>;

/// `Set Era Payout Damping Factor` proposal parameters
type SetEraPayoutDampingFactorProposalParameters: Get<
ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
>;

/// `Decrease Council Budget` proposal parameters
type DecreaseCouncilBudgetProposalParameters: Get<
ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
Expand Down Expand Up @@ -530,6 +535,11 @@ decl_module! {
const UpdateTokenPalletTokenConstraints:
ProposalParameters<T::BlockNumber, BalanceOf<T>> = T::UpdateTokenPalletTokenConstraints::get();

/// Era payout damping factor
const SetEraPayoutDampingFactorProposalParameters:
ProposalParameters<T::BlockNumber, BalanceOf<T>> = T::SetEraPayoutDampingFactorProposalParameters::get();


/// Create a proposal, the type of proposal depends on the `proposal_details` variant
///
/// <weight>
Expand Down Expand Up @@ -900,15 +910,18 @@ impl<T: Config> Module<T> {
ProposalDetails::SetPalletFozenStatus(..) => {
// Note: No checks for this proposal for now
}
ProposalDetails::UpdateTokenPalletTokenConstraints(..) => {
// Note: No checks for this proposal for now
}
ProposalDetails::DecreaseCouncilBudget(reduction_amount) => {
ensure!(
!(*reduction_amount).is_zero(),
Error::<T>::ReductionAmountZero
);
}
ProposalDetails::UpdateTokenPalletTokenConstraints(..) => {
// Note: No checks for this proposal for now
}
ProposalDetails::SetEraPayoutDampingFactor(..) => {
// Note: No checks for this proposal for now
}
}

Ok(())
Expand Down Expand Up @@ -979,11 +992,14 @@ impl<T: Config> Module<T> {
ProposalDetails::SetPalletFozenStatus(..) => {
T::SetPalletFozenStatusProposalParameters::get()
}
ProposalDetails::DecreaseCouncilBudget(..) => {
T::DecreaseCouncilBudgetProposalParameters::get()
}
ProposalDetails::UpdateTokenPalletTokenConstraints(..) => {
T::UpdateTokenPalletTokenConstraints::get()
}
ProposalDetails::DecreaseCouncilBudget(..) => {
T::DecreaseCouncilBudgetProposalParameters::get()
ProposalDetails::SetEraPayoutDampingFactor(..) => {
T::SetEraPayoutDampingFactorProposalParameters::get()
}
}
}
Expand Down Expand Up @@ -1152,14 +1168,20 @@ impl<T: Config> Module<T> {
to_kb(description_length.saturated_into()),
)
}
ProposalDetails::DecreaseCouncilBudget(..) => {
WeightInfoCodex::<T>::create_proposal_decrease_council_budget(
to_kb(title_length.saturated_into()),
to_kb(description_length.saturated_into()),
)
}
ProposalDetails::UpdateTokenPalletTokenConstraints(..) => {
WeightInfoCodex::<T>::create_proposal_update_token_pallet_governance_parameters(
WeightInfoCodex::<T>::create_proposal_update_token_pallet_token_constraints(
to_kb(title_length.saturated_into()),
to_kb(description_length.saturated_into()),
)
}
ProposalDetails::DecreaseCouncilBudget(..) => {
WeightInfoCodex::<T>::create_proposal_decrease_council_budget(
ProposalDetails::SetEraPayoutDampingFactor(..) => {
WeightInfoCodex::<T>::create_proposal_set_era_payout_damping_factor(
to_kb(title_length.saturated_into()),
to_kb(description_length.saturated_into()),
)
Expand Down
1 change: 1 addition & 0 deletions runtime-modules/proposals/codex/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ impl crate::Config for Test {
type SetMaxValidatorCountProposalMaxValidators = SetMaxValidatorCountProposalMaxValidators;
type SetPalletFozenStatusProposalParameters = DefaultProposalParameters;
type UpdateTokenPalletTokenConstraints = DefaultProposalParameters;
type SetEraPayoutDampingFactorProposalParameters = DefaultProposalParameters;
type DecreaseCouncilBudgetProposalParameters = DefaultProposalParameters;
}

Expand Down
4 changes: 4 additions & 0 deletions runtime-modules/proposals/codex/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sp_std::vec::Vec;
use common::working_group::WorkingGroup;
use common::BalanceKind;
use common::FundingRequestParameters;
use sp_runtime::Percent;

use content::NftLimitPeriod;
use working_group::StakePolicy;
Expand Down Expand Up @@ -128,6 +129,9 @@ pub enum ProposalDetails<
/// Update token constraints
UpdateTokenPalletTokenConstraints(TokenConstraints),

/// `SetEraPayoutDampingFactor` proposal
SetEraPayoutDampingFactor(Percent),

/// `DecreaseCouncilBudget` proposal
DecreaseCouncilBudget(Balance),
}
Expand Down
14 changes: 11 additions & 3 deletions runtime-modules/proposals/codex/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub trait WeightInfo {
fn create_proposal_update_global_nft_limit(_t: u32, _d: u32, ) -> Weight;
fn create_proposal_update_channel_payouts(_t: u32, _d: u32, _i: u32, ) -> Weight;
fn create_proposal_freeze_pallet(_t: u32, _d: u32, ) -> Weight;
fn create_proposal_update_token_pallet_governance_parameters(_t: u32, _d: u32, ) -> Weight;
fn create_proposal_update_token_pallet_token_constraints(_t: u32, _d: u32, ) -> Weight;
fn create_proposal_set_era_payout_damping_factor(_t: u32, _d: u32) -> Weight;
fn create_proposal_decrease_council_budget(_t: u32, _d: u32, ) -> Weight;
}

Expand Down Expand Up @@ -1021,9 +1022,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(9_u64))
}
fn create_proposal_update_token_pallet_governance_parameters(_t: u32, _d: u32) -> Weight {
fn create_proposal_update_token_pallet_token_constraints(_t: u32, _d: u32, ) -> Weight {
Weight::from_parts(0, 0)
}
fn create_proposal_set_era_payout_damping_factor(_t: u32, _d: u32) -> Weight {
Weight::from_parts(0,0)
}

fn create_proposal_decrease_council_budget(t: u32, d: u32, ) -> Weight {
Weight::from_parts(0, 0)
Expand Down Expand Up @@ -1104,9 +1108,13 @@ impl WeightInfo for () {
fn create_proposal_freeze_pallet(t: u32, d: u32, ) -> Weight {
Weight::from_parts(0, 0)
}
fn create_proposal_update_token_pallet_governance_parameters(_t: u32, _d: u32, ) -> Weight {
fn create_proposal_update_token_pallet_token_constraints(_t: u32, _d: u32, ) -> Weight {
Weight::from_parts(0, 0)
}

fn create_proposal_set_era_payout_damping_factor(_t: u32, _d: u32) -> Weight {
Weight::from_parts(0,0)
}
fn create_proposal_decrease_council_budget(t: u32, d: u32, ) -> Weight {
Weight::from_parts(0, 0)
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/integration/proposals/proposal_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ impl ProposalEncoder<Runtime> for ExtrinsicProposalEncoder {
RuntimeCall::ProjectToken(project_token::Call::set_frozen_status { freeze })
}
},
ProposalDetails::SetEraPayoutDampingFactor(new_parameter) => {
RuntimeCall::Council(council::Call::set_era_payout_damping_factor { new_parameter })
}
ProposalDetails::DecreaseCouncilBudget(reduction_amount) => {
RuntimeCall::Council(council::Call::decrease_council_budget { reduction_amount })
}
Expand Down
22 changes: 17 additions & 5 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod weights;
extern crate lazy_static; // for proposals_configuration module

use codec::Decode;
use core::ops::Div;
use frame_election_provider_support::{
onchain, BalancingConfig, ElectionDataProvider, SequentialPhragmen, VoteWeight,
};
Expand Down Expand Up @@ -536,11 +537,21 @@ impl EraPayout<Balance> for NoInflationIfNoEras {
// PoA mode: no inflation.
(0, 0)
} else {
<pallet_staking::ConvertCurve<RewardCurve> as EraPayout<Balance>>::era_payout(
total_staked,
total_issuance,
era_duration_millis,
)
// rescale the era payout according to the damping factor while keeping the invariant
// era_payou + rest = max_payout
let (era_payout_unscaled, rest_unscaled) =
<pallet_staking::ConvertCurve<RewardCurve> as EraPayout<Balance>>::era_payout(
total_staked,
total_issuance,
era_duration_millis,
);
let max_payout = era_payout_unscaled.saturating_add(rest_unscaled);
let damping_factor_parts = Council::era_payout_damping_factor().deconstruct();
let era_payout_scaled = era_payout_unscaled
.saturating_mul(damping_factor_parts.into())
.div(100u128);
let rest_scaled = max_payout.saturating_sub(era_payout_scaled);
(era_payout_scaled, rest_scaled)
}
}
}
Expand Down Expand Up @@ -1709,6 +1720,7 @@ impl proposals_codex::Config for Runtime {
type SetMaxValidatorCountProposalMaxValidators = SetMaxValidatorCountProposalMaxValidators;
type UpdateTokenPalletTokenConstraints = UpdateTokenPalletTokenConstraints;
type SetPalletFozenStatusProposalParameters = SetPalletFozenStatusProposalParameters;
type SetEraPayoutDampingFactorProposalParameters = SetEraPayoutDampingFactorProposalParameters;
type WeightInfo = proposals_codex::weights::SubstrateWeight<Runtime>;
}

Expand Down
Loading

0 comments on commit 3580787

Please sign in to comment.