Skip to content

Commit

Permalink
Merge branch 'mariari/inflation-rewards' into draft
Browse files Browse the repository at this point in the history
* mariari/inflation-rewards:
  masp_conversions: adjust debug output
  masp: implement remaining inflation logic
  Fixup clippy issues
  Update the test with the correct inflation values
  Corrected the compounding of the MASP transparent balance.
  Now handle native inflation rewards for native tokens.
  Make the formula for the conversion rates reliable, also dump logs for e2e
  Update e2e tests
  Change the last inflation amount to reflect the actual amount minted
  Fix dev.toml having inconsistent token values and inconsistent dot
  Convert test storage to use the WlStorage and added token initalizer
  Add the total token balance to the address of each token account
  Add default values for each token parameter
  Add Parameter data structure for holding an initalizing token params
  Update the update_allowed_conversions to use the pd controller
  Reaplace by hand reward depositing, with a call to mint
  Add Token Parameters module, and keys like inflation and lock ratio
  Move inflation to core from shared
  app/node: tidy up some commented out code and logging
  core/storage: refactor epoch update tracker
  apps/finalize_block: log error when last proposer is missing
  app/finalize_block: refactor log_block_rewards/apply_inflation
  clean redundant code and old comments
  fix `value` types in functions that update parameters storage
  fix block proposer look-up and unit tests
  pos/docs: fix docstring typo
  app/ledger/finalize_block: write inflation + locked ratio via write-log
  app/ledger: tidy up some logging
  app/ledger/finalize_block: fix and refactor block proposer look-up
  test/finalize_block: extend no-DB commit test to ensure we hit inflation
  apps/ledger: only specify num of validators in "dev" build
  changelog: add #714
  [ci] wasm checksums update
  redo block proposer storage in abciplus mode
  clean up documentation and print-outs
  fix e2e::ledger_tests::double_signing_gets_slashed
  fix e2e::ledger_tests::pos_init_validator error
  fix e2e::ledger_tests::proposal_submission
  upgrade total token supply tracking and balance tracking at genesis
  fix e2e test ibc
  WIP - current changes, debug printouts, etc stuff
  update comments and documentation
  Cargo lock and tomls
  tests for inflation
  configure genesis to set up any number of validators
  trigger 2-block countdown to new epoch for Tendermint
  log block rewards with accumulator LazyMaps and apply inflation with rewards products
  storage types and keys for inflation
  inflation and rewards modules
  • Loading branch information
juped committed Sep 21, 2023
2 parents a2213d8 + 0e93a37 commit bfad983
Show file tree
Hide file tree
Showing 26 changed files with 3,132 additions and 1,280 deletions.
6 changes: 6 additions & 0 deletions .changelog/unreleased/features/714-pos-inflation-rewards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- Introduce infrastructure for PoS inflation and rewards. Includes inflation
using the PD controller mechanism and rewards based on validator block voting
behavior. Rewards are tracked and effectively distributed using the F1 fee
mechanism. In this PR, rewards are calculated and stored, but they are not
yet applied to voting powers or considered when unbonding and withdrawing.
([#714](https://github.com/anoma/namada/pull/714))
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ lazy_static = "1.4.0"
libc = "0.2.97"
libloading = "0.7.2"
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9", default-features = false, features = ["std", "static-context"]}
# branch = "murisi/namada-integration"
masp_primitives = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e" }
masp_proofs = { git = "https://github.com/anoma/masp", rev = "50acc5028fbcd52a05970fe7991c7850ab04358e", default-features = false, features = ["local-prover"] }
# branch = "ray/i128-conversions"
masp_primitives = { git = "https://github.com/juped/masp", rev = "1a68887b118781f31960dc983d779c2bc6cce971" }
masp_proofs = { git = "https://github.com/juped/masp", rev = "1a68887b118781f31960dc983d779c2bc6cce971", default-features = false, features = ["local-prover"] }
num256 = "0.3.5"
num_cpus = "1.13.0"
num-derive = "0.3.3"
Expand Down
4 changes: 2 additions & 2 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2126,7 +2126,7 @@ pub async fn query_conversions<
// Track whether any non-sentinel conversions are found
let mut conversions_found = false;
for ((addr, _), epoch, conv, _) in conv_state.assets.values() {
let amt: masp_primitives::transaction::components::I32Sum =
let amt: masp_primitives::transaction::components::I128Sum =
conv.clone().into();
// If the user has specified any targets, then meet them
// If we have a sentinel conversion, then skip printing
Expand Down Expand Up @@ -2181,7 +2181,7 @@ pub async fn query_conversion<C: namada::ledger::queries::Client + Sync>(
Address,
MaspDenom,
Epoch,
masp_primitives::transaction::components::I32Sum,
masp_primitives::transaction::components::I128Sum,
MerklePath<Node>,
)> {
namada::sdk::rpc::query_conversion(client, asset_type).await
Expand Down
40 changes: 21 additions & 19 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use namada::types::key::dkg_session_keys::DkgPublicKey;
use namada::types::key::*;
use namada::types::time::{DateTimeUtc, DurationSecs};
use namada::types::token::Denomination;
use namada::types::uint::Uint;
use namada::types::uint::I256;
use namada::types::{storage, token};

/// Genesis configuration file format
Expand All @@ -38,6 +38,7 @@ pub mod genesis_config {
use namada::types::key::*;
use namada::types::time::Rfc3339String;
use namada::types::token::Denomination;
use namada::types::uint::I256;
use namada::types::{storage, token};
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand Down Expand Up @@ -213,6 +214,9 @@ pub mod genesis_config {
pub vp: Option<String>,
// Initial balances held by accounts defined elsewhere.
pub balances: Option<HashMap<String, token::Amount>>,
// Token parameters
// XXX: u64 doesn't work with toml-rs!
pub parameters: Option<token::parameters::Parameters>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -404,6 +408,9 @@ pub mod genesis_config {
implicit_accounts: &HashMap<String, ImplicitAccount>,
) -> TokenAccount {
TokenAccount {
last_locked_ratio: Dec::zero(),
last_inflation: I256::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 +825,13 @@ pub struct TokenAccount {
/// Accounts' balances of this token
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
pub balances: HashMap<Address, token::Amount>,
// please put the last inflation amount here.
/// Token parameters
pub parameters: token::parameters::Parameters,
/// Token inflation from the last epoch (read + write for every epoch)
pub last_inflation: I256,
/// Token shielded ratio from the last epoch (read + write for every epoch)
pub last_locked_ratio: Dec,
}

#[derive(
Expand Down Expand Up @@ -903,11 +917,10 @@ pub fn genesis(num_validators: u64) -> Genesis {
use namada::ledger::eth_bridge::{
Contracts, Erc20WhitelistEntry, UpgradeableContract,
};
use namada::types::address::{
self, apfel, btc, dot, eth, kartoffel, nam, schnitzel, wnam,
};
use namada::types::address::{self, nam, wnam};
use namada::types::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS;
use namada::types::ethereum_events::EthAddress;
use namada::types::uint::Uint;

use crate::wallet;

Expand Down Expand Up @@ -1074,21 +1087,7 @@ pub fn genesis(num_validators: u64) -> Genesis {
balances.insert((&validator.account_key).into(), default_key_tokens);
}

/// Deprecated function, soon to be deleted. Generates default tokens
fn tokens() -> HashMap<Address, (&'static str, Denomination)> {
vec![
(nam(), ("NAM", 6.into())),
(btc(), ("BTC", 8.into())),
(eth(), ("ETH", 18.into())),
(dot(), ("DOT", 10.into())),
(schnitzel(), ("Schnitzel", 6.into())),
(apfel(), ("Apfel", 6.into())),
(kartoffel(), ("Kartoffel", 6.into())),
]
.into_iter()
.collect()
}
let token_accounts = tokens()
let token_accounts = address::tokens()
.into_iter()
.map(|(address, (_, denom))| TokenAccount {
address,
Expand All @@ -1098,6 +1097,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::Parameters::default(),
last_inflation: I256::zero(),
last_locked_ratio: Dec::zero(),
})
.collect();
Genesis {
Expand Down
22 changes: 22 additions & 0 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,36 @@ 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::last_inflation(&address), last_inflation)
.unwrap();
self.wl_storage
.write(&token::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 @@ -1482,6 +1482,7 @@ mod test_utils {
use namada::proto::{Code, Data};
use namada::tendermint_proto::abci::VoteInfo;
use namada::types::address;
use namada::types::address::init_token_storage;
use namada::types::chain::ChainId;
use namada::types::ethereum_events::Uint;
use namada::types::hash::Hash;
Expand Down Expand Up @@ -1971,6 +1972,7 @@ mod test_utils {
.storage
.begin_block(BlockHash::default(), BlockHeight(1))
.expect("begin_block failed");
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 @@ -59,6 +59,7 @@ mod tests {
types, update_allowed_conversions, WlStorage,
};
use namada::ledger::storage_api::{self, StorageWrite};
use namada::types::address::init_token_storage;
use namada::types::chain::ChainId;
use namada::types::hash::Hash;
use namada::types::storage::{BlockHash, BlockHeight, Key};
Expand Down Expand Up @@ -144,6 +145,7 @@ mod tests {
storage.block.pred_epochs.new_epoch(BlockHeight(100), 1000);
// make wl_storage to update conversion for a new epoch
let mut wl_storage = WlStorage::new(WriteLog::default(), storage);
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
62 changes: 58 additions & 4 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,9 @@
//! proof-of-stake, providing liquity to shielded asset pools, and public goods
//! funding.
use namada_core::types::dec::Dec;

use crate::ledger::storage_api::{self, StorageRead, StorageWrite};
use crate::types::address::Address;
use crate::types::dec::Dec;
use crate::types::token;

/// The domains of inflation
Expand Down Expand Up @@ -47,6 +48,32 @@ pub struct RewardsController {
}

impl RewardsController {
/// Initialize a new PD controller
#[allow(clippy::too_many_arguments)]
pub fn new(
locked_tokens: token::Amount,
total_tokens: token::Amount,
locked_ratio_target: Dec,
locked_ratio_last: Dec,
max_reward_rate: Dec,
last_inflation_amount: token::Amount,
p_gain_nom: Dec,
d_gain_nom: Dec,
epochs_per_year: u64,
) -> Self {
Self {
locked_tokens,
total_tokens,
locked_ratio_target,
locked_ratio_last,
max_reward_rate,
last_inflation_amount,
p_gain_nom,
d_gain_nom,
epochs_per_year,
}
}

/// Calculate a new rewards rate
pub fn run(self) -> ValsToUpdate {
let Self {
Expand Down Expand Up @@ -110,13 +137,40 @@ impl RewardsController {
}
}

/// Function that allows the protocol to mint some number of tokens of a desired
/// type to a destination address TODO: think of error cases that must be
/// handled.
pub fn mint_tokens<S>(
storage: &mut S,
target: &Address,
token: &Address,
amount: token::Amount,
) -> storage_api::Result<()>
where
S: StorageWrite + StorageRead,
{
let dest_key = token::balance_key(token, target);
let mut dest_bal: token::Amount =
storage.read(&dest_key)?.unwrap_or_default();
dest_bal.receive(&amount);
storage.write(&dest_key, dest_bal)?;

// Update the total supply of the tokens in storage
let mut total_tokens: token::Amount = storage
.read(&token::minted_balance_key(token))?
.unwrap_or_default();
total_tokens.receive(&amount);
storage.write(&token::minted_balance_key(token), total_tokens)?;

Ok(())
}

#[cfg(test)]
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 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 bfad983

Please sign in to comment.