Skip to content

Commit

Permalink
Merge pull request #2981 from zeroxbt/v6/bug-fix/max-distance-calcula…
Browse files Browse the repository at this point in the history
…tion

add overflow warnings to linear sum, fix max distance calculation
  • Loading branch information
NZT48 authored Feb 14, 2024
2 parents f802f43 + f998938 commit d3c1676
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/service/proximity-scoring-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,19 @@ class ProximityScoringService {

let normalizedDistance = scaledDistance.div(adjustedDivisor);
if (normalizedDistance.gt(UINT64_MAX_BN)) {
this.logger.warn(
`Invalid normalized distance: ${normalizedDistance.toString()}. Max value: ${UINT64_MAX_BN.toString()}`,
);
normalizedDistance = normalizedDistance.mod(UINT64_MAX_BN.add(1));
}

let normalizedStake = stakeScaleFactor
.mul(mappedStake.sub(mappedMinStake))
.div(mappedMaxStake.sub(mappedMinStake));
if (normalizedStake.gt(UINT64_MAX_BN)) {
this.logger.warn(
`Invalid normalized stake: ${normalizedDistance.toString()}. Max value: ${UINT64_MAX_BN.toString()}`,
);
normalizedStake = normalizedStake.mod(UINT64_MAX_BN.add(1));
}

Expand Down
11 changes: 5 additions & 6 deletions src/service/service-agreement-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ class ServiceAgreementService {
);
let maxNeighborhoodDistance;
if (neighbourhoodEdges) {
maxNeighborhoodDistance = await this.proximityScoringService.callProximityFunction(
blockchainId,
proximityScoreFunctionsPairId,
neighbourhoodEdges.leftEdge[hashFunctionName],
neighbourhoodEdges.rightEdge[hashFunctionName],
);
maxNeighborhoodDistance = neighbourhoodEdges.leftEdge.distance.gt(
neighbourhoodEdges.rightEdge.distance,
)
? neighbourhoodEdges.leftEdge.distance
: neighbourhoodEdges.rightEdge.distance;
}

return this.proximityScoringService.callScoreFunction(
Expand Down
5 changes: 2 additions & 3 deletions src/service/sharding-table-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class ShardingTableService {
const hashFunctionName = this.hashingService.getHashFunctionName(hashFunctionId);
const peersWithDistance = await Promise.all(
peers.map(async (peer) => ({
peer,
...peer,
distance: await this.proximityScoringService.callProximityFunction(
blockchainId,
proximityScoreFunctionsPairId,
Expand All @@ -158,8 +158,7 @@ class ShardingTableService {
}
return 0;
});
const result = peersWithDistance.slice(0, count).map((pd) => pd.peer);
return result;
return peersWithDistance.slice(0, count);
}

async getBidSuggestion(
Expand Down

0 comments on commit d3c1676

Please sign in to comment.