From 7cc6e3266374f40adf06b99dc54657740689b494 Mon Sep 17 00:00:00 2001 From: brentstone Date: Thu, 28 Dec 2023 18:16:56 -0800 Subject: [PATCH] fixup! Merge branch 'brent/refactor-apply-inflation' (#2295) --- .../lib/node/ledger/shell/finalize_block.rs | 8 +- proof_of_stake/src/lib.rs | 80 ----------------- proof_of_stake/src/rewards.rs | 85 +++++++++++++++++-- 3 files changed, 83 insertions(+), 90 deletions(-) diff --git a/apps/src/lib/node/ledger/shell/finalize_block.rs b/apps/src/lib/node/ledger/shell/finalize_block.rs index a9b7c584db..b6ca38742a 100644 --- a/apps/src/lib/node/ledger/shell/finalize_block.rs +++ b/apps/src/lib/node/ledger/shell/finalize_block.rs @@ -4,7 +4,6 @@ use data_encoding::HEXUPPER; use masp_primitives::merkle_tree::CommitmentTree; use masp_primitives::sapling::Node; use masp_proofs::bls12_381; -use namada::core::ledger::inflation; use namada::core::ledger::masp_conversions::update_allowed_conversions; use namada::core::ledger::pgf::inflation as pgf_inflation; use namada::core::types::storage::KeySeg; @@ -15,8 +14,7 @@ use namada::ledger::protocol; use namada::ledger::storage::wl_storage::WriteLogAndStorage; use namada::ledger::storage::write_log::StorageModification; use namada::ledger::storage::EPOCH_SWITCH_BLOCKS_DELAY; -use namada::ledger::storage_api::token::credit_tokens; -use namada::ledger::storage_api::{pgf, ResultExt, StorageRead, StorageWrite}; +use namada::ledger::storage_api::{ResultExt, StorageRead, StorageWrite}; use namada::proof_of_stake::storage::{ find_validator_by_raw_hash, read_last_block_proposer_address, write_last_block_proposer_address, @@ -675,7 +673,7 @@ where self.wl_storage.storage.block.height.0 - first_block_of_last_epoch; // PoS inflation - namada_proof_of_stake::apply_inflation( + namada_proof_of_stake::rewards::apply_inflation( &mut self.wl_storage, last_epoch, num_blocks_in_last_epoch, @@ -837,7 +835,7 @@ mod test_finalize_block { use namada::proof_of_stake::storage::{ enqueued_slashes_handle, get_num_consensus_validators, read_consensus_validator_set_addresses_with_stake, read_total_stake, - read_validator_stake, rewards_accumulator_handle, unjail_validator, + read_validator_stake, rewards_accumulator_handle, validator_consensus_key_handle, validator_rewards_products_handle, validator_slashes_handle, validator_state_handle, write_pos_params, }; diff --git a/proof_of_stake/src/lib.rs b/proof_of_stake/src/lib.rs index bce9b6d176..a390d6ec00 100644 --- a/proof_of_stake/src/lib.rs +++ b/proof_of_stake/src/lib.rs @@ -27,8 +27,6 @@ use std::cmp::{self}; use std::collections::{BTreeMap, BTreeSet, HashSet}; pub use error::*; -use namada_core::ledger::inflation; -use namada_core::ledger::parameters::storage as params_storage; use namada_core::ledger::storage_api::collections::lazy_map::{ Collectable, LazyMap, NestedSubKey, SubKey, }; @@ -2737,81 +2735,3 @@ where } Ok(()) } - -/// Apply inflation to the Proof of Stake system. -pub fn apply_inflation( - storage: &mut S, - last_epoch: Epoch, - num_blocks_in_last_epoch: u64, -) -> storage_api::Result<()> -where - S: StorageRead + StorageWrite, -{ - // Read from Parameters storage - let epochs_per_year: u64 = storage - .read(¶ms_storage::get_epochs_per_year_key())? - .expect("Epochs per year should exist in storage"); - let pos_p_gain_nom: Dec = storage - .read(¶ms_storage::get_pos_gain_p_key())? - .expect("PoS P-gain factor should exist in storage"); - let pos_d_gain_nom: Dec = storage - .read(¶ms_storage::get_pos_gain_d_key())? - .expect("PoS D-gain factor should exist in storage"); - - let pos_last_staked_ratio: Dec = storage - .read(¶ms_storage::get_staked_ratio_key())? - .expect("PoS staked ratio should exist in storage"); - let pos_last_inflation_amount: token::Amount = storage - .read(¶ms_storage::get_pos_inflation_amount_key())? - .expect("PoS inflation amount should exist in storage"); - - // Read from PoS storage - let params = read_pos_params(storage)?; - let staking_token = staking_token_address(storage); - - let total_tokens: token::Amount = storage - .read(&token::minted_balance_key(&staking_token))? - .expect("Total NAM balance should exist in storage"); - let pos_locked_supply = read_total_stake(storage, ¶ms, last_epoch)?; - let pos_locked_ratio_target = params.target_staked_ratio; - let pos_max_inflation_rate = params.max_inflation_rate; - - // Run rewards PD controller - let pos_controller = inflation::RewardsController { - locked_tokens: pos_locked_supply.raw_amount(), - total_tokens: total_tokens.raw_amount(), - total_native_tokens: total_tokens.raw_amount(), - locked_ratio_target: pos_locked_ratio_target, - locked_ratio_last: pos_last_staked_ratio, - max_reward_rate: pos_max_inflation_rate, - last_inflation_amount: pos_last_inflation_amount.raw_amount(), - p_gain_nom: pos_p_gain_nom, - d_gain_nom: pos_d_gain_nom, - epochs_per_year, - }; - // Run the rewards controllers - let inflation::ValsToUpdate { - locked_ratio, - inflation, - } = pos_controller.run(); - - let inflation = - token::Amount::from_uint(inflation, 0).into_storage_result()?; - - update_rewards_products_and_mint_inflation( - storage, - ¶ms, - last_epoch, - num_blocks_in_last_epoch, - inflation, - &staking_token, - )?; - - // Write new rewards parameters that will be used for the inflation of - // the current new epoch - storage - .write(¶ms_storage::get_pos_inflation_amount_key(), inflation)?; - storage.write(¶ms_storage::get_staked_ratio_key(), locked_ratio)?; - - Ok(()) -} diff --git a/proof_of_stake/src/rewards.rs b/proof_of_stake/src/rewards.rs index 3b19bd6b79..880e7404c4 100644 --- a/proof_of_stake/src/rewards.rs +++ b/proof_of_stake/src/rewards.rs @@ -2,6 +2,8 @@ use std::collections::{HashMap, HashSet}; +use namada_core::ledger::inflation; +use namada_core::ledger::parameters::storage as params_storage; use namada_core::ledger::storage_api::collections::lazy_map::NestedSubKey; use namada_core::ledger::storage_api::token::credit_tokens; use namada_core::ledger::storage_api::{ @@ -16,14 +18,14 @@ use thiserror::Error; use crate::storage::{ consensus_validator_set_handle, get_last_reward_claim_epoch, - read_pos_params, read_validator_stake, rewards_accumulator_handle, - validator_commission_rate_handle, validator_rewards_products_handle, - validator_state_handle, + read_pos_params, read_total_stake, read_validator_stake, + rewards_accumulator_handle, validator_commission_rate_handle, + validator_rewards_products_handle, validator_state_handle, }; use crate::types::{into_tm_voting_power, BondId, ValidatorState, VoteInfo}; use crate::{ - bond_amounts_for_rewards, get_total_consensus_stake, storage_key, - InflationError, PosParams, + bond_amounts_for_rewards, get_total_consensus_stake, staking_token_address, + storage_key, InflationError, PosParams, }; /// This is equal to 0.01. @@ -257,6 +259,79 @@ where Ok(()) } +/// Apply inflation to the Proof of Stake system. +pub fn apply_inflation( + storage: &mut S, + last_epoch: Epoch, + num_blocks_in_last_epoch: u64, +) -> storage_api::Result<()> +where + S: StorageRead + StorageWrite, +{ + // Read from Parameters storage + let epochs_per_year: u64 = storage + .read(¶ms_storage::get_epochs_per_year_key())? + .expect("Epochs per year should exist in storage"); + let pos_last_staked_ratio: Dec = storage + .read(¶ms_storage::get_staked_ratio_key())? + .expect("PoS staked ratio should exist in storage"); + let pos_last_inflation_amount: token::Amount = storage + .read(¶ms_storage::get_pos_inflation_amount_key())? + .expect("PoS inflation amount should exist in storage"); + + // Read from PoS storage + let params = read_pos_params(storage)?; + let staking_token = staking_token_address(storage); + let pos_p_gain_nom = params.rewards_gain_p; + let pos_d_gain_nom = params.rewards_gain_d; + + let total_tokens: token::Amount = storage + .read(&token::minted_balance_key(&staking_token))? + .expect("Total NAM balance should exist in storage"); + let pos_locked_supply = read_total_stake(storage, ¶ms, last_epoch)?; + let pos_locked_ratio_target = params.target_staked_ratio; + let pos_max_inflation_rate = params.max_inflation_rate; + + // Run rewards PD controller + let pos_controller = inflation::RewardsController { + locked_tokens: pos_locked_supply.raw_amount(), + total_tokens: total_tokens.raw_amount(), + total_native_tokens: total_tokens.raw_amount(), + locked_ratio_target: pos_locked_ratio_target, + locked_ratio_last: pos_last_staked_ratio, + max_reward_rate: pos_max_inflation_rate, + last_inflation_amount: pos_last_inflation_amount.raw_amount(), + p_gain_nom: pos_p_gain_nom, + d_gain_nom: pos_d_gain_nom, + epochs_per_year, + }; + // Run the rewards controllers + let inflation::ValsToUpdate { + locked_ratio, + inflation, + } = pos_controller.run(); + + let inflation = + token::Amount::from_uint(inflation, 0).into_storage_result()?; + + update_rewards_products_and_mint_inflation( + storage, + ¶ms, + last_epoch, + num_blocks_in_last_epoch, + inflation, + &staking_token, + )?; + + // Write new rewards parameters that will be used for the inflation of + // the current new epoch + storage + .write(¶ms_storage::get_pos_inflation_amount_key(), inflation)?; + storage.write(¶ms_storage::get_staked_ratio_key(), locked_ratio)?; + + Ok(()) +} + #[derive(Clone, Debug)] struct Rewards { product: Dec,