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

Luxor/reward-curve-proposal #5086

Merged
merged 19 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
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.

6 changes: 5 additions & 1 deletion query-node/chain-metadata/2002.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions query-node/chain-metadata/2003.json

Large diffs are not rendered by default.

91 changes: 91 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;
ignazio-bovo marked this conversation as resolved.
Show resolved Hide resolved
ignazio-bovo marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -423,6 +428,11 @@ decl_event! {
/// Councilor reward has been updated.
CouncilorRewardUpdated(Balance),

/// Councilor budget has been decreased
/// Params:
/// - Reduction amount
CouncilBudgetDecreased(Balance),

/// Request has been funded
RequestFunded(AccountId, Balance),

Expand All @@ -438,6 +448,9 @@ decl_event! {

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

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

Expand Down Expand Up @@ -518,6 +531,9 @@ decl_error! {

/// Cannot withdraw: insufficient budget balance.
InsufficientBalanceForTransfer,

/// Cannot reduce the budget by the given amount.
ReductionAmountTooLarge
}
}

Expand Down Expand Up @@ -843,6 +859,64 @@ 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 = 10_000_000] // 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>
///
/// ## weight
/// `O (1)`
/// - db:
/// - `O(1)` doesn't depend on the state or parameters
/// # </weight>
#[weight = 10_000_000] // TODO: adjust
pub fn decrease_council_budget(origin, reduction_amount: Balance<T>) -> Result<(), Error<T>> {
ignazio-bovo marked this conversation as resolved.
Show resolved Hide resolved
// ensure action can be started
EnsureChecks::<T>::can_decrease_council_budget(origin)?;

// ensure reduction amount is not too large
ensure!(
reduction_amount <= Self::budget(),
Error::<T>::ReductionAmountTooLarge
);

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

// update state
Mutations::<T>::decrease_budget(reduction_amount);

// emit event
Self::deposit_event(RawEvent::CouncilBudgetDecreased(reduction_amount));

Ok(())
}

/// Transfers funds from council budget to account
///
Expand Down Expand Up @@ -1613,6 +1687,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 @@ -1788,6 +1866,19 @@ impl<T: Config> EnsureChecks<T> {

Ok(())
}

// Ensures there is no problem in decreasing the council budget
fn can_decrease_council_budget(origin: T::RuntimeOrigin) -> Result<(), Error<T>> {
ensure_root(origin)?;

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
22 changes: 22 additions & 0 deletions runtime-modules/proposals/codex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ pub trait Config:
type SetPalletFozenStatusProposalParameters: Get<
ProposalParameters<Self::BlockNumber, BalanceOf<Self>>,
>;

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

/// Specialized alias of GeneralProposalParams
Expand Down Expand Up @@ -503,9 +508,14 @@ decl_module! {
const SetMaxValidatorCountProposalMaxValidators: u32 =
T::SetMaxValidatorCountProposalMaxValidators::get();

/// Setting pallet as frozen
const SetPalletFozenStatusProposalParameters:
ProposalParameters<T::BlockNumber, BalanceOf<T>> = T::SetPalletFozenStatusProposalParameters::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
///
Expand Down Expand Up @@ -877,6 +887,9 @@ impl<T: Config> Module<T> {
ProposalDetails::SetPalletFozenStatus(..) => {
// Note: No checks for this proposal for now
}
ProposalDetails::SetEraPayoutDampingFactor(..) => {
// Note: No checks for this proposal for now
}
}

Ok(())
Expand Down Expand Up @@ -947,6 +960,9 @@ impl<T: Config> Module<T> {
ProposalDetails::SetPalletFozenStatus(..) => {
T::SetPalletFozenStatusProposalParameters::get()
}
ProposalDetails::SetEraPayoutDampingFactor(..) => {
T::SetEraPayoutDampingFactorProposalParameters::get()
}
}
}

Expand Down Expand Up @@ -1114,6 +1130,12 @@ impl<T: Config> Module<T> {
to_kb(description_length.saturated_into()),
)
}
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 @@ -670,6 +670,7 @@ impl crate::Config for Test {
type FundingRequestProposalMaxAccounts = FundingRequestProposalMaxAccounts;
type SetMaxValidatorCountProposalMaxValidators = SetMaxValidatorCountProposalMaxValidators;
type SetPalletFozenStatusProposalParameters = DefaultProposalParameters;
type SetEraPayoutDampingFactorProposalParameters = DefaultProposalParameters;
}

parameter_types! {
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 @@ -122,6 +123,9 @@ pub enum ProposalDetails<

/// `SetPalletFozenStatus` proposal
SetPalletFozenStatus(bool, FreezablePallet),

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

impl<
Expand Down
7 changes: 7 additions & 0 deletions runtime-modules/proposals/codex/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ 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_set_era_payout_damping_factor(_t: u32, _d: u32) -> Weight;
}

/// Weights for proposals_codex using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -1019,6 +1020,9 @@ 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_set_era_payout_damping_factor(_t: u32, _d: u32) -> Weight {
Weight::from_parts(0,0)
}
}

// Default implementation for tests
Expand Down Expand Up @@ -1095,4 +1099,7 @@ impl WeightInfo for () {
fn create_proposal_freeze_pallet(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)
}
}
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 @@ -171,6 +171,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 })
}
};

call.encode()
Expand Down
24 changes: 18 additions & 6 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 @@ -144,7 +145,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("joystream-node"),
impl_name: create_runtime_str!("joystream-node"),
authoring_version: 12,
spec_version: 2002,
spec_version: 2003,
impl_version: 0,
apis: crate::runtime_api::EXPORTED_RUNTIME_API_VERSIONS,
transaction_version: 2,
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
ignazio-bovo marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1698,6 +1709,7 @@ impl proposals_codex::Config for Runtime {
type FundingRequestProposalMaxAccounts = FundingRequestProposalMaxAccounts;
type SetMaxValidatorCountProposalMaxValidators = SetMaxValidatorCountProposalMaxValidators;
type SetPalletFozenStatusProposalParameters = SetPalletFozenStatusProposalParameters;
type SetEraPayoutDampingFactorProposalParameters = SetEraPayoutDampingFactorProposalParameters;
type WeightInfo = proposals_codex::weights::SubstrateWeight<Runtime>;
}

Expand Down
15 changes: 15 additions & 0 deletions runtime/src/proposals_configuration/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,18 @@ pub(crate) fn freeze_pallet_proposal() -> ProposalParameters<BlockNumber, Balanc
constitutionality: 1,
}
}

// TODO (luxor/reward-curve-proposal): adjust parameters
// Proposal parameters for the 'Set Era Payout Damping Factor' proposal
pub(crate) fn set_era_payout_damping_factor() -> ProposalParameters<BlockNumber, Balance> {
ProposalParameters {
voting_period: days!(3),
grace_period: 0,
approval_quorum_percentage: TWO_OUT_OF_THREE,
approval_threshold_percentage: TWO_OUT_OF_THREE,
slashing_quorum_percentage: ALL,
slashing_threshold_percentage: ALL,
required_stake: Some(dollars!(50)),
constitutionality: 1,
}
}
3 changes: 3 additions & 0 deletions runtime/src/proposals_configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@ parameter_types! {

pub SetPalletFozenStatusProposalParameters: ProposalParameters<BlockNumber, Balance> =
freeze_pallet_proposal();

pub SetEraPayoutDampingFactorProposalParameters: ProposalParameters<BlockNumber, Balance> =
set_era_payout_damping_factor();
}
15 changes: 15 additions & 0 deletions runtime/src/proposals_configuration/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,18 @@ pub(crate) fn freeze_pallet_proposal() -> ProposalParameters<BlockNumber, Balanc
constitutionality: 1,
}
}

// TODO (luxor/reward-curve-proposal): adjust parameters
// Proposal parameters for the 'Set Era Payout Damping Factor' proposal
pub(crate) fn set_era_payout_damping_factor() -> ProposalParameters<BlockNumber, Balance> {
ProposalParameters {
voting_period: days!(3),
grace_period: 0,
approval_quorum_percentage: TWO_OUT_OF_THREE,
approval_threshold_percentage: TWO_OUT_OF_THREE,
slashing_quorum_percentage: ALL,
slashing_threshold_percentage: ALL,
required_stake: Some(dollars!(50)),
constitutionality: 1,
}
}
Loading
Loading