Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(p2p): refactor pools #9065

Merged
merged 9 commits into from
Oct 9, 2024
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
36 changes: 15 additions & 21 deletions yarn-project/p2p/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { type DataStoreConfig, createStore } from '@aztec/kv-store/utils';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { type AttestationPool } from '../attestation_pool/attestation_pool.js';
import { InMemoryAttestationPool } from '../attestation_pool/memory_attestation_pool.js';
import { P2PClient } from '../client/p2p_client.js';
import { type P2PConfig } from '../config.js';
import { type EpochProofQuotePool } from '../epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { MemoryEpochProofQuotePool } from '../epoch_proof_quote_pool/memory_epoch_proof_quote_pool.js';
import { type AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
import { InMemoryAttestationPool } from '../mem_pools/attestation_pool/memory_attestation_pool.js';
import { type EpochProofQuotePool } from '../mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { MemoryEpochProofQuotePool } from '../mem_pools/epoch_proof_quote_pool/memory_epoch_proof_quote_pool.js';
import { type MemPools } from '../mem_pools/interface.js';
import { AztecKVTxPool, type TxPool } from '../mem_pools/tx_pool/index.js';
import { DiscV5Service } from '../service/discV5_service.js';
import { DummyP2PService } from '../service/dummy_service.js';
import { LibP2PService, createLibP2PPeerId } from '../service/index.js';
import { AztecKVTxPool, type TxPool } from '../tx_pool/index.js';
import { getPublicIp, resolveAddressIfNecessary, splitAddressPort } from '../util.js';

export * from './p2p_client.js';
Expand All @@ -28,15 +29,18 @@ export const createP2PClient = async (
deps: {
txPool?: TxPool;
store?: AztecKVStore;
attestationsPool?: AttestationPool;
attestationPool?: AttestationPool;
epochProofQuotePool?: EpochProofQuotePool;
} = {},
) => {
let config = { ..._config };
const store = deps.store ?? (await createStore('p2p', config, createDebugLogger('aztec:p2p:lmdb')));
const txPool = deps.txPool ?? new AztecKVTxPool(store, telemetry);
const attestationsPool = deps.attestationsPool ?? new InMemoryAttestationPool();
const epochProofQuotePool = deps.epochProofQuotePool ?? new MemoryEpochProofQuotePool();

const mempools: MemPools = {
txPool: deps.txPool ?? new AztecKVTxPool(store, telemetry),
attestationPool: deps.attestationPool ?? new InMemoryAttestationPool(),
epochProofQuotePool: deps.epochProofQuotePool ?? new MemoryEpochProofQuotePool(),
};

let p2pService;

Expand All @@ -51,9 +55,7 @@ export const createP2PClient = async (
config,
discoveryService,
peerId,
txPool,
attestationsPool,
epochProofQuotePool,
mempools,
l2BlockSource,
proofVerifier,
worldStateSynchronizer,
Expand All @@ -62,15 +64,7 @@ export const createP2PClient = async (
} else {
p2pService = new DummyP2PService();
}
return new P2PClient(
store,
l2BlockSource,
txPool,
attestationsPool,
epochProofQuotePool,
p2pService,
config.keepProvenTxsInPoolFor,
);
return new P2PClient(store, l2BlockSource, mempools, p2pService, config.keepProvenTxsInPoolFor);
};

async function configureP2PClientAddresses(_config: P2PConfig & DataStoreConfig): Promise<P2PConfig & DataStoreConfig> {
Expand Down
22 changes: 15 additions & 7 deletions yarn-project/p2p/src/client/p2p_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { openTmpStore } from '@aztec/kv-store/utils';

import { expect, jest } from '@jest/globals';

import { type AttestationPool } from '../attestation_pool/attestation_pool.js';
import { type EpochProofQuotePool, type P2PService } from '../index.js';
import { type TxPool } from '../tx_pool/index.js';
import { type AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
import { type MemPools } from '../mem_pools/interface.js';
import { type TxPool } from '../mem_pools/tx_pool/index.js';
import { P2PClient } from './p2p_client.js';

/**
Expand All @@ -22,6 +23,7 @@ describe('In-Memory P2P Client', () => {
let txPool: Mockify<TxPool>;
let attestationPool: Mockify<AttestationPool>;
let epochProofQuotePool: Mockify<EpochProofQuotePool>;
let mempools: MemPools;
let blockSource: MockBlockSource;
let p2pService: Mockify<P2PService>;
let kvStore: AztecKVStore;
Expand Down Expand Up @@ -64,8 +66,14 @@ describe('In-Memory P2P Client', () => {

blockSource = new MockBlockSource();

mempools = {
txPool,
attestationPool,
epochProofQuotePool,
};

kvStore = openTmpStore();
client = new P2PClient(kvStore, blockSource, txPool, attestationPool, epochProofQuotePool, p2pService, 0);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
});

const advanceToProvenBlock = async (getProvenBlockNumber: number, provenEpochNumber = getProvenBlockNumber) => {
Expand Down Expand Up @@ -135,7 +143,7 @@ describe('In-Memory P2P Client', () => {
await client.start();
await client.stop();

const client2 = new P2PClient(kvStore, blockSource, txPool, attestationPool, epochProofQuotePool, p2pService, 0);
const client2 = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
expect(client2.getSyncedLatestBlockNum()).toEqual(client.getSyncedLatestBlockNum());
});

Expand All @@ -150,7 +158,7 @@ describe('In-Memory P2P Client', () => {
});

it('deletes txs after waiting the set number of blocks', async () => {
client = new P2PClient(kvStore, blockSource, txPool, attestationPool, epochProofQuotePool, p2pService, 10);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10);
blockSource.setProvenBlockNumber(0);
await client.start();
expect(txPool.deleteTxs).not.toHaveBeenCalled();
Expand All @@ -167,7 +175,7 @@ describe('In-Memory P2P Client', () => {
});

it('stores and returns epoch proof quotes', async () => {
client = new P2PClient(kvStore, blockSource, txPool, attestationPool, epochProofQuotePool, p2pService, 0);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);

blockSource.setProvenEpochNumber(2);
await client.start();
Expand Down Expand Up @@ -198,7 +206,7 @@ describe('In-Memory P2P Client', () => {
});

it('deletes expired proof quotes', async () => {
client = new P2PClient(kvStore, blockSource, txPool, attestationPool, epochProofQuotePool, p2pService, 0);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);

blockSource.setProvenEpochNumber(1);
blockSource.setProvenBlockNumber(1);
Expand Down
19 changes: 13 additions & 6 deletions yarn-project/p2p/src/client/p2p_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { type AztecKVStore, type AztecSingleton } from '@aztec/kv-store';

import { type ENR } from '@chainsafe/enr';

import { type AttestationPool } from '../attestation_pool/attestation_pool.js';
import { getP2PConfigEnvVars } from '../config.js';
import { type EpochProofQuotePool } from '../epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
import { type EpochProofQuotePool } from '../mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type MemPools } from '../mem_pools/interface.js';
import { type TxPool } from '../mem_pools/tx_pool/index.js';
import { TX_REQ_PROTOCOL } from '../service/reqresp/interface.js';
import type { P2PService } from '../service/service.js';
import { type TxPool } from '../tx_pool/index.js';

/**
* Enum defining the possible states of the p2p client.
Expand Down Expand Up @@ -190,6 +191,10 @@ export class P2PClient implements P2P {
private synchedLatestBlockNumber: AztecSingleton<number>;
private synchedProvenBlockNumber: AztecSingleton<number>;

private txPool: TxPool;
private attestationPool: AttestationPool;
private epochProofQuotePool: EpochProofQuotePool;

/**
* In-memory P2P client constructor.
* @param store - The client's instance of the KV store.
Expand All @@ -202,9 +207,7 @@ export class P2PClient implements P2P {
constructor(
store: AztecKVStore,
private l2BlockSource: L2BlockSource,
private txPool: TxPool,
private attestationPool: AttestationPool,
private epochProofQuotePool: EpochProofQuotePool,
mempools: MemPools,
private p2pService: P2PService,
private keepProvenTxsFor: number,
private log = createDebugLogger('aztec:p2p'),
Expand All @@ -219,6 +222,10 @@ export class P2PClient implements P2P {

this.synchedLatestBlockNumber = store.openSingleton('p2p_pool_last_l2_block');
this.synchedProvenBlockNumber = store.openSingleton('p2p_pool_last_proven_l2_block');

this.txPool = mempools.txPool;
this.attestationPool = mempools.attestationPool;
this.epochProofQuotePool = mempools.epochProofQuotePool;
}

#assertIsReady() {
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/p2p/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './attestation_pool/index.js';
export * from './mem_pools/attestation_pool/index.js';
export * from './bootstrap/bootstrap.js';
export * from './client/index.js';
export * from './config.js';
export * from './epoch_proof_quote_pool/index.js';
export * from './mem_pools/epoch_proof_quote_pool/index.js';
export * from './service/index.js';
export * from './tx_pool/index.js';
export * from './mem_pools/tx_pool/index.js';
export * from './tx_validator/index.js';
4 changes: 4 additions & 0 deletions yarn-project/p2p/src/mem_pools/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { TxPool } from './tx_pool/tx_pool.js';
export { AttestationPool } from './attestation_pool/attestation_pool.js';
export { EpochProofQuotePool } from './epoch_proof_quote_pool/epoch_proof_quote_pool.js';
export { type MemPools } from './interface.js';
12 changes: 12 additions & 0 deletions yarn-project/p2p/src/mem_pools/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type AttestationPool } from './attestation_pool/attestation_pool.js';
import { type EpochProofQuotePool } from './epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type TxPool } from './tx_pool/tx_pool.js';

/**
* A interface the combines all mempools
*/
export interface MemPools {
txPool: TxPool;
attestationPool: AttestationPool;
epochProofQuotePool: EpochProofQuotePool;
}
24 changes: 8 additions & 16 deletions yarn-project/p2p/src/service/libp2p_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import { createFromJSON, createSecp256k1PeerId } from '@libp2p/peer-id-factory';
import { tcp } from '@libp2p/tcp';
import { createLibp2p } from 'libp2p';

import { type AttestationPool } from '../attestation_pool/attestation_pool.js';
import { type P2PConfig } from '../config.js';
import { type EpochProofQuotePool } from '../epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type TxPool } from '../tx_pool/index.js';
import { type MemPools } from '../mem_pools/interface.js';
import {
DataTxValidator,
DoubleSpendTxValidator,
Expand Down Expand Up @@ -98,9 +96,7 @@ export class LibP2PService implements P2PService {
private config: P2PConfig,
private node: PubSubLibp2p,
private peerDiscoveryService: PeerDiscoveryService,
private txPool: TxPool,
private attestationPool: AttestationPool,
private epochProofQuotePool: EpochProofQuotePool,
private mempools: MemPools,
private l2BlockSource: L2BlockSource,
private proofVerifier: ClientProtocolCircuitVerifier,
private worldStateSynchronizer: WorldStateSynchronizer,
Expand Down Expand Up @@ -203,9 +199,7 @@ export class LibP2PService implements P2PService {
config: P2PConfig,
peerDiscoveryService: PeerDiscoveryService,
peerId: PeerId,
txPool: TxPool,
attestationPool: AttestationPool,
epochProofQuotePool: EpochProofQuotePool,
mempools: MemPools,
l2BlockSource: L2BlockSource,
proofVerifier: ClientProtocolCircuitVerifier,
worldStateSynchronizer: WorldStateSynchronizer,
Expand Down Expand Up @@ -278,7 +272,7 @@ export class LibP2PService implements P2PService {
*/
const txHandler = (msg: Buffer): Promise<Uint8Array> => {
const txHash = TxHash.fromBuffer(msg);
const foundTx = txPool.getTxByHash(txHash);
const foundTx = mempools.txPool.getTxByHash(txHash);
const asUint8Array = Uint8Array.from(foundTx ? foundTx.toBuffer() : Buffer.alloc(0));
return Promise.resolve(asUint8Array);
};
Expand All @@ -293,9 +287,7 @@ export class LibP2PService implements P2PService {
config,
node,
peerDiscoveryService,
txPool,
attestationPool,
epochProofQuotePool,
mempools,
l2BlockSource,
proofVerifier,
worldStateSynchronizer,
Expand Down Expand Up @@ -392,7 +384,7 @@ export class LibP2PService implements P2PService {
*/
private async processAttestationFromPeer(attestation: BlockAttestation): Promise<void> {
this.logger.debug(`Received attestation ${attestation.p2pMessageIdentifier()} from external peer.`);
await this.attestationPool.addAttestations([attestation]);
await this.mempools.attestationPool.addAttestations([attestation]);
}

/**Process block from peer
Expand All @@ -415,7 +407,7 @@ export class LibP2PService implements P2PService {

private processEpochProofQuoteFromPeer(epochProofQuote: EpochProofQuote): void {
this.logger.verbose(`Received epoch proof quote ${epochProofQuote.p2pMessageIdentifier()} from external peer.`);
this.epochProofQuotePool.addQuote(epochProofQuote);
this.mempools.epochProofQuotePool.addQuote(epochProofQuote);
}

/**
Expand All @@ -434,7 +426,7 @@ export class LibP2PService implements P2PService {
const isValidTx = await this.validatePropagatedTx(tx, peerId);

if (isValidTx) {
await this.txPool.addTxs([tx]);
await this.mempools.txPool.addTxs([tx]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { describe, expect, it, jest } from '@jest/globals';
import { multiaddr } from '@multiformats/multiaddr';
import { generatePrivateKey } from 'viem/accounts';

import { type AttestationPool } from '../../attestation_pool/attestation_pool.js';
import { createP2PClient } from '../../client/index.js';
import { type P2PClient } from '../../client/p2p_client.js';
import { type P2PConfig, getP2PDefaultConfig } from '../../config.js';
import { type EpochProofQuotePool } from '../../epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type AttestationPool } from '../../mem_pools/attestation_pool/attestation_pool.js';
import { type EpochProofQuotePool } from '../../mem_pools/epoch_proof_quote_pool/epoch_proof_quote_pool.js';
import { type TxPool } from '../../mem_pools/tx_pool/index.js';
import { AlwaysFalseCircuitVerifier, AlwaysTrueCircuitVerifier } from '../../mocks/index.js';
import { type TxPool } from '../../tx_pool/index.js';
import { convertToMultiaddr } from '../../util.js';
import { AZTEC_ENR_KEY, AZTEC_NET } from '../discV5_service.js';
import { createLibP2PPeerId } from '../index.js';
Expand Down
Loading