Skip to content

Commit

Permalink
Updated CommitManagerV2
Browse files Browse the repository at this point in the history
  • Loading branch information
br41nl3t committed Jan 25, 2024
1 parent 7a86352 commit 0e1ce40
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 135 deletions.
274 changes: 167 additions & 107 deletions contracts/v2/CommitManagerV2.sol → contracts/v2/CommitManagerV1.sol

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions contracts/v2/ProximityScoringProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ contract ProximityScoringProxy is Named, Versioned, ContractStatus {
uint8 scoreFunctionId,
uint256 distance,
uint256 maxDistance,
uint72 maxNodesNumber,
uint72 nodesCount,
uint96 stake
) external view returns (uint64) {
) external view returns (uint40) {
IProximityScoreFunctionsPair proximityScoreFunctionsPair = IProximityScoreFunctionsPair(
scoreFunctionSet.get(scoreFunctionId).addr
);

return proximityScoreFunctionsPair.calculateScore(distance, maxDistance, maxNodesNumber, stake);
return proximityScoreFunctionsPair.calculateScore(distance, maxDistance, nodesCount, stake);
}

function callProximityFunction(
Expand Down
43 changes: 34 additions & 9 deletions contracts/v2/errors/CommitManagerErrorsV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,45 @@ pragma solidity ^0.8.16;
library CommitManagerErrorsV2 {
error ClosestNodeNotInNeighborhood(
bytes32 agreementId,
uint72 leftNeighborhoodEdge,
uint72 rightNeighborhoodEdge,
uint72 closestNode,
uint16 epoch,
uint72 closestNodeIndex,
uint72 leftEdgeNodeIndex,
uint72 rightEdgeNodeIndex,
uint256 timeNow
);
error NegihbourhoodWrongSize(
error InvalidNeighborhoodSize(
bytes32 agreementId,
uint72 leftNeighborhoodEdge,
uint72 rightNeighborhoodEdge,
uint256 numberOfNodes,
uint256 negihbourhoodExpectedSize,
uint256 negihbourhoodActualSize,
uint16 epoch,
uint72 leftEdgeNodeIndex,
uint72 rightEdgeNodeIndex,
uint72 numberOfNodes,
uint256 neighborhoodExpectedSize,
uint256 neighborhoodActualSize,
uint256 timeNow
);
error InvalidClosestNode(
bytes32 agreementId,
uint16 epoch,
uint72 closestNodeIndex,
uint256 closestNodeDistance,
uint256 leftAdjacentDistance,
uint256 rightAdjacentDistance,
uint256 timeNow
);
error InvalidLeftEdgeNode(
bytes32 agreementId,
uint16 epoch,
uint72 leftEdgeNodeIndex,
uint256 leftEdgeNodeDistance,
uint256 rightEdgeNodeAdjacentDistance,
uint256 timeNow
);
error InvalidRightEdgeNode(
bytes32 agreementId,
uint16 epoch,
uint72 rightEdgeNodeIndex,
uint256 rightEdgeNodeDistance,
uint256 leftEdgeNodeAdjacentDistance,
uint256 timeNow
);
}
3 changes: 1 addition & 2 deletions contracts/v2/errors/ServiceAgreementErrorsV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
pragma solidity ^0.8.16;

library ServiceAgreementErrorsV2 {
error WrongScoreFunctionId(
error InvalidProximityScoreFunctionsPairId(
bytes32 agreementId,
uint16 epoch,
uint8 agreementScoreFunctionId,
uint8 expectedScoreFunctionId,
uint256 timeNow
);
}
2 changes: 1 addition & 1 deletion contracts/v2/interface/IProximityScoreFunctionsPair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IProximityScoreFunctionsPair {
uint256 maxDistance,
uint72 maxNodesNumber,
uint96 stake
) external view returns (uint64);
) external view returns (uint40);

function calculateDistance(
uint8 hashFunctionId,
Expand Down
21 changes: 11 additions & 10 deletions contracts/v2/scoring/LinearSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende
uint256 maxDistance,
uint72 maxNodesNumber,
uint96 stake
) external view returns (uint64) {
return ((1e18 - normalizeDistance(distance, maxDistance, maxNodesNumber)) * w1 + normalizeStake(stake) * w2);
) external view returns (uint40) {
return
uint40((1e18 - normalizeDistance(distance, maxDistance, maxNodesNumber)) * w1 + normalizeStake(stake) * w2);
}

function calculateDistance(
Expand All @@ -62,25 +63,25 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende
uint256 nodePositionOnHashRing = uint256(hashingProxy.callHashFunction(hashFunctionId, nodeId));
uint256 keywordPositionOnHashRing = uint256(hashingProxy.callHashFunction(hashFunctionId, keyword));

uint256 directDistance = (
uint256 distanceClockwise = (
(nodePositionOnHashRing > keywordPositionOnHashRing)
? nodePositionOnHashRing - keywordPositionOnHashRing
: keywordPositionOnHashRing - nodePositionOnHashRing
);

return (directDistance < HASH_RING_SIZE - directDistance) ? directDistance : HASH_RING_SIZE - directDistance;
return (
(distanceClockwise < HASH_RING_SIZE - distanceClockwise)
? distanceClockwise
: HASH_RING_SIZE - distanceClockwise
);
}

function normalizeDistance(
uint256 distance,
uint256 maxDistance,
uint72 maxNodesNumber
) public view returns (uint64) {
function normalizeDistance(uint256 distance, uint256 maxDistance, uint72 nodesCount) public view returns (uint64) {
if (distance == 0) {
return 0;
}

uint256 idealMaxDistance = (HASH_RING_SIZE / maxNodesNumber) * (parametersStorage.r2() / 2);
uint256 idealMaxDistance = (HASH_RING_SIZE / nodesCount) * (parametersStorage.r2() / 2);
uint256 divisor = (maxDistance <= idealMaxDistance) ? maxDistance : idealMaxDistance;

uint256 maxMultiplier = type(uint256).max / distance;
Expand Down
6 changes: 3 additions & 3 deletions contracts/v2/structs/ServiceAgreementStructsV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ library ServiceAgreementStructsV2 {
bytes keyword;
uint8 hashFunctionId;
uint16 epoch;
uint72 closestNode;
uint72 leftNeighborhoodEdge;
uint72 rightNeighborhoodEdge;
uint72 closestNodeIndex;
uint72 leftEdgeNodeIndex;
uint72 rightEdgeNodeIndex;
}
}

0 comments on commit 0e1ce40

Please sign in to comment.