Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 1 addition & 49 deletions yarn-project/end-to-end/src/e2e_p2p/p2p_network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { InitialAccountData } from '@aztec/accounts/testing';
import type { AztecNodeConfig, AztecNodeService } from '@aztec/aztec-node';
import type { AccountWalletWithSecretKey } from '@aztec/aztec.js';
import { RollupContract, getExpectedAddress, getL1ContractsConfigEnvVars } from '@aztec/ethereum';
import { L1TxUtilsWithBlobs } from '@aztec/ethereum/l1-tx-utils-with-blobs';
import { ChainMonitor, EthCheatCodesWithState } from '@aztec/ethereum/test';
import { type Logger, createLogger } from '@aztec/foundation/log';
import { ForwarderAbi, ForwarderBytecode, RollupAbi, TestERC20Abi } from '@aztec/l1-artifacts';
Expand Down Expand Up @@ -66,10 +65,6 @@ export class P2PNetworkTest {

public bootstrapNode?: BootstrapNode;

private cleanupInterval: NodeJS.Timeout | undefined = undefined;

private gasUtils: L1TxUtilsWithBlobs | undefined = undefined;

constructor(
testName: string,
public bootstrapNodeEnr: string,
Expand Down Expand Up @@ -147,32 +142,6 @@ export class P2PNetworkTest {
return this.deployedAccounts[0];
}

/**
* Start a loop to sync the mock system time with the L1 block time
*/
public startSyncMockSystemTimeInterval() {
this.cleanupInterval = setInterval(() => {
void this.syncMockSystemTime().catch(err => this.logger.error('Error syncing mock system time', err));
}, l1ContractsConfig.aztecSlotDuration * 1000);
}

/**
* When using fake timers, we need to keep the system and anvil clocks in sync.
*/
public async syncMockSystemTime() {
this.logger.info('Syncing mock system time');
const { dateProvider, deployL1ContractsValues } = this.ctx!;
// Send a tx and only update the time after the tx is mined, as eth time is not continuous
const { receipt } = await this.gasUtils!.sendAndMonitorTransaction({
to: this.baseAccount.address,
data: '0x',
value: 1n,
});
const timestamp = await deployL1ContractsValues.publicClient.getBlock({ blockNumber: receipt.blockNumber });
this.logger.info(`Timestamp: ${timestamp.timestamp}`);
dateProvider.setTime(Number(timestamp.timestamp) * 1000);
}

async addBootstrapNode() {
await this.snapshotManager.snapshot('add-bootstrap-node', async ({ aztecNodeConfig }) => {
const telemetry = getEndToEndTestTelemetryClient(this.metricsPort);
Expand Down Expand Up @@ -341,22 +310,8 @@ export class P2PNetworkTest {
const { prefilledPublicData } = await getGenesisValues(initialFundedAccounts);
this.prefilledPublicData = prefilledPublicData;

this.startSyncMockSystemTimeInterval();

this.gasUtils = new L1TxUtilsWithBlobs(
this.ctx.deployL1ContractsValues.publicClient,
this.ctx.deployL1ContractsValues.walletClient,
this.logger,
{
gasLimitBufferPercentage: 20,
maxGwei: 500n,
maxAttempts: 3,
checkIntervalMs: 100,
stallTimeMs: 1000,
},
);

this.monitor = new ChainMonitor(RollupContract.getFromL1ContractsValues(this.ctx.deployL1ContractsValues)).start();
this.monitor.on('l1-block', ({ timestamp }) => this.ctx.dateProvider.setTime(Number(timestamp) * 1000));
}

async stopNodes(nodes: AztecNodeService[]) {
Expand All @@ -376,8 +331,5 @@ export class P2PNetworkTest {
this.monitor.stop();
await this.bootstrapNode?.stop();
await this.snapshotManager.teardown();
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
}
}
}
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_p2p/reex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ describe('e2e_p2p_reex', () => {
}

// Start a fresh slot and resume proposals
await t.ctx.cheatCodes.rollup.advanceToNextSlot();
await t.syncMockSystemTime();
const [ts] = await t.ctx.cheatCodes.rollup.advanceToNextSlot();
t.ctx.dateProvider.setTime(Number(ts) * 1000);

await resumeProposals();

Expand Down
12 changes: 11 additions & 1 deletion yarn-project/ethereum/src/test/chain_monitor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { RollupContract } from '@aztec/ethereum/contracts';
import { createLogger } from '@aztec/foundation/log';

import { EventEmitter } from 'events';
import type { PublicClient } from 'viem';

/** Utility class that polls the chain on quick intervals and logs new L1 blocks, L2 blocks, and L2 proofs. */
export class ChainMonitor {
export class ChainMonitor extends EventEmitter {
private readonly l1Client: PublicClient;
private handle: NodeJS.Timeout | undefined;

Expand All @@ -24,6 +25,7 @@ export class ChainMonitor {
private logger = createLogger('aztecjs:utils:chain_monitor'),
private readonly intervalMs = 200,
) {
super();
this.l1Client = rollup.client;
}

Expand All @@ -36,6 +38,7 @@ export class ChainMonitor {
}

stop() {
this.removeAllListeners();
if (this.handle) {
clearInterval(this.handle!);
this.handle = undefined;
Expand All @@ -59,6 +62,7 @@ export class ChainMonitor {
const timestamp = block.timestamp;
const timestampString = new Date(Number(timestamp) * 1000).toTimeString().split(' ')[0];

this.emit('l1-block', { l1BlockNumber: newL1BlockNumber, timestamp });
let msg = `L1 block ${newL1BlockNumber} mined at ${timestampString}`;

const newL2BlockNumber = Number(await this.rollup.getBlockNumber());
Expand All @@ -67,6 +71,7 @@ export class ChainMonitor {
msg += ` with new L2 block ${newL2BlockNumber} for epoch ${epochNumber}`;
this.l2BlockNumber = newL2BlockNumber;
this.l2BlockTimestamp = timestamp;
this.emit('l2-block', { l2BlockNumber: newL2BlockNumber, l1BlockNumber: newL1BlockNumber, timestamp });
}

const newL2ProvenBlockNumber = Number(await this.rollup.getProvenBlockNumber());
Expand All @@ -75,6 +80,11 @@ export class ChainMonitor {
msg += ` with proof up to L2 block ${newL2ProvenBlockNumber} for epoch ${epochNumber}`;
this.l2ProvenBlockNumber = newL2ProvenBlockNumber;
this.l2ProvenBlockTimestamp = timestamp;
this.emit('l2-block-proven', {
l2ProvenBlockNumber: newL2ProvenBlockNumber,
l1BlockNumber: newL1BlockNumber,
timestamp,
});
}

this.logger.info(msg, {
Expand Down