|
| 1 | +import { expect } from "chai"; |
| 2 | +import type { NetworkConnection } from "hardhat/types/network"; |
| 3 | +import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/types"; |
| 4 | +import { getTestConnection } from "../../setup/connection.ts"; |
| 5 | +import { ssvClustersHarnessFixture } from "../../setup/fixtures.ts"; |
| 6 | +import type { NetworkHelpersType } from "../../common/types.ts"; |
| 7 | +import { makePublicKey } from "../../common/helpers.ts"; |
| 8 | +import { DEFAULT_ETH_REGISTER_VALUE, DEFAULT_SHARES, EMPTY_CLUSTER } from "../../common/constants.ts"; |
| 9 | +import { Events } from "../../common/events.ts"; |
| 10 | +import { Errors } from "../../common/errors.ts"; |
| 11 | + |
| 12 | +const createCluster = () => ({ |
| 13 | + ...EMPTY_CLUSTER, |
| 14 | + active: true, |
| 15 | +}); |
| 16 | + |
| 17 | +describe("SSVClusters function `bulkExitValidator()`", async () => { |
| 18 | + let connection: NetworkConnection<"generic">; |
| 19 | + let networkHelpers: NetworkHelpersType; |
| 20 | + |
| 21 | + let clusterOwner: HardhatEthersSigner; |
| 22 | + |
| 23 | + before(async function () { |
| 24 | + ({ connection, networkHelpers } = await getTestConnection()); |
| 25 | + |
| 26 | + [clusterOwner] = await connection.ethers.getSigners(); |
| 27 | + }); |
| 28 | + |
| 29 | + const deploySSVClustersAndPrepareOperatorsFixture = async () => { |
| 30 | + return ssvClustersHarnessFixture(connection); |
| 31 | + }; |
| 32 | + |
| 33 | + it("Exits multiple validators and emits events", async function () { |
| 34 | + const { clusters, operatorIds } = |
| 35 | + await networkHelpers.loadFixture(deploySSVClustersAndPrepareOperatorsFixture); |
| 36 | + |
| 37 | + const publicKeys = [makePublicKey(1), makePublicKey(2)]; |
| 38 | + await clusters.bulkRegisterValidator( |
| 39 | + publicKeys, |
| 40 | + operatorIds, |
| 41 | + [DEFAULT_SHARES, DEFAULT_SHARES], |
| 42 | + 0, |
| 43 | + createCluster(), |
| 44 | + { value: DEFAULT_ETH_REGISTER_VALUE } |
| 45 | + ); |
| 46 | + |
| 47 | + const tx = await clusters.bulkExitValidator(publicKeys, operatorIds); |
| 48 | + |
| 49 | + await expect(tx).to.emit(clusters, Events.VALIDATOR_EXITED).withArgs(clusterOwner.address, operatorIds, publicKeys[0]); |
| 50 | + await expect(tx).to.emit(clusters, Events.VALIDATOR_EXITED).withArgs(clusterOwner.address, operatorIds, publicKeys[1]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("Is reverted with 'ValidatorDoesNotExist' when no public keys are provided", async function () { |
| 54 | + const { clusters, operatorIds } = |
| 55 | + await networkHelpers.loadFixture(deploySSVClustersAndPrepareOperatorsFixture); |
| 56 | + |
| 57 | + await expect(clusters.bulkExitValidator( |
| 58 | + [], |
| 59 | + operatorIds |
| 60 | + )).to.be.revertedWithCustomError(clusters, Errors.VALIDATOR_DOES_NOT_EXIST); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Is reverted with 'IncorrectValidatorStateWithData' when any validator is not registered", async function () { |
| 64 | + const { clusters, operatorIds } = |
| 65 | + await networkHelpers.loadFixture(deploySSVClustersAndPrepareOperatorsFixture); |
| 66 | + |
| 67 | + const publicKeys = [makePublicKey(1), makePublicKey(2)]; |
| 68 | + await clusters.registerValidator( |
| 69 | + publicKeys[0], |
| 70 | + operatorIds, |
| 71 | + DEFAULT_SHARES, |
| 72 | + 0, |
| 73 | + createCluster(), |
| 74 | + { value: DEFAULT_ETH_REGISTER_VALUE } |
| 75 | + ); |
| 76 | + |
| 77 | + await expect(clusters.bulkExitValidator( |
| 78 | + publicKeys, |
| 79 | + operatorIds |
| 80 | + )).to.be.revertedWithCustomError(clusters, Errors.INCORRECT_VALIDATOR_STATE_WITH_DATA).withArgs(publicKeys[1]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("Is reverted with 'IncorrectValidatorStateWithData' when operator ids do not match stored validators", async function () { |
| 84 | + const { clusters, operatorIds } = |
| 85 | + await networkHelpers.loadFixture(deploySSVClustersAndPrepareOperatorsFixture); |
| 86 | + |
| 87 | + const publicKeys = [makePublicKey(1), makePublicKey(2)]; |
| 88 | + await clusters.bulkRegisterValidator( |
| 89 | + publicKeys, |
| 90 | + operatorIds, |
| 91 | + [DEFAULT_SHARES, DEFAULT_SHARES], |
| 92 | + 0, |
| 93 | + createCluster(), |
| 94 | + { value: DEFAULT_ETH_REGISTER_VALUE } |
| 95 | + ); |
| 96 | + |
| 97 | + const mismatchedOperatorIds = [...operatorIds]; |
| 98 | + mismatchedOperatorIds[0] = mismatchedOperatorIds[0] + 1n; |
| 99 | + |
| 100 | + await expect(clusters.bulkExitValidator( |
| 101 | + publicKeys, |
| 102 | + mismatchedOperatorIds |
| 103 | + )).to.be.revertedWithCustomError(clusters, Errors.INCORRECT_VALIDATOR_STATE_WITH_DATA).withArgs(publicKeys[0]); |
| 104 | + }); |
| 105 | +}); |
0 commit comments