From bc50dbb73a8b9ba01ad32d3a2b9b6d13ee4dfbfe Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Mon, 30 Mar 2020 16:13:57 +0300 Subject: [PATCH 01/11] SKALE-2347 Add MathUtils --- contracts/ContractManager.sol | 2 +- contracts/utils/MathUtils.sol | 44 +++++++++++++++++++++++++++ contracts/{ => utils}/StringUtils.sol | 0 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 contracts/utils/MathUtils.sol rename contracts/{ => utils}/StringUtils.sol (100%) diff --git a/contracts/ContractManager.sol b/contracts/ContractManager.sol index 50eb4383f..0947d1857 100644 --- a/contracts/ContractManager.sol +++ b/contracts/ContractManager.sol @@ -21,7 +21,7 @@ pragma solidity 0.5.16; import "@openzeppelin/contracts-ethereum-package/contracts/ownership/Ownable.sol"; import "@openzeppelin/upgrades/contracts/Initializable.sol"; -import "./StringUtils.sol"; +import "./utils/StringUtils.sol"; /** diff --git a/contracts/utils/MathUtils.sol b/contracts/utils/MathUtils.sol new file mode 100644 index 000000000..097444f5b --- /dev/null +++ b/contracts/utils/MathUtils.sol @@ -0,0 +1,44 @@ +/* + StringUtils.sol - SKALE Manager + Copyright (C) 2018-Present SKALE Labs + @author DmytroStebaiev + + SKALE Manager is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SKALE Manager is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with SKALE Manager. If not, see . +*/ + +pragma solidity 0.5.16; + + +library MathUtils { + event UnderflowError( + uint a, + uint b + ); + + uint constant EPS = 1e6; + + function boundedSub(uint256 a, uint256 b) internal returns (uint256) { + if (a >= b) { + return a - b; + } else { + emit UnderflowError(a, b); + return 0; + } + } + + function approximatelyGreater(uint256 a, uint256 b) internal pure returns (bool) { + assert(uint(-1) - EPS > b); + return a > b + EPS; + } +} \ No newline at end of file diff --git a/contracts/StringUtils.sol b/contracts/utils/StringUtils.sol similarity index 100% rename from contracts/StringUtils.sol rename to contracts/utils/StringUtils.sol From 18f6b9b4354cdba751172e1e7176b4a35910b8fc Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Tue, 31 Mar 2020 14:21:33 +0300 Subject: [PATCH 02/11] SKALE-2347 Add bounded subtract --- contracts/SkaleManager.sol | 1 - contracts/delegation/DelegationController.sol | 18 ++++++++++-------- contracts/delegation/TokenLaunchLocker.sol | 11 +++++++---- contracts/utils/MathUtils.sol | 8 ++++++++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/contracts/SkaleManager.sol b/contracts/SkaleManager.sol index 86feb3ebb..8ac1ade32 100644 --- a/contracts/SkaleManager.sol +++ b/contracts/SkaleManager.sol @@ -182,7 +182,6 @@ contract SkaleManager is IERC777Recipient, Permissions { function getBounty(uint nodeIndex) external { address nodesDataAddress = contractManager.getContract("NodesData"); - ValidatorService validatorService = ValidatorService(contractManager.getContract("ValidatorService")); require(INodesData(nodesDataAddress).isNodeExist(msg.sender, nodeIndex), "Node does not exist for Message sender"); require(INodesData(nodesDataAddress).isTimeForReward(nodeIndex), "Not time for bounty"); diff --git a/contracts/delegation/DelegationController.sol b/contracts/delegation/DelegationController.sol index 5464fbc33..5e7017e75 100644 --- a/contracts/delegation/DelegationController.sol +++ b/contracts/delegation/DelegationController.sol @@ -25,6 +25,7 @@ import "@openzeppelin/contracts/math/SafeMath.sol"; import "../Permissions.sol"; import "../SkaleToken.sol"; +import "../utils/MathUtils.sol"; import "./DelegationPeriodManager.sol"; import "./Punisher.sol"; @@ -34,6 +35,7 @@ import "./ValidatorService.sol"; contract DelegationController is Permissions, ILocker { + using MathUtils for uint; enum State { PROPOSED, @@ -655,7 +657,7 @@ contract DelegationController is Permissions, ILocker { if (sequence.firstUnprocessedMonth <= month) { for (uint i = sequence.firstUnprocessedMonth; i <= month; ++i) { - sequence.value[i] = sequence.value[i - 1].add(sequence.addDiff[i]).sub(sequence.subtractDiff[i]); + sequence.value[i] = sequence.value[i - 1].add(sequence.addDiff[i]).boundedSub(sequence.subtractDiff[i]); delete sequence.addDiff[i]; delete sequence.subtractDiff[i]; } @@ -672,7 +674,7 @@ contract DelegationController is Permissions, ILocker { if (sequence.firstUnprocessedMonth <= month) { uint value = sequence.value[sequence.firstUnprocessedMonth - 1]; for (uint i = sequence.firstUnprocessedMonth; i <= month; ++i) { - value = value.add(sequence.addDiff[i]).sub(sequence.subtractDiff[i]); + value = value.add(sequence.addDiff[i]).boundedSubWithoutEvent(sequence.subtractDiff[i]); } return value; } else { @@ -715,7 +717,7 @@ contract DelegationController is Permissions, ILocker { if (month >= sequence.firstUnprocessedMonth) { sequence.subtractDiff[month] = sequence.subtractDiff[month].add(diff); } else { - sequence.value = sequence.value.sub(diff); + sequence.value = sequence.value.boundedSub(diff); } } @@ -727,7 +729,7 @@ contract DelegationController is Permissions, ILocker { if (sequence.firstUnprocessedMonth <= month) { for (uint i = sequence.firstUnprocessedMonth; i <= month; ++i) { - sequence.value = sequence.value.add(sequence.addDiff[i]).sub(sequence.subtractDiff[i]); + sequence.value = sequence.value.add(sequence.addDiff[i]).boundedSub(sequence.subtractDiff[i]); delete sequence.addDiff[i]; delete sequence.subtractDiff[i]; } @@ -752,7 +754,7 @@ contract DelegationController is Permissions, ILocker { _amount = value; } - Fraction memory reducingCoefficient = createFraction(value.sub(_amount), value); + Fraction memory reducingCoefficient = createFraction(value.boundedSub(_amount), value); reduce(sequence, reducingCoefficient, month); return reducingCoefficient; } @@ -802,14 +804,14 @@ contract DelegationController is Permissions, ILocker { uint newValue = sequence.value.mul(reducingCoefficient.numerator).div(reducingCoefficient.denominator); if (hasSumSequence) { - subtract(sumSequence, sequence.value.sub(newValue), month); + subtract(sumSequence, sequence.value.boundedSub(newValue), month); } sequence.value = newValue; for (uint i = month.add(1); i <= sequence.lastChangedMonth; ++i) { uint newDiff = sequence.subtractDiff[i].mul(reducingCoefficient.numerator).div(reducingCoefficient.denominator); if (hasSumSequence) { - sumSequence.subtractDiff[i] = sumSequence.subtractDiff[i].sub(sequence.subtractDiff[i].sub(newDiff)); + sumSequence.subtractDiff[i] = sumSequence.subtractDiff[i].boundedSub(sequence.subtractDiff[i].boundedSub(newDiff)); } sequence.subtractDiff[i] = newDiff; } @@ -918,7 +920,7 @@ contract DelegationController is Permissions, ILocker { _slashes[index].reducingCoefficient, month); slashingSignals[index.sub(begin)].holder = holder; - slashingSignals[index.sub(begin)].penalty = oldValue.sub(getAndUpdateDelegatedByHolderToValidator(holder, validatorId, month)); + slashingSignals[index.sub(begin)].penalty = oldValue.boundedSub(getAndUpdateDelegatedByHolderToValidator(holder, validatorId, month)); } } _firstUnprocessedSlashByHolder[holder] = end; diff --git a/contracts/delegation/TokenLaunchLocker.sol b/contracts/delegation/TokenLaunchLocker.sol index 1cd509c5b..f5da72614 100644 --- a/contracts/delegation/TokenLaunchLocker.sol +++ b/contracts/delegation/TokenLaunchLocker.sol @@ -24,12 +24,15 @@ import "@openzeppelin/contracts/math/SafeMath.sol"; import "../Permissions.sol"; import "../interfaces/delegation/ILocker.sol"; import "../ConstantsHolder.sol"; +import "../utils/MathUtils.sol"; import "./DelegationController.sol"; import "./TimeHelpers.sol"; contract TokenLaunchLocker is Permissions, ILocker { + using MathUtils for uint; + event Unlocked( address holder, uint amount @@ -82,7 +85,7 @@ contract TokenLaunchLocker is Permissions, ILocker { uint currentMonth = timeHelpers.getCurrentMonth(); uint fromLocked = amount; - uint locked = _locked[holder].sub(getAndUpdateDelegatedAmount(holder, currentMonth)); + uint locked = _locked[holder].boundedSub(getAndUpdateDelegatedAmount(holder, currentMonth)); if (fromLocked > locked) { fromLocked = locked; } @@ -118,7 +121,7 @@ contract TokenLaunchLocker is Permissions, ILocker { } else { uint lockedByDelegationController = getAndUpdateDelegatedAmount(wallet, currentMonth).add(delegationController.getLockedInPendingDelegations(wallet)); if (_locked[wallet] > lockedByDelegationController) { - return _locked[wallet].sub(lockedByDelegationController); + return _locked[wallet].boundedSub(lockedByDelegationController); } else { return 0; } @@ -209,7 +212,7 @@ contract TokenLaunchLocker is Permissions, ILocker { if (month >= sequence.firstUnprocessedMonth) { sequence.subtractDiff[month] = sequence.subtractDiff[month].add(diff); } else { - sequence.value = sequence.value.sub(diff); + sequence.value = sequence.value.boundedSub(diff); } } @@ -221,7 +224,7 @@ contract TokenLaunchLocker is Permissions, ILocker { if (sequence.firstUnprocessedMonth <= month) { for (uint i = sequence.firstUnprocessedMonth; i <= month; ++i) { - sequence.value = sequence.value.add(sequence.addDiff[i]).sub(sequence.subtractDiff[i]); + sequence.value = sequence.value.add(sequence.addDiff[i]).boundedSub(sequence.subtractDiff[i]); delete sequence.addDiff[i]; delete sequence.subtractDiff[i]; } diff --git a/contracts/utils/MathUtils.sol b/contracts/utils/MathUtils.sol index 097444f5b..490782d58 100644 --- a/contracts/utils/MathUtils.sol +++ b/contracts/utils/MathUtils.sol @@ -37,6 +37,14 @@ library MathUtils { } } + function boundedSubWithoutEvent(uint256 a, uint256 b) internal pure returns (uint256) { + if (a >= b) { + return a - b; + } else { + return 0; + } + } + function approximatelyGreater(uint256 a, uint256 b) internal pure returns (bool) { assert(uint(-1) - EPS > b); return a > b + EPS; From 35529cb4bea4140338136725bc602883406a5d63 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Wed, 1 Apr 2020 16:10:23 +0300 Subject: [PATCH 03/11] SKALE-2347 Add approximate comparison with zero --- contracts/delegation/DelegationController.sol | 8 +-- contracts/delegation/Distributor.sol | 5 +- contracts/utils/MathUtils.sol | 10 +++- test/delegation/Delegation.ts | 58 +++++++++++-------- test/delegation/TokenLaunch.ts | 8 +-- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/contracts/delegation/DelegationController.sol b/contracts/delegation/DelegationController.sol index 5e7017e75..681a3e964 100644 --- a/contracts/delegation/DelegationController.sol +++ b/contracts/delegation/DelegationController.sol @@ -745,7 +745,7 @@ contract DelegationController is Permissions, ILocker { return createFraction(0); } uint value = getAndUpdateValue(sequence, month); - if (value == 0) { + if (value.approximatelyEqual(0)) { return createFraction(0); } @@ -798,7 +798,7 @@ contract DelegationController is Permissions, ILocker { return; } uint value = getAndUpdateValue(sequence, month); - if (value == 0) { + if (value.approximatelyEqual(0)) { return; } @@ -828,7 +828,7 @@ contract DelegationController is Permissions, ILocker { return; } uint value = getAndUpdateValue(sequence, month); - if (value == 0) { + if (value.approximatelyEqual(0)) { return; } @@ -909,7 +909,7 @@ contract DelegationController is Permissions, ILocker { uint validatorId = _slashes[index].validatorId; uint month = _slashes[index].month; uint oldValue = getAndUpdateDelegatedByHolderToValidator(holder, validatorId, month); - if (oldValue > 0) { + if (oldValue.muchGreater(0)) { reduce( _delegatedByHolderToValidator[holder][validatorId], _delegatedByHolder[holder], diff --git a/contracts/delegation/Distributor.sol b/contracts/delegation/Distributor.sol index e78f2a9d4..4ebc4fb51 100644 --- a/contracts/delegation/Distributor.sol +++ b/contracts/delegation/Distributor.sol @@ -27,6 +27,7 @@ import "@openzeppelin/contracts/math/SafeMath.sol"; import "../Permissions.sol"; import "../SkaleToken.sol"; import "../ConstantsHolder.sol"; +import "../utils/MathUtils.sol"; import "./ValidatorService.sol"; import "./DelegationController.sol"; @@ -35,6 +36,8 @@ import "./TimeHelpers.sol"; contract Distributor is Permissions, IERC777Recipient { + using MathUtils for uint; + event WithdrawBounty( address holder, uint validatorId, @@ -164,7 +167,7 @@ contract Distributor is Permissions, IERC777Recipient { } for (uint i = startMonth; i < endMonth; ++i) { uint effectiveDelegatedToValidator = delegationController.getAndUpdateEffectiveDelegatedToValidator(validatorId, i); - if (effectiveDelegatedToValidator > 0) { + if (effectiveDelegatedToValidator.muchGreater(0)) { earned = earned.add( _bountyPaid[validatorId][i].mul( delegationController.getAndUpdateEffectiveDelegatedByHolderToValidator(wallet, validatorId, i)).div( diff --git a/contracts/utils/MathUtils.sol b/contracts/utils/MathUtils.sol index 490782d58..2ea22e809 100644 --- a/contracts/utils/MathUtils.sol +++ b/contracts/utils/MathUtils.sol @@ -45,8 +45,16 @@ library MathUtils { } } - function approximatelyGreater(uint256 a, uint256 b) internal pure returns (bool) { + function muchGreater(uint256 a, uint256 b) internal pure returns (bool) { assert(uint(-1) - EPS > b); return a > b + EPS; } + + function approximatelyEqual(uint256 a, uint256 b) internal pure returns (bool) { + if (a > b) { + return a - b < EPS; + } else { + return b - a < EPS; + } + } } \ No newline at end of file diff --git a/test/delegation/Delegation.ts b/test/delegation/Delegation.ts index f487faab9..9165bff6e 100644 --- a/test/delegation/Delegation.ts +++ b/test/delegation/Delegation.ts @@ -231,10 +231,14 @@ contract("Delegation", ([owner, }); describe("when 3 holders delegated", async () => { + const delegatedAmount1 = 2e6; + const delegatedAmount2 = 3e6; + const delegatedAmount3 = 5e6; beforeEach(async () => { - delegationController.delegate(validatorId, 2, 12, "D2 is even", {from: holder1}); - delegationController.delegate(validatorId, 3, 6, "D2 is even more even", {from: holder2}); - delegationController.delegate(validatorId, 5, 3, "D2 is the evenest", {from: holder3}); + delegationController.delegate(validatorId, delegatedAmount1, 12, "D2 is even", {from: holder1}); + delegationController.delegate(validatorId, delegatedAmount2, 6, + "D2 is even more even", {from: holder2}); + delegationController.delegate(validatorId, delegatedAmount3, 3, "D2 is the evenest", {from: holder3}); await delegationController.acceptPendingDelegation(0, {from: validator}); await delegationController.acceptPendingDelegation(1, {from: validator}); @@ -312,35 +316,41 @@ contract("Delegation", ([owner, await punisher.slash(validatorId, 5); // Stakes: - // holder1: $2 - // holder2: $3 - // holder3: $5 + // holder1: $2e6 + // holder2: $3e6 + // holder3: $5e6 - (await tokenState.getAndUpdateLockedAmount.call(holder1)).toNumber().should.be.equal(2); + (await tokenState.getAndUpdateLockedAmount.call(holder1)).toNumber() + .should.be.equal(delegatedAmount1); (await delegationController.getAndUpdateDelegatedAmount.call( - holder1)).toNumber().should.be.equal(1); + holder1)).toNumber().should.be.equal(delegatedAmount1 - 1); - (await tokenState.getAndUpdateLockedAmount.call(holder2)).toNumber().should.be.equal(3); + (await tokenState.getAndUpdateLockedAmount.call(holder2)).toNumber() + .should.be.equal(delegatedAmount2); (await delegationController.getAndUpdateDelegatedAmount.call( - holder2)).toNumber().should.be.equal(1); + holder2)).toNumber().should.be.equal(delegatedAmount2 - 2); - (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber().should.be.equal(5); + (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber() + .should.be.equal(delegatedAmount3); (await delegationController.getAndUpdateDelegatedAmount.call( - holder3)).toNumber().should.be.equal(2); + holder3)).toNumber().should.be.equal(delegatedAmount3 - 3); }); it("should not lock more tokens than were delegated", async () => { - await punisher.slash(validatorId, 100); + await punisher.slash(validatorId, 10 * (delegatedAmount1 + delegatedAmount2 + delegatedAmount3)); - (await tokenState.getAndUpdateLockedAmount.call(holder1)).toNumber().should.be.equal(2); + (await tokenState.getAndUpdateLockedAmount.call(holder1)).toNumber() + .should.be.equal(delegatedAmount1); (await delegationController.getAndUpdateDelegatedAmount.call( holder1)).toNumber().should.be.equal(0); - (await tokenState.getAndUpdateLockedAmount.call(holder2)).toNumber().should.be.equal(3); + (await tokenState.getAndUpdateLockedAmount.call(holder2)).toNumber() + .should.be.equal(delegatedAmount2); (await delegationController.getAndUpdateDelegatedAmount.call( holder2)).toNumber().should.be.equal(0); - (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber().should.be.equal(5); + (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber() + .should.be.equal(delegatedAmount3); (await delegationController.getAndUpdateDelegatedAmount.call( holder3)).toNumber().should.be.equal(0); }); @@ -348,23 +358,25 @@ contract("Delegation", ([owner, it("should allow to return slashed tokens back", async () => { await punisher.slash(validatorId, 10); - (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber().should.be.equal(5); + (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber() + .should.be.equal(delegatedAmount3); (await delegationController.getAndUpdateDelegatedAmount.call( - holder3)).toNumber().should.be.equal(0); + holder3)).toNumber().should.be.equal(delegatedAmount3 - 5); await delegationController.processAllSlashes(holder3); await punisher.forgive(holder3, 3); - (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber().should.be.equal(2); + (await tokenState.getAndUpdateLockedAmount.call(holder3)).toNumber() + .should.be.equal(delegatedAmount3 - 3); (await delegationController.getAndUpdateDelegatedAmount.call( - holder3)).toNumber().should.be.equal(0); + holder3)).toNumber().should.be.equal(delegatedAmount3 - 5); }); it("should not pay bounty for slashed tokens", async () => { // slash everything - await punisher.slash(validatorId, 10); + await punisher.slash(validatorId, delegatedAmount1 + delegatedAmount2 + delegatedAmount3); - delegationController.delegate(validatorId, 1, 3, "D2 is the evenest", {from: holder1}); + delegationController.delegate(validatorId, 1e7, 3, "D2 is the evenest", {from: holder1}); const delegationId = 3; await delegationController.acceptPendingDelegation(delegationId, {from: validator}); @@ -400,7 +412,7 @@ contract("Delegation", ([owner, console.log("Reduce holders amount to fit Travis timelimit"); holdersAmount = 10; } - const delegatedAmount = 1; + const delegatedAmount = 1e7; const holders = []; for (let i = 0; i < holdersAmount; ++i) { holders.push(web3.eth.accounts.create()); diff --git a/test/delegation/TokenLaunch.ts b/test/delegation/TokenLaunch.ts index cf4e73437..cf076f7aa 100644 --- a/test/delegation/TokenLaunch.ts +++ b/test/delegation/TokenLaunch.ts @@ -37,7 +37,7 @@ contract("TokenLaunchManager", ([owner, holder, delegation, validator, seller, h // each test will start from Nov 10 await skipTimeToDate(web3, 10, 11); - await skaleToken.mint(owner, TokenLaunchManager.address, 1000, "0x", "0x"); + await skaleToken.mint(owner, TokenLaunchManager.address, 1e9, "0x", "0x"); await validatorService.registerValidator("Validator", "D2 is even", 150, 0, {from: validator}); await validatorService.enableValidator(1, {from: owner}); }); @@ -66,7 +66,7 @@ contract("TokenLaunchManager", ([owner, holder, delegation, validator, seller, h }); it("should not allow to approve transfers with more then total money amount in sum", async () => { - await TokenLaunchManager.approve([holder, hacker], [500, 501], {from: seller}) + await TokenLaunchManager.approve([holder, hacker], [5e8, 5e8 + 1], {from: seller}) .should.be.eventually.rejectedWith("Balance is too low"); }); @@ -84,7 +84,7 @@ contract("TokenLaunchManager", ([owner, holder, delegation, validator, seller, h describe("when holder bought tokens", async () => { const validatorId = 1; - const totalAmount = 100; + const totalAmount = 1e7; const month = 60 * 60 * 24 * 31; beforeEach(async () => { @@ -115,7 +115,7 @@ contract("TokenLaunchManager", ([owner, holder, delegation, validator, seller, h }); it("should be able to delegate part of tokens", async () => { - const amount = 50; + const amount = Math.ceil(totalAmount / 2); const delegationPeriod = 3; await delegationController.delegate( validatorId, amount, delegationPeriod, "D2 is even", {from: holder}); From 994b7e5a65c3a8cf1ebef16e686006a3daec0976 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Wed, 1 Apr 2020 16:46:51 +0300 Subject: [PATCH 04/11] SKALE-2347 Fix DKG tests --- test/SkaleDKG.ts | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/test/SkaleDKG.ts b/test/SkaleDKG.ts index db4f0b758..01221f5e5 100644 --- a/test/SkaleDKG.ts +++ b/test/SkaleDKG.ts @@ -64,6 +64,8 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { let slashingTable: SlashingTableInstance; let delegationController: DelegationControllerInstance; + const failedDkgPenalty = 5; + beforeEach(async () => { contractManager = await deployContractManager(); @@ -76,7 +78,7 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { slashingTable = await deploySlashingTable(contractManager); delegationController = await deployDelegationController(contractManager); - await slashingTable.setPenalty("FailedDKG", 5); + await slashingTable.setPenalty("FailedDKG", failedDkgPenalty); }); describe("when 2 nodes are created", async () => { @@ -164,6 +166,7 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { const indexes = [0, 1]; let schainName = ""; + const delegatedAmount = 1e7; beforeEach(async () => { await validatorService.registerValidator("Validator1", "D2 is even", 0, 0, {from: validator1}); @@ -172,12 +175,13 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { await validatorService.registerValidator("Validator2", "D2 is even more even", 0, 0, {from: validator2}); const validator2Id = 2; await validatorService.linkNodeAddress(validator2, {from: validator2}); - await skaleToken.mint(owner, validator1, 1000, "0x", "0x"); - await skaleToken.mint(owner, validator2, 1000, "0x", "0x"); + await skaleToken.mint(owner, validator1, delegatedAmount, "0x", "0x"); + await skaleToken.mint(owner, validator2, delegatedAmount, "0x", "0x"); await validatorService.enableValidator(validator1Id, {from: owner}); await validatorService.enableValidator(validator2Id, {from: owner}); - await delegationController.delegate(validator1Id, 100, 3, "D2 is even", {from: validator1}); - await delegationController.delegate(validator2Id, 100, 3, "D2 is even more even", {from: validator2}); + await delegationController.delegate(validator1Id, delegatedAmount, 3, "D2 is even", {from: validator1}); + await delegationController.delegate(validator2Id, delegatedAmount, 3, "D2 is even more even", + {from: validator2}); await delegationController.acceptPendingDelegation(0, {from: validator1}); await delegationController.acceptPendingDelegation(1, {from: validator2}); @@ -434,9 +438,12 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { assert.equal(result.logs[0].event, "BadGuy"); assert.equal(result.logs[0].args.nodeIndex.toString(), "1"); - (await skaleToken.getAndUpdateLockedAmount.call(validator2)).toNumber().should.be.equal(100); - (await skaleToken.getAndUpdateDelegatedAmount.call(validator2)).toNumber().should.be.equal(95); - (await skaleToken.getAndUpdateSlashedAmount.call(validator2)).toNumber().should.be.equal(5); + (await skaleToken.getAndUpdateLockedAmount.call(validator2)).toNumber() + .should.be.equal(delegatedAmount); + (await skaleToken.getAndUpdateDelegatedAmount.call(validator2)).toNumber() + .should.be.equal(delegatedAmount - failedDkgPenalty); + (await skaleToken.getAndUpdateSlashedAmount.call(validator2)).toNumber() + .should.be.equal(failedDkgPenalty); }); }); }); @@ -511,9 +518,12 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { assert.equal(result.logs[0].event, "BadGuy"); assert.equal(result.logs[0].args.nodeIndex.toString(), "0"); - (await skaleToken.getAndUpdateLockedAmount.call(validator1)).toNumber().should.be.equal(100); - (await skaleToken.getAndUpdateDelegatedAmount.call(validator1)).toNumber().should.be.equal(95); - (await skaleToken.getAndUpdateSlashedAmount.call(validator1)).toNumber().should.be.equal(5); + (await skaleToken.getAndUpdateLockedAmount.call(validator1)).toNumber() + .should.be.equal(delegatedAmount); + (await skaleToken.getAndUpdateDelegatedAmount.call(validator1)).toNumber() + .should.be.equal(delegatedAmount - failedDkgPenalty); + (await skaleToken.getAndUpdateSlashedAmount.call(validator1)).toNumber() + .should.be.equal(failedDkgPenalty); }); it("accused node should send incorrect response", async () => { @@ -527,9 +537,12 @@ contract("SkaleDKG", ([owner, validator1, validator2]) => { assert.equal(result.logs[0].event, "BadGuy"); assert.equal(result.logs[0].args.nodeIndex.toString(), "0"); - (await skaleToken.getAndUpdateLockedAmount.call(validator1)).toNumber().should.be.equal(100); - (await skaleToken.getAndUpdateDelegatedAmount.call(validator1)).toNumber().should.be.equal(95); - (await skaleToken.getAndUpdateSlashedAmount.call(validator1)).toNumber().should.be.equal(5); + (await skaleToken.getAndUpdateLockedAmount.call(validator1)).toNumber() + .should.be.equal(delegatedAmount); + (await skaleToken.getAndUpdateDelegatedAmount.call(validator1)).toNumber() + .should.be.equal(delegatedAmount - failedDkgPenalty); + (await skaleToken.getAndUpdateSlashedAmount.call(validator1)).toNumber() + .should.be.equal(failedDkgPenalty); }); }); }); From ca51d1f67411240f1778f7d52155e18207b68b3e Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Wed, 1 Apr 2020 18:47:01 +0300 Subject: [PATCH 05/11] SKALE-2347 Fix SkaleManager tests --- test/SkaleManager.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/SkaleManager.ts b/test/SkaleManager.ts index 2c87a0c76..07b59130c 100644 --- a/test/SkaleManager.ts +++ b/test/SkaleManager.ts @@ -79,14 +79,15 @@ contract("SkaleManager", ([owner, validator, developer, hacker, nodeAddress]) => describe("when validator has delegated SKALE tokens", async () => { const validatorId = 1; const month = 60 * 60 * 24 * 31; + const delegatedAmount = 1e7; beforeEach(async () => { await validatorService.registerValidator("D2", "D2 is even", 150, 0, {from: validator}); await validatorService.linkNodeAddress(nodeAddress, {from: validator}); - await skaleToken.transfer(validator, "0x410D586A20A4C00000", {from: owner}); + await skaleToken.transfer(validator, 10 * delegatedAmount, {from: owner}); await validatorService.enableValidator(validatorId, {from: owner}); - await delegationController.delegate(validatorId, 100, 12, "Hello from D2", {from: validator}); + await delegationController.delegate(validatorId, delegatedAmount, 12, "Hello from D2", {from: validator}); const delegationId = 0; await delegationController.acceptPendingDelegation(delegationId, {from: validator}); @@ -328,7 +329,7 @@ contract("SkaleManager", ([owner, validator, developer, hacker, nodeAddress]) => }); it("should fail to create schain if validator doesn't meet MSR", async () => { - await constantsHolder.setMSR(6); + await constantsHolder.setMSR(delegatedAmount + 1); await skaleManager.createNode( "0x10" + // create schain From 04aea22a915b67c46765f3d24866a9f41f5f8670 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Thu, 2 Apr 2020 10:52:37 +0300 Subject: [PATCH 06/11] SKALE-2347 Fix ValidatorService tests --- test/delegation/ValidatorService.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/delegation/ValidatorService.ts b/test/delegation/ValidatorService.ts index 5811af9b5..4b780de54 100644 --- a/test/delegation/ValidatorService.ts +++ b/test/delegation/ValidatorService.ts @@ -240,6 +240,7 @@ contract("ValidatorService", ([owner, holder, validator1, validator2, validator3 let amount: number; let delegationPeriod: number; let info: string; + const month = 60 * 60 * 24 * 31; beforeEach(async () => { validatorId = 1; amount = 100; @@ -286,7 +287,7 @@ contract("ValidatorService", ([owner, holder, validator1, validator2, validator3 await delegationController.delegate(validatorId, amount, delegationPeriod, info, {from: holder}); const delegationId = 0; await delegationController.acceptPendingDelegation(delegationId, {from: validator1}); - skipTime(web3, 2592000); + skipTime(web3, month); await validatorService.checkPossibilityCreatingNode(nodeAddress) .should.be.eventually.rejectedWith("Validator must meet Minimum Staking Requirement"); @@ -310,7 +311,7 @@ contract("ValidatorService", ([owner, holder, validator1, validator2, validator3 const delegationId2 = 1; await delegationController.acceptPendingDelegation(delegationId1, {from: validator1}); await delegationController.acceptPendingDelegation(delegationId2, {from: validator1}); - skipTime(web3, 2592000); + skipTime(web3, month); await constantsHolder.setMSR(amount); await validatorService.checkPossibilityCreatingNode(nodeAddress); From c798752d68b6fba6547363dbe0e7152131d6412d Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Fri, 3 Apr 2020 13:28:35 +0300 Subject: [PATCH 07/11] SKALE-2347 Add MathUtils tests --- contracts/test/MathUtilsTester.sol | 24 ++++++++ test/ConstantsHolder.spec.ts | 6 +- test/ContractManager.ts | 4 +- test/Decryption.ts | 4 +- test/ECDH.ts | 6 +- test/ManagerData.spec.ts | 4 +- test/MonitorsData.ts | 4 +- test/MonitorsFunctionality.spec.ts | 14 ++--- test/NodesData.ts | 6 +- test/NodesFunctionality.ts | 10 ++-- test/Pricing.ts | 10 ++-- test/SchainsData.ts | 6 +- test/SchainsFunctionality.ts | 20 +++---- test/SkaleDKG.ts | 20 +++---- test/SkaleManager.ts | 24 ++++---- test/SkaleToken.ts | 6 +- test/SkaleVerifier.ts | 12 ++-- test/delegation/Delegation.ts | 22 +++---- test/delegation/DelegationController.ts | 14 ++--- test/delegation/TokenLaunch.ts | 16 +++--- test/delegation/TokenState.ts | 14 ++--- test/delegation/ValidatorService.ts | 12 ++-- test/{utils => tools}/command_line.ts | 0 .../deploy/constantsHolder.ts | 0 .../deploy/contractManager.ts | 0 test/{utils => tools}/deploy/dectyption.ts | 0 .../deploy/delegation/delegationController.ts | 0 .../delegation/delegationPeriodManager.ts | 0 .../deploy/delegation/distributor.ts | 0 .../deploy/delegation/punisher.ts | 0 .../deploy/delegation/timeHelpers.ts | 0 .../deploy/delegation/tokenLaunchLocker.ts | 0 .../deploy/delegation/tokenLaunchManager.ts | 0 .../deploy/delegation/tokenState.ts | 0 .../deploy/delegation/validatorService.ts | 0 test/{utils => tools}/deploy/ecdh.ts | 0 test/{utils => tools}/deploy/factory.ts | 0 test/{utils => tools}/deploy/managerData.ts | 0 test/{utils => tools}/deploy/monitorsData.ts | 0 .../deploy/monitorsFunctionality.ts | 0 test/{utils => tools}/deploy/nodesData.ts | 0 .../deploy/nodesFunctionality.ts | 0 test/{utils => tools}/deploy/pricing.ts | 0 test/{utils => tools}/deploy/schainsData.ts | 0 .../deploy/schainsFunctionality.ts | 0 .../deploy/schainsFunctionalityInternal.ts | 0 test/{utils => tools}/deploy/skaleDKG.ts | 0 test/{utils => tools}/deploy/skaleManager.ts | 0 test/{utils => tools}/deploy/skaleToken.ts | 0 test/{utils => tools}/deploy/skaleVerifier.ts | 0 test/{utils => tools}/deploy/slashingTable.ts | 0 .../deploy/test/reentracyTester.ts | 0 test/{utils => tools}/elliptic-types.ts | 0 test/{utils => tools}/time.ts | 0 test/{utils => tools}/types.ts | 0 test/utils/MathUtils.ts | 57 +++++++++++++++++++ 56 files changed, 198 insertions(+), 117 deletions(-) create mode 100644 contracts/test/MathUtilsTester.sol rename test/{utils => tools}/command_line.ts (100%) rename test/{utils => tools}/deploy/constantsHolder.ts (100%) rename test/{utils => tools}/deploy/contractManager.ts (100%) rename test/{utils => tools}/deploy/dectyption.ts (100%) rename test/{utils => tools}/deploy/delegation/delegationController.ts (100%) rename test/{utils => tools}/deploy/delegation/delegationPeriodManager.ts (100%) rename test/{utils => tools}/deploy/delegation/distributor.ts (100%) rename test/{utils => tools}/deploy/delegation/punisher.ts (100%) rename test/{utils => tools}/deploy/delegation/timeHelpers.ts (100%) rename test/{utils => tools}/deploy/delegation/tokenLaunchLocker.ts (100%) rename test/{utils => tools}/deploy/delegation/tokenLaunchManager.ts (100%) rename test/{utils => tools}/deploy/delegation/tokenState.ts (100%) rename test/{utils => tools}/deploy/delegation/validatorService.ts (100%) rename test/{utils => tools}/deploy/ecdh.ts (100%) rename test/{utils => tools}/deploy/factory.ts (100%) rename test/{utils => tools}/deploy/managerData.ts (100%) rename test/{utils => tools}/deploy/monitorsData.ts (100%) rename test/{utils => tools}/deploy/monitorsFunctionality.ts (100%) rename test/{utils => tools}/deploy/nodesData.ts (100%) rename test/{utils => tools}/deploy/nodesFunctionality.ts (100%) rename test/{utils => tools}/deploy/pricing.ts (100%) rename test/{utils => tools}/deploy/schainsData.ts (100%) rename test/{utils => tools}/deploy/schainsFunctionality.ts (100%) rename test/{utils => tools}/deploy/schainsFunctionalityInternal.ts (100%) rename test/{utils => tools}/deploy/skaleDKG.ts (100%) rename test/{utils => tools}/deploy/skaleManager.ts (100%) rename test/{utils => tools}/deploy/skaleToken.ts (100%) rename test/{utils => tools}/deploy/skaleVerifier.ts (100%) rename test/{utils => tools}/deploy/slashingTable.ts (100%) rename test/{utils => tools}/deploy/test/reentracyTester.ts (100%) rename test/{utils => tools}/elliptic-types.ts (100%) rename test/{utils => tools}/time.ts (100%) rename test/{utils => tools}/types.ts (100%) create mode 100644 test/utils/MathUtils.ts diff --git a/contracts/test/MathUtilsTester.sol b/contracts/test/MathUtilsTester.sol new file mode 100644 index 000000000..79fc2d67b --- /dev/null +++ b/contracts/test/MathUtilsTester.sol @@ -0,0 +1,24 @@ +pragma solidity 0.5.16; + +import "../utils/MathUtils.sol"; + + +contract MathUtilsTester { + using MathUtils for uint; + + function boundedSub(uint256 a, uint256 b) external returns (uint256) { + return a.boundedSub(b); + } + + function boundedSubWithoutEvent(uint256 a, uint256 b) external pure returns (uint256) { + return a.boundedSubWithoutEvent(b); + } + + function muchGreater(uint256 a, uint256 b) external pure returns (bool) { + return a.muchGreater(b); + } + + function approximatelyEqual(uint256 a, uint256 b) external pure returns (bool) { + return a.approximatelyEqual(b); + } +} \ No newline at end of file diff --git a/test/ConstantsHolder.spec.ts b/test/ConstantsHolder.spec.ts index a83e63f61..bd27389d2 100644 --- a/test/ConstantsHolder.spec.ts +++ b/test/ConstantsHolder.spec.ts @@ -2,11 +2,11 @@ import { BigNumber } from "bignumber.js"; import * as chaiAsPromised from "chai-as-promised"; import { ConstantsHolderInstance, ContractManagerInstance } from "../types/truffle-contracts"; -import { skipTime } from "./utils/time"; +import { skipTime } from "./tools/time"; import chai = require("chai"); -import { deployConstantsHolder } from "./utils/deploy/constantsHolder"; -import { deployContractManager } from "./utils/deploy/contractManager"; +import { deployConstantsHolder } from "./tools/deploy/constantsHolder"; +import { deployContractManager } from "./tools/deploy/contractManager"; chai.should(); chai.use((chaiAsPromised as any)); diff --git a/test/ContractManager.ts b/test/ContractManager.ts index f1a94afa4..e83c36ffe 100644 --- a/test/ContractManager.ts +++ b/test/ContractManager.ts @@ -1,7 +1,7 @@ import { ConstantsHolderInstance, ContractManagerInstance } from "../types/truffle-contracts"; -import { deployConstantsHolder } from "./utils/deploy/constantsHolder"; -import { deployContractManager } from "./utils/deploy/contractManager"; +import { deployConstantsHolder } from "./tools/deploy/constantsHolder"; +import { deployContractManager } from "./tools/deploy/contractManager"; contract("ContractManager", ([deployer, user]) => { let contractManager: ContractManagerInstance; diff --git a/test/Decryption.ts b/test/Decryption.ts index 7f8a7c3b5..70ae6dd7a 100644 --- a/test/Decryption.ts +++ b/test/Decryption.ts @@ -3,8 +3,8 @@ import * as chaiAsPromised from "chai-as-promised"; import { DecryptionContract, DecryptionInstance} from "../types/truffle-contracts"; -import { gasMultiplier } from "./utils/command_line"; -import { skipTime } from "./utils/time"; +import { gasMultiplier } from "./tools/command_line"; +import { skipTime } from "./tools/time"; // const truffleAssert = require('truffle-assertions'); // const truffleEvent = require('truffle-events'); diff --git a/test/ECDH.ts b/test/ECDH.ts index b1377abe9..e7a9a3db8 100644 --- a/test/ECDH.ts +++ b/test/ECDH.ts @@ -3,10 +3,10 @@ import * as chaiAsPromised from "chai-as-promised"; import * as elliptic from "elliptic"; import { ECDHContract, ECDHInstance} from "../types/truffle-contracts"; -import "./utils/elliptic-types"; +import "./tools/elliptic-types"; -import { gasMultiplier } from "./utils/command_line"; -import { skipTime } from "./utils/time"; +import { gasMultiplier } from "./tools/command_line"; +import { skipTime } from "./tools/time"; const EC = elliptic.ec; const ec = new EC("secp256k1"); diff --git a/test/ManagerData.spec.ts b/test/ManagerData.spec.ts index 1fd2209b1..21253e2ba 100644 --- a/test/ManagerData.spec.ts +++ b/test/ManagerData.spec.ts @@ -5,8 +5,8 @@ import { ContractManagerInstance, import chai = require("chai"); import * as chaiAsPromised from "chai-as-promised"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployManagerData } from "./utils/deploy/managerData"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployManagerData } from "./tools/deploy/managerData"; chai.should(); chai.use((chaiAsPromised)); diff --git a/test/MonitorsData.ts b/test/MonitorsData.ts index 075ad9dd9..595200b3f 100644 --- a/test/MonitorsData.ts +++ b/test/MonitorsData.ts @@ -2,8 +2,8 @@ import BigNumber from "bignumber.js"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; import { ContractManagerInstance, MonitorsDataInstance } from "../types/truffle-contracts"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployMonitorsData } from "./utils/deploy/monitorsData"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployMonitorsData } from "./tools/deploy/monitorsData"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/MonitorsFunctionality.spec.ts b/test/MonitorsFunctionality.spec.ts index aadba49b8..e8d922fd9 100644 --- a/test/MonitorsFunctionality.spec.ts +++ b/test/MonitorsFunctionality.spec.ts @@ -5,16 +5,16 @@ import { ConstantsHolderInstance, MonitorsFunctionalityInstance, NodesDataInstance, NodesFunctionalityInstance } from "../types/truffle-contracts"; -import { currentTime, skipTime } from "./utils/time"; +import { currentTime, skipTime } from "./tools/time"; import chai = require("chai"); import * as chaiAsPromised from "chai-as-promised"; -import { deployConstantsHolder } from "./utils/deploy/constantsHolder"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployMonitorsData } from "./utils/deploy/monitorsData"; -import { deployMonitorsFunctionality } from "./utils/deploy/monitorsFunctionality"; -import { deployNodesData } from "./utils/deploy/nodesData"; -import { deployNodesFunctionality } from "./utils/deploy/nodesFunctionality"; +import { deployConstantsHolder } from "./tools/deploy/constantsHolder"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployMonitorsData } from "./tools/deploy/monitorsData"; +import { deployMonitorsFunctionality } from "./tools/deploy/monitorsFunctionality"; +import { deployNodesData } from "./tools/deploy/nodesData"; +import { deployNodesFunctionality } from "./tools/deploy/nodesFunctionality"; chai.should(); chai.use((chaiAsPromised)); diff --git a/test/NodesData.ts b/test/NodesData.ts index b94debe2f..041813bfe 100644 --- a/test/NodesData.ts +++ b/test/NodesData.ts @@ -1,11 +1,11 @@ import * as chaiAsPromised from "chai-as-promised"; import { ContractManagerInstance, NodesDataInstance } from "../types/truffle-contracts"; -import { currentTime, skipTime } from "./utils/time"; +import { currentTime, skipTime } from "./tools/time"; import chai = require("chai"); -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployNodesData } from "./utils/deploy/nodesData"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployNodesData } from "./tools/deploy/nodesData"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/NodesFunctionality.ts b/test/NodesFunctionality.ts index 288c67bba..71734185a 100644 --- a/test/NodesFunctionality.ts +++ b/test/NodesFunctionality.ts @@ -5,11 +5,11 @@ import { ContractManagerInstance, NodesFunctionalityInstance, ValidatorServiceInstance } from "../types/truffle-contracts"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployValidatorService } from "./utils/deploy/delegation/validatorService"; -import { deployNodesData } from "./utils/deploy/nodesData"; -import { deployNodesFunctionality } from "./utils/deploy/nodesFunctionality"; -import { skipTime } from "./utils/time"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployValidatorService } from "./tools/deploy/delegation/validatorService"; +import { deployNodesData } from "./tools/deploy/nodesData"; +import { deployNodesFunctionality } from "./tools/deploy/nodesFunctionality"; +import { skipTime } from "./tools/time"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/Pricing.ts b/test/Pricing.ts index 1a1180bba..1b68091e9 100644 --- a/test/Pricing.ts +++ b/test/Pricing.ts @@ -6,11 +6,11 @@ import { ContractManagerInstance, NodesDataInstance, PricingInstance, SchainsDataInstance } from "../types/truffle-contracts"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployNodesData } from "./utils/deploy/nodesData"; -import { deployPricing } from "./utils/deploy/pricing"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { skipTime } from "./utils/time"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployNodesData } from "./tools/deploy/nodesData"; +import { deployPricing } from "./tools/deploy/pricing"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { skipTime } from "./tools/time"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SchainsData.ts b/test/SchainsData.ts index d94559ea9..b33038c50 100644 --- a/test/SchainsData.ts +++ b/test/SchainsData.ts @@ -4,9 +4,9 @@ import { ContractManagerInstance, import BigNumber from "bignumber.js"; import chai = require("chai"); import * as chaiAsPromised from "chai-as-promised"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { skipTime } from "./utils/time"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { skipTime } from "./tools/time"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SchainsFunctionality.ts b/test/SchainsFunctionality.ts index dac96f70c..e910d81f7 100644 --- a/test/SchainsFunctionality.ts +++ b/test/SchainsFunctionality.ts @@ -10,16 +10,16 @@ import { ContractManagerInstance, ValidatorServiceInstance } from "../types/truffle-contracts"; import BigNumber from "bignumber.js"; -import { skipTime } from "./utils/time"; - -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployValidatorService } from "./utils/deploy/delegation/validatorService"; -import { deployNodesData } from "./utils/deploy/nodesData"; -import { deployNodesFunctionality } from "./utils/deploy/nodesFunctionality"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { deploySchainsFunctionality } from "./utils/deploy/schainsFunctionality"; -import { deploySchainsFunctionalityInternal } from "./utils/deploy/schainsFunctionalityInternal"; -import { deploySkaleManager } from "./utils/deploy/skaleManager"; +import { skipTime } from "./tools/time"; + +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployValidatorService } from "./tools/deploy/delegation/validatorService"; +import { deployNodesData } from "./tools/deploy/nodesData"; +import { deployNodesFunctionality } from "./tools/deploy/nodesFunctionality"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { deploySchainsFunctionality } from "./tools/deploy/schainsFunctionality"; +import { deploySchainsFunctionalityInternal } from "./tools/deploy/schainsFunctionalityInternal"; +import { deploySkaleManager } from "./tools/deploy/skaleManager"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SkaleDKG.ts b/test/SkaleDKG.ts index 01221f5e5..2b91133c0 100644 --- a/test/SkaleDKG.ts +++ b/test/SkaleDKG.ts @@ -10,18 +10,18 @@ import { ContractManagerInstance, SlashingTableInstance, ValidatorServiceInstance } from "../types/truffle-contracts"; -import { skipTime } from "./utils/time"; +import { skipTime } from "./tools/time"; import BigNumber from "bignumber.js"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployDelegationController } from "./utils/deploy/delegation/delegationController"; -import { deployValidatorService } from "./utils/deploy/delegation/validatorService"; -import { deployNodesFunctionality } from "./utils/deploy/nodesFunctionality"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { deploySchainsFunctionality } from "./utils/deploy/schainsFunctionality"; -import { deploySkaleDKG } from "./utils/deploy/skaleDKG"; -import { deploySkaleToken } from "./utils/deploy/skaleToken"; -import { deploySlashingTable } from "./utils/deploy/slashingTable"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployDelegationController } from "./tools/deploy/delegation/delegationController"; +import { deployValidatorService } from "./tools/deploy/delegation/validatorService"; +import { deployNodesFunctionality } from "./tools/deploy/nodesFunctionality"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { deploySchainsFunctionality } from "./tools/deploy/schainsFunctionality"; +import { deploySkaleDKG } from "./tools/deploy/skaleDKG"; +import { deploySkaleToken } from "./tools/deploy/skaleToken"; +import { deploySlashingTable } from "./tools/deploy/slashingTable"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SkaleManager.ts b/test/SkaleManager.ts index 07b59130c..2afcb5549 100644 --- a/test/SkaleManager.ts +++ b/test/SkaleManager.ts @@ -12,18 +12,18 @@ import { ConstantsHolderInstance, SkaleTokenInstance, ValidatorServiceInstance} from "../types/truffle-contracts"; -import { deployConstantsHolder } from "./utils/deploy/constantsHolder"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployDelegationController } from "./utils/deploy/delegation/delegationController"; -import { deployDistributor } from "./utils/deploy/delegation/distributor"; -import { deployValidatorService } from "./utils/deploy/delegation/validatorService"; -import { deployMonitorsData } from "./utils/deploy/monitorsData"; -import { deployNodesData } from "./utils/deploy/nodesData"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { deploySchainsFunctionality } from "./utils/deploy/schainsFunctionality"; -import { deploySkaleManager } from "./utils/deploy/skaleManager"; -import { deploySkaleToken } from "./utils/deploy/skaleToken"; -import { skipTime } from "./utils/time"; +import { deployConstantsHolder } from "./tools/deploy/constantsHolder"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployDelegationController } from "./tools/deploy/delegation/delegationController"; +import { deployDistributor } from "./tools/deploy/delegation/distributor"; +import { deployValidatorService } from "./tools/deploy/delegation/validatorService"; +import { deployMonitorsData } from "./tools/deploy/monitorsData"; +import { deployNodesData } from "./tools/deploy/nodesData"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { deploySchainsFunctionality } from "./tools/deploy/schainsFunctionality"; +import { deploySkaleManager } from "./tools/deploy/skaleManager"; +import { deploySkaleToken } from "./tools/deploy/skaleToken"; +import { skipTime } from "./tools/time"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SkaleToken.ts b/test/SkaleToken.ts index 84db7d2a5..9c8272473 100644 --- a/test/SkaleToken.ts +++ b/test/SkaleToken.ts @@ -5,9 +5,9 @@ import { ContractManagerInstance, import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deploySkaleToken } from "./utils/deploy/skaleToken"; -import { deployReentrancyTester } from "./utils/deploy/test/reentracyTester"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deploySkaleToken } from "./tools/deploy/skaleToken"; +import { deployReentrancyTester } from "./tools/deploy/test/reentracyTester"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/SkaleVerifier.ts b/test/SkaleVerifier.ts index 39674db3a..7948b4898 100644 --- a/test/SkaleVerifier.ts +++ b/test/SkaleVerifier.ts @@ -7,12 +7,12 @@ import { ContractManagerInstance, SkaleVerifierInstance, ValidatorServiceInstance} from "../types/truffle-contracts"; -import { deployContractManager } from "./utils/deploy/contractManager"; -import { deployValidatorService } from "./utils/deploy/delegation/validatorService"; -import { deployNodesFunctionality } from "./utils/deploy/nodesFunctionality"; -import { deploySchainsData } from "./utils/deploy/schainsData"; -import { deploySchainsFunctionality } from "./utils/deploy/schainsFunctionality"; -import { deploySkaleVerifier } from "./utils/deploy/skaleVerifier"; +import { deployContractManager } from "./tools/deploy/contractManager"; +import { deployValidatorService } from "./tools/deploy/delegation/validatorService"; +import { deployNodesFunctionality } from "./tools/deploy/nodesFunctionality"; +import { deploySchainsData } from "./tools/deploy/schainsData"; +import { deploySchainsFunctionality } from "./tools/deploy/schainsFunctionality"; +import { deploySkaleVerifier } from "./tools/deploy/skaleVerifier"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/delegation/Delegation.ts b/test/delegation/Delegation.ts index 9165bff6e..522ad4e02 100644 --- a/test/delegation/Delegation.ts +++ b/test/delegation/Delegation.ts @@ -12,21 +12,21 @@ import { ConstantsHolderInstance, const SkaleManagerMock: SkaleManagerMockContract = artifacts.require("./SkaleManagerMock"); -import { currentTime, skipTime, skipTimeToDate } from "../utils/time"; +import { currentTime, skipTime, skipTimeToDate } from "../tools/time"; import BigNumber from "bignumber.js"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployConstantsHolder } from "../utils/deploy/constantsHolder"; -import { deployContractManager } from "../utils/deploy/contractManager"; -import { deployDelegationController } from "../utils/deploy/delegation/delegationController"; -import { deployDelegationPeriodManager } from "../utils/deploy/delegation/delegationPeriodManager"; -import { deployDistributor } from "../utils/deploy/delegation/distributor"; -import { deployPunisher } from "../utils/deploy/delegation/punisher"; -import { deployTokenState } from "../utils/deploy/delegation/tokenState"; -import { deployValidatorService } from "../utils/deploy/delegation/validatorService"; -import { deploySkaleToken } from "../utils/deploy/skaleToken"; -import { Delegation } from "../utils/types"; +import { deployConstantsHolder } from "../tools/deploy/constantsHolder"; +import { deployContractManager } from "../tools/deploy/contractManager"; +import { deployDelegationController } from "../tools/deploy/delegation/delegationController"; +import { deployDelegationPeriodManager } from "../tools/deploy/delegation/delegationPeriodManager"; +import { deployDistributor } from "../tools/deploy/delegation/distributor"; +import { deployPunisher } from "../tools/deploy/delegation/punisher"; +import { deployTokenState } from "../tools/deploy/delegation/tokenState"; +import { deployValidatorService } from "../tools/deploy/delegation/validatorService"; +import { deploySkaleToken } from "../tools/deploy/skaleToken"; +import { Delegation } from "../tools/types"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/delegation/DelegationController.ts b/test/delegation/DelegationController.ts index cde7aef1b..c409aceb2 100644 --- a/test/delegation/DelegationController.ts +++ b/test/delegation/DelegationController.ts @@ -4,16 +4,16 @@ import { ContractManagerInstance, TokenStateInstance, ValidatorServiceInstance } from "../../types/truffle-contracts"; -import { skipTime } from "../utils/time"; +import { skipTime } from "../tools/time"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployContractManager } from "../utils/deploy/contractManager"; -import { deployDelegationController } from "../utils/deploy/delegation/delegationController"; -import { deployTokenState } from "../utils/deploy/delegation/tokenState"; -import { deployValidatorService } from "../utils/deploy/delegation/validatorService"; -import { deploySkaleToken } from "../utils/deploy/skaleToken"; -import { Delegation, State } from "../utils/types"; +import { deployContractManager } from "../tools/deploy/contractManager"; +import { deployDelegationController } from "../tools/deploy/delegation/delegationController"; +import { deployTokenState } from "../tools/deploy/delegation/tokenState"; +import { deployValidatorService } from "../tools/deploy/delegation/validatorService"; +import { deploySkaleToken } from "../tools/deploy/skaleToken"; +import { Delegation, State } from "../tools/types"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/delegation/TokenLaunch.ts b/test/delegation/TokenLaunch.ts index cf076f7aa..8186590ae 100644 --- a/test/delegation/TokenLaunch.ts +++ b/test/delegation/TokenLaunch.ts @@ -5,17 +5,17 @@ import { ContractManagerInstance, TokenLaunchManagerInstance, ValidatorServiceInstance} from "../../types/truffle-contracts"; -import { isLeapYear, skipTime, skipTimeToDate } from "../utils/time"; +import { isLeapYear, skipTime, skipTimeToDate } from "../tools/time"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployContractManager } from "../utils/deploy/contractManager"; -import { deployDelegationController } from "../utils/deploy/delegation/delegationController"; -import { deployPunisher } from "../utils/deploy/delegation/punisher"; -import { deployTokenLaunchManager } from "../utils/deploy/delegation/tokenLaunchManager"; -import { deployValidatorService } from "../utils/deploy/delegation/validatorService"; -import { deploySkaleToken } from "../utils/deploy/skaleToken"; -import { State } from "../utils/types"; +import { deployContractManager } from "../tools/deploy/contractManager"; +import { deployDelegationController } from "../tools/deploy/delegation/delegationController"; +import { deployPunisher } from "../tools/deploy/delegation/punisher"; +import { deployTokenLaunchManager } from "../tools/deploy/delegation/tokenLaunchManager"; +import { deployValidatorService } from "../tools/deploy/delegation/validatorService"; +import { deploySkaleToken } from "../tools/deploy/skaleToken"; +import { State } from "../tools/types"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/delegation/TokenState.ts b/test/delegation/TokenState.ts index 9554a5f00..c002acf38 100644 --- a/test/delegation/TokenState.ts +++ b/test/delegation/TokenState.ts @@ -4,16 +4,16 @@ import { ContractManagerInstance, TokenStateInstance, ValidatorServiceInstance} from "../../types/truffle-contracts"; -import { deployContractManager } from "../utils/deploy/contractManager"; -import { currentTime, skipTime } from "../utils/time"; +import { deployContractManager } from "../tools/deploy/contractManager"; +import { currentTime, skipTime } from "../tools/time"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployDelegationController } from "../utils/deploy/delegation/delegationController"; -import { deployTokenState } from "../utils/deploy/delegation/tokenState"; -import { deployValidatorService } from "../utils/deploy/delegation/validatorService"; -import { deploySkaleToken } from "../utils/deploy/skaleToken"; -import { State } from "../utils/types"; +import { deployDelegationController } from "../tools/deploy/delegation/delegationController"; +import { deployTokenState } from "../tools/deploy/delegation/tokenState"; +import { deployValidatorService } from "../tools/deploy/delegation/validatorService"; +import { deploySkaleToken } from "../tools/deploy/skaleToken"; +import { State } from "../tools/types"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/delegation/ValidatorService.ts b/test/delegation/ValidatorService.ts index 4b780de54..64999a901 100644 --- a/test/delegation/ValidatorService.ts +++ b/test/delegation/ValidatorService.ts @@ -4,16 +4,16 @@ import { ConstantsHolderInstance, SkaleTokenInstance, ValidatorServiceInstance} from "../../types/truffle-contracts"; -import { skipTime } from "../utils/time"; +import { skipTime } from "../tools/time"; import BigNumber from "bignumber.js"; import * as chai from "chai"; import * as chaiAsPromised from "chai-as-promised"; -import { deployConstantsHolder } from "../utils/deploy/constantsHolder"; -import { deployContractManager } from "../utils/deploy/contractManager"; -import { deployDelegationController } from "../utils/deploy/delegation/delegationController"; -import { deployValidatorService } from "../utils/deploy/delegation/validatorService"; -import { deploySkaleToken } from "../utils/deploy/skaleToken"; +import { deployConstantsHolder } from "../tools/deploy/constantsHolder"; +import { deployContractManager } from "../tools/deploy/contractManager"; +import { deployDelegationController } from "../tools/deploy/delegation/delegationController"; +import { deployValidatorService } from "../tools/deploy/delegation/validatorService"; +import { deploySkaleToken } from "../tools/deploy/skaleToken"; chai.should(); chai.use(chaiAsPromised); diff --git a/test/utils/command_line.ts b/test/tools/command_line.ts similarity index 100% rename from test/utils/command_line.ts rename to test/tools/command_line.ts diff --git a/test/utils/deploy/constantsHolder.ts b/test/tools/deploy/constantsHolder.ts similarity index 100% rename from test/utils/deploy/constantsHolder.ts rename to test/tools/deploy/constantsHolder.ts diff --git a/test/utils/deploy/contractManager.ts b/test/tools/deploy/contractManager.ts similarity index 100% rename from test/utils/deploy/contractManager.ts rename to test/tools/deploy/contractManager.ts diff --git a/test/utils/deploy/dectyption.ts b/test/tools/deploy/dectyption.ts similarity index 100% rename from test/utils/deploy/dectyption.ts rename to test/tools/deploy/dectyption.ts diff --git a/test/utils/deploy/delegation/delegationController.ts b/test/tools/deploy/delegation/delegationController.ts similarity index 100% rename from test/utils/deploy/delegation/delegationController.ts rename to test/tools/deploy/delegation/delegationController.ts diff --git a/test/utils/deploy/delegation/delegationPeriodManager.ts b/test/tools/deploy/delegation/delegationPeriodManager.ts similarity index 100% rename from test/utils/deploy/delegation/delegationPeriodManager.ts rename to test/tools/deploy/delegation/delegationPeriodManager.ts diff --git a/test/utils/deploy/delegation/distributor.ts b/test/tools/deploy/delegation/distributor.ts similarity index 100% rename from test/utils/deploy/delegation/distributor.ts rename to test/tools/deploy/delegation/distributor.ts diff --git a/test/utils/deploy/delegation/punisher.ts b/test/tools/deploy/delegation/punisher.ts similarity index 100% rename from test/utils/deploy/delegation/punisher.ts rename to test/tools/deploy/delegation/punisher.ts diff --git a/test/utils/deploy/delegation/timeHelpers.ts b/test/tools/deploy/delegation/timeHelpers.ts similarity index 100% rename from test/utils/deploy/delegation/timeHelpers.ts rename to test/tools/deploy/delegation/timeHelpers.ts diff --git a/test/utils/deploy/delegation/tokenLaunchLocker.ts b/test/tools/deploy/delegation/tokenLaunchLocker.ts similarity index 100% rename from test/utils/deploy/delegation/tokenLaunchLocker.ts rename to test/tools/deploy/delegation/tokenLaunchLocker.ts diff --git a/test/utils/deploy/delegation/tokenLaunchManager.ts b/test/tools/deploy/delegation/tokenLaunchManager.ts similarity index 100% rename from test/utils/deploy/delegation/tokenLaunchManager.ts rename to test/tools/deploy/delegation/tokenLaunchManager.ts diff --git a/test/utils/deploy/delegation/tokenState.ts b/test/tools/deploy/delegation/tokenState.ts similarity index 100% rename from test/utils/deploy/delegation/tokenState.ts rename to test/tools/deploy/delegation/tokenState.ts diff --git a/test/utils/deploy/delegation/validatorService.ts b/test/tools/deploy/delegation/validatorService.ts similarity index 100% rename from test/utils/deploy/delegation/validatorService.ts rename to test/tools/deploy/delegation/validatorService.ts diff --git a/test/utils/deploy/ecdh.ts b/test/tools/deploy/ecdh.ts similarity index 100% rename from test/utils/deploy/ecdh.ts rename to test/tools/deploy/ecdh.ts diff --git a/test/utils/deploy/factory.ts b/test/tools/deploy/factory.ts similarity index 100% rename from test/utils/deploy/factory.ts rename to test/tools/deploy/factory.ts diff --git a/test/utils/deploy/managerData.ts b/test/tools/deploy/managerData.ts similarity index 100% rename from test/utils/deploy/managerData.ts rename to test/tools/deploy/managerData.ts diff --git a/test/utils/deploy/monitorsData.ts b/test/tools/deploy/monitorsData.ts similarity index 100% rename from test/utils/deploy/monitorsData.ts rename to test/tools/deploy/monitorsData.ts diff --git a/test/utils/deploy/monitorsFunctionality.ts b/test/tools/deploy/monitorsFunctionality.ts similarity index 100% rename from test/utils/deploy/monitorsFunctionality.ts rename to test/tools/deploy/monitorsFunctionality.ts diff --git a/test/utils/deploy/nodesData.ts b/test/tools/deploy/nodesData.ts similarity index 100% rename from test/utils/deploy/nodesData.ts rename to test/tools/deploy/nodesData.ts diff --git a/test/utils/deploy/nodesFunctionality.ts b/test/tools/deploy/nodesFunctionality.ts similarity index 100% rename from test/utils/deploy/nodesFunctionality.ts rename to test/tools/deploy/nodesFunctionality.ts diff --git a/test/utils/deploy/pricing.ts b/test/tools/deploy/pricing.ts similarity index 100% rename from test/utils/deploy/pricing.ts rename to test/tools/deploy/pricing.ts diff --git a/test/utils/deploy/schainsData.ts b/test/tools/deploy/schainsData.ts similarity index 100% rename from test/utils/deploy/schainsData.ts rename to test/tools/deploy/schainsData.ts diff --git a/test/utils/deploy/schainsFunctionality.ts b/test/tools/deploy/schainsFunctionality.ts similarity index 100% rename from test/utils/deploy/schainsFunctionality.ts rename to test/tools/deploy/schainsFunctionality.ts diff --git a/test/utils/deploy/schainsFunctionalityInternal.ts b/test/tools/deploy/schainsFunctionalityInternal.ts similarity index 100% rename from test/utils/deploy/schainsFunctionalityInternal.ts rename to test/tools/deploy/schainsFunctionalityInternal.ts diff --git a/test/utils/deploy/skaleDKG.ts b/test/tools/deploy/skaleDKG.ts similarity index 100% rename from test/utils/deploy/skaleDKG.ts rename to test/tools/deploy/skaleDKG.ts diff --git a/test/utils/deploy/skaleManager.ts b/test/tools/deploy/skaleManager.ts similarity index 100% rename from test/utils/deploy/skaleManager.ts rename to test/tools/deploy/skaleManager.ts diff --git a/test/utils/deploy/skaleToken.ts b/test/tools/deploy/skaleToken.ts similarity index 100% rename from test/utils/deploy/skaleToken.ts rename to test/tools/deploy/skaleToken.ts diff --git a/test/utils/deploy/skaleVerifier.ts b/test/tools/deploy/skaleVerifier.ts similarity index 100% rename from test/utils/deploy/skaleVerifier.ts rename to test/tools/deploy/skaleVerifier.ts diff --git a/test/utils/deploy/slashingTable.ts b/test/tools/deploy/slashingTable.ts similarity index 100% rename from test/utils/deploy/slashingTable.ts rename to test/tools/deploy/slashingTable.ts diff --git a/test/utils/deploy/test/reentracyTester.ts b/test/tools/deploy/test/reentracyTester.ts similarity index 100% rename from test/utils/deploy/test/reentracyTester.ts rename to test/tools/deploy/test/reentracyTester.ts diff --git a/test/utils/elliptic-types.ts b/test/tools/elliptic-types.ts similarity index 100% rename from test/utils/elliptic-types.ts rename to test/tools/elliptic-types.ts diff --git a/test/utils/time.ts b/test/tools/time.ts similarity index 100% rename from test/utils/time.ts rename to test/tools/time.ts diff --git a/test/utils/types.ts b/test/tools/types.ts similarity index 100% rename from test/utils/types.ts rename to test/tools/types.ts diff --git a/test/utils/MathUtils.ts b/test/utils/MathUtils.ts new file mode 100644 index 000000000..6b91f5870 --- /dev/null +++ b/test/utils/MathUtils.ts @@ -0,0 +1,57 @@ +import chai = require("chai"); +import * as chaiAsPromised from "chai-as-promised"; +import { MathUtilsTesterContract, MathUtilsTesterInstance } from "../../types/truffle-contracts"; + +const MathUtils: MathUtilsTesterContract = artifacts.require("MathUtilsTester"); + +chai.should(); +chai.use((chaiAsPromised as any)); + +contract("MathUtils", ([owner]) => { + let mathUtils: MathUtilsTesterInstance; + before(async () => { + mathUtils = await MathUtils.new(); + }); + + describe("in transaction", async () => { + it("should subtract normally if reduced is greater than subtracted", async () => { + (await mathUtils.boundedSub.call(5, 3)).toNumber().should.be.equal(2); + (await mathUtils.boundedSubWithoutEvent(5, 3)).toNumber().should.be.equal(2); + }); + + it("should return 0 if reduced is less than subtracted and emit event", async () => { + (await mathUtils.boundedSub.call(3, 5)).toNumber().should.be.equal(0); + const response = await mathUtils.boundedSub(3, 5); + response.receipt.rawLogs.length.should.be.equal(1); + const log = response.receipt.rawLogs[0]; + log.topics.should.contain(web3.utils.keccak256("UnderflowError(uint256,uint256)")); + const params = web3.eth.abi.decodeParameters(["uint256", "uint256"], log.data); + console.log(params[0]); + console.log(typeof params[0]); + params[0].should.be.equal("3"); + params[1].should.be.equal("5"); + }); + }); + + describe("in call", async () => { + it("should subtract normally if reduced is greater than subtracted", async () => { + (await mathUtils.boundedSubWithoutEvent(5, 3)).toNumber().should.be.equal(2); + }); + + it("should return 0 if reduced is less than subtracted ", async () => { + (await mathUtils.boundedSubWithoutEvent(3, 5)).toNumber().should.be.equal(0); + }); + }); + + it("should properly compare", async () => { + (await mathUtils.muchGreater(100, 0)).should.be.equal(false); + (await mathUtils.muchGreater(1e6 + 1, 0)).should.be.equal(true); + }); + + it("should properly approximately check equality", async () => { + (await mathUtils.approximatelyEqual(100, 0)).should.be.equal(true); + (await mathUtils.approximatelyEqual(1e6 + 1, 0)).should.be.equal(false); + (await mathUtils.approximatelyEqual(0, 100)).should.be.equal(true); + (await mathUtils.approximatelyEqual(0, 1e6 + 1)).should.be.equal(false); + }); +}); From 0f5f28d67c4773732fcf586a3997fd5d016c2700 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Fri, 3 Apr 2020 17:04:51 +0300 Subject: [PATCH 08/11] SKALE-2347 Modify testing script --- scripts/coverage.sh | 11 ++----- scripts/move.sh | 78 +++++++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 28 deletions(-) mode change 100644 => 100755 scripts/move.sh diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 06e1c302d..a7ed5a491 100644 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -1,9 +1,4 @@ -for file in test/$TESTFOLDERS/* -do -file=${file:7} -if [ ! -f "test/$file" ]; then - file="delegation/$file" -fi -npx buidler coverage --testfiles test/$file --solcoverjs .solcover.js || exit 4 +#!/bin/bash + +npx buidler coverage --solcoverjs .solcover.js || exit $? bash <(curl -s https://codecov.io/bash) -done diff --git a/scripts/move.sh b/scripts/move.sh old mode 100644 new mode 100755 index adc221dd4..e962475fd --- a/scripts/move.sh +++ b/scripts/move.sh @@ -1,21 +1,59 @@ +#!/bin/bash + cd test -mkdir 1 -cp SkaleManager.ts 1/ -mkdir 2 -cp SchainsFunctionality.ts 2/ -cp SkaleDKG.ts 2/ -cp MonitorsFunctionality.spec.ts 2/ -cp SchainsData.ts 2/ -cp ECDH.ts 2/ -cp Decryption.ts 2/ -cp ManagerData.spec.ts 2/ -cp ContractManager.ts 2/ -mkdir 3 -cp -r delegation/* 3/ -cp SkaleToken.ts 3/ -cp Pricing.ts 3/ -cp NodesFunctionality.ts 3/ -cp SkaleVerifier.ts 3/ -cp MonitorsData.ts 3/ -cp NodesData.ts 3/ -cp ConstantsHolder.spec.ts 3/ + +all=(*.ts delegation/*.ts utils/*.ts) +group1=( + SkaleManager.ts +) +group2=( + SchainsFunctionality.ts + SkaleDKG.ts + MonitorsFunctionality.spec.ts + SchainsData.ts + ECDH.ts + Decryption.ts + ManagerData.spec.ts + ContractManager.ts +) +group3=() +for file in "${all[@]}" +do + listed=false + for listedFile in "${group1[@]}" "${group2[@]}" + do + if [[ $file == $listedFile ]] + then + listed=true + break + fi + done + if [ $listed = false ] + then + group3+=( $file ) + fi +done + +removingFiles=() +if [ "$TESTFOLDERS" = 1 ] +then + removingFiles+=(${group2[@]}) + removingFiles+=(${group3[@]}) +elif [ "$TESTFOLDERS" = 2 ] +then + removingFiles+=(${group1[@]}) + removingFiles+=(${group3[@]}) +elif [ "$TESTFOLDERS" = 3 ] +then + removingFiles+=(${group1[@]}) + removingFiles+=(${group2[@]}) +else + echo "Testing group is not set" + exit 1 +fi + +for file in "${removingFiles[@]}" +do + echo "Remove $file" + rm $file +done From d18c9adf81bd5397b187d71d1f842774cfbff109 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Fri, 3 Apr 2020 18:13:35 +0300 Subject: [PATCH 09/11] SKALE-2347 Remove unused functions --- contracts/delegation/DelegationController.sol | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/contracts/delegation/DelegationController.sol b/contracts/delegation/DelegationController.sol index 681a3e964..831dde62a 100644 --- a/contracts/delegation/DelegationController.sol +++ b/contracts/delegation/DelegationController.sol @@ -667,26 +667,6 @@ contract DelegationController is Permissions, ILocker { return sequence.value[month]; } - function getValue(PartialDifferences storage sequence, uint month) internal view returns (uint) { - if (sequence.firstUnprocessedMonth == 0) { - return 0; - } - if (sequence.firstUnprocessedMonth <= month) { - uint value = sequence.value[sequence.firstUnprocessedMonth - 1]; - for (uint i = sequence.firstUnprocessedMonth; i <= month; ++i) { - value = value.add(sequence.addDiff[i]).boundedSubWithoutEvent(sequence.subtractDiff[i]); - } - return value; - } else { - return sequence.value[month]; - } - } - - function getAndUpdateValueBeforeMonthAndGetAtMonth(PartialDifferences storage sequence, uint month) internal returns (uint) { - getAndUpdateValue(sequence, month.sub(1)); - return getValue(sequence, month); - } - function add(PartialDifferencesValue storage sequence, uint diff, uint month) internal { require(sequence.firstUnprocessedMonth <= month, "Cannot add to the past"); if (sequence.firstUnprocessedMonth == 0) { From f1446bfc51201718c5f7c38cf970cddcecff09e2 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Mon, 6 Apr 2020 16:53:20 +0300 Subject: [PATCH 10/11] SKALE-2347 Add test of slashing --- test/delegation/Delegation.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/delegation/Delegation.ts b/test/delegation/Delegation.ts index 522ad4e02..c17cc8336 100644 --- a/test/delegation/Delegation.ts +++ b/test/delegation/Delegation.ts @@ -397,6 +397,13 @@ contract("Delegation", ([owner, (await distributor.getAndUpdateEarnedBountyAmount.call( validatorId, {from: holder3}))[0].toNumber().should.be.equal(0); }); + + it("should reduce delegated amount immediately after slashing", async () => { + await punisher.slash(validatorId, 1); + + (await delegationController.getAndUpdateDelegatedAmount.call(holder1, {from: holder1})).toNumber() + .should.be.equal(delegatedAmount1 - 1); + }); }); }); From d895276538ad255dd98d6c9470ca5d764f109831 Mon Sep 17 00:00:00 2001 From: Dmytro Stebaiev Date: Mon, 6 Apr 2020 19:38:21 +0300 Subject: [PATCH 11/11] SKALE-2347 Modify test --- test/delegation/Delegation.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/delegation/Delegation.ts b/test/delegation/Delegation.ts index c17cc8336..aba109347 100644 --- a/test/delegation/Delegation.ts +++ b/test/delegation/Delegation.ts @@ -399,6 +399,8 @@ contract("Delegation", ([owner, }); it("should reduce delegated amount immediately after slashing", async () => { + await delegationController.getAndUpdateDelegatedAmount(holder1, {from: holder1}); + await punisher.slash(validatorId, 1); (await delegationController.getAndUpdateDelegatedAmount.call(holder1, {from: holder1})).toNumber()