Skip to content

Commit

Permalink
fix: added a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohsen-T committed Nov 28, 2023
1 parent 68cc9a8 commit 6951d28
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
5 changes: 1 addition & 4 deletions contracts/SSVNetworkViews.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 0 additions & 2 deletions contracts/interfaces/ISSVViews.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion contracts/modules/SSVOperators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 0 additions & 5 deletions contracts/modules/SSVViews.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 0 additions & 3 deletions contracts/test/SSVViewsT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
95 changes: 64 additions & 31 deletions test/sanity/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand All @@ -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');
});

Expand All @@ -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);

Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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],
Expand All @@ -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,
Expand All @@ -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 -
Expand Down

0 comments on commit 6951d28

Please sign in to comment.