Skip to content

Commit

Permalink
Merge pull request #220 from OriginTrail/improvement/staking-balancing
Browse files Browse the repository at this point in the history
Added Stake balancing for score calculations, added possibility to on…
  • Loading branch information
0xbraindevd authored Feb 2, 2024
2 parents de9ecf8 + 4f38101 commit 3b4cc22
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
26 changes: 26 additions & 0 deletions abi/NodeOperatorFeeChangesStorage.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "delayFreePeriodEnd",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -153,6 +166,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "timestamp",
"type": "uint256"
}
],
"name": "setDelayFreePeriodEnd",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "version",
Expand Down
2 changes: 1 addition & 1 deletion contracts/v1/Profile.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract Profile is Named, Versioned, ContractStatus, Initializable {
event AskUpdated(uint72 indexed identityId, bytes nodeId, uint96 ask);

string private constant _NAME = "Profile";
string private constant _VERSION = "1.0.2";
string private constant _VERSION = "1.0.3";

HashingProxy public hashingProxy;
Identity public identityContract;
Expand Down
9 changes: 6 additions & 3 deletions contracts/v1/scoring/log2pldsf.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ contract Log2PLDSF is IScoreFunction, Indexable, Named, HubDependent, Initializa
}

function calculateScore(uint256 distance, uint96 stake) external view returns (uint40) {
uint256 mappedDistance = distance / distanceMappingCoefficient;
uint96 mappedStake = stake / (parametersStorage.maximumStake() / stakeRangeMax);
uint64 coefficient = 1e18;
uint96 maxStake = parametersStorage.maximumStake();

uint96 balancedStake = stake <= maxStake ? stake : maxStake;
uint96 mappedStake = balancedStake / (maxStake / stakeRangeMax);

uint64 coefficient = 1 ether;
uint256 mappedDistance = distance / distanceMappingCoefficient;

return
uint40(
Expand Down
2 changes: 1 addition & 1 deletion contracts/v1/storage/ParametersStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract ParametersStorage is Named, Versioned, HubDependent {
event ParameterChanged(string parameterName, uint256 parameterValue);

string private constant _NAME = "ParametersStorage";
string private constant _VERSION = "1.1.0";
string private constant _VERSION = "1.1.1";

// 0 - minProofWindowOffsetPerc
// 1 - maxProofWindowOffsetPerc
Expand Down
8 changes: 6 additions & 2 deletions contracts/v2/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ contract StakingV2 is Named, Versioned, ContractStatus, Initializable {
function startOperatorFeeChange(uint72 identityId, uint8 newOperatorFee) external onlyAdmin(identityId) {
if (newOperatorFee > 100) revert StakingErrors.InvalidOperatorFee();

uint256 feeChangeDelayEnd = block.timestamp + parametersStorage.stakeWithdrawalDelay();
nodeOperatorFeeChangesStorage.createOperatorFeeChangeRequest(identityId, newOperatorFee, feeChangeDelayEnd);
NodeOperatorFeeChangesStorage nofcs = nodeOperatorFeeChangesStorage;

uint256 feeChangeDelayEnd = block.timestamp > nofcs.delayFreePeriodEnd()
? block.timestamp + parametersStorage.stakeWithdrawalDelay()
: block.timestamp;
nofcs.createOperatorFeeChangeRequest(identityId, newOperatorFee, feeChangeDelayEnd);

emit OperatorFeeChangeStarted(
identityId,
Expand Down
3 changes: 2 additions & 1 deletion contracts/v2/scoring/LinearSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ contract LinearSum is IProximityScoreFunctionsPair, Indexable, Named, HubDepende

uint96 minStake = ps.minimumStake();
uint96 maxStake = ps.maximumStake();
uint96 balancedStake = stake <= maxStake ? stake : maxStake;

return uint64((uint256(stakeScaleFactor) * (stake - minStake)) / (maxStake - minStake));
return uint64((uint256(stakeScaleFactor) * (balancedStake - minStake)) / (maxStake - minStake));
}

function getParameters() external view returns (uint96, uint96, uint32, uint32) {
Expand Down
13 changes: 13 additions & 0 deletions contracts/v2/storage/NodeOperatorFeeChangesStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ contract NodeOperatorFeeChangesStorage is Named, Versioned, HubDependent {
uint256 timestamp;
}

bool private _delayFreePeriodSet;
uint256 public delayFreePeriodEnd;

// identityId => operatorFeeChangeRequest
mapping(uint72 => OperatorFeeChangeRequest) public operatorFeeChangeRequests;

// solhint-disable-next-line no-empty-blocks
constructor(address hubAddress) HubDependent(hubAddress) {}

modifier onlyOnce() {
require(!_delayFreePeriodSet, "Function has already been executed.");

Check warning on line 28 in contracts/v2/storage/NodeOperatorFeeChangesStorage.sol

View workflow job for this annotation

GitHub Actions / lint

Error message for require is too long
_;
_delayFreePeriodSet = true;
}

function name() external pure virtual override returns (string memory) {
return _NAME;
}
Expand Down Expand Up @@ -48,4 +57,8 @@ contract NodeOperatorFeeChangesStorage is Named, Versioned, HubDependent {
function operatorFeeChangeRequestExists(uint72 identityId) external view returns (bool) {
return operatorFeeChangeRequests[identityId].timestamp != 0;
}

function setDelayFreePeriodEnd(uint256 timestamp) external onlyHubOwner onlyOnce {
delayFreePeriodEnd = timestamp;
}
}
4 changes: 2 additions & 2 deletions test/v1/unit/ParametersStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('@v1 @unit ParametersStorage contract', function () {
});

it('validate stake withdrawal delay for owner, expect to pass', async () => {
const valueInContract = 5;
const valueInContract = 1;
const newValue = '7';
stakeWithdrawalDelay = await ParametersStorage.stakeWithdrawalDelay();
const expectedValue = `${stakeWithdrawalDelay}/60`;
Expand All @@ -336,7 +336,7 @@ describe('@v1 @unit ParametersStorage contract', function () {
});

it('validate reward withdrawal delay for owner, expect to pass', async () => {
const valueInContract = 5;
const valueInContract = 1;
const newValue = '7';
rewardWithdrawalDelay = await ParametersStorage.rewardWithdrawalDelay();
const expectedValue = `${rewardWithdrawalDelay}/60`;
Expand Down
4 changes: 2 additions & 2 deletions test/v1/unit/Profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ describe('@v1 @unit Profile contract', function () {
expect(await Profile.name()).to.equal('Profile');
});

it('The contract is version "1.0.2"', async () => {
expect(await Profile.version()).to.equal('1.0.2');
it('The contract is version "1.0.3"', async () => {
expect(await Profile.version()).to.equal('1.0.3');
});

it('Create a profile with whitelisted node, expect to pass', async () => {
Expand Down

0 comments on commit 3b4cc22

Please sign in to comment.