Skip to content

Commit 96125b6

Browse files
tzemanovicbrentstone
authored andcommitted
Merge branch 'brent/refine-commission-tx' (#1973)
* origin/brent/refine-commission-tx: check if rate > 1 in lib code changelog: add #1973 add checks and simplify code
2 parents 09d606c + 66a7567 commit 96125b6

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Add missing checks for the commission rate change tx and code clean-up
2+
([\#1973](https://github.com/anoma/namada/pull/1973))

proof_of_stake/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ pub enum SlashError {
9797
pub enum CommissionRateChangeError {
9898
#[error("Unexpected negative commission rate {0} for validator {1}")]
9999
NegativeRate(Dec, Address),
100+
#[error(
101+
"Unexpected commission rate {0} larger than 1.0 for validator {1}"
102+
)]
103+
LargerThanOne(Dec, Address),
100104
#[error("Rate change of {0} is too large for validator {1}")]
101105
RateChangeTooLarge(Dec, Address),
102106
#[error(

proof_of_stake/src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,13 +2996,21 @@ pub fn change_validator_commission_rate<S>(
29962996
where
29972997
S: StorageRead + StorageWrite,
29982998
{
2999-
// if new_rate < Uint::zero() {
3000-
// return Err(CommissionRateChangeError::NegativeRate(
3001-
// new_rate,
3002-
// validator.clone(),
3003-
// )
3004-
// .into());
3005-
// }
2999+
if new_rate.is_negative() {
3000+
return Err(CommissionRateChangeError::NegativeRate(
3001+
new_rate,
3002+
validator.clone(),
3003+
)
3004+
.into());
3005+
}
3006+
3007+
if new_rate > Dec::one() {
3008+
return Err(CommissionRateChangeError::LargerThanOne(
3009+
new_rate,
3010+
validator.clone(),
3011+
)
3012+
.into());
3013+
}
30063014

30073015
let max_change =
30083016
read_validator_max_commission_rate_change(storage, validator)?;
@@ -3027,14 +3035,7 @@ where
30273035
.get(storage, pipeline_epoch.prev(), &params)?
30283036
.expect("Could not find a rate in given epoch");
30293037

3030-
// TODO: change this back if we use `Dec` type with a signed integer
3031-
// let change_from_prev = new_rate - rate_before_pipeline;
3032-
// if change_from_prev.abs() > max_change.unwrap() {
3033-
let change_from_prev = if new_rate > rate_before_pipeline {
3034-
new_rate - rate_before_pipeline
3035-
} else {
3036-
rate_before_pipeline - new_rate
3037-
};
3038+
let change_from_prev = new_rate.abs_diff(&rate_before_pipeline);
30383039
if change_from_prev > max_change.unwrap() {
30393040
return Err(CommissionRateChangeError::RateChangeTooLarge(
30403041
change_from_prev,

sdk/src/tx.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,18 @@ pub async fn build_validator_commission_change<'a>(
544544
commission_rate,
545545
max_commission_change_per_epoch,
546546
}) => {
547+
if rate.is_negative() || *rate > Dec::one() {
548+
edisplay_line!(
549+
context.io(),
550+
"New rate is outside of the allowed range of values \
551+
between 0.0 and 1.0."
552+
);
553+
if !tx_args.force {
554+
return Err(Error::from(
555+
TxError::InvalidCommissionRate(*rate),
556+
));
557+
}
558+
}
547559
if rate.abs_diff(&commission_rate)
548560
> max_commission_change_per_epoch
549561
{

wasm_for_tests/wasm_source/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)