Skip to content

Commit

Permalink
refactor conversion of token amount into voting power
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Feb 18, 2024
1 parent eb531f4 commit 38d8aa0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
11 changes: 2 additions & 9 deletions crates/apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use namada::ethereum_bridge::protocol::validation::validator_set_update::validat
use namada::ledger::events::log::EventLog;
use namada::ledger::events::Event;
use namada::ledger::gas::{Gas, TxGasMeter};
use namada::ledger::pos::into_tm_voting_power;
use namada::ledger::pos::namada_proof_of_stake::types::{
ConsensusValidator, ValidatorSetUpdate,
};
Expand Down Expand Up @@ -1305,14 +1304,8 @@ where
let (consensus_key, power) = match update {
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key,
bonded_stake,
}) => {
let power: i64 = into_tm_voting_power(
pos_params.tm_votes_per_token,
bonded_stake,
);
(consensus_key, power)
}
bonded_stake: power,
}) => (consensus_key, power),
ValidatorSetUpdate::Deactivated(consensus_key) => {
// Any validators that have been dropped from the
// consensus set must have voting power set to 0 to
Expand Down
5 changes: 4 additions & 1 deletion crates/proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use namada_storage::collections::lazy_map::{self, Collectable, LazyMap};
use namada_storage::{StorageRead, StorageWrite};
pub use namada_trans_token as token;
pub use parameters::{OwnedPosParams, PosParams};
use types::into_tm_voting_power;

use crate::queries::{find_bonds, has_bonds};
use crate::rewards::{
Expand Down Expand Up @@ -1843,9 +1844,11 @@ where
let consensus_key = validator_consensus_key_handle(&address)
.get(storage, current_epoch, params)?
.unwrap();
let new_tm_voting_power =
into_tm_voting_power(params.tm_votes_per_token, new_stake);
let converted = f(ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key,
bonded_stake: new_stake,
bonded_stake: new_tm_voting_power,
}));
Ok(converted)
})
Expand Down
32 changes: 25 additions & 7 deletions crates/proof_of_stake/src/tests/test_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ fn test_validator_sets() {
tm_updates[0],
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: pk3,
bonded_stake: stake3,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake3
),
})
);

Expand Down Expand Up @@ -611,7 +614,10 @@ fn test_validator_sets() {
tm_updates[0],
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: pk5,
bonded_stake: stake5,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake5
),
})
);
assert_eq!(tm_updates[1], ValidatorSetUpdate::Deactivated(pk2));
Expand Down Expand Up @@ -812,7 +818,10 @@ fn test_validator_sets() {
tm_updates[0],
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: pk4.clone(),
bonded_stake: stake4,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake4
),
})
);
assert_eq!(tm_updates[1], ValidatorSetUpdate::Deactivated(pk1));
Expand Down Expand Up @@ -927,7 +936,10 @@ fn test_validator_sets() {
tm_updates[0],
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: pk6,
bonded_stake: stake6,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake6
),
})
);
assert_eq!(tm_updates[1], ValidatorSetUpdate::Deactivated(pk4));
Expand Down Expand Up @@ -1187,7 +1199,10 @@ fn test_validator_sets_swap() {
tm_updates[0],
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: pk3.clone(),
bonded_stake: stake3,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake3
),
})
);

Expand Down Expand Up @@ -1226,14 +1241,17 @@ fn test_validator_sets_swap() {
assert_eq!(epoch, bonds_epoch_3);

let tm_updates = get_tendermint_set_updates(&s, &params, epoch);
dbg!(&tm_updates);
// dbg!(&tm_updates);
assert_eq!(tm_updates.len(), 2);
assert_eq!(
tm_updates,
vec![
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: new_ck2,
bonded_stake: stake2,
bonded_stake: into_tm_voting_power(
params.tm_votes_per_token,
stake2
),
}),
ValidatorSetUpdate::Deactivated(pk3),
]
Expand Down
4 changes: 2 additions & 2 deletions crates/proof_of_stake/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ pub enum ValidatorSetUpdate {
Deactivated(common::PublicKey),
}

/// Consensus validator's consensus key and its bonded stake.
/// Newly updated consensus validator's consensus key and bonded stake.
#[derive(Debug, Clone, PartialEq)]
pub struct ConsensusValidator {
/// A public key used for signing validator's consensus actions
pub consensus_key: common::PublicKey,
/// Total bonded stake of the validator
pub bonded_stake: token::Amount,
pub bonded_stake: i64,
}

/// ID of a bond and/or an unbond.
Expand Down
6 changes: 3 additions & 3 deletions crates/proof_of_stake/src/validator_set_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ where
return vec![ValidatorSetUpdate::Consensus(
ConsensusValidator {
consensus_key: new_consensus_key,
bonded_stake: new_stake,
bonded_stake: *new_tm_voting_power,
},
)];
}
Expand All @@ -667,7 +667,7 @@ where
vec![
ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: new_consensus_key,
bonded_stake: new_stake,
bonded_stake: *new_tm_voting_power,
}),
ValidatorSetUpdate::Deactivated(
prev_consensus_key.unwrap(),
Expand All @@ -682,7 +682,7 @@ where
} else {
vec![ValidatorSetUpdate::Consensus(ConsensusValidator {
consensus_key: new_consensus_key,
bonded_stake: new_stake,
bonded_stake: *new_tm_voting_power,
})]
}
});
Expand Down

0 comments on commit 38d8aa0

Please sign in to comment.