Skip to content

Commit

Permalink
Merge branch 'brent/remove-pgf-from-total-supply' (#3002)
Browse files Browse the repository at this point in the history
* origin/brent/remove-pgf-from-total-supply:
  changelog: add #3002
  cleanup balance reading
  update total supply RPC call
  use effective total supply in inflation
  get effective total native supply

# Conflicts:
#	crates/proof_of_stake/src/rewards.rs
#	crates/shielded_token/src/conversion.rs
  • Loading branch information
tzemanovic committed Apr 11, 2024
2 parents 0c24b71 + eb9d7a7 commit 5de7646
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- For inflation computations and the relevant RPC, don't
include the PGF balance in the total native supply
([\#3002](https://github.com/anoma/namada/pull/3002))
8 changes: 2 additions & 6 deletions crates/governance/src/pgf/inflation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! PGF lib code.
use namada_core::address::Address;
use namada_core::token;
use namada_parameters::storage as params_storage;
use namada_storage::{Result, StorageRead, StorageWrite};
use namada_trans_token::credit_tokens;
use namada_trans_token::storage_key::minted_balance_key;
use namada_trans_token::{credit_tokens, get_effective_total_native_supply};

use crate::pgf::storage::{get_parameters, get_payments, get_stewards};
use crate::storage::proposal::{PGFIbcTarget, PGFTarget};
Expand All @@ -25,9 +23,7 @@ where
let epochs_per_year: u64 = storage
.read(&params_storage::get_epochs_per_year_key())?
.expect("Epochs per year should exist in storage");
let total_supply: token::Amount = storage
.read(&minted_balance_key(&staking_token))?
.expect("Total native token balance should exist in storage");
let total_supply = get_effective_total_native_supply(storage)?;

let pgf_inflation_amount =
(pgf_parameters.pgf_inflation_rate * total_supply) / epochs_per_year;
Expand Down
12 changes: 5 additions & 7 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use namada_core::uint::{Uint, I256};
use namada_parameters::storage as params_storage;
use namada_storage::collections::lazy_map::NestedSubKey;
use namada_storage::{ResultExt, StorageRead, StorageWrite};
use namada_trans_token::get_effective_total_native_supply;
use thiserror::Error;

use crate::storage::{
Expand All @@ -21,7 +22,6 @@ use crate::storage::{
write_last_staked_ratio,
};
use crate::token::credit_tokens;
use crate::token::storage_key::minted_balance_key;
use crate::types::{into_tm_voting_power, BondId, ValidatorState, VoteInfo};
use crate::{
bond_amounts_for_rewards, get_total_consensus_stake, staking_token_address,
Expand Down Expand Up @@ -344,9 +344,7 @@ where
.expect("Epochs per year should exist in parameters storage");

let staking_token = staking_token_address(storage);
let total_amount: token::Amount = storage
.read(&minted_balance_key(&staking_token))?
.expect("Total NAM balance should exist in storage");
let total_tokens = get_effective_total_native_supply(storage)?;

// Read from PoS storage
let params = read_pos_params(storage)?;
Expand All @@ -365,7 +363,7 @@ where
// Compute the new inflation
let inflation = compute_inflation(
locked_amount,
total_amount,
total_tokens,
max_inflation_rate,
last_inflation_amount,
p_gain_nom,
Expand All @@ -384,13 +382,13 @@ where
num_blocks_in_last_epoch,
inflation,
&staking_token,
total_amount,
total_tokens,
)?;

// Write new rewards parameters that will be used for the inflation of
// the current new epoch
let locked_amount = Dec::from(locked_amount);
let total_amount = Dec::from(total_amount);
let total_amount = Dec::from(total_tokens);
let locked_ratio = locked_amount / total_amount;

write_last_staked_ratio(storage, locked_ratio)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/proof_of_stake/src/tests/test_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use proptest::test_runner::Config;
// Use `RUST_LOG=info` (or another tracing level) and `--nocapture` to see
// `tracing` logs from tests
use test_log::test;
use token::storage_key::minted_balance_key;
use token::get_effective_total_native_supply;

use crate::parameters::testing::arb_pos_params;
use crate::parameters::OwnedPosParams;
Expand Down Expand Up @@ -1416,10 +1416,7 @@ fn test_update_rewards_products_aux(validators: Vec<GenesisValidator>) {
.unwrap();
}

let total_native_tokens: token::Amount = s
.read(&minted_balance_key(&staking_token))
.unwrap()
.expect("Total NAM balance should exist in storage");
let total_native_tokens = get_effective_total_native_supply(&s).unwrap();

// Distribute inflation into rewards
let last_epoch = current_epoch.prev();
Expand Down
11 changes: 9 additions & 2 deletions crates/sdk/src/queries/vp/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use namada_core::address::Address;
use namada_core::token;
use namada_state::{DBIter, StorageHasher, DB};
use namada_token::{read_denom, read_total_supply};
use namada_token::{
get_effective_total_native_supply, read_denom, read_total_supply,
};

use crate::queries::RequestCtx;

Expand Down Expand Up @@ -34,7 +36,12 @@ where
D: 'static + DB + for<'iter> DBIter<'iter> + Sync,
H: 'static + StorageHasher + Sync,
{
read_total_supply(ctx.state, &addr)
let native_token = ctx.state.in_mem().native_token.clone();
if addr == native_token {
get_effective_total_native_supply(ctx.state)
} else {
read_total_supply(ctx.state, &addr)
}
}

#[cfg(any(test, feature = "async-client"))]
Expand Down
22 changes: 10 additions & 12 deletions crates/shielded_token/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use namada_core::hash::Hash;
use namada_core::uint::Uint;
use namada_parameters as parameters;
use namada_storage::{StorageRead, StorageWrite};
use namada_trans_token::storage_key::{balance_key, minted_balance_key};
use namada_trans_token::{read_denom, Amount, DenominatedAmount, Denomination};
use namada_trans_token::{
get_effective_total_native_supply, read_balance, read_denom, Amount,
DenominatedAmount, Denomination,
};

#[cfg(any(feature = "multicore", test))]
use crate::storage_key::{masp_assets_hash_key, masp_token_map_key};
Expand Down Expand Up @@ -92,16 +94,11 @@ where

// Query the storage for information -------------------------

let native_token = storage.get_native_token()?;
//// information about the amount of native tokens on the chain
let total_native_tokens: Amount = storage
.read(&minted_balance_key(&native_token))?
.expect("the total supply key should be here");
let total_native_tokens = get_effective_total_native_supply(storage)?;

// total locked amount in the Shielded pool
let total_tokens_in_masp: Amount = storage
.read(&balance_key(token, &masp_addr))?
.unwrap_or_default();
let total_tokens_in_masp = read_balance(storage, token, &masp_addr)?;

let epochs_per_year: u64 = storage
.read(&parameters::storage::get_epochs_per_year_key())?
Expand Down Expand Up @@ -247,6 +244,7 @@ where
use namada_core::masp::encode_asset_type;
use namada_core::storage::Epoch;
use namada_storage::ResultExt;
use namada_trans_token::storage_key::balance_key;
use namada_trans_token::{MaspDigitPos, NATIVE_MAX_DECIMAL_PLACES};
use rayon::iter::{
IndexedParallelIterator, IntoParallelIterator, ParallelIterator,
Expand Down Expand Up @@ -329,9 +327,8 @@ where
let (reward, denom) = calculate_masp_rewards(storage, token)?;
masp_reward_denoms.insert(token.clone(), denom);
// Dispense a transparent reward in parallel to the shielded rewards
let addr_bal: Amount = storage
.read(&balance_key(token, &masp_addr))?
.unwrap_or_default();
let addr_bal = read_balance(storage, token, &masp_addr)?;

// Get the last rewarded amount of the native token
let normed_inflation = *storage
.conversion_state_mut()
Expand Down Expand Up @@ -568,6 +565,7 @@ mod tests {
use namada_core::dec::testing::arb_non_negative_dec;
use namada_core::token::testing::arb_amount;
use namada_storage::testing::TestStorage;
use namada_trans_token::storage_key::{balance_key, minted_balance_key};
use namada_trans_token::write_denom;
use proptest::prelude::*;
use proptest::test_runner::Config;
Expand Down
19 changes: 19 additions & 0 deletions crates/trans_token/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ where
Ok(balance)
}

/// Get the effective circulating total supply of native tokens.
pub fn get_effective_total_native_supply<S>(
storage: &S,
) -> namada_storage::Result<token::Amount>
where
S: StorageRead,
{
let native_token = storage.get_native_token()?;
let pgf_address = Address::Internal(InternalAddress::Pgf);

let raw_total = read_total_supply(storage, &native_token)?;
let pgf_balance = read_balance(storage, &native_token, &pgf_address)?;

// Remove native balance in PGF address from the total supply
Ok(raw_total
.checked_sub(pgf_balance)
.expect("Raw total supply should be larger than PGF balance"))
}

/// Read the denomination of a given token, if any. Note that native
/// transparent tokens do not have this set and instead use the constant
/// [`token::NATIVE_MAX_DECIMAL_PLACES`].
Expand Down

0 comments on commit 5de7646

Please sign in to comment.