Skip to content

Commit 673f555

Browse files
committed
fix(validations): avoid division by 0 in reppoe
1 parent bd03469 commit 673f555

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

validations/src/validations.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,8 @@ pub fn calculate_reppoe_threshold(
21032103
block_epoch: u32,
21042104
minimum_difficulty: u32,
21052105
) -> (Hash, f64) {
2106+
// Set minimum total_active_reputation to 1 to avoid division by zero
2107+
let total_active_rep = std::cmp::max(rep_eng.total_active_reputation(), 1);
21062108
// Add 1 to reputation because otherwise a node with 0 reputation would
21072109
// never be eligible for a data request
21082110
let my_eligibility = u64::from(rep_eng.get_eligibility(pkh)) + 1;
@@ -2111,17 +2113,14 @@ pub fn calculate_reppoe_threshold(
21112113
let max = u64::max_value();
21122114
// Compute target eligibility and hard-cap it if required
21132115
let target = if after_third_hard_fork(block_epoch, get_environment()) {
2114-
// TODO: Review if next line is required (check if total active reputation could be zero)
2115-
let total_active_rep = std::cmp::max(rep_eng.total_active_reputation(), 1);
2116-
// If eligibility is more than 100%, cap it to (max/minimum_difficulty*factor)%
2116+
// Eligibility must never be greater than (max/minimum_difficulty)
21172117
std::cmp::min(
21182118
max / u64::from(minimum_difficulty),
21192119
(max / total_active_rep).saturating_mul(my_eligibility),
21202120
)
21212121
.saturating_mul(factor)
21222122
} else {
21232123
// Check for overflow: when the probability is more than 100%, cap it to 100%
2124-
let total_active_rep = rep_eng.total_active_reputation();
21252124
(max / total_active_rep)
21262125
.saturating_mul(my_eligibility)
21272126
.saturating_mul(factor)

0 commit comments

Comments
 (0)