Skip to content

Commit f709fab

Browse files
authored
feat(p2p): optional P2P_BROADCAST_PORT (#13525)
fixes: #13165
1 parent f02123d commit f709fab

File tree

8 files changed

+78
-18
lines changed

8 files changed

+78
-18
lines changed

aztec-up/bin/aztec

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ while [ "$#" -gt 0 ]; do
1717
P2P_PORT="$2"
1818
shift 2
1919
;;
20+
--p2p.p2pBroadcastPort)
21+
P2P_BROADCAST_PORT="$2"
22+
shift 2
23+
;;
2024
-a | --anvil-port)
2125
# Override default port exposed on container.
2226
ANVIL_PORT="$2"
@@ -93,11 +97,21 @@ case ${1:-} in
9397
node --no-warnings /usr/src/yarn-project/aztec/dest/bin/index.js start --sandbox
9498
"
9599
else
100+
96101
export P2P_PORT="${P2P_PORT:-40400}"
97-
P2P_TCP_LISTEN_MAPPING="$P2P_PORT:$P2P_PORT"
98-
P2P_UDP_LISTEN_MAPPING="${P2P_TCP_LISTEN_MAPPING}/udp"
102+
# If the p2p broadcast port if provided, then map it to the p2p port on the container.
103+
if [ -n "${P2P_BROADCAST_PORT:-}" ]; then
104+
export P2P_BROADCAST_PORT
105+
P2P_TCP_BROADCAST_MAPPING="$P2P_BROADCAST_PORT:$P2P_PORT"
106+
P2P_UDP_BROADCAST_MAPPING="${P2P_TCP_BROADCAST_MAPPING}/udp"
107+
108+
PORTS_TO_EXPOSE="${PORTS_TO_EXPOSE:-} $P2P_TCP_BROADCAST_MAPPING $P2P_UDP_BROADCAST_MAPPING"
109+
else
110+
P2P_TCP_LISTEN_MAPPING="$P2P_PORT:$P2P_PORT"
111+
P2P_UDP_LISTEN_MAPPING="${P2P_TCP_LISTEN_MAPPING}/udp"
99112

100-
PORTS_TO_EXPOSE="${PORTS_TO_EXPOSE:-} $P2P_TCP_LISTEN_MAPPING $P2P_UDP_LISTEN_MAPPING"
113+
PORTS_TO_EXPOSE="${PORTS_TO_EXPOSE:-} $P2P_TCP_LISTEN_MAPPING $P2P_UDP_LISTEN_MAPPING"
114+
fi
101115

102116
exec $(dirname $0)/.aztec-run aztec-start \
103117
node --no-warnings /usr/src/yarn-project/aztec/dest/bin/index.js "$@"

yarn-project/foundation/src/config/env_var.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export type EnvVar =
112112
| 'P2P_DOUBLE_SPEND_SEVERE_PEER_PENALTY_WINDOW'
113113
| 'P2P_LISTEN_ADDR'
114114
| 'P2P_PORT'
115+
| 'P2P_BROADCAST_PORT'
115116
| 'P2P_IP'
116117
| 'P2P_TX_POOL_KEEP_PROVEN_FOR'
117118
| 'P2P_ATTESTATION_POOL_KEEP_FOR'

yarn-project/p2p/src/bootstrap/bootstrap.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@ export class BootstrapNode implements P2PBootstrapApi {
3131
* @returns An empty promise.
3232
*/
3333
public async start(config: BootnodeConfig) {
34-
const { p2pIp, p2pPort, listenAddress } = config;
35-
const listenAddrUdp = multiaddr(convertToMultiaddr(listenAddress, p2pPort, 'udp'));
36-
34+
const { p2pIp, p2pPort, listenAddress, p2pBroadcastPort } = config;
3735
if (!p2pIp) {
3836
throw new Error('You need to provide a P2P IP address.');
3937
}
4038

39+
if (!p2pBroadcastPort) {
40+
config.p2pBroadcastPort = p2pPort;
41+
}
42+
43+
const listenAddrUdp = multiaddr(convertToMultiaddr(listenAddress, config.p2pBroadcastPort!, 'udp'));
44+
4145
const peerIdPrivateKey = await getPeerIdPrivateKey(config, this.store, this.logger);
4246

4347
const { enr: ourEnr, peerId } = await createBootnodeENRandPeerId(
4448
peerIdPrivateKey,
4549
p2pIp,
46-
p2pPort,
50+
config.p2pBroadcastPort!,
4751
config.l1ChainId,
4852
);
4953
this.peerId = peerId;

yarn-project/p2p/src/config.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export interface P2PConfig extends P2PReqRespConfig, ChainConfig {
5353
*/
5454
p2pPort: number;
5555

56+
/**
57+
* The port to broadcast the P2P service on (included in the node's ENR).
58+
*/
59+
p2pBroadcastPort?: number;
60+
5661
/**
5762
* The IP address for the P2P service.
5863
*/
@@ -194,6 +199,8 @@ export interface P2PConfig extends P2PReqRespConfig, ChainConfig {
194199
maxTxPoolSize: number;
195200
}
196201

202+
export const DEFAULT_P2P_PORT = 40400;
203+
197204
export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
198205
p2pEnabled: {
199206
env: 'P2P_ENABLED',
@@ -227,8 +234,12 @@ export const p2pConfigMappings: ConfigMappingsType<P2PConfig> = {
227234
},
228235
p2pPort: {
229236
env: 'P2P_PORT',
230-
description: 'The port for the P2P service.',
231-
...numberConfigHelper(40400),
237+
description: `The port for the P2P service. Defaults to ${DEFAULT_P2P_PORT}`,
238+
...numberConfigHelper(DEFAULT_P2P_PORT),
239+
},
240+
p2pBroadcastPort: {
241+
env: 'P2P_BROADCAST_PORT',
242+
description: `The port to broadcast the P2P service on (included in the node's ENR). Defaults to P2P_PORT.`,
232243
},
233244
p2pIp: {
234245
env: 'P2P_IP',
@@ -410,7 +421,13 @@ export function getP2PDefaultConfig(): P2PConfig {
410421
*/
411422
export type BootnodeConfig = Pick<
412423
P2PConfig,
413-
'p2pIp' | 'p2pPort' | 'peerIdPrivateKey' | 'peerIdPrivateKeyPath' | 'bootstrapNodes' | 'listenAddress'
424+
| 'p2pIp'
425+
| 'p2pPort'
426+
| 'p2pBroadcastPort'
427+
| 'peerIdPrivateKey'
428+
| 'peerIdPrivateKeyPath'
429+
| 'bootstrapNodes'
430+
| 'listenAddress'
414431
> &
415432
Required<Pick<P2PConfig, 'p2pIp' | 'p2pPort'>> &
416433
Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKB'> &
@@ -419,6 +436,7 @@ export type BootnodeConfig = Pick<
419436
const bootnodeConfigKeys: (keyof BootnodeConfig)[] = [
420437
'p2pIp',
421438
'p2pPort',
439+
'p2pBroadcastPort',
422440
'listenAddress',
423441
'peerIdPrivateKey',
424442
'peerIdPrivateKeyPath',

yarn-project/p2p/src/enr/generate-enr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { setAztecEnrKey } from '../versioning.js';
1212
export async function createBootnodeENRandPeerId(
1313
privateKey: string,
1414
p2pIp: string,
15-
p2pPort: number,
15+
p2pBroadcastPort: number,
1616
l1ChainId: number,
1717
): Promise<{ enr: SignableENR; peerId: PeerId }> {
1818
const peerId = await createLibP2PPeerIdFromPrivateKey(privateKey);
1919
const enr = SignableENR.createFromPeerId(peerId);
20-
const publicAddr = multiaddr(convertToMultiaddr(p2pIp, p2pPort, 'udp'));
20+
const publicAddr = multiaddr(convertToMultiaddr(p2pIp, p2pBroadcastPort, 'udp'));
2121
enr.setLocationMultiaddr(publicAddr);
2222

2323
const config: ChainConfig = {

yarn-project/p2p/src/services/discv5/discV5_service.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
5252
configOverrides: Partial<IDiscv5CreateOptions> = {},
5353
) {
5454
super();
55-
const { p2pIp, p2pPort, bootstrapNodes, trustedPeers, privatePeers } = config;
55+
const { p2pIp, p2pPort, p2pBroadcastPort, bootstrapNodes, trustedPeers, privatePeers } = config;
5656
this.bootstrapNodeEnrs = bootstrapNodes.map(x => ENR.decodeTxt(x));
5757
const privatePeerEnrs = new Set(privatePeers);
5858
this.trustedPeerEnrs = trustedPeers.filter(x => !privatePeerEnrs.has(x)).map(x => ENR.decodeTxt(x));
@@ -61,14 +61,22 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
6161
// Add aztec identification to ENR
6262
this.versions = setAztecEnrKey(this.enr, config);
6363

64+
// If no overridden broadcast port is provided, use the p2p port as the broadcast port
65+
if (!p2pBroadcastPort) {
66+
config.p2pBroadcastPort = p2pPort;
67+
}
68+
6469
const bindAddrs: any = {
6570
ip4: multiaddr(convertToMultiaddr(config.listenAddress, p2pPort, 'udp')),
6671
};
6772

6873
if (p2pIp) {
69-
const multiAddrTcp = multiaddr(`${convertToMultiaddr(p2pIp!, p2pPort, 'tcp')}/p2p/${peerId.toString()}`);
70-
// if no udp announce address is provided, use the tcp announce address
71-
const multiAddrUdp = multiaddr(`${convertToMultiaddr(p2pIp!, p2pPort, 'udp')}/p2p/${peerId.toString()}`);
74+
const multiAddrTcp = multiaddr(
75+
`${convertToMultiaddr(p2pIp!, config.p2pBroadcastPort!, 'tcp')}/p2p/${peerId.toString()}`,
76+
);
77+
const multiAddrUdp = multiaddr(
78+
`${convertToMultiaddr(p2pIp!, config.p2pBroadcastPort!, 'udp')}/p2p/${peerId.toString()}`,
79+
);
7280

7381
// set location multiaddr in ENR record
7482
this.enr.setLocationMultiaddr(multiAddrUdp);
@@ -116,7 +124,8 @@ export class DiscV5Service extends EventEmitter implements PeerDiscoveryService
116124

117125
private onMultiaddrUpdated(m: Multiaddr) {
118126
// We want to update our tcp port to match the udp port
119-
const multiAddrTcp = multiaddr(convertToMultiaddr(m.nodeAddress().address, this.config.p2pPort, 'tcp'));
127+
// p2pBroadcastPort is optional on config, however it is set to default within the p2p client factory
128+
const multiAddrTcp = multiaddr(convertToMultiaddr(m.nodeAddress().address, this.config.p2pBroadcastPort!, 'tcp'));
120129
this.enr.setLocationMultiaddr(multiAddrTcp);
121130
this.logger.info('Multiaddr updated', { multiaddr: multiAddrTcp.toString() });
122131
}

yarn-project/p2p/src/services/discv5/discv5_service.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ describe('Discv5Service', () => {
8181
await node.stop();
8282
});
8383

84+
it('should allow broadcast port to be set', async () => {
85+
const broadcastPort = 7891;
86+
const node = await createNode({ p2pBroadcastPort: broadcastPort });
87+
const enr = node.getEnr();
88+
expect(enr.ip).toEqual('127.0.0.1');
89+
expect(enr.udp).toEqual(broadcastPort);
90+
expect(enr.tcp).toEqual(broadcastPort);
91+
});
92+
8493
it('should discover & add a peer', async () => {
8594
const node1 = await createNode();
8695
const node2 = await createNode();

yarn-project/p2p/src/util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ export async function configureP2PClientAddresses(
8080
_config: P2PConfig & DataStoreConfig,
8181
): Promise<P2PConfig & DataStoreConfig> {
8282
const config = { ..._config };
83-
const { p2pIp, queryForIp } = config;
83+
const { p2pIp, queryForIp, p2pBroadcastPort, p2pPort } = config;
84+
85+
// If no broadcast port is provided, use the given p2p port as the broadcast port
86+
if (!p2pBroadcastPort) {
87+
config.p2pBroadcastPort = p2pPort;
88+
}
8489

8590
// check if no announce IP was provided
8691
if (!p2pIp) {

0 commit comments

Comments
 (0)