Skip to content

Commit

Permalink
feat(p2p): remaining p2p topic validators (#10734)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Dec 18, 2024
1 parent e867e87 commit a17d319
Show file tree
Hide file tree
Showing 49 changed files with 706 additions and 200 deletions.
1 change: 1 addition & 0 deletions yarn-project/aztec-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@aztec/bb-prover": "workspace:^",
"@aztec/circuit-types": "workspace:^",
"@aztec/circuits.js": "workspace:^",
"@aztec/epoch-cache": "workspace:^",
"@aztec/ethereum": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/kv-store": "workspace:^",
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
type PublicDataTreeLeafPreimage,
} from '@aztec/circuits.js';
import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash';
import { EpochCache } from '@aztec/epoch-cache';
import { type L1ContractAddresses, createEthereumChain } from '@aztec/ethereum';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
Expand Down Expand Up @@ -166,20 +167,23 @@ export class AztecNodeService implements AztecNode, Traceable {
log.warn(`Aztec node is accepting fake proofs`);
}

const epochCache = await EpochCache.create(config.l1Contracts.rollupAddress, config, { dateProvider });

// create the tx pool and the p2p client, which will need the l2 block source
const p2pClient = await createP2PClient(
P2PClientType.Full,
config,
archiver,
proofVerifier,
worldStateSynchronizer,
epochCache,
telemetry,
);

// start both and wait for them to sync from the block source
await Promise.all([p2pClient.start(), worldStateSynchronizer.start()]);

const validatorClient = await createValidatorClient(config, rollupAddress, { p2pClient, telemetry, dateProvider });
const validatorClient = createValidatorClient(config, { p2pClient, telemetry, dateProvider, epochCache });

// now create the sequencer
const sequencer = config.disableValidator
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
{
"path": "../circuits.js"
},
{
"path": "../epoch-cache"
},
{
"path": "../ethereum"
},
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/circuit-types/src/p2p/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export * from './interface.js';
export * from './signature_utils.js';
export * from './topic_type.js';
export * from './client_type.js';
export * from './message_validator.js';
export * from './peer_error.js';
export * from './mocks.js';
10 changes: 10 additions & 0 deletions yarn-project/circuit-types/src/p2p/message_validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type PeerErrorSeverity } from './peer_error.js';

/**
* P2PValidator
*
* A validator for P2P messages, which returns a severity of error to be applied to the peer
*/
export interface P2PValidator<T> {
validate(message: T): Promise<PeerErrorSeverity | undefined>;
}
17 changes: 17 additions & 0 deletions yarn-project/circuit-types/src/p2p/peer_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum PeerErrorSeverity {
/**
* Not malicious action, but it must not be tolerated
* ~2 occurrences will get the peer banned
*/
LowToleranceError = 'LowToleranceError',
/**
* Negative action that can be tolerated only sometimes
* ~10 occurrences will get the peer banned
*/
MidToleranceError = 'MidToleranceError',
/**
* Some error that can be tolerated multiple times
* ~50 occurrences will get the peer banned
*/
HighToleranceError = 'HighToleranceError',
}
4 changes: 3 additions & 1 deletion yarn-project/p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
".": "./dest/index.js",
"./mocks": "./dest/mocks/index.js",
"./bootstrap": "./dest/bootstrap/bootstrap.js",
"./config": "./dest/config.js"
"./config": "./dest/config.js",
"./msg_validators": "./dest/msg_validators/index.js"
},
"typedocOptions": {
"entryPoints": [
Expand Down Expand Up @@ -69,6 +70,7 @@
"dependencies": {
"@aztec/circuit-types": "workspace:^",
"@aztec/circuits.js": "workspace:^",
"@aztec/epoch-cache": "workspace:^",
"@aztec/foundation": "workspace:^",
"@aztec/kv-store": "workspace:^",
"@aztec/telemetry-client": "workspace:^",
Expand Down
97 changes: 97 additions & 0 deletions yarn-project/p2p/src/client/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
type ClientProtocolCircuitVerifier,
type L2BlockSource,
P2PClientType,
type WorldStateSynchronizer,
} from '@aztec/circuit-types';
import { type EpochCache } from '@aztec/epoch-cache';
import { createLogger } from '@aztec/foundation/log';
import { type AztecKVStore } from '@aztec/kv-store';
import { type DataStoreConfig } from '@aztec/kv-store/config';
import { createStore } from '@aztec/kv-store/lmdb';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { P2PClient } from '../client/p2p_client.js';
import { type P2PConfig } from '../config.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 '../services/discv5/discV5_service.js';
import { DummyP2PService } from '../services/dummy_service.js';
import { LibP2PService } from '../services/index.js';
import { configureP2PClientAddresses, createLibP2PPeerIdFromPrivateKey, getPeerIdPrivateKey } from '../util.js';

type P2PClientDeps<T extends P2PClientType> = {
txPool?: TxPool;
store?: AztecKVStore;
attestationPool?: T extends P2PClientType.Full ? AttestationPool : undefined;
epochProofQuotePool?: EpochProofQuotePool;
};

export const createP2PClient = async <T extends P2PClientType>(
clientType: T,
_config: P2PConfig & DataStoreConfig,
l2BlockSource: L2BlockSource,
proofVerifier: ClientProtocolCircuitVerifier,
worldStateSynchronizer: WorldStateSynchronizer,
epochCache: EpochCache,
telemetry: TelemetryClient = new NoopTelemetryClient(),
deps: P2PClientDeps<T> = {},
) => {
let config = { ..._config };
const logger = createLogger('p2p');
const store = deps.store ?? (await createStore('p2p', config, createLogger('p2p:lmdb')));

const mempools: MemPools<T> = {
txPool: deps.txPool ?? new AztecKVTxPool(store, telemetry),
epochProofQuotePool: deps.epochProofQuotePool ?? new MemoryEpochProofQuotePool(telemetry),
attestationPool:
clientType === P2PClientType.Full
? ((deps.attestationPool ?? new InMemoryAttestationPool(telemetry)) as T extends P2PClientType.Full
? AttestationPool
: undefined)
: undefined,
};

let p2pService;

if (_config.p2pEnabled) {
logger.verbose('P2P is enabled. Using LibP2P service.');
config = await configureP2PClientAddresses(_config);

// Create peer discovery service
const peerIdPrivateKey = await getPeerIdPrivateKey(config, store);
const peerId = await createLibP2PPeerIdFromPrivateKey(peerIdPrivateKey);
const discoveryService = new DiscV5Service(peerId, config, telemetry);

p2pService = await LibP2PService.new<T>(
clientType,
config,
discoveryService,
peerId,
mempools,
l2BlockSource,
epochCache,
proofVerifier,
worldStateSynchronizer,
store,
telemetry,
);
} else {
logger.verbose('P2P is disabled. Using dummy P2P service');
p2pService = new DummyP2PService();
}
return new P2PClient(
clientType,
store,
l2BlockSource,
mempools,
p2pService,
config.keepProvenTxsInPoolFor,
telemetry,
);
};
96 changes: 1 addition & 95 deletions yarn-project/p2p/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,2 @@
import {
type ClientProtocolCircuitVerifier,
type L2BlockSource,
P2PClientType,
type WorldStateSynchronizer,
} from '@aztec/circuit-types';
import { createLogger } from '@aztec/foundation/log';
import { type AztecKVStore } from '@aztec/kv-store';
import { type DataStoreConfig } from '@aztec/kv-store/config';
import { createStore } from '@aztec/kv-store/lmdb';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { P2PClient } from '../client/p2p_client.js';
import { type P2PConfig } from '../config.js';
import { type AttestationPool } from '../mem_pools/attestation_pool/attestation_pool.js';
import { KvAttestationPool } from '../mem_pools/attestation_pool/kv_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 '../services/discv5/discV5_service.js';
import { DummyP2PService } from '../services/dummy_service.js';
import { LibP2PService } from '../services/index.js';
import { configureP2PClientAddresses, createLibP2PPeerIdFromPrivateKey, getPeerIdPrivateKey } from '../util.js';

export * from './p2p_client.js';

type P2PClientDeps<T extends P2PClientType> = {
txPool?: TxPool;
store?: AztecKVStore;
attestationPool?: T extends P2PClientType.Full ? AttestationPool : undefined;
epochProofQuotePool?: EpochProofQuotePool;
};

export const createP2PClient = async <T extends P2PClientType>(
clientType: T,
_config: P2PConfig & DataStoreConfig,
l2BlockSource: L2BlockSource,
proofVerifier: ClientProtocolCircuitVerifier,
worldStateSynchronizer: WorldStateSynchronizer,
telemetry: TelemetryClient = new NoopTelemetryClient(),
deps: P2PClientDeps<T> = {},
) => {
let config = { ..._config };
const logger = createLogger('p2p');
const store = deps.store ?? (await createStore('p2p', config, createLogger('p2p:lmdb')));

const mempools: MemPools<T> = {
txPool: deps.txPool ?? new AztecKVTxPool(store, telemetry),
epochProofQuotePool: deps.epochProofQuotePool ?? new MemoryEpochProofQuotePool(telemetry),
attestationPool:
clientType === P2PClientType.Full
? ((deps.attestationPool ?? new KvAttestationPool(store, telemetry)) as T extends P2PClientType.Full
? AttestationPool
: undefined)
: undefined,
};

let p2pService;

if (_config.p2pEnabled) {
logger.verbose('P2P is enabled. Using LibP2P service.');
config = await configureP2PClientAddresses(_config);

// Create peer discovery service
const peerIdPrivateKey = await getPeerIdPrivateKey(config, store);
const peerId = await createLibP2PPeerIdFromPrivateKey(peerIdPrivateKey);
const discoveryService = new DiscV5Service(peerId, config, telemetry);

p2pService = await LibP2PService.new<T>(
clientType,
config,
discoveryService,
peerId,
mempools,
l2BlockSource,
proofVerifier,
worldStateSynchronizer,
store,
telemetry,
);
} else {
logger.verbose('P2P is disabled. Using dummy P2P service');
p2pService = new DummyP2PService();
}
return new P2PClient(
clientType,
store,
l2BlockSource,
mempools,
p2pService,
config.keepProvenTxsInPoolFor,
telemetry,
);
};
export * from './factory.js';
2 changes: 1 addition & 1 deletion yarn-project/p2p/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './config.js';
export * from './mem_pools/epoch_proof_quote_pool/index.js';
export * from './services/index.js';
export * from './mem_pools/tx_pool/index.js';
export * from './tx_validator/index.js';
export * from './msg_validators/index.js';
3 changes: 3 additions & 0 deletions yarn-project/p2p/src/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type Tx,
type WorldStateSynchronizer,
} from '@aztec/circuit-types';
import { type EpochCache } from '@aztec/epoch-cache';
import { type DataStoreConfig } from '@aztec/kv-store/config';
import { openTmpStore } from '@aztec/kv-store/lmdb';
import { type TelemetryClient } from '@aztec/telemetry-client';
Expand Down Expand Up @@ -101,6 +102,7 @@ export async function createTestLibP2PService<T extends P2PClientType>(
boostrapAddrs: string[] = [],
l2BlockSource: L2BlockSource,
worldStateSynchronizer: WorldStateSynchronizer,
epochCache: EpochCache,
mempools: MemPools<T>,
telemetry: TelemetryClient,
port: number = 0,
Expand Down Expand Up @@ -132,6 +134,7 @@ export async function createTestLibP2PService<T extends P2PClientType>(
discoveryService,
mempools,
l2BlockSource,
epochCache,
proofVerifier,
worldStateSynchronizer,
telemetry,
Expand Down
Loading

0 comments on commit a17d319

Please sign in to comment.