Skip to content

Commit

Permalink
add reward constraints parameter
Browse files Browse the repository at this point in the history
the initial setting is the reward_drawing_limit_max
  • Loading branch information
NicolasDP committed Dec 11, 2019
1 parent d169f22 commit f6d12d9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ blockchain_configuration:
# the rate at which the contribution is tweaked related to epoch.
epoch_rate: 3

# set some reward constraints and limits
#
# this value is optional, the default is no constraints at all
reward_constraints:
# limit the epoch total reward drawing limit to a portion of the total
# active stake of the system.
#
# for example, if set to 10%, the reward drawn will be bounded by the
# 10% of the total active stake.
#
# this value is optional, the default is no reward drawing limit
reward_drawing_limit_max: "10/100"

# Initial state of the ledger. Each item is applied in order of this list
initial:
# Initial deposits present in the blockchain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
interfaces::{
ActiveSlotCoefficient, BlockContentMaxSize, ConsensusLeaderId, EpochStabilityDepth,
FeesGoTo, KESUpdateSpeed, LinearFeeDef, NumberOfSlotsPerEpoch, RewardParams, SlotDuration,
TaxType, Value,
FeesGoTo, KESUpdateSpeed, LinearFeeDef, NumberOfSlotsPerEpoch, RewardConstraints,
RewardParams, SlotDuration, TaxType, Value,
},
time::SecondsSinceUnixEpoch,
};
Expand Down Expand Up @@ -121,6 +121,10 @@ pub struct BlockchainConfiguration {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub reward_parameters: Option<RewardParams>,

#[serde(default)]
#[serde(skip_serializing_if = "RewardConstraints::is_none")]
pub reward_constraints: RewardConstraints,
}

impl From<BlockchainConfiguration> for ConfigParams {
Expand Down Expand Up @@ -172,6 +176,7 @@ impl BlockchainConfiguration {
treasury_parameters: None,
total_reward_supply: None,
reward_parameters: None,
reward_constraints: RewardConstraints::default(),
}
}

Expand All @@ -197,6 +202,7 @@ impl BlockchainConfiguration {
let mut reward_parameters = None;
let mut per_certificate_fees = None;
let mut fees_go_to = None;
let mut reward_constraints = RewardConstraints::default();

for param in params.iter().cloned() {
match param {
Expand Down Expand Up @@ -254,6 +260,13 @@ impl BlockchainConfiguration {
ConfigParam::RewardParams(param) => reward_parameters
.replace(param.into())
.map(|_| "reward_parameters"),
ConfigParam::RewardLimitNone => {
panic!("ConfigParam::RewardLimitNone should not be in the block0")
}
ConfigParam::RewardLimitByAbsoluteStake(ratio) => reward_constraints
.reward_drawing_limit_max
.replace(ratio.into())
.map(|_| "reward_constraints.reward_drawing_limit_max"),
ConfigParam::PerCertificateFees(param) => per_certificate_fees
.replace(param)
.map(|_| "per_certificate_fees"),
Expand Down Expand Up @@ -290,6 +303,7 @@ impl BlockchainConfiguration {
treasury_parameters,
total_reward_supply,
reward_parameters,
reward_constraints: reward_constraints,
})
}

Expand All @@ -311,6 +325,7 @@ impl BlockchainConfiguration {
treasury_parameters,
total_reward_supply,
reward_parameters,
reward_constraints,
} = self;

let mut params = ConfigParams::new();
Expand Down Expand Up @@ -358,6 +373,12 @@ impl BlockchainConfiguration {
params.push(ConfigParam::RewardParams(reward_parameters.into()));
}

if let Some(reward_drawing_limit_max) = reward_constraints.reward_drawing_limit_max {
params.push(ConfigParam::RewardLimitByAbsoluteStake(
reward_drawing_limit_max.into(),
));
}

consensus_leader_ids
.into_iter()
.map(ConfigParam::from)
Expand Down Expand Up @@ -424,6 +445,7 @@ mod test {
treasury_parameters: Arbitrary::arbitrary(g),
total_reward_supply: Arbitrary::arbitrary(g),
reward_parameters: Arbitrary::arbitrary(g),
reward_constraints: Arbitrary::arbitrary(g),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions jormungandr-lib/src/interfaces/block0_configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod initial_fragment;
mod kes_update_speed;
mod leader_id;
mod number_of_slots_per_epoch;
mod reward_constraint;
mod slots_duration;

pub use self::active_slot_coefficient::ActiveSlotCoefficient;
Expand All @@ -20,6 +21,7 @@ pub use self::initial_fragment::{Initial, InitialUTxO, LegacyUTxO};
pub use self::kes_update_speed::KESUpdateSpeed;
pub use self::leader_id::ConsensusLeaderId;
pub use self::number_of_slots_per_epoch::NumberOfSlotsPerEpoch;
pub use self::reward_constraint::RewardConstraints;
pub use self::slots_duration::SlotDuration;
use chain_impl_mockchain::{
block::{self, Block},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::interfaces::Ratio;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct RewardConstraints {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub reward_drawing_limit_max: Option<Ratio>,
}

impl RewardConstraints {
pub fn is_none(&self) -> bool {
self.reward_drawing_limit_max.is_none()
}

pub fn set_reward_drawing_limit_max(&mut self, limit: Option<Ratio>) {
self.reward_drawing_limit_max = limit
}
}

#[cfg(test)]
mod test {
use super::*;
use quickcheck::{Arbitrary, Gen};

impl Arbitrary for RewardConstraints {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Self {
reward_drawing_limit_max: Arbitrary::arbitrary(g),
}
}
}
}
11 changes: 10 additions & 1 deletion jormungandr-lib/src/interfaces/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use chain_impl_mockchain::block::Epoch;
use chain_impl_mockchain::fee::LinearFee;
use chain_impl_mockchain::rewards::{CompoundingType, Parameters, Ratio, TaxType};
use chain_impl_mockchain::rewards::{CompoundingType, Limit, Parameters, Ratio, TaxType};
use chain_impl_mockchain::value::Value;
use serde::{Deserialize, Serialize};
use std::num::{NonZeroU32, NonZeroU64};
Expand Down Expand Up @@ -45,6 +45,13 @@ pub struct TaxTypeDef {
#[serde(transparent)]
pub struct TaxTypeSerde(#[serde(with = "TaxTypeDef")] pub TaxType);

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(remote = "Limit")]
pub enum LimitDef {
None,
ByStakeAbsolute(#[serde(with = "RatioDef")] Ratio),
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(remote = "Ratio")]
pub struct RatioDef {
Expand All @@ -62,6 +69,8 @@ pub struct ParametersDef {
pub compounding_type: CompoundingType,
pub epoch_rate: NonZeroU32,
pub epoch_start: Epoch,
#[serde(with = "LimitDef")]
pub reward_drawing_limit_max: Limit,
}

#[derive(Deserialize, Serialize)]
Expand Down

0 comments on commit f6d12d9

Please sign in to comment.