Skip to content

Commit

Permalink
Merge pull request #2906 from OriginTrail/fix/linear-sum-formula-2
Browse files Browse the repository at this point in the history
Fixed LinearSum score calculation to avoid underflows
  • Loading branch information
NZT48 authored Jan 31, 2024
2 parents 0370e84 + 32b4e36 commit 5f3f510
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/service/proximity-scoring-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,22 @@ class ProximityScoringService {
'1000000000000000000',
);

const proximityScore = oneEther.sub(normalizedDistance).mul(w1);
const isProximityScorePositive = oneEther.gte(normalizedDistance);

const proximityScore = isProximityScorePositive
? oneEther.sub(normalizedDistance).mul(w1)
: normalizedDistance.sub(oneEther).mul(w1);
const stakeScore = normalizedStake.mul(w2);

let finalScore = proximityScore.add(stakeScore);
let finalScore;
if (isProximityScorePositive) {
finalScore = proximityScore.add(stakeScore);
} else if (stakeScore.gte(proximityScore)) {
finalScore = stakeScore.sub(proximityScore);
} else {
finalScore = await this.blockchainModuleManager.toBigNumber(blockchain, 0);
}

if (finalScore.gt(UINT40_MAX_BN)) {
finalScore = finalScore.mod(UINT40_MAX_BN.add(1));
}
Expand Down

0 comments on commit 5f3f510

Please sign in to comment.