Skip to content

Commit

Permalink
Merge branch 'brent/pos-inflation-rewards' (#714)
Browse files Browse the repository at this point in the history
* brent/pos-inflation-rewards:
  [ci] wasm checksums update
  changelog: add #714
  various import, comment and print clean-ups
  upgrade and refactor vp_token + related logic
  pos: update tests for validator sets updates
  pos: use the native token address as staking token
  core/ledger: fix `value` types in functions that update parameters storage
  fixes/upgrades for tests
  core/token/credit_tokens: handle overflows
  upgrade total token supply tracking and balance tracking at genesis
  pos misc: cargo files, debugging, test parameters
  tests for inflation
  apps/lib: 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
  shared/pos: re-use PoS address from address mod
  core: refactor gov address
  • Loading branch information
tzemanovic committed Mar 30, 2023
2 parents 17044de + 02c6e5a commit 6641457
Show file tree
Hide file tree
Showing 41 changed files with 2,102 additions and 539 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 @@
- 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))
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2418,7 +2418,7 @@ pub async fn submit_unbond(ctx: Context, args: args::Unbond) {
let data = pos::Unbond {
validator: validator.clone(),
amount: args.amount,
source,
source: Some(bond_source.clone()),
};
let data = data.try_to_vec().expect("Encoding tx data shouldn't fail");

Expand Down
45 changes: 41 additions & 4 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ pub fn genesis(base_dir: impl AsRef<Path>, chain_id: &ChainId) -> Genesis {
genesis_config::read_genesis_config(path)
}
#[cfg(feature = "dev")]
pub fn genesis() -> Genesis {
pub fn genesis(num_validators: u64) -> Genesis {
use namada::types::address;
use rust_decimal_macros::dec;

Expand All @@ -888,6 +888,9 @@ pub fn genesis() -> Genesis {
// NOTE When the validator's key changes, tendermint must be reset with
// `namada reset` command. To generate a new validator, use the
// `tests::gen_genesis_validator` below.
let mut validators = Vec::<Validator>::new();

// Use hard-coded keys for the first validator to avoid breaking other code
let consensus_keypair = wallet::defaults::validator_keypair();
let account_keypair = wallet::defaults::validator_keypair();
let address = wallet::defaults::validator_address();
Expand All @@ -908,6 +911,37 @@ pub fn genesis() -> Genesis {
validator_vp_code_path: vp_user_path.into(),
validator_vp_sha256: Default::default(),
};
validators.push(validator);

// Add other validators with randomly generated keys if needed
for _ in 0..(num_validators - 1) {
let consensus_keypair: common::SecretKey =
testing::gen_keypair::<ed25519::SigScheme>()
.try_to_sk()
.unwrap();
let account_keypair = consensus_keypair.clone();
let address = address::gen_established_address("validator account");
let (protocol_keypair, dkg_keypair) =
wallet::defaults::validator_keys();
let validator = Validator {
pos_data: GenesisValidator {
address,
tokens: token::Amount::whole(200_000),
consensus_key: consensus_keypair.ref_to(),
commission_rate: dec!(0.05),
max_commission_rate_change: dec!(0.01),
},
account_key: account_keypair.ref_to(),
protocol_key: protocol_keypair.ref_to(),
dkg_public_key: dkg_keypair.public(),
non_staked_balance: token::Amount::whole(100_000),
// TODO replace with https://github.com/anoma/namada/issues/25)
validator_vp_code_path: vp_user_path.into(),
validator_vp_sha256: Default::default(),
};
validators.push(validator);
}

let parameters = Parameters {
epoch_duration: EpochDuration {
min_num_of_blocks: 10,
Expand Down Expand Up @@ -960,7 +994,7 @@ pub fn genesis() -> Genesis {
}];
let default_user_tokens = token::Amount::whole(1_000_000);
let default_key_tokens = token::Amount::whole(1_000);
let balances: HashMap<Address, token::Amount> = HashMap::from_iter([
let mut balances: HashMap<Address, token::Amount> = HashMap::from_iter([
// established accounts' balances
(wallet::defaults::albert_address(), default_user_tokens),
(wallet::defaults::bertha_address(), default_user_tokens),
Expand All @@ -980,8 +1014,11 @@ pub fn genesis() -> Genesis {
christel.public_key.as_ref().unwrap().into(),
default_key_tokens,
),
((&validator.account_key).into(), default_key_tokens),
]);
for validator in &validators {
balances.insert((&validator.account_key).into(), default_key_tokens);
}

let token_accounts = address::tokens()
.into_keys()
.map(|address| TokenAccount {
Expand All @@ -993,7 +1030,7 @@ pub fn genesis() -> Genesis {
.collect();
Genesis {
genesis_time: DateTimeUtc::now(),
validators: vec![validator],
validators,
established_accounts: vec![albert, bertha, christel, masp],
implicit_accounts,
token_accounts,
Expand Down
9 changes: 7 additions & 2 deletions apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ impl Shell {
match req {
Request::InitChain(init) => {
tracing::debug!("Request InitChain");
self.init_chain(init).map(Response::InitChain)
self.init_chain(
init,
#[cfg(feature = "dev")]
1,
)
.map(Response::InitChain)
}
Request::Info(_) => Ok(Response::Info(self.last_state())),
Request::Query(query) => Ok(Response::Query(self.query(query))),
Expand Down Expand Up @@ -446,7 +451,7 @@ fn start_abci_broadcaster_shell(
#[cfg(not(feature = "dev"))]
let genesis = genesis::genesis(&config.shell.base_dir, &config.chain_id);
#[cfg(feature = "dev")]
let genesis = genesis::genesis();
let genesis = genesis::genesis(1);
let (shell, abci_service) = AbcippShim::new(
config,
wasm_dir,
Expand Down
Loading

0 comments on commit 6641457

Please sign in to comment.