From 347ba037619803323637161892f0695ed72cc8dc Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 24 Aug 2023 13:22:38 +0200 Subject: [PATCH] cluster not liquidatable if it has zero validators --- contracts/libraries/ClusterLib.sol | 12 +++++--- test/helpers/gas-usage.ts | 4 +-- test/liquidate/liquidated-cluster.ts | 42 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/contracts/libraries/ClusterLib.sol b/contracts/libraries/ClusterLib.sol index ea0bb36f..178701f7 100644 --- a/contracts/libraries/ClusterLib.sol +++ b/contracts/libraries/ClusterLib.sol @@ -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 { diff --git a/test/helpers/gas-usage.ts b/test/helpers/gas-usage.ts index b5ba133e..355b9832 100644 --- a/test/helpers/gas-usage.ts +++ b/test/helpers/gas-usage.ts @@ -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, diff --git a/test/liquidate/liquidated-cluster.ts b/test/liquidate/liquidated-cluster.ts index e8d2b1b2..497a501d 100644 --- a/test/liquidate/liquidated-cluster.ts +++ b/test/liquidate/liquidated-cluster.ts @@ -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 -> liquidate', 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'); + + }); }); \ No newline at end of file