Skip to content

Commit 1010564

Browse files
committed
test: refactor block advancer and use 100ms block time
1 parent 0602080 commit 1010564

File tree

4 files changed

+76
-67
lines changed

4 files changed

+76
-67
lines changed

src/integrationTestHelpers/anvilHarness.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
469469
parentChainBeaconRpcUrl: sepoliaBeaconRpc,
470470
});
471471

472+
if (l2NodeConfig.execution === undefined || l2NodeConfig.execution.sequencer === undefined) {
473+
throw new Error('L2 node config execution is undefined');
474+
}
475+
476+
l2NodeConfig.execution.sequencer['max-block-speed'] = '100ms';
477+
472478
if (
473479
l2NodeConfig.node === undefined ||
474480
l2NodeConfig.node['batch-poster'] === undefined ||
@@ -558,6 +564,15 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
558564
}),
559565
account: l2BlockAdvancerAccount,
560566
});
567+
const fundL2BlockAdvancerTxHash = await l2WalletClient.sendTransaction({
568+
account: harnessDeployer,
569+
chain: l2BootstrapChain,
570+
to: l2BlockAdvancerAccount.address,
571+
value: parseEther('1'),
572+
});
573+
await l2Client.waitForTransactionReceipt({ hash: fundL2BlockAdvancerTxHash });
574+
startBlockAdvancing(l2BlockAdvancer);
575+
persistentBlockAdvancers.push(l2BlockAdvancer);
561576

562577
console.log('Configuring L2 fee settings...');
563578
await configureL2Fees(l2Client, l2WalletClient, harnessDeployer);
@@ -584,13 +599,6 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
584599

585600
console.log('L2 custom gas token deployed\n');
586601

587-
const tx = await l2Signer.sendTransaction({
588-
to: l2BlockAdvancerAccount.address,
589-
value: parseEther('1'),
590-
...testConstants.LOW_L2_FEE_OVERRIDES,
591-
});
592-
await tx.wait();
593-
594602
console.log('Deploying L2 rollup creator...');
595603
const l2RollupCreator = await deployRollupCreator({
596604
rpcUrl: l2RpcUrl,
@@ -691,6 +699,12 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
691699
parentChainRpcUrl: `http://${l2ContainerName}:${l2RpcPort}`,
692700
});
693701

702+
if (l3NodeConfig.execution === undefined || l3NodeConfig.execution.sequencer === undefined) {
703+
throw new Error('L3 node config execution is undefined');
704+
}
705+
706+
l3NodeConfig.execution.sequencer['max-block-speed'] = '100ms';
707+
694708
if (
695709
l3NodeConfig.node === undefined ||
696710
l3NodeConfig.node['batch-poster'] === undefined ||
@@ -743,11 +757,13 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
743757
failIfContainerExited: l3ContainerName,
744758
});
745759
console.log('L3 Nitro node is ready\n');
760+
746761
const l3Client = createPublicClient({
747762
chain: l3Chain,
748763
transport: http(l3RpcUrl),
749764
pollingInterval: testConstants.POLLING_INTERVAL,
750765
});
766+
751767
const l3WalletClient = createWalletClient({
752768
chain: l3Chain,
753769
transport: http(l3RpcUrl),
@@ -774,6 +790,17 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
774790
});
775791
console.log('Deployer funded on L3\n');
776792

793+
const l3BlockAdvancer = createBlockAdvancer({
794+
publicClient: l3Client,
795+
walletClient: createWalletClient({
796+
chain: l3Chain,
797+
transport: http(l3RpcUrl),
798+
account: l3BlockAdvancerAccount,
799+
pollingInterval: testConstants.POLLING_INTERVAL,
800+
}),
801+
account: l3BlockAdvancerAccount,
802+
});
803+
777804
console.log('Funding block advancer on L3...');
778805
const fundL3BlockAdvancerTxHash = await l3WalletClient.sendTransaction({
779806
account: harnessDeployer,
@@ -784,6 +811,9 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
784811
await l3Client.waitForTransactionReceipt({ hash: fundL3BlockAdvancerTxHash });
785812
console.log('Block advancer funded on L3\n');
786813

814+
startBlockAdvancing(l3BlockAdvancer);
815+
persistentBlockAdvancers.push(l3BlockAdvancer);
816+
787817
console.log('Deploying L3 token bridge contracts on L2...');
788818
const { tokenBridgeContracts } = await createTokenBridge({
789819
rollupOwner: harnessDeployer.address,
@@ -820,29 +850,6 @@ export async function setupAnvilTestStack(): Promise<AnvilTestStack> {
820850
});
821851
console.log('L3 token bridge contracts deployed on L2\n');
822852

823-
l2BlockAdvancer.publicClient = l2Client;
824-
l2BlockAdvancer.walletClient = createWalletClient({
825-
chain: l2Chain,
826-
transport: http(l2RpcUrl),
827-
account: l2BlockAdvancerAccount,
828-
pollingInterval: testConstants.POLLING_INTERVAL,
829-
});
830-
831-
const l3BlockAdvancer = createBlockAdvancer({
832-
publicClient: l3Client,
833-
walletClient: createWalletClient({
834-
chain: l3Chain,
835-
transport: http(l3RpcUrl),
836-
account: l3BlockAdvancerAccount,
837-
pollingInterval: testConstants.POLLING_INTERVAL,
838-
}),
839-
account: l3BlockAdvancerAccount,
840-
});
841-
842-
startBlockAdvancing(l2BlockAdvancer);
843-
startBlockAdvancing(l3BlockAdvancer);
844-
persistentBlockAdvancers.push(l2BlockAdvancer, l3BlockAdvancer);
845-
846853
initializedEnv = {
847854
l1: {
848855
rpcUrl: l1RpcUrl,
@@ -896,23 +903,24 @@ export function getInitializedAnvilTestStackEnv(): AnvilTestStack {
896903
return initializedEnv;
897904
}
898905

899-
export function teardownAnvilTestStack() {
906+
export async function teardownAnvilTestStack() {
900907
if (teardownStarted) {
901908
return;
902909
}
903910
teardownStarted = true;
904911

905-
for (const blockAdvancer of persistentBlockAdvancers) {
906-
void blockAdvancer.stop();
907-
}
912+
const blockAdvancers = persistentBlockAdvancers;
908913
persistentBlockAdvancers = [];
909914

915+
await Promise.all(blockAdvancers.map((blockAdvancer) => blockAdvancer.stop()));
916+
910917
if (l1RpcCachingProxy) {
911918
for (const line of l1RpcCachingProxy.getSummaryLines()) {
912919
console.log(line);
913920
}
914921

915-
l1RpcCachingProxy.close();
922+
await l1RpcCachingProxy.close();
923+
l1RpcCachingProxy = undefined;
916924
}
917925

918926
cleanupCurrentHarnessResources({

src/integrationTestHelpers/anvilHarnessHelpers.ts

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ export function startBlockAdvancing(blockAdvancer: BlockAdvancer): void {
428428
return;
429429
}
430430

431+
const blockAdvanceIntervalMs = 100;
431432
let keepAdvancingBlocks = true;
432433
const state: BlockAdvancingState = {
433434
stopAdvancing() {
@@ -439,13 +440,18 @@ export function startBlockAdvancing(blockAdvancer: BlockAdvancer): void {
439440
state.done = (async () => {
440441
try {
441442
while (keepAdvancingBlocks) {
443+
const startedAt = Date.now();
444+
442445
try {
443446
await advanceBlock(blockAdvancer);
444447
} catch {
445448
// ignore and keep advancing blocks
446449
}
447450

448-
await sleep(100);
451+
const remainingDelayMs = blockAdvanceIntervalMs - (Date.now() - startedAt);
452+
if (remainingDelayMs > 0) {
453+
await sleep(remainingDelayMs);
454+
}
449455
}
450456
} finally {
451457
if (blockAdvancingStates.get(blockAdvancer) === state) {
@@ -457,33 +463,18 @@ export function startBlockAdvancing(blockAdvancer: BlockAdvancer): void {
457463
blockAdvancingStates.set(blockAdvancer, state);
458464
}
459465

460-
async function withBlockAdvancing<T>(
461-
blockAdvancer: BlockAdvancer,
462-
fn: () => Promise<T>,
463-
): Promise<T> {
464-
startBlockAdvancing(blockAdvancer);
465-
466-
try {
467-
return await fn();
468-
} finally {
469-
await blockAdvancer.stop();
470-
}
471-
}
472-
473466
export async function deployRollupCreator(params: DeployRollupCreatorParams): Promise<Address> {
474467
const intTestContractsImage = getIntTestContractsImage();
475-
const stdout = await withBlockAdvancing(params.blockAdvancer, () =>
476-
dockerAsync(
477-
getRollupCreatorDockerArgs(
478-
{
479-
rpcUrl: params.rpcUrl.replace(new URL(params.rpcUrl).hostname, 'host.docker.internal'),
480-
deployerPrivateKey: params.deployerPrivateKey,
481-
factoryOwner: params.factoryOwner,
482-
maxDataSize: params.maxDataSize,
483-
chainId: params.chainId,
484-
},
485-
intTestContractsImage,
486-
),
468+
const stdout = await dockerAsync(
469+
getRollupCreatorDockerArgs(
470+
{
471+
rpcUrl: params.rpcUrl.replace(new URL(params.rpcUrl).hostname, 'host.docker.internal'),
472+
deployerPrivateKey: params.deployerPrivateKey,
473+
factoryOwner: params.factoryOwner,
474+
maxDataSize: params.maxDataSize,
475+
chainId: params.chainId,
476+
},
477+
intTestContractsImage,
487478
),
488479
);
489480

@@ -505,9 +496,8 @@ export async function deployTokenBridgeCreator(
505496
),
506497
);
507498

508-
const stdout = params.blockAdvancer
509-
? await withBlockAdvancing(params.blockAdvancer, deploy)
510-
: await deploy();
499+
const stdout = await deploy();
500+
511501
const address = parseTokenBridgeCreatorAddress(stdout);
512502
const chain = rpcUrlToChain.get(params.rpcUrl);
513503

src/integrationTestHelpers/globalSetup.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export async function setup(project) {
99
project.provide('anvilTestStack', structuredClone(dehydrateAnvilTestStack(env)));
1010

1111
return async () => {
12-
teardownAnvilTestStack();
12+
await teardownAnvilTestStack();
1313
};
1414
}

src/integrationTestHelpers/rpcCachingProxy.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CacheFile = {
3737

3838
export type RpcCachingProxy = {
3939
proxyUrl: string;
40-
close: () => void;
40+
close: () => Promise<void>;
4141
getSummaryLines: () => string[];
4242
};
4343

@@ -243,6 +243,17 @@ export async function startRpcCachingProxy(
243243
` cache misses: ${stats.cacheMisses}`,
244244
` upstream HTTP requests: ${stats.upstreamRequests}`,
245245
],
246-
close: () => server.close(),
246+
close: () =>
247+
new Promise((resolve, reject) => {
248+
server.closeAllConnections?.();
249+
server.close((error) => {
250+
if (error) {
251+
reject(error);
252+
return;
253+
}
254+
255+
resolve();
256+
});
257+
}),
247258
};
248259
}

0 commit comments

Comments
 (0)