From bfc4be6d0239de8b3c1e6a2017c4e71d3c9c2e50 Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Tue, 6 Feb 2024 15:16:23 +0100 Subject: [PATCH 1/2] Fixed overflow issue in LinearSum, redeployed LinearSum on Chiado for Dev/Test envs --- contracts/v2/scoring/LinearSum.sol | 9 ++++++--- deployments/gnosis_chiado_dev_contracts.json | 8 ++++---- deployments/gnosis_chiado_test_contracts.json | 8 ++++---- deployments/parameters.json | 8 ++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/contracts/v2/scoring/LinearSum.sol b/contracts/v2/scoring/LinearSum.sol index ad5736a7..64f55f6b 100644 --- a/contracts/v2/scoring/LinearSum.sol +++ b/contracts/v2/scoring/LinearSum.sol @@ -57,10 +57,13 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende if (1e18 >= normalizedDistance) { return - ScaleDownLib.toUint40((1e18 - normalizedDistance) * w1 + normalizeStake(stake) * w2, (w1 + w2) * 1e18); + ScaleDownLib.toUint40( + uint216(1e18 - normalizedDistance) * w1 + uint216(normalizeStake(stake)) * w2, + (w1 + w2) * 1e18 + ); } else { - uint64 proximityScore = (normalizedDistance - 1e18) * w1; - uint64 stakeScore = normalizeStake(stake) * w2; + uint216 proximityScore = uint216(normalizedDistance - 1e18) * w1; + uint216 stakeScore = uint216(normalizeStake(stake)) * w2; if (stakeScore <= proximityScore) { return 0; } diff --git a/deployments/gnosis_chiado_dev_contracts.json b/deployments/gnosis_chiado_dev_contracts.json index 279d7261..099f38ec 100644 --- a/deployments/gnosis_chiado_dev_contracts.json +++ b/deployments/gnosis_chiado_dev_contracts.json @@ -89,11 +89,11 @@ "deployed": true }, "LinearSum": { - "evmAddress": "0x50c8Bcea7935242b991934Eb0251ff0CdAD4ED7f", + "evmAddress": "0x5D7cb786cd1ce96C8494e51029363564e8168011", "version": null, - "gitBranch": "release/delegations", - "gitCommitHash": "3170df6307d65977d66d8bc22134ecaf5fef8fe5", - "deploymentTimestamp": 1706896901040, + "gitBranch": "main", + "gitCommitHash": "097a19771ed52e4dfa1f8117a5dbdd375baae7bf", + "deploymentTimestamp": 1707228838804, "deployed": true }, "Log2PLDSF": { diff --git a/deployments/gnosis_chiado_test_contracts.json b/deployments/gnosis_chiado_test_contracts.json index f221be7f..f7304080 100644 --- a/deployments/gnosis_chiado_test_contracts.json +++ b/deployments/gnosis_chiado_test_contracts.json @@ -237,11 +237,11 @@ "version": "1.0.0" }, "LinearSum": { - "evmAddress": "0x669C7e51EbBdA5C57503Ff19feb463109874C2D5", + "evmAddress": "0xcaC24e93241fd0e1DC9eF9381127D7c561Ba2325", "version": null, - "gitBranch": "release/delegations", - "gitCommitHash": "3170df6307d65977d66d8bc22134ecaf5fef8fe5", - "deploymentTimestamp": 1706896932563, + "gitBranch": "main", + "gitCommitHash": "097a19771ed52e4dfa1f8117a5dbdd375baae7bf", + "deploymentTimestamp": 1707228917320, "deployed": true }, "NodeOperatorFeeChangesStorage": { diff --git a/deployments/parameters.json b/deployments/parameters.json index 0db5c951..0b087809 100644 --- a/deployments/parameters.json +++ b/deployments/parameters.json @@ -70,7 +70,7 @@ }, "LinearSum": { "w1": "1", - "w2": "1", + "w2": "100", "distanceScaleFactor": "1000000000000000000", "stakeScaleFactor": "1000000000000000000" }, @@ -232,7 +232,7 @@ }, "LinearSum": { "w1": "1", - "w2": "1", + "w2": "100", "distanceScaleFactor": "1000000000000000000", "stakeScaleFactor": "1000000000000000000" }, @@ -394,7 +394,7 @@ }, "LinearSum": { "w1": "1", - "w2": "1", + "w2": "100", "distanceScaleFactor": "1000000000000000000", "stakeScaleFactor": "1000000000000000000" }, @@ -556,7 +556,7 @@ }, "LinearSum": { "w1": "1", - "w2": "1", + "w2": "100", "distanceScaleFactor": "1000000000000000000", "stakeScaleFactor": "1000000000000000000" }, From a02629ce248a4364718c55ec027db97fd554a3e6 Mon Sep 17 00:00:00 2001 From: Uladzislau Hubar Date: Tue, 6 Feb 2024 17:00:05 +0100 Subject: [PATCH 2/2] Fixed another overflow issue, redeployed LinearSum --- contracts/v2/scoring/LinearSum.sol | 4 ++-- deployments/gnosis_chiado_dev_contracts.json | 8 ++++---- deployments/gnosis_chiado_test_contracts.json | 8 ++++---- test/v2/unit/LinearSum.test.ts | 14 ++++++-------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/contracts/v2/scoring/LinearSum.sol b/contracts/v2/scoring/LinearSum.sol index 64f55f6b..e9ad4b91 100644 --- a/contracts/v2/scoring/LinearSum.sol +++ b/contracts/v2/scoring/LinearSum.sol @@ -59,7 +59,7 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende return ScaleDownLib.toUint40( uint216(1e18 - normalizedDistance) * w1 + uint216(normalizeStake(stake)) * w2, - (w1 + w2) * 1e18 + uint216(w1 + w2) * 1e18 ); } else { uint216 proximityScore = uint216(normalizedDistance - 1e18) * w1; @@ -67,7 +67,7 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende if (stakeScore <= proximityScore) { return 0; } - return ScaleDownLib.toUint40(stakeScore - proximityScore, (w1 + w2) * 1e18); + return ScaleDownLib.toUint40(stakeScore - proximityScore, uint216(w1 + w2) * 1e18); } } diff --git a/deployments/gnosis_chiado_dev_contracts.json b/deployments/gnosis_chiado_dev_contracts.json index 099f38ec..f358adc9 100644 --- a/deployments/gnosis_chiado_dev_contracts.json +++ b/deployments/gnosis_chiado_dev_contracts.json @@ -89,11 +89,11 @@ "deployed": true }, "LinearSum": { - "evmAddress": "0x5D7cb786cd1ce96C8494e51029363564e8168011", + "evmAddress": "0x5dA70b2fF6aAc33EE21350df19183173F72E01dF", "version": null, - "gitBranch": "main", - "gitCommitHash": "097a19771ed52e4dfa1f8117a5dbdd375baae7bf", - "deploymentTimestamp": 1707228838804, + "gitBranch": "chiado-contracts-redeployment", + "gitCommitHash": "bfc4be6d0239de8b3c1e6a2017c4e71d3c9c2e50", + "deploymentTimestamp": 1707235148480, "deployed": true }, "Log2PLDSF": { diff --git a/deployments/gnosis_chiado_test_contracts.json b/deployments/gnosis_chiado_test_contracts.json index f7304080..229b63e0 100644 --- a/deployments/gnosis_chiado_test_contracts.json +++ b/deployments/gnosis_chiado_test_contracts.json @@ -237,11 +237,11 @@ "version": "1.0.0" }, "LinearSum": { - "evmAddress": "0xcaC24e93241fd0e1DC9eF9381127D7c561Ba2325", + "evmAddress": "0xEad3f049298d483aAf2C1341EE8E84237f0853cB", "version": null, - "gitBranch": "main", - "gitCommitHash": "097a19771ed52e4dfa1f8117a5dbdd375baae7bf", - "deploymentTimestamp": 1707228917320, + "gitBranch": "chiado-contracts-redeployment", + "gitCommitHash": "bfc4be6d0239de8b3c1e6a2017c4e71d3c9c2e50", + "deploymentTimestamp": 1707235181232, "deployed": true }, "NodeOperatorFeeChangesStorage": { diff --git a/test/v2/unit/LinearSum.test.ts b/test/v2/unit/LinearSum.test.ts index 0f55a82c..366c6491 100644 --- a/test/v2/unit/LinearSum.test.ts +++ b/test/v2/unit/LinearSum.test.ts @@ -138,10 +138,7 @@ describe('@v2 @unit LinearSum', function () { const linearSumParams = await LinearSum.getParameters(); const [, stakeScaleFactor, , w2] = linearSumParams; - let normalizedStake = stakeScaleFactor.mul(stake.sub(minStake)).div(maxStake.sub(minStake)); - if (normalizedStake.gt(UINT64_MAX_BN)) { - normalizedStake = normalizedStake.mod(UINT64_MAX_BN.add(1)); - } + const normalizedStake = stakeScaleFactor.mul(stake.sub(minStake)).div(maxStake.sub(minStake)); const stakeScore = normalizedStake.mul(w2); return stakeScore; @@ -227,7 +224,7 @@ describe('@v2 @unit LinearSum', function () { BigNumber.from('1000000000000000000'), BigNumber.from('1000000000000000000'), 1, - 1, + 100, ]); }); @@ -295,6 +292,7 @@ describe('@v2 @unit LinearSum', function () { }); it('Manual verification of score function', async function () { + const w2 = await LinearSum.w2(); const minStake = await ParametersStorage.minimumStake(); const maxStake = await ParametersStorage.maximumStake(); const peerHash = ZERO_BYTES32; @@ -310,7 +308,7 @@ describe('@v2 @unit LinearSum', function () { const stakeScore = await calculateStakeScore(distance, stake, maxDistance, 1, 1, minStake, maxStake); // 50k / 1950k = 0.0256410256410256410256410256410256410256410256410256410256410256 - expect(stakeScore).to.be.equal(BigNumber.from('25641025641025641')); + expect(stakeScore).to.be.equal(BigNumber.from('25641025641025641').mul(w2)); const scoreBeforeScaling = await calculateScoreBeforeScaling( distance, @@ -321,9 +319,9 @@ describe('@v2 @unit LinearSum', function () { minStake, maxStake, ); - expect(scoreBeforeScaling).to.be.equal(BigNumber.from('525641025641025641')); + expect(scoreBeforeScaling).to.be.equal(BigNumber.from('3064102564102564100')); const score = await calculateScore(distance, stake, maxDistance, 1, 1, minStake, maxStake); - expect(score).to.be.equal(BigNumber.from('288974209863')); + expect(score).to.be.equal(BigNumber.from('33356597999')); }); });