Skip to content

Commit 309c4ce

Browse files
committed
remove BasisPoints and change relevant parameters to Decimal type
1 parent 1329579 commit 309c4ce

File tree

4 files changed

+49
-90
lines changed

4 files changed

+49
-90
lines changed

apps/src/lib/config/genesis.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ pub mod genesis_config {
2929
use hex;
3030
use namada::ledger::governance::parameters::GovParams;
3131
use namada::ledger::parameters::{EpochDuration, Parameters};
32-
use namada::ledger::pos::types::BasisPoints;
3332
use namada::ledger::pos::{GenesisValidator, PosParams};
3433
use namada::ledger::treasury::parameters::TreasuryParams;
3534
use namada::types::address::Address;
3635
use namada::types::key::dkg_session_keys::DkgPublicKey;
3736
use namada::types::key::*;
3837
use namada::types::time::Rfc3339String;
3938
use namada::types::{storage, token};
39+
use rust_decimal::Decimal;
4040
use serde::{Deserialize, Serialize};
4141
use thiserror::Error;
4242

@@ -265,24 +265,24 @@ pub mod genesis_config {
265265
pub unbonding_len: u64,
266266
// Votes per token (in basis points).
267267
// XXX: u64 doesn't work with toml-rs!
268-
pub votes_per_token: u64,
268+
pub votes_per_token: Decimal,
269269
// Reward for proposing a block.
270270
// XXX: u64 doesn't work with toml-rs!
271-
pub block_proposer_reward: u64,
271+
pub block_proposer_reward: Decimal,
272272
// Reward for voting on a block.
273273
// XXX: u64 doesn't work with toml-rs!
274-
pub block_vote_reward: u64,
274+
pub block_vote_reward: Decimal,
275275
// Maximum staking APY
276276
// XXX: u64 doesn't work with toml-rs!
277-
pub max_staking_rewards_rate: u64,
277+
pub max_staking_rewards_rate: Decimal,
278278
// Portion of a validator's stake that should be slashed on a
279279
// duplicate vote (in basis points).
280280
// XXX: u64 doesn't work with toml-rs!
281-
pub duplicate_vote_slash_rate: u64,
281+
pub duplicate_vote_slash_rate: Decimal,
282282
// Portion of a validator's stake that should be slashed on a
283283
// light client attack (in basis points).
284284
// XXX: u64 doesn't work with toml-rs!
285-
pub light_client_attack_slash_rate: u64,
285+
pub light_client_attack_slash_rate: Decimal,
286286
}
287287

288288
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -569,20 +569,18 @@ pub mod genesis_config {
569569
max_validator_slots: config.pos_params.max_validator_slots,
570570
pipeline_len: config.pos_params.pipeline_len,
571571
unbonding_len: config.pos_params.unbonding_len,
572-
votes_per_token: BasisPoints::new(
573-
config.pos_params.votes_per_token,
574-
),
572+
votes_per_token: config.pos_params.votes_per_token,
575573
block_proposer_reward: config.pos_params.block_proposer_reward,
576574
block_vote_reward: config.pos_params.block_vote_reward,
577-
max_staking_rewards_rate: BasisPoints::new(
578-
config.pos_params.max_staking_rewards_rate,
579-
),
580-
duplicate_vote_slash_rate: BasisPoints::new(
581-
config.pos_params.duplicate_vote_slash_rate,
582-
),
583-
light_client_attack_slash_rate: BasisPoints::new(
584-
config.pos_params.light_client_attack_slash_rate,
585-
),
575+
max_staking_rewards_rate: config
576+
.pos_params
577+
.max_staking_rewards_rate,
578+
duplicate_vote_slash_rate: config
579+
.pos_params
580+
.duplicate_vote_slash_rate,
581+
light_client_attack_slash_rate: config
582+
.pos_params
583+
.light_client_attack_slash_rate,
586584
};
587585

588586
let mut genesis = Genesis {

proof_of_stake/src/parameters.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Proof-of-Stake system parameters
22
33
use borsh::{BorshDeserialize, BorshSerialize};
4+
use rust_decimal::prelude::ToPrimitive;
5+
use rust_decimal::Decimal;
6+
use rust_decimal_macros::dec;
47
use thiserror::Error;
58

6-
use crate::types::BasisPoints;
7-
89
/// Proof-of-Stake system parameters
910
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
1011
pub struct PosParams {
@@ -20,20 +21,20 @@ pub struct PosParams {
2021
pub unbonding_len: u64,
2122
/// Used in validators' voting power calculation. Given in basis points
2223
/// (voting power per ten thousand tokens).
23-
pub votes_per_token: BasisPoints,
24+
pub votes_per_token: Decimal,
2425
/// Amount of tokens rewarded to a validator for proposing a block
25-
pub block_proposer_reward: u64,
26+
pub block_proposer_reward: Decimal,
2627
/// Amount of tokens rewarded to each validator that voted on a block
2728
/// proposal
28-
pub block_vote_reward: u64,
29+
pub block_vote_reward: Decimal,
2930
/// Maximum staking rewards rate per annum
30-
pub max_staking_rewards_rate: BasisPoints,
31+
pub max_staking_rewards_rate: Decimal,
3132
/// Portion of validator's stake that should be slashed on a duplicate
3233
/// vote. Given in basis points (slashed amount per ten thousand tokens).
33-
pub duplicate_vote_slash_rate: BasisPoints,
34+
pub duplicate_vote_slash_rate: Decimal,
3435
/// Portion of validator's stake that should be slashed on a light client
3536
/// attack. Given in basis points (slashed amount per ten thousand tokens).
36-
pub light_client_attack_slash_rate: BasisPoints,
37+
pub light_client_attack_slash_rate: Decimal,
3738
}
3839

3940
impl Default for PosParams {
@@ -42,16 +43,17 @@ impl Default for PosParams {
4243
max_validator_slots: 128,
4344
pipeline_len: 2,
4445
unbonding_len: 6,
45-
// 1 voting power per 1 fundamental token (10^6 per NAM or 1 per namnam)
46-
votes_per_token: BasisPoints::new(10000),
47-
block_proposer_reward: 100,
48-
block_vote_reward: 1,
46+
// 1 voting power per 1 fundamental token (10^6 per NAM or 1 per
47+
// namnam)
48+
votes_per_token: dec!(1.0),
49+
block_proposer_reward: dec!(0.0625),
50+
block_vote_reward: dec!(0.05),
4951
// staking APY 20%
50-
max_staking_rewards_rate: BasisPoints::new(2000),
52+
max_staking_rewards_rate: dec!(0.2),
5153
// slash 5%
52-
duplicate_vote_slash_rate: BasisPoints::new(500),
54+
duplicate_vote_slash_rate: dec!(0.05),
5355
// slash 5%
54-
light_client_attack_slash_rate: BasisPoints::new(500),
56+
light_client_attack_slash_rate: dec!(0.05),
5557
}
5658
}
5759
}
@@ -65,7 +67,7 @@ pub enum ValidationError {
6567
)]
6668
TotalVotingPowerTooLarge(u64),
6769
#[error("Votes per token cannot be greater than 1, got {0}")]
68-
VotesPerTokenGreaterThanOne(BasisPoints),
70+
VotesPerTokenGreaterThanOne(Decimal),
6971
#[error("Pipeline length must be >= 2, got {0}")]
7072
PipelineLenTooShort(u64),
7173
#[error(
@@ -105,8 +107,8 @@ impl PosParams {
105107

106108
// Check maximum total voting power cannot get larger than what
107109
// Tendermint allows
108-
let max_total_voting_power = self.max_validator_slots
109-
* (self.votes_per_token * TOKEN_MAX_AMOUNT);
110+
let max_total_voting_power = Decimal::from(self.max_validator_slots)
111+
* self.votes_per_token * Decimal::from(TOKEN_MAX_AMOUNT);
110112
match i64::try_from(max_total_voting_power) {
111113
Ok(max_total_voting_power_i64) => {
112114
if max_total_voting_power_i64 > MAX_TOTAL_VOTING_POWER {
@@ -121,7 +123,7 @@ impl PosParams {
121123
}
122124

123125
// Check that there is no more than 1 vote per token
124-
if self.votes_per_token > BasisPoints::new(10_000) {
126+
if self.votes_per_token > dec!(1.0) {
125127
errors.push(ValidationError::VotesPerTokenGreaterThanOne(
126128
self.votes_per_token,
127129
))
@@ -174,7 +176,7 @@ pub mod testing {
174176
max_validator_slots,
175177
pipeline_len,
176178
unbonding_len,
177-
votes_per_token: BasisPoints::new(votes_per_token),
179+
votes_per_token: Decimal::from(votes_per_token) / dec!(10_000),
178180
// The rest of the parameters that are not being used in the PoS
179181
// VP are constant for now
180182
..Default::default()

proof_of_stake/src/rewards.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! PoS rewards
22
33
use rust_decimal::Decimal;
4+
use rust_decimal_macros::dec;
45
use thiserror::Error;
56

67
use crate::types::VotingPower;
@@ -29,20 +30,20 @@ pub struct PosRewards {
2930
/// bing
3031
#[derive(Debug, Copy, Clone)]
3132
pub struct PosRewardsCalculator {
32-
proposer_param: u64,
33-
signer_param: u64,
3433
signing_stake: VotingPower,
3534
total_stake: VotingPower,
35+
proposer_param: Decimal,
36+
signer_param: Decimal,
3637
pos_rewards: Option<PosRewards>,
3738
}
3839

3940
impl PosRewardsCalculator {
4041
/// new
4142
pub fn new(
42-
proposer_param: u64,
43-
signer_param: u64,
4443
signing_stake: VotingPower,
4544
total_stake: VotingPower,
45+
proposer_param: Decimal,
46+
signer_param: Decimal,
4647
) -> Self {
4748
Self {
4849
proposer_param,

proof_of_stake/src/types.rs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use std::convert::TryFrom;
66
use std::fmt::Display;
77
use std::hash::Hash;
88
use std::num::TryFromIntError;
9-
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
9+
use std::ops::{Add, AddAssign, Sub, SubAssign};
1010

1111
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
12+
use rust_decimal::prelude::ToPrimitive;
13+
use rust_decimal::Decimal;
1214

1315
use crate::epoched::{
1416
Epoched, EpochedDelta, OffsetPipelineLen, OffsetUnbondingLen,
@@ -324,7 +326,7 @@ pub struct Slash {
324326
/// A type of slashsable event.
325327
pub r#type: SlashType,
326328
/// A rate is the portion of staked tokens that are slashed.
327-
pub rate: BasisPoints,
329+
pub rate: Decimal,
328330
}
329331

330332
/// Slashes applied to validator, to punish byzantine behavior by removing
@@ -362,23 +364,6 @@ impl From<tendermint_proto::abci::VoteInfo> for VoteInfo {
362364
}
363365
}
364366

365-
/// ‱ (Parts per ten thousand). This can be multiplied by any type that
366-
/// implements [`Into<u64>`] or [`Into<i128>`].
367-
#[derive(
368-
Debug,
369-
Clone,
370-
Copy,
371-
BorshDeserialize,
372-
BorshSerialize,
373-
BorshSchema,
374-
PartialOrd,
375-
Ord,
376-
PartialEq,
377-
Eq,
378-
Hash,
379-
)]
380-
pub struct BasisPoints(u64);
381-
382367
/// Derive Tendermint raw hash from the public key
383368
pub trait PublicKeyTmRawHash {
384369
/// Derive Tendermint raw hash from the public key
@@ -737,7 +722,7 @@ where
737722
impl SlashType {
738723
/// Get the slash rate applicable to the given slash type from the PoS
739724
/// parameters.
740-
pub fn get_slash_rate(&self, params: &PosParams) -> BasisPoints {
725+
pub fn get_slash_rate(&self, params: &PosParams) -> Decimal {
741726
match self {
742727
SlashType::DuplicateVote => params.duplicate_vote_slash_rate,
743728
SlashType::LightClientAttack => {
@@ -756,35 +741,8 @@ impl Display for SlashType {
756741
}
757742
}
758743

759-
impl BasisPoints {
760-
/// Initialize basis points from an integer.
761-
pub fn new(value: u64) -> Self {
762-
Self(value)
763-
}
764-
}
765-
766-
impl Display for BasisPoints {
767-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
768-
write!(f, "{}‱", self.0)
769-
}
770-
}
771-
772-
impl Mul<u64> for BasisPoints {
773-
type Output = u64;
774-
775-
fn mul(self, rhs: u64) -> Self::Output {
776-
// TODO checked arithmetics
777-
rhs * self.0 / 10_000
778-
}
779744
}
780745

781-
impl Mul<i128> for BasisPoints {
782-
type Output = i128;
783-
784-
fn mul(self, rhs: i128) -> Self::Output {
785-
// TODO checked arithmetics
786-
rhs * self.0 as i128 / 10_000
787-
}
788746
}
789747

790748
#[cfg(test)]

0 commit comments

Comments
 (0)