From 6951d2881724f3655f204d2966895d3e32e8a2ff Mon Sep 17 00:00:00 2001 From: Mohsen-T Date: Tue, 28 Nov 2023 19:44:08 +0100 Subject: [PATCH] fix: added a test case --- contracts/SSVNetworkViews.sol | 5 +- contracts/interfaces/ISSVViews.sol | 2 - contracts/modules/SSVOperators.sol | 1 - contracts/modules/SSVViews.sol | 5 -- contracts/test/SSVViewsT.sol | 3 - test/sanity/balances.ts | 95 ++++++++++++++++++++---------- 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/contracts/SSVNetworkViews.sol b/contracts/SSVNetworkViews.sol index 26fa8834..84bcfa9c 100644 --- a/contracts/SSVNetworkViews.sol +++ b/contracts/SSVNetworkViews.sol @@ -108,10 +108,7 @@ contract SSVNetworkViews is UUPSUpgradeable, Ownable2StepUpgradeable, ISSVViews /*******************************/ /* DAO External View Functions */ /*******************************/ - function getNetworkFeeIndex() external view returns (uint256) { - return ssvNetwork.getNetworkFeeIndex(); - } - + function getNetworkFee() external view override returns (uint256) { return ssvNetwork.getNetworkFee(); } diff --git a/contracts/interfaces/ISSVViews.sol b/contracts/interfaces/ISSVViews.sol index 07184e5b..b635013c 100644 --- a/contracts/interfaces/ISSVViews.sol +++ b/contracts/interfaces/ISSVViews.sol @@ -85,8 +85,6 @@ interface ISSVViews is ISSVNetworkCore { Cluster memory cluster ) external view returns (uint256 balance); - function getNetworkFeeIndex() external view returns (uint256); - /// @notice Gets the network fee /// @return networkFee The fee associated with the network (SSV) function getNetworkFee() external view returns (uint256 networkFee); diff --git a/contracts/modules/SSVOperators.sol b/contracts/modules/SSVOperators.sol index 638ca8f4..b27455f7 100644 --- a/contracts/modules/SSVOperators.sol +++ b/contracts/modules/SSVOperators.sol @@ -62,7 +62,6 @@ contract SSVOperators is ISSVOperators { operator.snapshot.balance = 0; operator.validatorCount = 0; operator.fee = 0; - operator.whitelisted = false; s.operators[operatorId] = operator; diff --git a/contracts/modules/SSVViews.sol b/contracts/modules/SSVViews.sol index 11874e04..48a7fc17 100644 --- a/contracts/modules/SSVViews.sol +++ b/contracts/modules/SSVViews.sol @@ -9,7 +9,6 @@ import "../libraries/CoreLib.sol"; import "../libraries/ProtocolLib.sol"; import "../libraries/SSVStorage.sol"; import "../libraries/SSVStorageProtocol.sol"; -import "hardhat/console.sol"; contract SSVViews is ISSVViews { using Types64 for uint64; @@ -164,10 +163,6 @@ contract SSVViews is ISSVViews { /* DAO External View Functions */ /*******************************/ - function getNetworkFeeIndex() external view override returns (uint256) { - return SSVStorageProtocol.load().currentNetworkFeeIndex(); - } - function getNetworkFee() external view override returns (uint256) { return SSVStorageProtocol.load().networkFee.expand(); } diff --git a/contracts/test/SSVViewsT.sol b/contracts/test/SSVViewsT.sol index 8fd729af..b9d756ff 100644 --- a/contracts/test/SSVViewsT.sol +++ b/contracts/test/SSVViewsT.sol @@ -166,9 +166,6 @@ contract SSVViewsT is ISSVViews { /*******************************/ /* DAO External View Functions */ /*******************************/ - function getNetworkFeeIndex() external view override returns (uint256) { - return SSVStorageProtocol.load().currentNetworkFeeIndex(); - } function getNetworkFee() external view override returns (uint256) { return SSVStorageProtocol.load().networkFee.expand(); diff --git a/test/sanity/balances.ts b/test/sanity/balances.ts index c47b19a7..ed13863e 100644 --- a/test/sanity/balances.ts +++ b/test/sanity/balances.ts @@ -34,7 +34,7 @@ describe('Balance Tests', () => { // cold register await helpers.coldRegisterValidator(); - cluster1 = await helpers.registerValidators(4, 1, minDepositAmount, helpers.DataGenerator.cluster.new(), [ + cluster1 = await helpers.registerValidators(0, 1, minDepositAmount, helpers.DataGenerator.cluster.new(), [ GasGroup.REGISTER_VALIDATOR_NEW_STATE, ]); initNetworkFeeBalance = await ssvViews.getNetworkEarnings(); @@ -46,7 +46,7 @@ describe('Balance Tests', () => { let prevBalance: any; for (let i = 1; i <= 13; i++) { await ssvNetworkContract.connect(helpers.DB.owners[0]).removeOperator(i); - let balance = await ssvViews.getBalance(helpers.DB.owners[4].address, operatorIds, cluster); + let balance = await ssvViews.getBalance(helpers.DB.owners[0].address, operatorIds, cluster); let networkFee = await ssvViews.getNetworkFee(); if (i > 4) { expect(prevBalance - balance).to.equal(networkFee); @@ -55,39 +55,72 @@ describe('Balance Tests', () => { } }); + it.only('Check cluster balance after removing operator, progress blocks and confirm', async () => { + const operatorIds = cluster1.args.operatorIds; + const cluster = cluster1.args.cluster; + const owner = cluster1.args.owner; + + // get difference of account balance between blocks before removing operator + let balance1 = await ssvViews.getBalance(helpers.DB.owners[0].address, operatorIds, cluster); + await utils.progressBlocks(1); + let balance2 = await ssvViews.getBalance(helpers.DB.owners[0].address, operatorIds, cluster); + + await ssvNetworkContract.connect(helpers.DB.owners[0]).removeOperator(1); + + // get difference of account balance between blocks after removing operator + let balance3 = await ssvViews.getBalance(helpers.DB.owners[0].address, operatorIds, cluster); + await utils.progressBlocks(1); + let balance4 = await ssvViews.getBalance(helpers.DB.owners[0].address, operatorIds, cluster); + + // check the reducing the balance after removing operator (only 3 operators) + expect(balance1 - balance2).to.be.greaterThan(balance3 - balance4); + + // try to register a new validator in the new cluster with the same operator Ids, check revert + const newOperatorIds = operatorIds.map((id: any) => id.toNumber()); + await expect(helpers.registerValidators(1, 1, minDepositAmount, newOperatorIds)).to.be.revertedWithCustomError( + ssvNetworkContract, + 'OperatorDoesNotExist', + ); + + // try to remove the validator again and check the operator removed is skipped + await ssvNetworkContract + .connect(helpers.DB.owners[0]) + .removeValidator(helpers.DataGenerator.publicKey(1), operatorIds, cluster); + }); + it('Check cluster balance in three blocks, one after the other', async () => { await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock); await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2); await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 3); }); it('Check cluster balance in two and twelve blocks, after network fee updates', async () => { await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock); const newBurnPerBlock = burnPerBlock + networkFee; await ssvNetworkContract.updateNetworkFee(networkFee * 2); await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock); await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock * 2); await utils.progressBlocks(10); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock * 12); }); @@ -159,21 +192,21 @@ describe('Balance Tests', () => { it('Check cluster balance with removed operator', async () => { await ssvNetworkContract.removeOperator(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).not.equals(0); }); it('Check cluster balance with not enough balance', async () => { await utils.progressBlocks(helpers.CONFIG.minimalBlocksBeforeLiquidation + 10); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.be.equals(0); }); it('Check cluster balance in a non liquidated cluster', async () => { await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock); }); @@ -190,7 +223,7 @@ describe('Balance Tests', () => { await ssvViews.isLiquidated(updatedCluster.owner, updatedCluster.operatorIds, updatedCluster.cluster), ).to.equal(true); await expect( - ssvViews.getBalance(helpers.DB.owners[4].address, updatedCluster.operatorIds, updatedCluster.cluster), + ssvViews.getBalance(helpers.DB.owners[0].address, updatedCluster.operatorIds, updatedCluster.cluster), ).to.be.revertedWithCustomError(ssvViews, 'ClusterIsLiquidated'); }); @@ -207,7 +240,7 @@ describe('Balance Tests', () => { helpers.CONFIG.minimalOperatorFee * 3 + helpers.CONFIG.minimalOperatorFee, ); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock); expect((await ssvViews.getNetworkEarnings()) - initNetworkFeeBalance).to.equal(networkFee * 2); @@ -218,12 +251,12 @@ describe('Balance Tests', () => { await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock); expect((await ssvViews.getNetworkEarnings()) - initNetworkFeeBalance).to.equal(networkFee * 4 + newNetworkFee * 2); const minDep2 = minDepositAmount * 2; - const cluster2 = await helpers.registerValidators(4, 1, minDep2.toString(), [3, 4, 5, 6]); + const cluster2 = await helpers.registerValidators(0, 1, minDep2.toString(), [3, 4, 5, 6]); await utils.progressBlocks(2); expect(await ssvViews.getOperatorEarnings(1)).to.equal( @@ -239,10 +272,10 @@ describe('Balance Tests', () => { helpers.CONFIG.minimalOperatorFee * 6 + helpers.CONFIG.minimalOperatorFee * 5, ); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock * 5); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster2.args.operatorIds, cluster2.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster2.args.operatorIds, cluster2.args.cluster), ).to.equal(minDep2 - newBurnPerBlock * 2); // cold cluster + cluster1 * networkFee (4) + (cold cluster + cluster1 * newNetworkFee (5 + 5)) + cluster2 * newNetworkFee (2) @@ -254,10 +287,10 @@ describe('Balance Tests', () => { await utils.progressBlocks(4); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock * 2 - newBurnPerBlock * 6 - burnPerBlock * 4); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster2.args.operatorIds, cluster2.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster2.args.operatorIds, cluster2.args.cluster), ).to.equal(minDep2 - newBurnPerBlock * 3 - burnPerBlock * 4); expect(await ssvViews.getOperatorEarnings(1)).to.equal( @@ -288,22 +321,22 @@ describe('Balance Tests', () => { helpers.CONFIG.minimalOperatorFee * 4 + helpers.CONFIG.minimalOperatorFee + newFee * 2, ); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock - (helpers.CONFIG.minimalOperatorFee * 3 + networkFee) * 2 - newFee * 2); }); it('Check cluster balance after withdraw and deposit"', async () => { await utils.progressBlocks(1); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster1.args.operatorIds, cluster1.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster1.args.operatorIds, cluster1.args.cluster), ).to.equal(minDepositAmount - burnPerBlock); await helpers.DB.ssvToken - .connect(helpers.DB.owners[4]) + .connect(helpers.DB.owners[0]) .approve(helpers.DB.ssvNetwork.contract.address, minDepositAmount * 2); let validator2 = await trackGas( ssvNetworkContract - .connect(helpers.DB.owners[4]) + .connect(helpers.DB.owners[0]) .registerValidator( helpers.DataGenerator.publicKey(3), [1, 2, 3, 4], @@ -315,28 +348,28 @@ describe('Balance Tests', () => { let cluster2 = validator2.eventsByName.ValidatorAdded[0]; expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster2.args.operatorIds, cluster2.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster2.args.operatorIds, cluster2.args.cluster), ).to.equal(minDepositAmount * 3 - burnPerBlock * 3); validator2 = await trackGas( ssvNetworkContract - .connect(helpers.DB.owners[4]) + .connect(helpers.DB.owners[0]) .withdraw(cluster2.args.operatorIds, helpers.CONFIG.minimalOperatorFee, cluster2.args.cluster), ); cluster2 = validator2.eventsByName.ClusterWithdrawn[0]; expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster2.args.operatorIds, cluster2.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster2.args.operatorIds, cluster2.args.cluster), ).to.equal(minDepositAmount * 3 - burnPerBlock * 4 - burnPerBlock - helpers.CONFIG.minimalOperatorFee); await helpers.DB.ssvToken - .connect(helpers.DB.owners[4]) + .connect(helpers.DB.owners[0]) .approve(helpers.DB.ssvNetwork.contract.address, minDepositAmount); validator2 = await trackGas( ssvNetworkContract - .connect(helpers.DB.owners[4]) + .connect(helpers.DB.owners[0]) .deposit( - helpers.DB.owners[4].address, + helpers.DB.owners[0].address, cluster2.args.operatorIds, helpers.CONFIG.minimalOperatorFee, cluster2.args.cluster, @@ -346,7 +379,7 @@ describe('Balance Tests', () => { await utils.progressBlocks(2); expect( - await ssvViews.getBalance(helpers.DB.owners[4].address, cluster2.args.operatorIds, cluster2.args.cluster), + await ssvViews.getBalance(helpers.DB.owners[0].address, cluster2.args.operatorIds, cluster2.args.cluster), ).to.equal( minDepositAmount * 3 - burnPerBlock * 8 -