|
1 | 1 | import { expect } from 'chai'; |
| 2 | +import { ethers } from 'hardhat'; |
2 | 3 |
|
3 | 4 | import { createInstances } from '../instance'; |
4 | 5 | import { getSigners, initSigners } from '../signers'; |
5 | | -import { getTxHCUFromTxReceipt } from '../utils'; |
| 6 | +import { getTxHCUFromTxReceipt, mineNBlocks } from '../utils'; |
6 | 7 | import { deployEncryptedERC20Fixture } from './EncryptedERC20.fixture'; |
7 | 8 |
|
| 9 | +// Minimal ABI for HCULimit — the contract is deployed by the host-sc stack |
| 10 | +// but not compiled in the E2E test suite. |
| 11 | +const HCU_LIMIT_ABI = [ |
| 12 | + 'function getBlockMeter() view returns (uint48, uint48)', |
| 13 | + 'function getGlobalHCUCapPerBlock() view returns (uint48)', |
| 14 | + 'function getMaxHCUPerTx() view returns (uint48)', |
| 15 | + 'function getMaxHCUDepthPerTx() view returns (uint48)', |
| 16 | + 'function setHCUPerBlock(uint48)', |
| 17 | + 'function setMaxHCUPerTx(uint48)', |
| 18 | + 'function setMaxHCUDepthPerTx(uint48)', |
| 19 | + 'function addToBlockHCUWhitelist(address)', |
| 20 | + 'function removeFromBlockHCUWhitelist(address)', |
| 21 | + 'function isBlockHCUWhitelisted(address) view returns (bool)', |
| 22 | + 'error NotHostOwner(address)', |
| 23 | +]; |
| 24 | + |
8 | 25 | describe('EncryptedERC20:HCU', function () { |
9 | 26 | before(async function () { |
10 | 27 | await initSigners(2); |
@@ -86,4 +103,212 @@ describe('EncryptedERC20:HCU', function () { |
86 | 103 | // Le euint64 (149000) + And ebool (25000) + Select euint64 (55000) + Sub euint64 (162000) |
87 | 104 | expect(HCUMaxDepthTransferFrom).to.eq(391_000, 'HCU Depth incorrect'); |
88 | 105 | }); |
| 106 | + |
| 107 | + describe('block cap scenarios', function () { |
| 108 | + let savedHCUPerBlock: bigint; |
| 109 | + let savedMaxHCUPerTx: bigint; |
| 110 | + let savedMaxHCUDepthPerTx: bigint; |
| 111 | + let wasWhitelisted: boolean; |
| 112 | + |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 114 | + async function sendEncryptedTransfer(ctx: any, sender: string, recipient: string, amount: number, overrides?: any) { |
| 115 | + const erc20 = ctx.erc20.connect(ctx.signers[sender]); |
| 116 | + const input = ctx.instances[sender].createEncryptedInput(ctx.contractAddress, ctx.signers[sender].address); |
| 117 | + input.add64(amount); |
| 118 | + const enc = await input.encrypt(); |
| 119 | + return erc20['transfer(address,bytes32,bytes)'](recipient, enc.handles[0], enc.inputProof, overrides ?? {}); |
| 120 | + } |
| 121 | + |
| 122 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 123 | + async function mintAndDistribute(ctx: any) { |
| 124 | + const mintTx = await ctx.erc20.mint(10000); |
| 125 | + await mintTx.wait(); |
| 126 | + const setupTx = await sendEncryptedTransfer(ctx, 'alice', ctx.signers.bob.address, 5000); |
| 127 | + await setupTx.wait(); |
| 128 | + } |
| 129 | + |
| 130 | + before(async function () { |
| 131 | + const hcuLimitAddress = process.env.HCU_LIMIT_CONTRACT_ADDRESS; |
| 132 | + if (!hcuLimitAddress) { |
| 133 | + throw new Error('HCU_LIMIT_CONTRACT_ADDRESS env var is required for block cap tests'); |
| 134 | + } |
| 135 | + this.hcuLimit = new ethers.Contract(hcuLimitAddress, HCU_LIMIT_ABI, ethers.provider); |
| 136 | + |
| 137 | + const deployerKey = process.env.DEPLOYER_PRIVATE_KEY; |
| 138 | + if (!deployerKey) { |
| 139 | + throw new Error('DEPLOYER_PRIVATE_KEY env var is required for block cap tests'); |
| 140 | + } |
| 141 | + this.deployer = new ethers.Wallet(deployerKey, ethers.provider); |
| 142 | + }); |
| 143 | + |
| 144 | + beforeEach(async function () { |
| 145 | + [savedHCUPerBlock, savedMaxHCUPerTx, savedMaxHCUDepthPerTx, wasWhitelisted] = await Promise.all([ |
| 146 | + this.hcuLimit.getGlobalHCUCapPerBlock(), |
| 147 | + this.hcuLimit.getMaxHCUPerTx(), |
| 148 | + this.hcuLimit.getMaxHCUDepthPerTx(), |
| 149 | + this.hcuLimit.isBlockHCUWhitelisted(this.contractAddress), |
| 150 | + ]); |
| 151 | + }); |
| 152 | + |
| 153 | + afterEach(async function () { |
| 154 | + // Restore automine + 1-second interval mining (Anvil --block-time 1) |
| 155 | + await ethers.provider.send('evm_setAutomine', [true]); |
| 156 | + await ethers.provider.send('evm_setIntervalMining', [1]); |
| 157 | + |
| 158 | + const ownerHcuLimit = this.hcuLimit.connect(this.deployer); |
| 159 | + await (await ownerHcuLimit.setHCUPerBlock(savedHCUPerBlock)).wait(); |
| 160 | + await (await ownerHcuLimit.setMaxHCUPerTx(savedMaxHCUPerTx)).wait(); |
| 161 | + await (await ownerHcuLimit.setMaxHCUDepthPerTx(savedMaxHCUDepthPerTx)).wait(); |
| 162 | + |
| 163 | + const isWhitelisted = await this.hcuLimit.isBlockHCUWhitelisted(this.contractAddress); |
| 164 | + if (wasWhitelisted && !isWhitelisted) { |
| 165 | + await (await ownerHcuLimit.addToBlockHCUWhitelist(this.contractAddress)).wait(); |
| 166 | + } else if (!wasWhitelisted && isWhitelisted) { |
| 167 | + await (await ownerHcuLimit.removeFromBlockHCUWhitelist(this.contractAddress)).wait(); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + it('should accumulate HCU from multiple users in the same block', async function () { |
| 172 | + await mintAndDistribute(this); |
| 173 | + await ethers.provider.send('evm_setIntervalMining', [0]); |
| 174 | + |
| 175 | + // Fresh block after setup: meter should be 0 |
| 176 | + await mineNBlocks(1); |
| 177 | + const [, meter0] = await this.hcuLimit.getBlockMeter(); |
| 178 | + expect(meter0).to.eq(0n); |
| 179 | + |
| 180 | + // Single tx (auto-mines its own block) |
| 181 | + const tx1 = await sendEncryptedTransfer(this, 'alice', this.signers.bob.address, 100); |
| 182 | + await tx1.wait(); |
| 183 | + const [, meter1] = await this.hcuLimit.getBlockMeter(); |
| 184 | + expect(meter1).to.be.greaterThan(meter0); |
| 185 | + |
| 186 | + // Two txs batched in same block — meter must exceed the single-tx reading |
| 187 | + // Disable automine and batch both txs into one block |
| 188 | + await ethers.provider.send('evm_setAutomine', [false]); |
| 189 | + const txA = await sendEncryptedTransfer(this, 'alice', this.signers.bob.address, 100); |
| 190 | + const txB = await sendEncryptedTransfer(this, 'bob', this.signers.alice.address, 100); |
| 191 | + await ethers.provider.send('evm_mine'); |
| 192 | + await ethers.provider.send('evm_setAutomine', [true]); |
| 193 | + const [receiptA, receiptB] = await Promise.all([txA.wait(), txB.wait()]); |
| 194 | + expect(receiptA?.status).to.eq(1); |
| 195 | + expect(receiptB?.status).to.eq(1); |
| 196 | + expect(receiptA?.blockNumber).to.eq(receiptB?.blockNumber); |
| 197 | + const [, meter2] = await this.hcuLimit.getBlockMeter(); |
| 198 | + expect(meter2).to.be.greaterThan(meter1); |
| 199 | + |
| 200 | + // Single tx in a new block — meter resets (lower than the two-tx block) |
| 201 | + const tx3 = await sendEncryptedTransfer(this, 'alice', this.signers.bob.address, 100); |
| 202 | + await tx3.wait(); |
| 203 | + const [, meter3] = await this.hcuLimit.getBlockMeter(); |
| 204 | + expect(meter3).to.be.greaterThan(0n); |
| 205 | + expect(meter3).to.be.lessThan(meter2); |
| 206 | + }); |
| 207 | + |
| 208 | + describe('with lowered limits', function () { |
| 209 | + const TIGHT_DEPTH_PER_TX = 400_000; |
| 210 | + const TIGHT_MAX_PER_TX = 600_000; |
| 211 | + const TIGHT_PER_BLOCK = 600_000; |
| 212 | + |
| 213 | + beforeEach(async function () { |
| 214 | + // Narrowest-first when lowering: hcuPerBlock >= maxHCUPerTx >= maxHCUDepthPerTx |
| 215 | + const ownerHcuLimit = this.hcuLimit.connect(this.deployer); |
| 216 | + await ownerHcuLimit.setMaxHCUDepthPerTx(TIGHT_DEPTH_PER_TX); |
| 217 | + await ownerHcuLimit.setMaxHCUPerTx(TIGHT_MAX_PER_TX); |
| 218 | + await ownerHcuLimit.setHCUPerBlock(TIGHT_PER_BLOCK); |
| 219 | + }); |
| 220 | + |
| 221 | + it('should revert when block HCU cap is exhausted', async function () { |
| 222 | + await mintAndDistribute(this); |
| 223 | + |
| 224 | + await mineNBlocks(1); |
| 225 | + await ethers.provider.send('evm_setIntervalMining', [0]); |
| 226 | + await ethers.provider.send('evm_setAutomine', [false]); |
| 227 | + |
| 228 | + // Alice fills the cap, Bob would push block total over — use fixed gasLimit |
| 229 | + // to bypass estimateGas (which reverts against pending state) |
| 230 | + const tx1 = await sendEncryptedTransfer(this, 'alice', this.signers.carol.address, 100); |
| 231 | + const tx2 = await sendEncryptedTransfer(this, 'bob', this.signers.carol.address, 100, { gasLimit: 1_000_000 }); |
| 232 | + |
| 233 | + await ethers.provider.send('evm_mine'); |
| 234 | + await ethers.provider.send('evm_setAutomine', [true]); |
| 235 | + await ethers.provider.send('evm_setIntervalMining', [1]); |
| 236 | + |
| 237 | + const receipt1 = await tx1.wait(); |
| 238 | + expect(receipt1?.status).to.eq(1, 'First transfer should succeed'); |
| 239 | + |
| 240 | + // Use getTransactionReceipt to avoid ethers throwing on reverted tx |
| 241 | + const receipt2 = await ethers.provider.getTransactionReceipt(tx2.hash); |
| 242 | + expect(receipt2?.status).to.eq(0, 'Second transfer should revert (block cap exceeded)'); |
| 243 | + expect(receipt1?.blockNumber).to.eq(receipt2?.blockNumber); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should allow previously blocked caller to succeed after block rollover', async function () { |
| 247 | + await mintAndDistribute(this); |
| 248 | + |
| 249 | + // Block N: alice fills the cap, bob gets blocked |
| 250 | + await mineNBlocks(1); |
| 251 | + await ethers.provider.send('evm_setIntervalMining', [0]); |
| 252 | + await ethers.provider.send('evm_setAutomine', [false]); |
| 253 | + |
| 254 | + const txAlice = await sendEncryptedTransfer(this, 'alice', this.signers.carol.address, 100); |
| 255 | + const txBob = await sendEncryptedTransfer(this, 'bob', this.signers.carol.address, 100, { gasLimit: 1_000_000 }); |
| 256 | + |
| 257 | + await ethers.provider.send('evm_mine'); |
| 258 | + await ethers.provider.send('evm_setAutomine', [true]); |
| 259 | + await ethers.provider.send('evm_setIntervalMining', [1]); |
| 260 | + |
| 261 | + const receiptAlice = await txAlice.wait(); |
| 262 | + expect(receiptAlice?.status).to.eq(1, 'Alice should succeed'); |
| 263 | + |
| 264 | + const receiptBob = await ethers.provider.getTransactionReceipt(txBob.hash); |
| 265 | + expect(receiptBob?.status).to.eq(0, 'Bob should be blocked in block N'); |
| 266 | + |
| 267 | + // Block N+1: meter resets, bob retries and succeeds |
| 268 | + await mineNBlocks(1); |
| 269 | + |
| 270 | + const [, usedHCUAfterReset] = await this.hcuLimit.getBlockMeter(); |
| 271 | + expect(usedHCUAfterReset).to.eq(0n, 'Meter should reset after new block'); |
| 272 | + |
| 273 | + const retryBob = await sendEncryptedTransfer(this, 'bob', this.signers.carol.address, 100); |
| 274 | + const receiptRetry = await retryBob.wait(); |
| 275 | + expect(receiptRetry?.status).to.eq(1, 'Bob should succeed after rollover'); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should count HCU after whitelist removal', async function () { |
| 280 | + const ownerHcuLimit = this.hcuLimit.connect(this.deployer); |
| 281 | + await ethers.provider.send('evm_setIntervalMining', [0]); |
| 282 | + |
| 283 | + const mintTx = await this.erc20.mint(10000); |
| 284 | + await mintTx.wait(); |
| 285 | + |
| 286 | + await (await ownerHcuLimit.addToBlockHCUWhitelist(this.contractAddress)).wait(); |
| 287 | + await mineNBlocks(1); |
| 288 | + |
| 289 | + // Transfer while whitelisted — meter stays at 0 |
| 290 | + const tx1 = await sendEncryptedTransfer(this, 'alice', this.signers.bob.address, 100); |
| 291 | + await tx1.wait(); |
| 292 | + |
| 293 | + const [, usedHCUWhitelisted] = await this.hcuLimit.getBlockMeter(); |
| 294 | + expect(usedHCUWhitelisted).to.eq(0n, 'Whitelisted contract should not count HCU'); |
| 295 | + |
| 296 | + await (await ownerHcuLimit.removeFromBlockHCUWhitelist(this.contractAddress)).wait(); |
| 297 | + |
| 298 | + // Transfer after removal — meter should count HCU |
| 299 | + const tx2 = await sendEncryptedTransfer(this, 'alice', this.signers.bob.address, 100); |
| 300 | + await tx2.wait(); |
| 301 | + |
| 302 | + const [, usedHCUAfterRemoval] = await this.hcuLimit.getBlockMeter(); |
| 303 | + expect(usedHCUAfterRemoval).to.be.greaterThan(0n, 'Should count HCU after whitelist removal'); |
| 304 | + }); |
| 305 | + |
| 306 | + it('should reject setHCUPerBlock from non-owner', async function () { |
| 307 | + const aliceHcuLimit = this.hcuLimit.connect(this.signers.alice); |
| 308 | + await expect(aliceHcuLimit.setHCUPerBlock(1_000_000)).to.be.revertedWithCustomError( |
| 309 | + this.hcuLimit, |
| 310 | + 'NotHostOwner', |
| 311 | + ); |
| 312 | + }); |
| 313 | + }); |
89 | 314 | }); |
0 commit comments