|
| 1 | +import { expect } from "chai"; |
| 2 | +import type { NetworkConnection } from "hardhat/types/network"; |
| 3 | +import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/types"; |
| 4 | +import { ethers } from "ethers"; |
| 5 | +import { getTestConnection } from "../../setup/connection.ts"; |
| 6 | +import { ssvClustersHarnessFixture } from "../../setup/fixtures.ts"; |
| 7 | +import type { Cluster, NetworkHelpersType } from "../../common/types.ts"; |
| 8 | +import { createCluster, makePublicKeys, parseClusterFromEvent } from "../../common/helpers.ts"; |
| 9 | +import { DEDUCTED_DIGITS } from "../../common/constants.ts"; |
| 10 | +import { Events } from "../../common/events.ts"; |
| 11 | + |
| 12 | +type Snapshot = { |
| 13 | + block: bigint; |
| 14 | + index: bigint; |
| 15 | +}; |
| 16 | + |
| 17 | +describe("SSVClusters legacy SSV accounting", async () => { |
| 18 | + let connection: NetworkConnection<"generic">; |
| 19 | + let networkHelpers: NetworkHelpersType; |
| 20 | + let clusterOwner: HardhatEthersSigner; |
| 21 | + |
| 22 | + before(async function () { |
| 23 | + ({ connection, networkHelpers } = await getTestConnection()); |
| 24 | + [clusterOwner] = await connection.ethers.getSigners(); |
| 25 | + }); |
| 26 | + |
| 27 | + const deployLegacySSVFixture = async (operatorFeeRaw: bigint, networkFeeRaw: bigint) => { |
| 28 | + const { clusters, operatorIds } = await ssvClustersHarnessFixture(connection); |
| 29 | + const operatorFeeUnpacked = operatorFeeRaw * DEDUCTED_DIGITS; |
| 30 | + |
| 31 | + for (const operatorId of operatorIds) { |
| 32 | + await clusters.mockOperatorSSVFee(operatorId, operatorFeeUnpacked); |
| 33 | + } |
| 34 | + |
| 35 | + await clusters.mockSSVNetworkFee(networkFeeRaw); |
| 36 | + const networkFeeIndexTx = await clusters.mockCurrentNetworkFeeIndexSSV(0n); |
| 37 | + const networkFeeIndexReceipt = await networkFeeIndexTx.wait(); |
| 38 | + |
| 39 | + return { |
| 40 | + clusters, |
| 41 | + operatorIds, |
| 42 | + networkFeeIndexBlock: BigInt(networkFeeIndexReceipt!.blockNumber), |
| 43 | + }; |
| 44 | + }; |
| 45 | + |
| 46 | + const deployOperatorFeeFixture = async () => deployLegacySSVFixture(2_000n, 0n); |
| 47 | + const deployNetworkFeeFixture = async () => deployLegacySSVFixture(0n, 75n); |
| 48 | + |
| 49 | + const createLegacySSVCluster = (overrides: Partial<Cluster> = {}): Cluster => |
| 50 | + createCluster({ |
| 51 | + validatorCount: 2n, |
| 52 | + index: 0n, |
| 53 | + networkFeeIndex: 0n, |
| 54 | + balance: ethers.parseEther("100"), |
| 55 | + ...overrides, |
| 56 | + }); |
| 57 | + |
| 58 | + const captureSnapshots = async (clusters: any, operatorIds: bigint[]): Promise<Snapshot[]> => |
| 59 | + Promise.all( |
| 60 | + operatorIds.map(async (operatorId) => { |
| 61 | + const [index, blockNumber] = await clusters.getOperatorSnapshot(operatorId); |
| 62 | + return { |
| 63 | + block: BigInt(blockNumber), |
| 64 | + index: BigInt(index), |
| 65 | + }; |
| 66 | + }) |
| 67 | + ); |
| 68 | + |
| 69 | + const calculateClusterIndex = (snapshots: Snapshot[], currentBlock: bigint, operatorFeeRaw: bigint): bigint => |
| 70 | + snapshots.reduce( |
| 71 | + (sum, snapshot) => sum + snapshot.index + (currentBlock - snapshot.block) * operatorFeeRaw, |
| 72 | + 0n |
| 73 | + ); |
| 74 | + |
| 75 | + const calculateNetworkFeeIndex = ( |
| 76 | + currentBlock: bigint, |
| 77 | + feeIndexBlock: bigint, |
| 78 | + networkFeeRaw: bigint |
| 79 | + ): bigint => (currentBlock - feeIndexBlock) * networkFeeRaw; |
| 80 | + |
| 81 | + const calculateSettledFees = ( |
| 82 | + cluster: Cluster, |
| 83 | + currentClusterIndex: bigint, |
| 84 | + currentNetworkFeeIndex: bigint |
| 85 | + ): bigint => |
| 86 | + ( |
| 87 | + (currentClusterIndex - cluster.index) * BigInt(cluster.validatorCount) + |
| 88 | + (currentNetworkFeeIndex - cluster.networkFeeIndex) * BigInt(cluster.validatorCount) |
| 89 | + ) * DEDUCTED_DIGITS; |
| 90 | + |
| 91 | + it("removeValidator settles accrued legacy SSV operator fees before decrementing validator count", async function () { |
| 92 | + const operatorFeeRaw = 2_000n; |
| 93 | + const { clusters, operatorIds, networkFeeIndexBlock } = |
| 94 | + await networkHelpers.loadFixture(deployOperatorFeeFixture); |
| 95 | + |
| 96 | + const [publicKey1, publicKey2] = makePublicKeys(2); |
| 97 | + const cluster = createLegacySSVCluster({ validatorCount: 2n }); |
| 98 | + |
| 99 | + await clusters.mockRegisterSSVValidator(publicKey1, operatorIds, clusterOwner.address, cluster); |
| 100 | + await clusters.mockRegisterSSVValidator(publicKey2, operatorIds, clusterOwner.address, cluster); |
| 101 | + |
| 102 | + const snapshots = await captureSnapshots(clusters, operatorIds); |
| 103 | + |
| 104 | + await networkHelpers.mine(25); |
| 105 | + |
| 106 | + const removeTx = await clusters.connect(clusterOwner).removeValidator(publicKey1, operatorIds, cluster); |
| 107 | + const removeReceipt = await removeTx.wait(); |
| 108 | + const clusterAfterRemove = parseClusterFromEvent(clusters, removeReceipt, Events.VALIDATOR_REMOVED); |
| 109 | + const removeBlock = BigInt(removeReceipt!.blockNumber); |
| 110 | + |
| 111 | + const expectedClusterIndex = calculateClusterIndex(snapshots, removeBlock, operatorFeeRaw); |
| 112 | + const expectedNetworkFeeIndex = calculateNetworkFeeIndex(removeBlock, networkFeeIndexBlock, 0n); |
| 113 | + const expectedFees = calculateSettledFees(cluster, expectedClusterIndex, expectedNetworkFeeIndex); |
| 114 | + |
| 115 | + expect(clusterAfterRemove.validatorCount).to.equal(1n); |
| 116 | + expect(clusterAfterRemove.index).to.equal(expectedClusterIndex); |
| 117 | + expect(clusterAfterRemove.networkFeeIndex).to.equal(expectedNetworkFeeIndex); |
| 118 | + expect(clusterAfterRemove.balance).to.equal(cluster.balance - expectedFees); |
| 119 | + expect(expectedFees).to.equal(expectedClusterIndex * BigInt(cluster.validatorCount) * DEDUCTED_DIGITS); |
| 120 | + expect(expectedFees % DEDUCTED_DIGITS).to.equal(0n); |
| 121 | + }); |
| 122 | + |
| 123 | + it("bulkRemoveValidator settles legacy SSV network fees on active clusters", async function () { |
| 124 | + const networkFeeRaw = 75n; |
| 125 | + const { clusters, operatorIds, networkFeeIndexBlock } = |
| 126 | + await networkHelpers.loadFixture(deployNetworkFeeFixture); |
| 127 | + |
| 128 | + const publicKeys = makePublicKeys(3, 11); |
| 129 | + const cluster = createLegacySSVCluster({ |
| 130 | + validatorCount: 3n, |
| 131 | + balance: ethers.parseEther("60"), |
| 132 | + }); |
| 133 | + |
| 134 | + for (const publicKey of publicKeys) { |
| 135 | + await clusters.mockRegisterSSVValidator(publicKey, operatorIds, clusterOwner.address, cluster); |
| 136 | + } |
| 137 | + |
| 138 | + const snapshots = await captureSnapshots(clusters, operatorIds); |
| 139 | + |
| 140 | + await networkHelpers.mine(40); |
| 141 | + |
| 142 | + const removeTx = await clusters.connect(clusterOwner).bulkRemoveValidator( |
| 143 | + [publicKeys[0], publicKeys[1]], |
| 144 | + operatorIds, |
| 145 | + cluster |
| 146 | + ); |
| 147 | + const removeReceipt = await removeTx.wait(); |
| 148 | + const clusterAfterRemove = parseClusterFromEvent(clusters, removeReceipt, Events.VALIDATOR_REMOVED); |
| 149 | + const removeBlock = BigInt(removeReceipt!.blockNumber); |
| 150 | + |
| 151 | + const expectedClusterIndex = calculateClusterIndex(snapshots, removeBlock, 0n); |
| 152 | + const expectedNetworkFeeIndex = calculateNetworkFeeIndex(removeBlock, networkFeeIndexBlock, networkFeeRaw); |
| 153 | + const expectedFees = calculateSettledFees(cluster, expectedClusterIndex, expectedNetworkFeeIndex); |
| 154 | + |
| 155 | + expect(expectedClusterIndex).to.equal(0n); |
| 156 | + expect(clusterAfterRemove.validatorCount).to.equal(1n); |
| 157 | + expect(clusterAfterRemove.index).to.equal(0n); |
| 158 | + expect(clusterAfterRemove.networkFeeIndex).to.equal(expectedNetworkFeeIndex); |
| 159 | + expect(clusterAfterRemove.balance).to.equal(cluster.balance - expectedFees); |
| 160 | + expect(expectedFees).to.equal(expectedNetworkFeeIndex * BigInt(cluster.validatorCount) * DEDUCTED_DIGITS); |
| 161 | + }); |
| 162 | +}); |
0 commit comments