Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor handling of previous PoS inflation data #2559

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 0 additions & 4 deletions crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions crates/apps/src/lib/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions crates/apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 0 additions & 2 deletions crates/apps/src/lib/node/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 0 additions & 5 deletions crates/core/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
51 changes: 0 additions & 51 deletions crates/parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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<S>(
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<S>(
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<S>(
storage: &mut S,
Expand Down Expand Up @@ -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)?;
Expand All @@ -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,
Expand Down
25 changes: 0 additions & 25 deletions crates/parameters/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions crates/proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)?;
Expand Down
37 changes: 19 additions & 18 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -309,26 +311,26 @@ where
// Read from Parameters storage
let epochs_per_year: u64 = storage
.read(&params_storage::get_epochs_per_year_key())?
.expect("Epochs per year should exist in storage");
let pos_last_staked_ratio: Dec = storage
.read(&params_storage::get_staked_ratio_key())?
.expect("PoS staked ratio should exist in storage");
let pos_last_inflation_amount: token::Amount = storage
.read(&params_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, &params, 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 {
Expand Down Expand Up @@ -365,9 +367,8 @@ where

// Write new rewards parameters that will be used for the inflation of
// the current new epoch
storage
.write(&params_storage::get_pos_inflation_amount_key(), inflation)?;
storage.write(&params_storage::get_staked_ratio_key(), locked_ratio)?;
write_last_staked_ratio(storage, locked_ratio)?;
write_last_pos_inflation_amount(storage, inflation)?;

Ok(())
}
Expand Down
46 changes: 46 additions & 0 deletions crates/proof_of_stake/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,52 @@ where
storage.write(&key, address)
}

/// Read last epoch's staked ratio.
pub fn read_last_staked_ratio<S>(
storage: &S,
) -> namada_storage::Result<Option<Dec>>
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<S>(
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<S>(
storage: &S,
) -> namada_storage::Result<Option<token::Amount>>
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<S>(
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<S>(
storage: &S,
Expand Down
16 changes: 16 additions & 0 deletions crates/proof_of_stake/src/storage_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
2 changes: 0 additions & 2 deletions crates/shielded_token/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading
Loading