From d979482804ddaab9770bdfbcd84fce5fbe39acaf Mon Sep 17 00:00:00 2001 From: brentstone Date: Sat, 14 Sep 2024 11:03:26 +0200 Subject: [PATCH] testing --- crates/proof_of_stake/src/rewards.rs | 69 +++++++++++++++++++- crates/tests/src/integration/ledger_tests.rs | 10 +++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/crates/proof_of_stake/src/rewards.rs b/crates/proof_of_stake/src/rewards.rs index 45e6422362..00a77f27c3 100644 --- a/crates/proof_of_stake/src/rewards.rs +++ b/crates/proof_of_stake/src/rewards.rs @@ -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() { @@ -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(), @@ -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(); @@ -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() @@ -965,4 +1012,22 @@ mod tests { // ); } } + + fn update_state_for_pos_playground( + 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(); + } } diff --git a/crates/tests/src/integration/ledger_tests.rs b/crates/tests/src/integration/ledger_tests.rs index ed34692bb7..7d6322d448 100644 --- a/crates/tests/src/integration/ledger_tests.rs +++ b/crates/tests/src/integration/ledger_tests.rs @@ -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(()) }