diff --git a/.changelog/unreleased/improvements/2559-remove-pos-inflation-from-params.md b/.changelog/unreleased/improvements/2559-remove-pos-inflation-from-params.md new file mode 100644 index 0000000000..80cf23d175 --- /dev/null +++ b/.changelog/unreleased/improvements/2559-remove-pos-inflation-from-params.md @@ -0,0 +1,3 @@ +- Remove last staked ratio and pos inflation amount from + Parameters and initialize them in pos genesis initialization. + ([\#2559](https://github.com/anoma/namada/pull/2559)) \ No newline at end of file diff --git a/crates/apps/src/lib/config/genesis.rs b/crates/apps/src/lib/config/genesis.rs index 62b31afa52..4dce63292d 100644 --- a/crates/apps/src/lib/config/genesis.rs +++ b/crates/apps/src/lib/config/genesis.rs @@ -310,10 +310,6 @@ pub struct Parameters { pub epochs_per_year: u64, /// Maximum amount of signatures per transaction pub max_signatures_per_transaction: u8, - /// PoS staked ratio (read + write for every epoch) - pub staked_ratio: Dec, - /// PoS inflation amount from the last epoch (read + write for every epoch) - pub pos_inflation_amount: token::Amount, /// Fee unshielding gas limit pub fee_unshielding_gas_limit: u64, /// Fee unshielding descriptions limit diff --git a/crates/apps/src/lib/config/genesis/chain.rs b/crates/apps/src/lib/config/genesis/chain.rs index cfe4659bb6..4c40d50f97 100644 --- a/crates/apps/src/lib/config/genesis/chain.rs +++ b/crates/apps/src/lib/config/genesis/chain.rs @@ -9,7 +9,6 @@ use namada::core::address::{ Address, EstablishedAddress, EstablishedAddressGen, }; use namada::core::chain::{ChainId, ChainIdPrefix}; -use namada::core::dec::Dec; use namada::core::hash::Hash; use namada::core::key::{common, RefTo}; use namada::core::time::{DateTimeUtc, DurationNanos, Rfc3339String}; @@ -321,8 +320,6 @@ impl Finalized { .into(); let vp_allowlist = vp_allowlist.unwrap_or_default(); let tx_allowlist = tx_allowlist.unwrap_or_default(); - let staked_ratio = Dec::zero(); - let pos_inflation_amount = 0; namada::ledger::parameters::Parameters { max_tx_bytes, @@ -332,8 +329,6 @@ impl Finalized { tx_allowlist, implicit_vp_code_hash, epochs_per_year, - staked_ratio, - pos_inflation_amount: Amount::native_whole(pos_inflation_amount), max_proposal_bytes, max_signatures_per_transaction, fee_unshielding_gas_limit, diff --git a/crates/apps/src/lib/node/ledger/shell/mod.rs b/crates/apps/src/lib/node/ledger/shell/mod.rs index fd5968ee5b..dabc9ebc17 100644 --- a/crates/apps/src/lib/node/ledger/shell/mod.rs +++ b/crates/apps/src/lib/node/ledger/shell/mod.rs @@ -1993,8 +1993,6 @@ mod test_utils { implicit_vp_code_hash: Default::default(), epochs_per_year: 365, max_signatures_per_transaction: 10, - staked_ratio: Default::default(), - pos_inflation_amount: Default::default(), fee_unshielding_gas_limit: 0, fee_unshielding_descriptions_limit: 0, minimum_gas_price: Default::default(), diff --git a/crates/apps/src/lib/node/ledger/storage/mod.rs b/crates/apps/src/lib/node/ledger/storage/mod.rs index 2e1c383ca4..a32738e29c 100644 --- a/crates/apps/src/lib/node/ledger/storage/mod.rs +++ b/crates/apps/src/lib/node/ledger/storage/mod.rs @@ -167,8 +167,6 @@ mod tests { implicit_vp_code_hash: Default::default(), epochs_per_year: 365, max_signatures_per_transaction: 10, - staked_ratio: Default::default(), - pos_inflation_amount: Default::default(), fee_unshielding_gas_limit: 0, fee_unshielding_descriptions_limit: 0, minimum_gas_price: Default::default(), diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index 38840e3fd3..27012ecef0 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -8,7 +8,6 @@ use namada_migrations::*; use super::address::Address; use super::chain::ProposalBytes; -use super::dec::Dec; use super::hash::Hash; use super::time::DurationSecs; use super::token; @@ -49,10 +48,6 @@ pub struct Parameters { pub epochs_per_year: u64, /// Maximum number of signature per transaction pub max_signatures_per_transaction: u8, - /// PoS staked ratio (read + write for every epoch) - pub staked_ratio: Dec, - /// PoS inflation amount from the last epoch (read + write for every epoch) - pub pos_inflation_amount: token::Amount, /// Fee unshielding gas limit pub fee_unshielding_gas_limit: u64, /// Fee unshielding descriptions limit diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index b0c0a93410..91833501a8 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -5,7 +5,6 @@ use std::collections::BTreeMap; use namada_core::address::{Address, InternalAddress}; use namada_core::chain::ProposalBytes; -use namada_core::dec::Dec; use namada_core::hash::Hash; pub use namada_core::parameters::*; use namada_core::storage::Key; @@ -59,8 +58,6 @@ where implicit_vp_code_hash, epochs_per_year, max_signatures_per_transaction, - staked_ratio, - pos_inflation_amount, minimum_gas_price, fee_unshielding_gas_limit, fee_unshielding_descriptions_limit, @@ -138,12 +135,6 @@ where max_signatures_per_transaction, )?; - let staked_ratio_key = storage::get_staked_ratio_key(); - storage.write(&staked_ratio_key, staked_ratio)?; - - let pos_inflation_key = storage::get_pos_inflation_amount_key(); - storage.write(&pos_inflation_key, pos_inflation_amount)?; - let gas_cost_key = storage::get_gas_cost_key(); storage.write(&gas_cost_key, minimum_gas_price)?; @@ -238,32 +229,6 @@ where storage.write(&key, value) } -/// Update the PoS staked ratio parameter in storage. Returns the parameters and -/// gas cost. -pub fn update_staked_ratio_parameter( - storage: &mut S, - value: &Dec, -) -> namada_storage::Result<()> -where - S: StorageRead + StorageWrite, -{ - let key = storage::get_staked_ratio_key(); - storage.write(&key, value) -} - -/// Update the PoS inflation rate parameter in storage. Returns the parameters -/// and gas cost. -pub fn update_pos_inflation_amount_parameter( - storage: &mut S, - value: &u64, -) -> namada_storage::Result<()> -where - S: StorageRead + StorageWrite, -{ - let key = storage::get_pos_inflation_amount_key(); - storage.write(&key, value) -} - /// Update the implicit VP parameter in storage. Return the gas cost. pub fn update_implicit_vp( storage: &mut S, @@ -409,20 +374,6 @@ where .ok_or(ReadError::ParametersMissing) .into_storage_result()?; - // read staked ratio - let staked_ratio_key = storage::get_staked_ratio_key(); - let value = storage.read(&staked_ratio_key)?; - let staked_ratio = value - .ok_or(ReadError::ParametersMissing) - .into_storage_result()?; - - // read PoS inflation rate - let pos_inflation_key = storage::get_pos_inflation_amount_key(); - let value = storage.read(&pos_inflation_key)?; - let pos_inflation_amount = value - .ok_or(ReadError::ParametersMissing) - .into_storage_result()?; - // read gas cost let gas_cost_key = storage::get_gas_cost_key(); let value = storage.read(&gas_cost_key)?; @@ -448,8 +399,6 @@ where implicit_vp_code_hash: Some(implicit_vp_code_hash), epochs_per_year, max_signatures_per_transaction, - staked_ratio, - pos_inflation_amount, minimum_gas_price, fee_unshielding_gas_limit, fee_unshielding_descriptions_limit, diff --git a/crates/parameters/src/storage.rs b/crates/parameters/src/storage.rs index abf3fa743f..ed66efdaed 100644 --- a/crates/parameters/src/storage.rs +++ b/crates/parameters/src/storage.rs @@ -25,11 +25,6 @@ struct Keys { /// Sub-lkey for storing the Ethereum address of the bridge contract. bridge_contract_address: &'static str, // ======================================== - // PoS parameters - // ======================================== - pos_inflation_amount: &'static str, - staked_ratio: &'static str, - // ======================================== // Core parameters // ======================================== epoch_duration: &'static str, @@ -95,16 +90,6 @@ pub fn is_epochs_per_year_key(key: &Key) -> bool { is_epochs_per_year_key_at_addr(key, &ADDRESS) } -/// Returns if the key is the staked ratio key. -pub fn is_staked_ratio_key(key: &Key) -> bool { - is_staked_ratio_key_at_addr(key, &ADDRESS) -} - -/// Returns if the key is the PoS reward rate key. -pub fn is_pos_inflation_amount_key(key: &Key) -> bool { - is_pos_inflation_amount_key_at_addr(key, &ADDRESS) -} - /// Returns if the key is the max proposal bytes key. pub fn is_max_proposal_bytes_key(key: &Key) -> bool { is_max_proposal_bytes_key_at_addr(key, &ADDRESS) @@ -155,16 +140,6 @@ pub fn get_epochs_per_year_key() -> Key { get_epochs_per_year_key_at_addr(ADDRESS) } -/// Storage key used for staked ratio parameter. -pub fn get_staked_ratio_key() -> Key { - get_staked_ratio_key_at_addr(ADDRESS) -} - -/// Storage key used for the inflation amount parameter. -pub fn get_pos_inflation_amount_key() -> Key { - get_pos_inflation_amount_key_at_addr(ADDRESS) -} - /// Storage key used for the max proposal bytes. pub fn get_max_proposal_bytes_key() -> Key { get_max_proposal_bytes_key_at_addr(ADDRESS) diff --git a/crates/proof_of_stake/src/lib.rs b/crates/proof_of_stake/src/lib.rs index 02b9ada912..a7463a6bd7 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -67,8 +67,10 @@ use crate::storage::{ validator_rewards_products_handle, validator_set_positions_handle, validator_slashes_handle, validator_state_handle, validator_total_redelegated_bonded_handle, - validator_total_redelegated_unbonded_handle, write_last_reward_claim_epoch, - write_pos_params, write_validator_address_raw_hash, write_validator_avatar, + validator_total_redelegated_unbonded_handle, + write_last_pos_inflation_amount, write_last_reward_claim_epoch, + write_last_staked_ratio, write_pos_params, + write_validator_address_raw_hash, write_validator_avatar, write_validator_description, write_validator_discord_handle, write_validator_email, write_validator_max_commission_rate_change, write_validator_metadata, write_validator_website, @@ -113,6 +115,11 @@ where tracing::debug!("Initializing PoS genesis"); write_pos_params(storage, params)?; + // Initialize values for PoS inflation + write_last_staked_ratio(storage, Dec::zero())?; + write_last_pos_inflation_amount(storage, token::Amount::zero())?; + + // Initialize validator set data consensus_validator_set_handle().init(storage, current_epoch)?; below_capacity_validator_set_handle().init(storage, current_epoch)?; validator_set_positions_handle().init(storage, current_epoch)?; diff --git a/crates/proof_of_stake/src/rewards.rs b/crates/proof_of_stake/src/rewards.rs index 05bc89613a..15a5e8407e 100644 --- a/crates/proof_of_stake/src/rewards.rs +++ b/crates/proof_of_stake/src/rewards.rs @@ -14,9 +14,11 @@ use thiserror::Error; use crate::storage::{ consensus_validator_set_handle, get_last_reward_claim_epoch, - read_pos_params, read_total_stake, read_validator_stake, - rewards_accumulator_handle, validator_commission_rate_handle, - validator_rewards_products_handle, validator_state_handle, + read_last_pos_inflation_amount, read_last_staked_ratio, read_pos_params, + read_total_stake, read_validator_stake, rewards_accumulator_handle, + validator_commission_rate_handle, validator_rewards_products_handle, + validator_state_handle, write_last_pos_inflation_amount, + write_last_staked_ratio, }; use crate::token::storage_key::minted_balance_key; use crate::token::{credit_tokens, inflation}; @@ -309,26 +311,26 @@ where // 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"); + .expect("Epochs per year should exist in parameters 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(&minted_balance_key(&staking_token))? .expect("Total NAM balance should exist in storage"); + + // Read from PoS storage + let params = read_pos_params(storage)?; let pos_locked_supply = read_total_stake(storage, ¶ms, last_epoch)?; + + let pos_last_staked_ratio = read_last_staked_ratio(storage)? + .expect("Last staked ratio should exist in PoS storage"); + let pos_last_inflation_amount = read_last_pos_inflation_amount(storage)? + .expect("Last inflation amount should exist in PoS storage"); + let pos_locked_ratio_target = params.target_staked_ratio; let pos_max_inflation_rate = params.max_inflation_rate; + let pos_p_gain_nom = params.rewards_gain_p; + let pos_d_gain_nom = params.rewards_gain_d; // Run rewards PD controller let pos_controller = inflation::PosRewardsController { @@ -365,9 +367,8 @@ where // 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)?; + write_last_staked_ratio(storage, locked_ratio)?; + write_last_pos_inflation_amount(storage, inflation)?; Ok(()) } diff --git a/crates/proof_of_stake/src/storage.rs b/crates/proof_of_stake/src/storage.rs index 9cae1168a9..63684566d4 100644 --- a/crates/proof_of_stake/src/storage.rs +++ b/crates/proof_of_stake/src/storage.rs @@ -387,6 +387,52 @@ where storage.write(&key, address) } +/// Read last epoch's staked ratio. +pub fn read_last_staked_ratio( + storage: &S, +) -> namada_storage::Result> +where + S: StorageRead, +{ + let key = storage_key::last_staked_ratio_key(); + storage.read(&key) +} + +/// Write last epoch's staked ratio. +pub fn write_last_staked_ratio( + storage: &mut S, + ratio: Dec, +) -> namada_storage::Result<()> +where + S: StorageRead + StorageWrite, +{ + let key = storage_key::last_staked_ratio_key(); + storage.write(&key, ratio) +} + +/// Read last epoch's PoS inflation amount. +pub fn read_last_pos_inflation_amount( + storage: &S, +) -> namada_storage::Result> +where + S: StorageRead, +{ + let key = storage_key::last_pos_inflation_amount_key(); + storage.read(&key) +} + +/// Write last epoch's pos inflation amount. +pub fn write_last_pos_inflation_amount( + storage: &mut S, + inflation: token::Amount, +) -> namada_storage::Result<()> +where + S: StorageRead + StorageWrite, +{ + let key = storage_key::last_pos_inflation_amount_key(); + storage.write(&key, inflation) +} + /// Read PoS validator's delta value. pub fn read_validator_deltas_value( storage: &S, diff --git a/crates/proof_of_stake/src/storage_key.rs b/crates/proof_of_stake/src/storage_key.rs index 4efb24a06b..1c19b065eb 100644 --- a/crates/proof_of_stake/src/storage_key.rs +++ b/crates/proof_of_stake/src/storage_key.rs @@ -58,6 +58,8 @@ const VALIDATOR_AVATAR_KEY: &str = "avatar"; const LIVENESS_PREFIX: &str = "liveness"; const LIVENESS_MISSED_VOTES: &str = "missed_votes"; const LIVENESS_MISSED_VOTES_SUM: &str = "sum_missed_votes"; +const LAST_STAKED_RATIO_KEY: &str = "last_staked_ratio"; +const LAST_POS_INFLATION_AMOUNT_KEY: &str = "last_inflation_amount"; /// Is the given key a PoS storage key? pub fn is_pos_key(key: &Key) -> bool { @@ -1044,3 +1046,17 @@ pub fn liveness_sum_missed_votes_key() -> Key { .push(&LIVENESS_MISSED_VOTES_SUM.to_owned()) .expect("Cannot obtain a storage key") } + +/// Storage key for the last epoch's staked ratio. +pub fn last_staked_ratio_key() -> Key { + Key::from(ADDRESS.to_db_key()) + .push(&LAST_STAKED_RATIO_KEY.to_owned()) + .expect("Cannot obtain a storage key") +} + +/// Storage key for the last epoch's PoS inflation amount. +pub fn last_pos_inflation_amount_key() -> Key { + Key::from(ADDRESS.to_db_key()) + .push(&LAST_POS_INFLATION_AMOUNT_KEY.to_owned()) + .expect("Cannot obtain a storage key") +} diff --git a/crates/shielded_token/src/conversion.rs b/crates/shielded_token/src/conversion.rs index b0fa016a53..5bf03594b2 100644 --- a/crates/shielded_token/src/conversion.rs +++ b/crates/shielded_token/src/conversion.rs @@ -583,8 +583,6 @@ mod tests { implicit_vp_code_hash: Default::default(), epochs_per_year: 365, max_signatures_per_transaction: 10, - staked_ratio: Default::default(), - pos_inflation_amount: Default::default(), fee_unshielding_gas_limit: 0, fee_unshielding_descriptions_limit: 0, minimum_gas_price: Default::default(), diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index c7f112c500..9165c14afe 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -650,10 +650,8 @@ mod tests { use chrono::{TimeZone, Utc}; use namada_core::address::InternalAddress; use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; - use namada_core::dec::Dec; use namada_core::storage::DbKeySeg; use namada_core::time::{self, DateTimeUtc, Duration}; - use namada_core::token; use namada_parameters::{EpochDuration, Parameters}; use proptest::prelude::*; use proptest::test_runner::Config; @@ -737,8 +735,6 @@ mod tests { implicit_vp_code_hash: Some(Hash::zero()), epochs_per_year: 100, max_signatures_per_transaction: 15, - staked_ratio: Dec::new(1,1).expect("Cannot fail"), - pos_inflation_amount: token::Amount::zero(), fee_unshielding_gas_limit: 20_000, fee_unshielding_descriptions_limit: 15, minimum_gas_price: BTreeMap::default(),