Skip to content

Commit

Permalink
A cluster is not liquidatable if it has zero validators (#256)
Browse files Browse the repository at this point in the history
* cluster not liquidatable if it has zero validators

* update test name
  • Loading branch information
mtabasco authored Aug 28, 2023
1 parent 786a99d commit c9eb8f6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
12 changes: 8 additions & 4 deletions contracts/libraries/ClusterLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ library ClusterLib {
uint64 networkFee,
uint64 minimumBlocksBeforeLiquidation,
uint64 minimumLiquidationCollateral
) internal pure returns (bool) {
if (cluster.balance < minimumLiquidationCollateral.expand()) return true;
uint64 liquidationThreshold = minimumBlocksBeforeLiquidation * (burnRate + networkFee) * cluster.validatorCount;
) internal pure returns (bool liquidatable) {
if (cluster.validatorCount != 0) {
if (cluster.balance < minimumLiquidationCollateral.expand()) return true;
uint64 liquidationThreshold = minimumBlocksBeforeLiquidation *
(burnRate + networkFee) *
cluster.validatorCount;

return cluster.balance < liquidationThreshold.expand();
return cluster.balance < liquidationThreshold.expand();
}
}

function validateClusterIsNotLiquidated(ISSVNetworkCore.Cluster memory cluster) internal pure {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/gas-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const MAX_GAS_PER_GROUP: any = {
[GasGroup.DEPOSIT]: 77500,
[GasGroup.WITHDRAW_CLUSTER_BALANCE]: 94500,
[GasGroup.WITHDRAW_OPERATOR_BALANCE]: 64900,
[GasGroup.LIQUIDATE_CLUSTER_4]: 129200,
[GasGroup.LIQUIDATE_CLUSTER_7]: 170400,
[GasGroup.LIQUIDATE_CLUSTER_4]: 129300,
[GasGroup.LIQUIDATE_CLUSTER_7]: 170500,
[GasGroup.LIQUIDATE_CLUSTER_10]: 211600,
[GasGroup.LIQUIDATE_CLUSTER_13]: 252800,
[GasGroup.REACTIVATE_CLUSTER]: 120600,
Expand Down
42 changes: 42 additions & 0 deletions test/liquidate/liquidated-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,46 @@ describe('Liquidate Tests', () => {
await utils.progressBlocks(2);
expect(await ssvViews.getBalance(helpers.DB.owners[1].address, clusterEventData.operatorIds, clusterEventData.cluster)).to.be.equals(minDepositAmount * 2 - burnPerBlock * 2);
});

it('Remove validator -> withdraw -> try liquidate reverts "ClusterNotLiquidatable"', async () => {
await ssvNetworkContract.setRegisterAuth(helpers.DB.owners[2].address, false, true);

await helpers.DB.ssvToken.connect(helpers.DB.owners[2]).approve(ssvNetworkContract.address, minDepositAmount);
const register = await trackGas(ssvNetworkContract.connect(helpers.DB.owners[2]).registerValidator(
helpers.DataGenerator.publicKey(2),
[1, 2, 3, 4],
helpers.DataGenerator.shares(4),
minDepositAmount,
{
validatorCount: 0,
networkFeeIndex: 0,
index: 0,
balance: 0,
active: true
}
));
let clusterEventData = register.eventsByName.ValidatorAdded[0].args;
await utils.progressBlocks(helpers.CONFIG.minimalBlocksBeforeLiquidation - 10);

const remove = await trackGas(ssvNetworkContract.connect(helpers.DB.owners[2]).removeValidator(
helpers.DataGenerator.publicKey(2),
clusterEventData.operatorIds,
clusterEventData.cluster
));
clusterEventData = remove.eventsByName.ValidatorRemoved[0].args;

let balance = await ssvViews.getBalance(helpers.DB.owners[2].address, clusterEventData.operatorIds, clusterEventData.cluster);

clusterEventData = await helpers.withdraw(2,
clusterEventData.operatorIds,
((balance - helpers.CONFIG.minimumLiquidationCollateral) * 1.01).toString(),
clusterEventData.cluster);

await expect(ssvNetworkContract.liquidate(
clusterEventData.owner,
clusterEventData.operatorIds,
clusterEventData.cluster
)).to.be.revertedWithCustomError(ssvNetworkContract, 'ClusterNotLiquidatable');

});
});

0 comments on commit c9eb8f6

Please sign in to comment.