Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine commission rate change tx #1973

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add missing checks for the commission rate change tx and code clean-up
([\#1973](https://github.com/anoma/namada/pull/1973))
35 changes: 20 additions & 15 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ pub enum SlashError {
pub enum CommissionRateChangeError {
#[error("Unexpected negative commission rate {0} for validator {1}")]
NegativeRate(Dec, Address),
#[error(
"Unexpected commission rate {0} larger than 1.0 for validator {1}"
)]
LargerThanOne(Dec, Address),
#[error("Rate change of {0} is too large for validator {1}")]
RateChangeTooLarge(Dec, Address),
#[error(
Expand Down Expand Up @@ -2355,13 +2359,21 @@ pub fn change_validator_commission_rate<S>(
where
S: StorageRead + StorageWrite,
{
// if new_rate < Uint::zero() {
// return Err(CommissionRateChangeError::NegativeRate(
// new_rate,
// validator.clone(),
// )
// .into());
// }
if new_rate.is_negative() {
tzemanovic marked this conversation as resolved.
Show resolved Hide resolved
return Err(CommissionRateChangeError::NegativeRate(
new_rate,
validator.clone(),
)
.into());
}

if new_rate > Dec::one() {
return Err(CommissionRateChangeError::LargerThanOne(
new_rate,
validator.clone(),
)
.into());
}

let max_change =
read_validator_max_commission_rate_change(storage, validator)?;
Expand All @@ -2386,14 +2398,7 @@ where
.get(storage, pipeline_epoch.prev(), &params)?
.expect("Could not find a rate in given epoch");

// TODO: change this back if we use `Dec` type with a signed integer
// let change_from_prev = new_rate - rate_before_pipeline;
// if change_from_prev.abs() > max_change.unwrap() {
let change_from_prev = if new_rate > rate_before_pipeline {
new_rate - rate_before_pipeline
} else {
rate_before_pipeline - new_rate
};
let change_from_prev = new_rate.abs_diff(&rate_before_pipeline);
if change_from_prev > max_change.unwrap() {
return Err(CommissionRateChangeError::RateChangeTooLarge(
change_from_prev,
Expand Down
12 changes: 12 additions & 0 deletions shared/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,18 @@ pub async fn build_validator_commission_change<
commission_rate,
max_commission_change_per_epoch,
}) => {
if rate.is_negative() || rate > Dec::one() {
edisplay_line!(
IO,
"New rate is outside of the allowed range of values \
between 0.0 and 1.0."
);
if !tx_args.force {
return Err(Error::from(
TxError::InvalidCommissionRate(rate),
));
}
}
if rate.abs_diff(&commission_rate)
> max_commission_change_per_epoch
{
Expand Down
Loading