Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Mar 1, 2025
1 parent d05edfd commit 9e603da
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
1 change: 0 additions & 1 deletion yarn-project/p2p/src/client/p2p_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MockL2BlockSource } from '@aztec/archiver/test';
import { Fr } from '@aztec/foundation/fields';
import { sleep } from '@aztec/foundation/sleep';
import { L2Block } from '@aztec/stdlib/block';
import { P2PClientType } from '@aztec/stdlib/p2p';
import { mockTx } from '@aztec/stdlib/testing';

Expand Down
45 changes: 38 additions & 7 deletions yarn-project/p2p/src/client/p2p_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
/** How many slots to keep proven txs for. */
private keepProvenTxsFor: number;

// Store event handler references
private eventHandlers: {
handleChainPruned: (event: L2BlockSourceChainPrunedEvent) => void;
handleBlocksAdded: (blocks: L2Block[]) => void;
handleChainProven: (event: L2BlockSourceChainProvenEvent) => void;
} | null = null;

/**
* In-memory P2P client constructor.
* @param store - The client's instance of the KV store.
Expand Down Expand Up @@ -217,9 +224,30 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
}

await this.p2pService.start();
this.l2BlockSource.on(L2BlockSourceEvents.ChainPruned, this.handlePruneL2Blocks.bind(this));
this.l2BlockSource.on(L2BlockSourceEvents.BlocksAdded, this.handleLatestL2Blocks.bind(this));
this.l2BlockSource.on(L2BlockSourceEvents.ChainProven, this.handleProvenL2Blocks.bind(this));

// Create wrapper functions that handle the promises
const handleChainPruned = (event: L2BlockSourceChainPrunedEvent) => {
this.handlePruneL2Blocks(event).catch(err => this.log.error(`Error handling chain pruned event: ${err}`));
};

const handleBlocksAdded = (blocks: L2Block[]) => {
this.handleLatestL2Blocks(blocks).catch(err => this.log.error(`Error handling blocks added event: ${err}`));
};

const handleChainProven = (event: L2BlockSourceChainProvenEvent) => {
this.handleProvenL2Blocks(event).catch(err => this.log.error(`Error handling chain proven event: ${err}`));
};

// Store references to the wrapper functions for later removal
this.eventHandlers = {
handleChainPruned,
handleBlocksAdded,
handleChainProven,
};

this.l2BlockSource.on(L2BlockSourceEvents.ChainPruned, handleChainPruned);
this.l2BlockSource.on(L2BlockSourceEvents.BlocksAdded, handleBlocksAdded);
this.l2BlockSource.on(L2BlockSourceEvents.ChainProven, handleChainProven);

this.setCurrentState(P2PClientState.RUNNING);
this.log.verbose(`Started p2p service`);
Expand All @@ -233,10 +261,13 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
this.log.debug('Stopping p2p client...');
await this.p2pService.stop();

// Remove event listeners
this.l2BlockSource.removeListener(L2BlockSourceEvents.ChainPruned, this.handlePruneL2Blocks.bind(this));
this.l2BlockSource.removeListener(L2BlockSourceEvents.BlocksAdded, this.handleLatestL2Blocks.bind(this));
this.l2BlockSource.removeListener(L2BlockSourceEvents.ChainProven, this.handleProvenL2Blocks.bind(this));
// Remove event listeners using the stored references
if (this.eventHandlers) {
this.l2BlockSource.removeListener(L2BlockSourceEvents.ChainPruned, this.eventHandlers.handleChainPruned);
this.l2BlockSource.removeListener(L2BlockSourceEvents.BlocksAdded, this.eventHandlers.handleBlocksAdded);
this.l2BlockSource.removeListener(L2BlockSourceEvents.ChainProven, this.eventHandlers.handleChainProven);
this.eventHandlers = null;
}

this.log.debug('Stopped p2p service');
await this.runningPromise;
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/prover-node/src/prover-coordination/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DataStoreConfig } from '@aztec/kv-store/config';
import { getVKTreeRoot } from '@aztec/noir-protocol-circuits-types/vk-tree';
import { createP2PClient } from '@aztec/p2p';
import { protocolContractTreeRoot } from '@aztec/protocol-contracts';
import type { L2BlockSourceEventEmitter } from '@aztec/stdlib/block';
import { createAztecNodeClient } from '@aztec/stdlib/interfaces/client';
import type { ProverCoordination, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
import { P2PClientType } from '@aztec/stdlib/p2p';
Expand Down Expand Up @@ -51,7 +52,7 @@ export async function createProverCoordination(
const p2pClient = await createP2PClient(
P2PClientType.Prover,
config,
deps.archiver,
deps.archiver as L2BlockSourceEventEmitter,
proofVerifier,
deps.worldStateSynchronizer,
deps.epochCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,7 @@ describe('sequencer', () => {
return Promise.resolve({ treeId, root: Fr.random().toBuffer(), size: 99n, depth: 5 });
});

p2p = mock<P2P>({
getStatus: mockFn().mockResolvedValue({
state: P2PClientState.IDLE,
syncedToL2Block: { number: lastBlockNumber, hash },
}),
});
p2p = mock<P2P>();

fork = mock<MerkleTreeWriteOperations>({
getInitialHeader: () => initialBlockHeader,
Expand Down Expand Up @@ -503,7 +498,6 @@ describe('sequencer', () => {
} as WorldStateSyncStatus,
}),
);
p2p.getStatus.mockImplementation(() => Promise.resolve({ state: P2PClientState.IDLE, syncedToL2Block }));
l2BlockSource.getL2Tips.mockImplementation(() =>
Promise.resolve({
latest: syncedToL2Block,
Expand Down

0 comments on commit 9e603da

Please sign in to comment.