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