Skip to content

Commit

Permalink
Integrated PD controller support.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Oct 17, 2023
1 parent 79af00e commit a9964d6
Show file tree
Hide file tree
Showing 29 changed files with 588 additions and 221 deletions.
14 changes: 14 additions & 0 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ pub mod genesis_config {
pub vp: Option<String>,
// Initial balances held by accounts defined elsewhere.
pub balances: Option<HashMap<String, token::Amount>>,
// Token parameters
pub parameters: Option<token::Parameters>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -404,6 +406,9 @@ pub mod genesis_config {
implicit_accounts: &HashMap<String, ImplicitAccount>,
) -> TokenAccount {
TokenAccount {
last_locked_ratio: Dec::zero(),
last_inflation: token::Amount::zero(),
parameters: config.parameters.as_ref().unwrap().to_owned(),
address: Address::decode(config.address.as_ref().unwrap()).unwrap(),
denom: config.denom,
balances: config
Expand Down Expand Up @@ -818,6 +823,12 @@ pub struct TokenAccount {
/// Accounts' balances of this token
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
pub balances: HashMap<Address, token::Amount>,
/// Token parameters
pub parameters: token::Parameters,
/// Token inflation from the last epoch (read + write for every epoch)
pub last_inflation: token::Amount,
/// Token shielded ratio from the last epoch (read + write for every epoch)
pub last_locked_ratio: Dec,
}

#[derive(
Expand Down Expand Up @@ -1100,6 +1111,9 @@ pub fn genesis(num_validators: u64) -> Genesis {
.into_iter()
.map(|(k, v)| (k, token::Amount::from_uint(v, denom).unwrap()))
.collect(),
parameters: token::Parameters::default(),
last_inflation: token::Amount::zero(),
last_locked_ratio: Dec::zero(),
})
.collect();
Genesis {
Expand Down
5 changes: 4 additions & 1 deletion apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;

use data_encoding::HEXUPPER;
use namada::core::ledger::inflation;
use namada::core::ledger::pgf::ADDRESS as pgf_address;
use namada::ledger::events::EventType;
use namada::ledger::gas::{GasMetering, TxGasMeter};
Expand All @@ -11,7 +12,7 @@ use namada::ledger::pos::{namada_proof_of_stake, staking_token_address};
use namada::ledger::storage::EPOCH_SWITCH_BLOCKS_DELAY;
use namada::ledger::storage_api::token::credit_tokens;
use namada::ledger::storage_api::{pgf, StorageRead, StorageWrite};
use namada::ledger::{inflation, protocol, replay_protection};
use namada::ledger::{protocol, replay_protection};
use namada::proof_of_stake::{
delegator_rewards_products_handle, find_validator_by_raw_hash,
read_last_block_proposer_address, read_pos_params, read_total_stake,
Expand Down Expand Up @@ -665,6 +666,7 @@ where
let pos_controller = inflation::RewardsController {
locked_tokens: pos_locked_supply,
total_tokens,
total_native_tokens: total_tokens,
locked_ratio_target: pos_locked_ratio_target,
locked_ratio_last: pos_last_staked_ratio,
max_reward_rate: pos_max_inflation_rate,
Expand All @@ -676,6 +678,7 @@ where
let _masp_controller = inflation::RewardsController {
locked_tokens: masp_locked_supply,
total_tokens,
total_native_tokens: total_tokens,
locked_ratio_target: masp_locked_ratio_target,
locked_ratio_last: masp_locked_ratio_last,
max_reward_rate: masp_max_inflation_rate,
Expand Down
24 changes: 24 additions & 0 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,38 @@ where
address,
denom,
balances,
parameters,
last_inflation,
last_locked_ratio,
} in accounts
{
// Init token parameters and last inflation and caching rates
parameters.init_storage(&address, &mut self.wl_storage);
self.wl_storage
.write(&token::masp_last_inflation(&address), last_inflation)
.unwrap();
self.wl_storage
.write(
&token::masp_last_locked_ratio(&address),
last_locked_ratio,
)
.unwrap();
// associate a token with its denomination.
write_denom(&mut self.wl_storage, &address, denom).unwrap();

let mut total_balance_for_token = token::Amount::default();
for (owner, amount) in balances {
total_balance_for_token += amount;
credit_tokens(&mut self.wl_storage, &address, &owner, amount)
.unwrap();
}
// Write the total amount of tokens for the ratio
self.wl_storage
.write(
&token::minted_balance_key(&address),
total_balance_for_token,
)
.unwrap();
}
}

Expand Down
2 changes: 2 additions & 0 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,7 @@ mod test_utils {

/// We test that on shell shutdown, the tx queue gets persisted in a DB, and
/// on startup it is read successfully
#[cfg(feature = "testing")]
#[test]
fn test_tx_queue_persistence() {
let base_dir = tempdir().unwrap().as_ref().canonicalize().unwrap();
Expand Down Expand Up @@ -2017,6 +2018,7 @@ mod test_utils {
.storage
.begin_block(BlockHash::default(), BlockHeight(1))
.expect("begin_block failed");
token::testing::init_token_storage(&mut shell.wl_storage, 60);
let keypair = gen_keypair();
// enqueue a wrapper tx
let mut wrapper =
Expand Down
2 changes: 2 additions & 0 deletions apps/src/lib/node/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ mod tests {
assert_eq!(result, None);
}

#[cfg(feature = "testing")]
#[test]
fn test_commit_block() {
let db_path =
Expand All @@ -144,6 +145,7 @@ mod tests {
storage.block.pred_epochs.new_epoch(BlockHeight(100));
// make wl_storage to update conversion for a new epoch
let mut wl_storage = WlStorage::new(WriteLog::default(), storage);
namada::types::token::testing::init_token_storage(&mut wl_storage, 60);
update_allowed_conversions(&mut wl_storage)
.expect("update conversions failed");
wl_storage.commit_block().expect("commit failed");
Expand Down
23 changes: 18 additions & 5 deletions shared/src/ledger/inflation.rs → core/src/ledger/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//! proof-of-stake, providing liquity to shielded asset pools, and public goods
//! funding.

use namada_core::types::dec::Dec;

use crate::types::dec::Dec;
use crate::types::token;

/// The domains of inflation
Expand All @@ -30,6 +29,8 @@ pub struct RewardsController {
pub locked_tokens: token::Amount,
/// Total token supply
pub total_tokens: token::Amount,
/// Total native token supply
pub total_native_tokens: token::Amount,
/// PD target locked ratio
pub locked_ratio_target: Dec,
/// PD last locked ratio
Expand All @@ -52,6 +53,7 @@ impl RewardsController {
let Self {
locked_tokens,
total_tokens,
total_native_tokens,
locked_ratio_target,
locked_ratio_last,
max_reward_rate,
Expand All @@ -67,10 +69,12 @@ impl RewardsController {
.expect("Should not fail to convert token Amount to Dec");
let total = Dec::try_from(total_tokens.raw_amount())
.expect("Should not fail to convert token Amount to Dec");
let total_native = Dec::try_from(total_native_tokens.raw_amount())
.expect("Should not fail to convert token Amount to Dec");
let epochs_py: Dec = epochs_per_year.into();

let locked_ratio = locked / total;
let max_inflation = total * max_reward_rate / epochs_py;
let max_inflation = total_native * max_reward_rate / epochs_py;
let p_gain = p_gain_nom * max_inflation;
let d_gain = d_gain_nom * max_inflation;

Expand Down Expand Up @@ -114,9 +118,8 @@ impl RewardsController {
mod test {
use std::str::FromStr;

use namada_core::types::token::NATIVE_MAX_DECIMAL_PLACES;

use super::*;
use crate::types::token::NATIVE_MAX_DECIMAL_PLACES;

#[test]
fn test_inflation_calc_up() {
Expand All @@ -131,6 +134,11 @@ mod test {
NATIVE_MAX_DECIMAL_PLACES,
)
.unwrap(),
total_native_tokens: token::Amount::from_uint(
4_000,
NATIVE_MAX_DECIMAL_PLACES,
)
.unwrap(),
locked_ratio_target: Dec::from_str("0.66666666").unwrap(),
locked_ratio_last: Dec::from_str("0.5").unwrap(),
max_reward_rate: Dec::from_str("0.1").unwrap(),
Expand Down Expand Up @@ -202,6 +210,11 @@ mod test {
NATIVE_MAX_DECIMAL_PLACES,
)
.unwrap(),
total_native_tokens: token::Amount::from_uint(
1_000,
NATIVE_MAX_DECIMAL_PLACES,
)
.unwrap(),
locked_ratio_target: Dec::from_str("0.66666666").unwrap(),
locked_ratio_last: Dec::from_str("0.9").unwrap(),
max_reward_rate: Dec::from_str("0.1").unwrap(),
Expand Down
1 change: 1 addition & 0 deletions core/src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod gas;
pub mod governance;
#[cfg(any(feature = "abciplus", feature = "abcipp"))]
pub mod ibc;
pub mod inflation;
pub mod parameters;
pub mod pgf;
pub mod replay_protection;
Expand Down
Loading

0 comments on commit a9964d6

Please sign in to comment.