Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Sep 14, 2024
1 parent 70e547c commit d979482
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
69 changes: 67 additions & 2 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,13 @@ where
mod tests {
use std::str::FromStr;

use namada_parameters::storage::get_epochs_per_year_key;
use namada_state::testing::TestState;
use namada_trans_token::storage_key::minted_balance_key;
use storage::write_pos_params;

use super::*;
use crate::OwnedPosParams;

#[test]
fn test_inflation_calc_up() {
Expand Down Expand Up @@ -877,10 +883,19 @@ mod tests {

#[test]
fn test_pos_inflation_playground() {
let mut storage = TestState::default();
let gov_params =
namada_governance::parameters::GovernanceParameters::default();
gov_params.init_storage(&mut storage).unwrap();
write_pos_params(&mut storage, &OwnedPosParams::default()).unwrap();

let epochs_per_year = 365_u64;
let epy_key = get_epochs_per_year_key();
storage.write(&epy_key, epochs_per_year).unwrap();

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 locked_amount = u64::try_from(
(init_locked_ratio * total_native_tokens).to_uint().unwrap(),
Expand All @@ -891,6 +906,13 @@ mod tests {
let mut total_native_tokens =
token::Amount::native_whole(total_native_tokens);

update_state_for_pos_playground(
&mut storage,
last_locked_ratio,
last_inflation_amount,
total_native_tokens,
);

let max_reward_rate = Dec::from_str("0.1").unwrap();
let target_ratio = Dec::from_str("0.66666666").unwrap();
let p_gain_nom = Dec::from_str("0.25").unwrap();
Expand All @@ -917,17 +939,42 @@ mod tests {
let locked_ratio = Dec::try_from(locked_amount).unwrap()
/ Dec::try_from(total_native_tokens).unwrap();

let rate = Dec::try_from(inflation).unwrap()
let inflation_rate = Dec::try_from(inflation).unwrap()
* Dec::from(epochs_per_year)
/ Dec::try_from(total_native_tokens).unwrap();
let staking_rate = inflation_rate / locked_ratio;

println!(
"Round {round}: Locked ratio: {locked_ratio}, inflation rate: \
{rate}",
{inflation_rate}, staking rate: {staking_rate}",
);

last_inflation_amount = inflation;
total_native_tokens += inflation;
last_locked_ratio = locked_ratio;
update_state_for_pos_playground(
&mut storage,
last_locked_ratio,
last_inflation_amount,
total_native_tokens,
);

let query_staking_rate = estimate_staking_reward_rate::<
_,
namada_trans_token::Store<_>,
namada_parameters::Store<_>,
>(&storage)
.unwrap();
// println!(" ----> Query staking rate: {query_staking_rate}");
if !staking_rate.is_zero() && !query_staking_rate.is_zero() {
let ratio = staking_rate / query_staking_rate;
let residual = ratio.abs_diff(Dec::one()).unwrap();
assert!(residual < Dec::from_str("0.001").unwrap());
// println!(
// " ----> Ratio: {}\n",
// staking_rate / query_staking_rate
// );
}

// if rate.abs_diff(&controller.max_reward_rate)
// < Dec::from_str("0.01").unwrap()
Expand Down Expand Up @@ -965,4 +1012,22 @@ mod tests {
// );
}
}

fn update_state_for_pos_playground<S>(
storage: &mut S,
last_staked_ratio: Dec,
last_inflation_amount: token::Amount,
total_native_amount: token::Amount,
) where
S: StorageRead + StorageWrite,
{
write_last_staked_ratio(storage, last_staked_ratio).unwrap();
write_last_pos_inflation_amount(storage, last_inflation_amount)
.unwrap();
let total_native_tokens_key =
minted_balance_key(&storage.get_native_token().unwrap());
storage
.write(&total_native_tokens_key, total_native_amount)
.unwrap();
}
}
10 changes: 10 additions & 0 deletions crates/tests/src/integration/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,16 @@ fn pos_rewards() -> Result<()> {
.unwrap();
assert!(amount_post > amount_pre);

let query_staking_rewards_rate =
vec!["staking-rewards-rate", "--node", &validator_one_rpc];
let captured = CapturedOutput::of(|| {
run(&node, Bin::Client, query_staking_rewards_rate)
});
assert_matches!(captured.result, Ok(_));
let _res = captured
.matches(r"Current annual staking rewards rate: 63.483")
.expect("Test failed");

Ok(())
}

Expand Down

0 comments on commit d979482

Please sign in to comment.