Skip to content

Commit f0b6a35

Browse files
committed
Fix that bug!
1 parent 747296e commit f0b6a35

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

validator_worker/src/core/fees.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use num::rational::Ratio;
2+
use num_traits::CheckedSub;
23
use primitives::{BalancesMap, BigNum, Channel, DomainError, ValidatorDesc};
34

45
pub fn get_balances_after_fees_tree(
56
balances: &BalancesMap,
67
channel: &Channel,
78
) -> Result<BalancesMap, DomainError> {
8-
let deposit = channel.deposit_amount.clone();
9+
let deposit_amount = channel.deposit_amount.clone();
910

1011
let total_distributed = balances.iter().map(|(_, balance)| balance).sum::<BigNum>();
1112

@@ -14,22 +15,22 @@ pub fn get_balances_after_fees_tree(
1415
.map(|validator| &validator.fee)
1516
.sum::<BigNum>();
1617

17-
if total_validators_fee > deposit {
18+
if total_validators_fee > deposit_amount {
1819
return Err(DomainError::RuleViolation(
1920
"total fees <= deposit: fee constraint violated".into(),
2021
));
2122
}
2223

23-
if total_distributed > deposit {
24+
if total_distributed > deposit_amount {
2425
return Err(DomainError::RuleViolation(
2526
"distributed <= deposit: OUTPACE rule #4".into(),
2627
));
2728
}
2829

29-
let to_distribute = &deposit - &total_validators_fee;
30+
let deposit_to_distribute = &deposit_amount - &total_validators_fee;
3031

31-
let ratio = Ratio::new(to_distribute.clone(), deposit.clone());
32-
let fee_ratio = Ratio::new(total_distributed.clone(), deposit.clone());
32+
let ratio = Ratio::new(deposit_to_distribute.clone(), deposit_amount.clone());
33+
let fee_ratio = Ratio::new(total_distributed.clone(), deposit_amount.clone());
3334

3435
let mut balances_after_fees = BalancesMap::default();
3536
let mut total = BigNum::from(0);
@@ -41,18 +42,16 @@ pub fn get_balances_after_fees_tree(
4142
balances_after_fees.insert(key.clone(), adjusted_balance);
4243
}
4344

44-
let rounding_error = if deposit == total_distributed {
45-
&to_distribute - &total_distributed
45+
let rounding_error = if deposit_amount == total_distributed {
46+
deposit_to_distribute
47+
.checked_sub(&total)
48+
.ok_or(DomainError::RuleViolation(
49+
"rounding_err should never be negative".to_owned(),
50+
))?
4651
} else {
4752
BigNum::from(0)
4853
};
4954

50-
if rounding_error < BigNum::from(0) {
51-
return Err(DomainError::RuleViolation(
52-
"The Rounding error should never be negative".into(),
53-
));
54-
}
55-
5655
let balances_after_fees = distribute_fee(
5756
balances_after_fees,
5857
rounding_error,

0 commit comments

Comments
 (0)