Skip to content

Commit

Permalink
fix refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Apr 3, 2024
1 parent 5c63f6d commit 88e33f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
17 changes: 11 additions & 6 deletions crates/controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ impl PDController {
}
}

pub fn compute_inflation(&self, current_metric: Dec) -> Uint {
let control = self.compute_control(current_metric);
pub fn compute_inflation(
&self,
control_coeff: Dec,
current_metric: Dec,
) -> Uint {
let control = self.compute_control(control_coeff, current_metric);
self.compute_inflation_aux(control)
}

Expand All @@ -47,6 +51,10 @@ impl PDController {
.expect("Should not fail to convert Uint to Dec")
}

pub fn get_epochs_per_year(&self) -> u64 {
self.epochs_per_year
}

fn get_max_inflation(&self) -> Uint {
let total_native = self.get_total_native_dec();
let epochs_py: Dec = self.epochs_per_year.into();
Expand Down Expand Up @@ -77,10 +85,7 @@ impl PDController {

// NOTE: This formula is the comactification of all the old intermediate
// computations that were done in multiple steps (as in the specs)
fn compute_control(&self, current_metric: Dec) -> Dec {
let total_native = self.get_total_native_dec();
let epochs_py: Dec = self.epochs_per_year.into();
let coeff = total_native * self.max_reward_rate / epochs_py;
fn compute_control(&self, coeff: Dec, current_metric: Dec) -> Dec {
let val = current_metric * (self.d_gain_nom - self.p_gain_nom)
+ (self.target_metric * self.p_gain_nom)
- (self.last_metric * self.d_gain_nom);
Expand Down
29 changes: 18 additions & 11 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ pub fn compute_inflation(
last_ratio,
);
let metric = Dec::from(locked_amount) / Dec::from(total_native_amount);
let amount_uint = controller.compute_inflation(metric);
token::Amount::from_uint(amount_uint, 0)
.into_storage_result()
let control_coeff = controller.get_total_native_dec() * max_reward_rate
/ controller.get_epochs_per_year();
let amount_uint = controller.compute_inflation(control_coeff, metric);
token::Amount::from_uint(amount_uint, 0).into_storage_result()
}

/// Holds coefficients for the three different ways to get PoS rewards
Expand Down Expand Up @@ -654,7 +655,7 @@ mod tests {
"Round 0: Locked ratio: {locked_ratio_0}, inflation: {inflation_0}"
);
assert_eq!(locked_ratio_0, Dec::from_str("0.5").unwrap());
assert_eq!(inflation_0, token::Amount::native_whole(18_264));
assert_eq!(inflation_0, token::Amount::from_u64(18264839452));

let locked_amount = locked_amount + inflation_0;
let last_inflation_amount = inflation_0;
Expand Down Expand Up @@ -684,7 +685,7 @@ mod tests {
assert!(locked_ratio_1 > locked_ratio_0);
assert!(locked_ratio_1 > Dec::from_str("0.5").unwrap());
assert!(locked_ratio_1 < Dec::from_str("0.51").unwrap());
assert_eq!(inflation_1, token::Amount::native_whole(36_528));
assert_eq!(inflation_1, token::Amount::from_u64(36529678904));

let locked_amount = locked_amount + inflation_1;
let last_inflation_amount = inflation_1;
Expand All @@ -711,7 +712,7 @@ mod tests {
assert!(locked_ratio_2 > locked_ratio_1);
assert!(locked_ratio_2 > Dec::from_str("0.5").unwrap());
assert!(locked_ratio_2 < Dec::from_str("0.51").unwrap());
assert_eq!(inflation_2, token::Amount::native_whole(54_792));
assert_eq!(inflation_2, token::Amount::from_u64(54794017950));
}

#[test]
Expand Down Expand Up @@ -744,7 +745,7 @@ mod tests {
"Round 0: Locked ratio: {locked_ratio_0}, inflation: {inflation_0}"
);
assert_eq!(locked_ratio_0, Dec::from_str("0.9").unwrap());
assert_eq!(inflation_0, token::Amount::native_whole(3_607));
assert_eq!(inflation_0, token::Amount::from_u64(3607305753));

let locked_amount = locked_amount + inflation_0;
let last_inflation_amount = inflation_0;
Expand Down Expand Up @@ -807,9 +808,13 @@ mod tests {
let epochs_per_year = 365_u64;

let init_locked_ratio = Dec::from_str("0.1").unwrap();
let mut last_locked_ratio = init_locked_ratio;
let total_native_tokens = 1_000_000_000_u64;
let mut locked_amount =
token::Amount::from(init_locked_ratio * total_native_tokens);
let locked_amount = u64::try_from(
(init_locked_ratio * total_native_tokens).to_uint().unwrap(),
)
.unwrap();
let mut locked_amount = token::Amount::native_whole(locked_amount);
let mut last_inflation_amount = token::Amount::zero();
let mut total_native_tokens =
token::Amount::native_whole(total_native_tokens);
Expand All @@ -834,7 +839,7 @@ mod tests {
d_gain_nom,
epochs_per_year,
target_ratio,
init_locked_ratio,
last_locked_ratio,
)
.unwrap();
let locked_ratio =
Expand All @@ -849,14 +854,16 @@ mod tests {

last_inflation_amount = inflation;
total_native_tokens += inflation;
last_locked_ratio = locked_ratio;

// if rate.abs_diff(&controller.max_reward_rate)
// < Dec::from_str("0.01").unwrap()
// {
// controller.locked_tokens = controller.total_tokens;
// }

let tot_tokens = Dec::from(total_native_tokens);
let tot_tokens =
Dec::try_from(total_native_tokens.raw_amount()).unwrap();
let change_staked_tokens =
token::Amount::from(staking_growth * tot_tokens);

Expand Down
4 changes: 3 additions & 1 deletion crates/shielded_token/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub fn compute_inflation(

let metric = Dec::try_from(locked_amount)
.expect("Should not fail to convert Uint to Dec");
controller.compute_inflation(metric)
let control_coeff = max_reward_rate / controller.get_epochs_per_year();
controller.compute_inflation(control_coeff, metric)
}

/// Compute the precision of MASP rewards for the given token. This function
Expand Down Expand Up @@ -704,6 +705,7 @@ mod tests {
let mut locked_tokens_last = init_locked_tokens;

let num_rounds = 10;
println!();

for round in 0..num_rounds {
let inflation = compute_inflation(
Expand Down

0 comments on commit 88e33f8

Please sign in to comment.