Skip to content

Commit

Permalink
chore: rename p2p vars (#6916)
Browse files Browse the repository at this point in the history
"Simplify" by using just:
- UDP announce address
- TCP announce address
- UPD listen address - defaults to 0.0.0.0:40400
- TCP listen address - defaults to 0.0.0.0:40400
  • Loading branch information
spypsy authored Jun 6, 2024
1 parent 06f03fd commit ae7d757
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 164 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,4 @@
"*.cmake"
],
"flagWords": ["anonymous"]
}
}
15 changes: 7 additions & 8 deletions docs/docs/reference/sandbox_reference/sandbox-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ P2P_ENABLED='false' # A flag to enable P2P networking for this node. (default: f
P2P_BLOCK_CHECK_INTERVAL_MS=100 # The frequency in which to check for new L2 blocks.
P2P_PEER_CHECK_INTERVAL_MS=1000 # The frequency in which to check for peers.
P2P_L2_BLOCK_QUEUE_SIZE=1000 # Size of queue of L2 blocks to store.
P2P_TCP_LISTEN_PORT=40400 # The tcp port on which the P2P service should listen for connections.
P2P_TCP_LISTEN_IP= #The tcp IP on which the P2P service should listen for connections.
P2P_TCP_LISTEN_ADDR=0.0.0.0:40400 # The tcp address on which the P2P service should listen for connections.(default: 0.0.0.0:40400)
P2P_UDP_LISTEN_ADDR=0.0.0.0:40400 # The udp address on which the P2P service should listen for peer discovery requests.(default: 0.0.0.0:40400)
PEER_ID_PRIVATE_KEY='' # An optional peer id private key. If blank, will generate a random key.
BOOTSTRAP_NODES='' # A list of bootstrap peers to connect to, separated by commas
P2P_ANNOUNCE_TCP_HOSTNAME='' # TCP Hostname to announce to the p2p network
P2P_ANNOUNCE_UDP_HOSTNAME='' # UDP Hostname to announce to the p2p network (used for peer discovery)
P2P_TCP_ANNOUNCE_ADDR='' # TCP Address to announce to the p2p network. Format: <address>:<port>
P2P_UDP_ANNOUNCE_ADDR='' # UDP Hostname to announce to the p2p network (used for peer discovery). Uses TCP announce addr if not provided
P2P_ANNOUNCE_PORT='' # Port to announce to the p2p network
P2P_NAT_ENABLED='false' # Whether to enable NAT from libp2p
P2P_MIN_PEERS=10 # The minimum number of peers (a peer count below this will cause the node to look for more peers)
Expand Down Expand Up @@ -153,11 +153,10 @@ PXE_L2_STARTING_BLOCK=1 # L2 Block to start synching the PXE from (default: 1)
The P2P Bootstrap node is a standalone app whose purpose is to assist new P2P network participants in acquiring peers.
```sh
P2P_TCP_LISTEN_IP='0.0.0.0' # The IP Address on which to listen for connections.
P2P_TCP_LISTEN_PORT=40400 # The port on which to listen for connections.
P2P_UDP_LISTEN_ADDR=0.0.0.0:40400 # The udp address on which the P2P service should listen for peer discovery requests. (default: 0.0.0.0:40400)
PEER_ID_PRIVATE_KEY='' # The private key to be used by the peer for secure communications with other peers. This key will also be used to derive the Peer ID.
P2P_ANNOUNCE_UDP_HOSTNAME='' # The IPAddress/Hostname that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_IP if e.g. the node is behind a NAT.
P2P_ANNOUNCE_PORT='' # The port that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_PORT if e.g. the node is behind a NAT.
P2P_UDP_ANNOUNCE_ADDR='' # The IPAddress/Hostname that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_ADDR if e.g. the node is behind a NAT.
P2P_ANNOUNCE_PORT='' # The port that other peers should use to connect to this node, this may be different to P2P_UDP_LISTEN_ADDR if e.g. the node is behind a NAT.
```
## Cheat Codes
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/cli/cmds/start_p2p_bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const startP2PBootstrap = async (options: any, userLog: LogFn, debugLogge
const cliOptions = parseModuleOptions(options.p2pBootstrap);
const config = mergeEnvVarsAndCliOptions<P2PConfig>(envVars, cliOptions);
await runBootstrapNode(config, debugLogger);
userLog(`P2P bootstrap node started on ${config.udpListenIp}:${config.udpListenPort}`);
userLog(`P2P bootstrap node started on ${config.udpListenAddress}`);
};
8 changes: 3 additions & 5 deletions yarn-project/aztec/src/cli/texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ const p2pOptions =
'p2pBlockCheckIntervalMS:P2P_BLOCK_CHECK_INTERVAL_MS - number - The frequency in which to check for blocks. Default: 100\n' +
'p2pPeerCheckIntervalMS:P2P_PEER_CHECK_INTERVAL_MS - number - The frequency in which to check for peers. Default: 1000\n' +
'p2pL2QueueSize:P2P_L2_QUEUE_SIZE - number - Size of queue of L2 blocks to store. Default: 1000\n' +
'tcpListenPort:TCP_LISTEN_PORT - number - The tcp port on which the P2P service should listen for connections. Default: 40400\n' +
'tcpListenIp:TCP_LISTEN_IP - string - The tcp IP on which the P2P service should listen for connections. Default: 0.0.0.0\n' +
'tcpListenAddr:TCP_LISTEN_ADDR - string - The tcp address on which the P2P service should listen for connections. Default: 0.0.0.0:40400\n' +
'peerIdPrivateKey:PEER_ID_PRIVATE_KEY - string - An optional peer id private key. If blank, will generate a random key.\n' +
'bootstrapNodes:BOOTSTRAP_NODES - string - A list of bootstrap peers to connect to.\n' +
'announceTcpHostname:P2P_ANNOUNCE_TCP_HOSTNAME - string - P2P Hostname to announce for TCP communications.\n' +
'announceUdpHostname:P2P_ANNOUNCE_UDP_HOSTNAME - string - P2P Hostname to announce for UDP communications used in peer discovery.\n' +
'tcpAnnounceAddress:P2P_TCP_ANNOUNCE_ADDR - string - P2P address to announce for TCP communications.\n' +
'udpAnnounceAddress:P2P_UDP_ANNOUNCE_ADDR - string - P2P address to announce for UDP communications used in peer discovery.\n' +
'queryForIp:P2P_QUERY_FOR_IP - boolean - Whether to query for the public IP address if a hostname has not been provided. Default: false\n' +
'announcePort:P2P_ANNOUNCE_PORT - number - P2P Port to announce.\n' +
'enableNat:P2P_NAT_ENABLED - boolean - Whether to enable NAT from libp2p (ignored for bootstrap node). Default: false\n' +
'minPeerCount:P2P_MIN_PEERS - number - The minimum number of peers to connect to. Default: 10\n' +
'maxPeerCount:P2P_MAX_PEERS - number - The maximum number of peers to connect to. Default: 100\n';
Expand Down
16 changes: 8 additions & 8 deletions yarn-project/aztec/terraform/node/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,20 @@ resource "aws_ecs_task_definition" "aztec-node" {
"value": "/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}"
},
{
"name": "P2P_TCP_LISTEN_PORT",
"value": "${var.NODE_P2P_TCP_PORT + count.index}"
"name": "P2P_TCP_LISTEN_ADDR",
"value": "0.0.0.0:${var.NODE_P2P_TCP_PORT + count.index}"
},
{
"name": "P2P_UDP_LISTEN_PORT",
"value": "${var.NODE_P2P_UDP_PORT + count.index}"
"name": "P2P_UDP_LISTEN_ADDR",
"value": "0.0.0.0:${var.NODE_P2P_UDP_PORT + count.index}"
},
{
"name": "P2P_TCP_LISTEN_IP",
"value": "0.0.0.0"
"name": "P2P_TCP_ANNOUNCE_ADDR",
"value": ":${var.NODE_P2P_TCP_PORT + count.index}"
},
{
"name": "P2P_ANNOUNCE_PORT",
"value": "${var.NODE_P2P_TCP_PORT + count.index}"
"name": "P2P_UDP_ANNOUNCE_ADDR",
"value": ":${var.NODE_P2P_UDP_PORT + count.index}"
},
{
"name": "P2P_QUERY_FOR_IP",
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/end-to-end/scripts/docker-compose-p2p.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ services:
image: aztecprotocol/aztec:${AZTEC_DOCKER_TAG:-latest}
command: 'start --p2p-bootstrap'
expose:
- '40400'
- '40400:40400/tcp'
- '40400:40400/udp'
environment:
DEBUG: 'aztec:*'
DEBUG_COLORS: 1
P2P_TCP_LISTEN_PORT: 40400
P2P_TCP_LISTEN_IP: '0.0.0.0'
P2P_ANNOUNCE_TCP_HOSTNAME: 'p2p-bootstrap'
P2P_ANNOUNCE_UDP_HOSTNAME: 'p2p-bootstrap'
P2P_TCP_LISTEN_ADDR: '0.0.0.0:40400'
P2P_TCP_ANNOUNCE_ADDR: 'p2p-bootstrap:40400'
P2P_UDP_ANNOUNCE_ADDR: 'p2p-bootstrap:40400'
PEER_ID_PRIVATE_KEY: '0a260024080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1224080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1a44080112402df8b977f356c6e34fa021c9647973234dff4df706c185794405aafb556723cf5ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e'

end-to-end:
Expand All @@ -35,7 +35,7 @@ services:
WS_CHECK_INTERVAL: 50
SEQ_MAX_TX_PER_BLOCK: 32
SEQ_MIN_TX_PER_BLOCK: 1
P2P_TCP_LISTEN_IP: '0.0.0.0'
P2P_TCP_LISTEN_ADDR: '0.0.0.0:40400'
P2P_NAT_ENABLED: 'false'
P2P_ENABLED: 'true'
BOOTSTRAP_NODES: '/ip4/p2p-bootstrap/tcp/40400/p2p/12D3KooWGBpbC6qQFkaCYphjNeY6sV99o4SnEWyTeBigoVriDn4D'
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/scripts/start_p2p_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export SEQ_TX_POLLING_INTERVAL=50
export SEQ_MAX_TX_PER_BLOCK=32
export SEQ_MIN_TX_PER_BLOCK=32
export BOOTSTRAP_NODES='/ip4/127.0.0.1/tcp/40400/p2p/12D3KooWGBpbC6qQFkaCYphjNeY6sV99o4SnEWyTeBigoVriDn4D'
export P2P_TCP_LISTEN_IP='0.0.0.0'
export P2P_TCP_LISTEN_ADDR='0.0.0.0:40400'
export P2P_NAT_ENABLED='false'
export P2P_ENABLED='true'
export DEBUG='aztec:*,libp2p:*'
Expand Down
20 changes: 8 additions & 12 deletions yarn-project/end-to-end/src/flakey_e2e_p2p_network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { setup } from './fixtures/utils.js';
const NUM_NODES = 4;
const NUM_TXS_PER_BLOCK = 4;
const NUM_TXS_PER_NODE = 2;
const BOOT_NODE_TCP_PORT = 40400;
const BOOT_NODE_UDP_PORT = 40400;

interface NodeContext {
node: AztecNodeService;
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('e2e_p2p_network', () => {
// is if the txs are successfully gossiped around the nodes.
const contexts: NodeContext[] = [];
for (let i = 0; i < NUM_NODES; i++) {
const node = await createNode(i + 1 + BOOT_NODE_TCP_PORT, bootstrapNodeEnr?.encodeTxt(), i);
const node = await createNode(i + 1 + BOOT_NODE_UDP_PORT, bootstrapNodeEnr?.encodeTxt(), i);
const context = await createPXEServiceAndSubmitTransactions(node, NUM_TXS_PER_NODE);
contexts.push(context);
}
Expand All @@ -74,10 +74,8 @@ describe('e2e_p2p_network', () => {
const peerId = await createLibP2PPeerId();
const bootstrapNode = new BootstrapNode();
const config: BootNodeConfig = {
udpListenPort: BOOT_NODE_TCP_PORT,
udpListenIp: '0.0.0.0',
announceUdpHostname: '/ip4/127.0.0.1',
announcePort: BOOT_NODE_TCP_PORT,
udpListenAddress: `0.0.0.0:${BOOT_NODE_UDP_PORT}`,
udpAnnounceAddress: `127.0.0.1:${BOOT_NODE_UDP_PORT}`,
peerIdPrivateKey: Buffer.from(peerId.privateKey!).toString('hex'),
minPeerCount: 10,
maxPeerCount: 100,
Expand All @@ -98,12 +96,10 @@ describe('e2e_p2p_network', () => {

const newConfig: AztecNodeConfig = {
...config,
tcpListenPort,
udpListenPort: tcpListenPort,
tcpListenIp: '0.0.0.0',
udpListenIp: '0.0.0.0',
announceTcpHostname: '/ip4/127.0.0.1',
announceUdpHostname: '/ip4/127.0.0.1',
udpListenAddress: `0.0.0.0:${tcpListenPort}`,
tcpListenAddress: `0.0.0.0:${tcpListenPort}`,
tcpAnnounceAddress: `127.0.0.1:${tcpListenPort}`,
udpAnnounceAddress: `127.0.0.1:${tcpListenPort}`,
bootstrapNodes: [bootstrapNode],
minTxsPerBlock: NUM_TXS_PER_BLOCK,
maxTxsPerBlock: NUM_TXS_PER_BLOCK,
Expand Down
6 changes: 2 additions & 4 deletions yarn-project/p2p-bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ To build the package simply type `yarn build`, to start the boot node, simply ty

The node will require a number of environment variables:

P2P_TCP_LISTEN_IP - The IP Address on which to listen for connections.
P2P_TCP_LISTEN_PORT - The port on which to listen for connections.
P2P_UDP_LISTEN_ADDR - The address on which to listen for connections.
PEER_ID_PRIVATE_KEY - The private key to be used by the peer for secure communications with other peers. This key will also be used to derive the Peer ID.
P2P_ANNOUNCE_UDP_HOSTNAME - The IPAddress/Hostname that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_IP if e.g. the node is behind a NAT.
P2P_ANNOUNCE_PORT - The port that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_PORT if e.g. the node is behind a NAT.
P2P_UDP_ANNOUNCE_ADDR - The address that other peers should use to connect to this node, this may be different to P2P_TCP_LISTEN_ADDR if e.g. the node is behind a NAT.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ services:
- '40400:40400'
environment:
DEBUG: 'aztec:*'
P2P_TCP_LISTEN_PORT: 40400
P2P_TCP_LISTEN_ADDR: '0.0.0.0:40400'
PEER_ID: '0a260024080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1224080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1a44080112402df8b977f356c6e34fa021c9647973234dff4df706c185794405aafb556723cf5ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e'
2 changes: 1 addition & 1 deletion yarn-project/p2p-bootstrap/scripts/start_bootnode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

cd ..
export P2P_TCP_LISTEN_PORT=40400
export P2P_UDP_LISTEN_ADDR='0.0.0.0:40400'
export PEER_ID='0a260024080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1224080112205ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e1a44080112402df8b977f356c6e34fa021c9647973234dff4df706c185794405aafb556723cf5ea53185db2e52dae74d0d4d6cadc494174810d0a713cd09b0ac517c38bc781e'
echo "Building..."
yarn build
Expand Down
16 changes: 4 additions & 12 deletions yarn-project/p2p-bootstrap/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,17 @@ resource "aws_ecs_task_definition" "p2p-bootstrap" {
"value": "production"
},
{
"name": "P2P_UDP_LISTEN_PORT",
"value": "${var.BOOTNODE_LISTEN_PORT + count.index}"
"name": "P2P_UDP_LISTEN_ADDR",
"value": "0.0.0.0:${var.BOOTNODE_LISTEN_PORT + count.index}"
},
{
"name": "P2P_UDP_LISTEN_IP",
"value": "0.0.0.0"
"name": "P2P_UDP_ANNOUNCE_ADDR",
"value": "${data.terraform_remote_state.aztec-network_iac.outputs.p2p_eip}:${var.BOOTNODE_LISTEN_PORT + count.index}"
},
{
"name": "PEER_ID_PRIVATE_KEY",
"value": "${local.bootnode_keys[count.index]}"
},
{
"name": "P2P_ANNOUNCE_UDP_HOSTNAME",
"value": "/ip4/${data.terraform_remote_state.aztec-network_iac.outputs.p2p_eip}"
},
{
"name": "P2P_ANNOUNCE_PORT",
"value": "${var.BOOTNODE_LISTEN_PORT + count.index}"
},
{
"name": "DEBUG",
"value": "aztec:*,discv5:*"
Expand Down
16 changes: 11 additions & 5 deletions yarn-project/p2p/src/bootstrap/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { type Multiaddr, multiaddr } from '@multiformats/multiaddr';
import { type P2PConfig } from '../config.js';
import { AZTEC_ENR_KEY, AZTEC_NET } from '../service/discV5_service.js';
import { createLibP2PPeerId } from '../service/index.js';
import { convertToMultiaddr } from '../util.js';

/**
* Required P2P config values for a bootstrap node.
*/
export type BootNodeConfig = Partial<P2PConfig> &
Pick<P2PConfig, 'announceUdpHostname' | 'announcePort'> &
Required<Pick<P2PConfig, 'udpListenIp' | 'udpListenPort'>>;
Pick<P2PConfig, 'udpAnnounceAddress'> &
Required<Pick<P2PConfig, 'udpListenAddress'>>;

/**
* Encapsulates a 'Bootstrap' node, used for the purpose of assisting new joiners in acquiring peers.
Expand All @@ -31,13 +32,18 @@ export class BootstrapNode {
* @returns An empty promise.
*/
public async start(config: BootNodeConfig) {
const { peerIdPrivateKey, udpListenIp, udpListenPort, announceUdpHostname, announcePort } = config;
const { peerIdPrivateKey, udpListenAddress, udpAnnounceAddress } = config;
const peerId = await createLibP2PPeerId(peerIdPrivateKey);
this.peerId = peerId;
const enr = SignableENR.createFromPeerId(peerId);

const listenAddrUdp = multiaddr(`/ip4/${udpListenIp}/udp/${udpListenPort}`);
const publicAddr = multiaddr(`${announceUdpHostname}/udp/${announcePort}`);
const listenAddrUdp = multiaddr(convertToMultiaddr(udpListenAddress, 'udp'));

if (!udpAnnounceAddress) {
throw new Error('You need to provide a UDP announce address.');
}

const publicAddr = multiaddr(convertToMultiaddr(udpAnnounceAddress, 'udp'));
enr.setLocationMultiaddr(publicAddr);
enr.set(AZTEC_ENR_KEY, Uint8Array.from([AZTEC_NET]));

Expand Down
41 changes: 25 additions & 16 deletions yarn-project/p2p/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { type P2PConfig } from '../config.js';
import { DiscV5Service } from '../service/discV5_service.js';
import { DummyP2PService, DummyPeerDiscoveryService } from '../service/dummy_service.js';
import { LibP2PService, createLibP2PPeerId } from '../service/index.js';
import { getPublicIp } from '../service/ip_query.js';
import { type TxPool } from '../tx_pool/index.js';
import { getPublicIp, splitAddressPort } from '../util.js';

export * from './p2p_client.js';

Expand All @@ -21,30 +21,39 @@ export const createP2PClient = async (
let p2pService;

if (config.p2pEnabled) {
// If announceTcpHostname or announceUdpHostname are not provided, query for public IP if config allows
// If announceTcpAddress or announceUdpAddress are not provided, query for public IP if config allows
const {
announceTcpHostname: configAnnounceTcpHostname,
announceUdpHostname: configAnnounceUdpHostname,
tcpAnnounceAddress: configTcpAnnounceAddress,
udpAnnounceAddress: configUdpAnnounceAddress,
queryForIp,
} = config;
if (!configAnnounceTcpHostname) {

// create variable for re-use if needed
let publicIp;

// check if no announce IP was provided
const splitTcpAnnounceAddress = splitAddressPort(configTcpAnnounceAddress || '', true);
if (splitTcpAnnounceAddress.length == 2 && splitTcpAnnounceAddress[0] === '') {
if (queryForIp) {
const publicIp = await getPublicIp();
const announceHostname = `/ip4/${publicIp}`;
config.announceTcpHostname = announceHostname;
publicIp = await getPublicIp();
const tcpAnnounceAddress = `${publicIp}:${splitTcpAnnounceAddress[1]}`;
config.tcpAnnounceAddress = tcpAnnounceAddress;
} else {
throw new Error('No announceTcpHostname provided');
throw new Error(
`Invalid announceTcpAddress provided: ${splitTcpAnnounceAddress}. Expected format: <addr>:<port>`,
);
}
}

if (!configAnnounceUdpHostname) {
// If announceUdpHostname is not provided, use announceTcpHostname
if (!queryForIp && config.announceTcpHostname) {
config.announceUdpHostname = config.announceTcpHostname;
const splitUdpAnnounceAddress = splitAddressPort(configUdpAnnounceAddress || '', true);
if (splitUdpAnnounceAddress.length == 2 && splitUdpAnnounceAddress[0] === '') {
// If announceUdpAddress is not provided, use announceTcpAddress
if (!queryForIp && config.tcpAnnounceAddress) {
config.udpAnnounceAddress = config.tcpAnnounceAddress;
} else if (queryForIp) {
const publicIp = await getPublicIp();
const announceHostname = `/ip4/${publicIp}`;
config.announceUdpHostname = announceHostname;
const udpPublicIp = publicIp || (await getPublicIp());
const udpAnnounceAddress = `${udpPublicIp}:${splitUdpAnnounceAddress[1]}`;
config.udpAnnounceAddress = udpAnnounceAddress;
}
}

Expand Down
Loading

0 comments on commit ae7d757

Please sign in to comment.