Skip to content

Commit b166cfc

Browse files
authored
Merge pull request #795 from balancer/fix-broken-create-tests
Fix broken create tests
2 parents 5102554 + 863fd25 commit b166cfc

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

test/anvil/anvil-global-setup.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ function getAnvilOptions(
162162
forkUrl,
163163
port,
164164
forkBlockNumber,
165+
mnemonic:
166+
'you twelve word test phrase boat cat like this example dog car', // mnemonic for deterministic accounts - should not have delegated accounts
165167
};
166168
}
167169

170+
// Counter to ensure unique ports for each fork instance
171+
let forkCounter = 0;
172+
168173
// Controls the current running forks to avoid starting the same fork twice
169174
let runningForks: Record<number, Anvil> = {};
170175

@@ -177,9 +182,14 @@ export async function stopAnvilForks() {
177182
}),
178183
);
179184
runningForks = {};
185+
// Reset fork counter after stopping all forks to prevent unbounded growth
186+
forkCounter = 0;
180187
}
181188

182189
// Stop a specific anvil fork
190+
// Note: Since forks are no longer reused (each startFork() creates a new instance),
191+
// this function may not reliably find forks without tracking the exact port.
192+
// For cleanup, prefer using stopAnvilForks() which stops all forks.
183193
export async function stopAnvilFork(
184194
network: NetworkSetup,
185195
jobId = Number(process.env.VITEST_WORKER_ID) || 0,
@@ -188,8 +198,10 @@ export async function stopAnvilFork(
188198
const anvilOptions = getAnvilOptions(network, blockNumber);
189199

190200
const defaultAnvilPort = 8545;
201+
// Note: This port calculation doesn't account for forkCounter, so it may not find the fork
202+
// if it was created with a counter offset. This function is kept for compatibility but
203+
// stopAnvilForks() is recommended for cleanup.
191204
const port = (anvilOptions.port || defaultAnvilPort) + jobId;
192-
// Avoid starting fork if it was running already
193205
if (!runningForks[port]) return;
194206

195207
await runningForks[port].stop();
@@ -201,6 +213,9 @@ export async function stopAnvilFork(
201213
In vitest, each thread is assigned a unique, numerical id (`process.env.VITEST_POOL_ID`).
202214
When jobId is provided, the fork uses this id to create a different local rpc url (e.g. `http://127.0.0.1:<8545+jobId>/`
203215
so that tests can be run in parallel (depending on the number of threads of the host machine)
216+
217+
IMPORTANT: Each call to startFork() creates a new fork instance with a unique port.
218+
This ensures complete isolation between test suites, preventing state interference and snapshot collisions.
204219
*/
205220
export async function startFork(
206221
network: NetworkSetup,
@@ -212,7 +227,11 @@ export async function startFork(
212227
const anvilOptions = getAnvilOptions(network, blockNumber);
213228

214229
const defaultAnvilPort = 8545;
215-
const port = (anvilOptions.port || defaultAnvilPort) + jobId;
230+
const basePort = anvilOptions.port || defaultAnvilPort;
231+
// Calculate unique port: basePort + jobId + (forkCounter * 100)
232+
// This ensures each fork gets a unique port while maintaining network separation
233+
const port = basePort + jobId + forkCounter * 100;
234+
forkCounter++;
216235

217236
if (!anvilOptions.forkUrl) {
218237
throw Error(
@@ -221,10 +240,11 @@ export async function startFork(
221240
}
222241
const rpcUrl = `http://127.0.0.1:${port}`;
223242

224-
console.log('checking rpcUrl', port, runningForks);
225-
226-
// Avoid starting fork if it was running already
227-
if (runningForks[port]) return { rpcUrl };
243+
console.log('Starting new fork', {
244+
port,
245+
forkBlockNumber: blockNumber ?? anvilOptions.forkBlockNumber,
246+
runningForks: Object.keys(runningForks).length,
247+
});
228248

229249
// https://www.npmjs.com/package/@viem/anvil
230250
// Pass startTimeout directly to createAnvil - it defaults to 10_000ms

test/v3/createPool/liquidityBootstrapping/liquidityBootstrapping.integration.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ import { ANVIL_NETWORKS, startFork } from '../../../anvil/anvil-global-setup';
2626
import { doCreatePool } from '../../../lib/utils/createPoolHelper';
2727
import { TOKENS } from 'test/lib/utils/addresses';
2828
import { MAX_UINT256, PublicWalletClient } from '@/utils';
29-
import {
30-
lBPMigrationRouterAbi_V3,
31-
vaultExtensionAbi_V3,
32-
weightedPoolFactoryAbiExtended_V3,
33-
} from 'src/abi/';
29+
import { lBPMigrationRouterAbi_V3, vaultExtensionAbi_V3 } from 'src/abi/';
3430
import { assertInitPool } from 'test/lib/utils/initPoolHelper';
3531
import {
3632
setTokenBalances,
@@ -312,13 +308,14 @@ describe('create liquidityBootstrapping pool test', () => {
312308
);
313309

314310
// extract the address of the created weighted pool from the transaction receipt
311+
// The PoolMigrated event is emitted by the LBPMigrationRouter and contains the new weighted pool address
315312
const {
316-
args: { pool: newWeightedPoolAddress },
313+
args: { weightedPool: newWeightedPoolAddress },
317314
} = findEventInReceiptLogs({
318315
receipt: txOutput.transactionReceipt,
319-
eventName: 'PoolCreated',
320-
abi: weightedPoolFactoryAbiExtended_V3,
321-
to: AddressProvider.WeightedPoolFactory(chainId),
316+
eventName: 'PoolMigrated',
317+
abi: lBPMigrationRouterAbi_V3,
318+
to: AddressProvider.LBPoolMigrationRouter(chainId),
322319
});
323320

324321
expect(txOutput.balanceDeltas[0]).toBeGreaterThan(0n); // WETH

test/v3/createPool/stable/stable.integration.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// pnpm test -- v3/createPool/stable/stable.integration.test.ts
1+
// pnpm test test/v3/createPool/stable/stable.integration.test.ts
22

33
import {
44
Address,
@@ -35,7 +35,11 @@ import {
3535
} from 'test/lib/utils/helper';
3636
import { AddressProvider } from '@/entities/inputValidator/utils/addressProvider';
3737

38-
const { rpcUrl } = await startFork(ANVIL_NETWORKS.SEPOLIA);
38+
const { rpcUrl } = await startFork(
39+
ANVIL_NETWORKS.SEPOLIA,
40+
undefined,
41+
10140629n,
42+
);
3943
const protocolVersion = 3;
4044
const chainId = ChainId.SEPOLIA;
4145
const poolType = PoolType.Stable;

test/v3/createPool/stableSurge/stableSurge.integration.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// pnpm test -- v3/createPool/stableSurge/stableSurge.integration.test.ts
1+
// pnpm test test/v3/createPool/stableSurge/stableSurge.integration.test.ts
22
import {
33
Address,
44
createTestClient,
@@ -47,7 +47,11 @@ describe('create stableSurge pool test', () => {
4747
let poolAddress: Address;
4848

4949
beforeAll(async () => {
50-
({ rpcUrl } = await startFork(ANVIL_NETWORKS.SEPOLIA));
50+
({ rpcUrl } = await startFork(
51+
ANVIL_NETWORKS.SEPOLIA,
52+
undefined,
53+
10140629n,
54+
));
5155
client = createTestClient({
5256
mode: 'anvil',
5357
chain: CHAINS[chainId],

test/v3/createPool/weighted/weighted.integration.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// pnpm test -- v3/createPool/weighted/weighted.integration.test.ts
1+
// pnpm test test/v3/createPool/weighted/weighted.integration.test.ts
22

33
import {
44
Address,
@@ -35,7 +35,11 @@ import {
3535
} from 'test/lib/utils/helper';
3636
import { AddressProvider } from '@/entities/inputValidator/utils/addressProvider';
3737

38-
const { rpcUrl } = await startFork(ANVIL_NETWORKS.SEPOLIA);
38+
const { rpcUrl } = await startFork(
39+
ANVIL_NETWORKS.SEPOLIA,
40+
undefined,
41+
10140629n,
42+
);
3943
const protocolVersion = 3;
4044
const chainId = ChainId.SEPOLIA;
4145
const poolType = PoolType.Weighted;

vitest.config.mts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export default defineConfig(({ mode }) => {
2828
setupFiles: ['/test/vitest-setup.ts'],
2929
globals: true,
3030
pool: 'forks',
31+
// Limit concurrent forks to avoid overwhelming RPC providers with too many connections
32+
// Each fork creates its own Anvil instance which makes RPC calls
33+
poolOptions: {
34+
forks: {
35+
maxForks: 3,
36+
minForks: 1,
37+
},
38+
},
3139
// Uncomment to debug suite excluding some tests
3240
// exclude: ['test/*weighted*.integration.*', 'node_modules', 'dist'],
3341
// Uncomment to run integration tests sequentially

0 commit comments

Comments
 (0)