From e401ae17bbcbf93c5814f0e8e6b9f71d5dd2baa7 Mon Sep 17 00:00:00 2001 From: brentstone Date: Wed, 7 Feb 2024 14:33:38 -0500 Subject: [PATCH 1/2] refactor handling of previous PoS inflation data --- crates/apps/src/lib/config/genesis.rs | 4 -- crates/apps/src/lib/config/genesis/chain.rs | 5 -- crates/apps/src/lib/node/ledger/shell/mod.rs | 2 - .../apps/src/lib/node/ledger/storage/mod.rs | 2 - crates/core/src/types/parameters.rs | 5 -- crates/parameters/src/lib.rs | 51 ------------------- crates/parameters/src/storage.rs | 25 --------- crates/proof_of_stake/src/lib.rs | 11 +++- crates/proof_of_stake/src/rewards.rs | 37 +++++++------- crates/proof_of_stake/src/storage.rs | 46 +++++++++++++++++ crates/proof_of_stake/src/storage_key.rs | 16 ++++++ crates/shielded_token/src/conversion.rs | 2 - crates/state/src/lib.rs | 4 -- 13 files changed, 90 insertions(+), 120 deletions(-) diff --git a/crates/apps/src/lib/config/genesis.rs b/crates/apps/src/lib/config/genesis.rs index 82e5f7cdab..d3e35f2a54 100644 --- a/crates/apps/src/lib/config/genesis.rs +++ b/crates/apps/src/lib/config/genesis.rs @@ -287,10 +287,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 88a95ec9f5..61142ff3dc 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::types::address::{ Address, EstablishedAddress, EstablishedAddressGen, }; use namada::types::chain::{ChainId, ChainIdPrefix}; -use namada::types::dec::Dec; use namada::types::hash::Hash; use namada::types::key::{common, RefTo}; use namada::types::time::{DateTimeUtc, DurationNanos, Rfc3339String}; @@ -318,8 +317,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, @@ -329,8 +326,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 d33656aa9d..4d86e6da05 100644 --- a/crates/apps/src/lib/node/ledger/shell/mod.rs +++ b/crates/apps/src/lib/node/ledger/shell/mod.rs @@ -2137,8 +2137,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 385405fed1..1381e375ef 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/types/parameters.rs b/crates/core/src/types/parameters.rs index ee2b9ecf6c..951baa06c4 100644 --- a/crates/core/src/types/parameters.rs +++ b/crates/core/src/types/parameters.rs @@ -4,7 +4,6 @@ use std::collections::BTreeMap; use super::address::Address; use super::chain::ProposalBytes; -use super::dec::Dec; use super::hash::Hash; use super::time::DurationSecs; use super::token; @@ -44,10 +43,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 8fd77ad58f..c11f8b5fdb 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -5,7 +5,6 @@ use std::collections::BTreeMap; use namada_core::types::address::{Address, InternalAddress}; use namada_core::types::chain::ProposalBytes; -use namada_core::types::dec::Dec; use namada_core::types::hash::Hash; pub use namada_core::types::parameters::*; use namada_core::types::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 7bacb2c6fa..8c8fbf55e4 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 558c40e0ea..35971b7e26 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -64,8 +64,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, @@ -110,6 +112,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 78e7accdb4..3675cc3664 100644 --- a/crates/proof_of_stake/src/rewards.rs +++ b/crates/proof_of_stake/src/rewards.rs @@ -15,9 +15,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::credit_tokens; use crate::token::storage_key::minted_balance_key; @@ -270,26 +272,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 { @@ -325,9 +327,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 36c0af84ac..670d7df39e 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 6135468a95..382f179382 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 23db438d42..ec0a1a23da 100644 --- a/crates/shielded_token/src/conversion.rs +++ b/crates/shielded_token/src/conversion.rs @@ -569,8 +569,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 8274277158..61008c60a4 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -1172,9 +1172,7 @@ mod tests { use std::collections::BTreeMap; use chrono::{TimeZone, Utc}; - use namada_core::types::dec::Dec; use namada_core::types::time::{self, Duration}; - use namada_core::types::token; use namada_parameters::Parameters; use proptest::prelude::*; use proptest::test_runner::Config; @@ -1261,8 +1259,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(), From 9d409a0e98712b1bec5d6dfab7a9865f84891477 Mon Sep 17 00:00:00 2001 From: brentstone Date: Thu, 8 Feb 2024 13:17:21 -0500 Subject: [PATCH 2/2] changelog: add #2559 --- .../improvements/2559-remove-pos-inflation-from-params.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/unreleased/improvements/2559-remove-pos-inflation-from-params.md 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