Skip to content

Commit 244796c

Browse files
committed
client: added RLPx port as a CLI option (local option was inactive before -> always defaulted to 30303)
1 parent 34ec4df commit 244796c

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

packages/client/bin/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const args = require('yargs')
5050
describe: 'Network bootnodes',
5151
array: true,
5252
},
53+
port: {
54+
describe: 'RLPx listening port',
55+
default: Config.PORT_DEFAULT,
56+
},
5357
multiaddrs: {
5458
describe: 'Network multiaddrs',
5559
array: true,
@@ -185,6 +189,7 @@ async function run() {
185189
key,
186190
transports: args.transports,
187191
bootnodes: args.bootnodes ? parseMultiaddrs(args.bootnodes) : undefined,
192+
port: args.port,
188193
multiaddrs: args.multiaddrs ? parseMultiaddrs(args.multiaddrs) : undefined,
189194
rpc: args.rpc,
190195
rpcport: args.rpcport,

packages/client/lib/config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface ConfigOptions {
5353
/**
5454
* Network transports ('rlpx' and/or 'libp2p')
5555
*
56-
* Default: `['rlpx:port=30303', 'libp2p']`
56+
* Default: `['rlpx', 'libp2p']`
5757
*/
5858
transports?: string[]
5959

@@ -63,6 +63,13 @@ export interface ConfigOptions {
6363
*/
6464
bootnodes?: Multiaddr[]
6565

66+
/**
67+
* RLPx listening port
68+
*
69+
* Default: `30303`
70+
*/
71+
port?: number
72+
6673
/**
6774
* Network multiaddrs for libp2p
6875
* (e.g. /ip4/127.0.0.1/tcp/50505/p2p/QmABC)
@@ -167,7 +174,8 @@ export class Config {
167174
public static readonly SYNCMODE_DEFAULT = 'full'
168175
public static readonly LIGHTSERV_DEFAULT = false
169176
public static readonly DATADIR_DEFAULT = `./datadir`
170-
public static readonly TRANSPORTS_DEFAULT = ['rlpx:port=30303', 'libp2p']
177+
public static readonly TRANSPORTS_DEFAULT = ['rlpx', 'libp2p']
178+
public static readonly PORT_DEFAULT = 30303
171179
public static readonly RPC_DEFAULT = false
172180
public static readonly RPCPORT_DEFAULT = 8545
173181
public static readonly RPCADDR_DEFAULT = 'localhost'
@@ -185,6 +193,7 @@ export class Config {
185193
public readonly key: Buffer
186194
public readonly transports: string[]
187195
public readonly bootnodes?: Multiaddr[]
196+
public readonly port?: number
188197
public readonly multiaddrs?: Multiaddr[]
189198
public readonly rpc: boolean
190199
public readonly rpcport: number
@@ -208,6 +217,7 @@ export class Config {
208217
this.lightserv = options.lightserv ?? Config.LIGHTSERV_DEFAULT
209218
this.transports = options.transports ?? Config.TRANSPORTS_DEFAULT
210219
this.bootnodes = options.bootnodes
220+
this.port = options.port ?? Config.PORT_DEFAULT
211221
this.multiaddrs = options.multiaddrs
212222
this.datadir = options.datadir ?? Config.DATADIR_DEFAULT
213223
this.key = options.key ?? genPrivateKey()

packages/client/lib/net/server/rlpxserver.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import { RlpxPeer } from '../peer/rlpxpeer'
33
import { Server, ServerOptions } from './server'
44

55
export interface RlpxServerOptions extends ServerOptions {
6-
/* Local port to listen on (default: 30303) */
7-
port?: number
8-
96
/* List of supported clients */
107
clientFilter?: string[]
118
}
@@ -49,7 +46,6 @@ const ignoredErrors = new RegExp(
4946
export class RlpxServer extends Server {
5047
private peers: Map<string, RlpxPeer> = new Map()
5148

52-
public port: number
5349
public discovery: boolean
5450
private clientFilter: string[]
5551

@@ -66,7 +62,6 @@ export class RlpxServer extends Server {
6662

6763
// TODO: get the external ip from the upnp service
6864
this.ip = '::'
69-
this.port = options.port ?? 30303
7065
this.discovery = options.config.discV4 || options.config.discDns
7166
this.clientFilter = options.clientFilter ?? [
7267
'go1.5',
@@ -99,17 +94,17 @@ export class RlpxServer extends Server {
9994
enode: undefined,
10095
id: undefined,
10196
ip: this.ip,
102-
listenAddr: `[${this.ip}]:${this.port}`,
103-
ports: { discovery: this.port, listener: this.port },
97+
listenAddr: `[${this.ip}]:${this.config.port}`,
98+
ports: { discovery: this.config.port, listener: this.config.port },
10499
}
105100
}
106101
const id = this.rlpx._id.toString('hex')
107102
return {
108-
enode: `enode://${id}@[${this.ip}]:${this.port}`,
103+
enode: `enode://${id}@[${this.ip}]:${this.config.port}`,
109104
id: id,
110105
ip: this.ip,
111-
listenAddr: `[${this.ip}]:${this.port}`,
112-
ports: { discovery: this.port, listener: this.port },
106+
listenAddr: `[${this.ip}]:${this.config.port}`,
107+
ports: { discovery: this.config.port, listener: this.config.port },
113108
}
114109
}
115110

@@ -232,8 +227,8 @@ export class RlpxServer extends Server {
232227

233228
this.dpt.on('error', (e: Error) => this.error(e))
234229

235-
if (this.port) {
236-
this.dpt.bind(this.port, '0.0.0.0')
230+
if (this.config.port) {
231+
this.dpt.bind(this.config.port, '0.0.0.0')
237232
}
238233
}
239234

@@ -247,7 +242,7 @@ export class RlpxServer extends Server {
247242
maxPeers: this.config.maxPeers,
248243
capabilities: RlpxPeer.capabilities(Array.from(this.protocols)),
249244
remoteClientIdFilter: this.clientFilter,
250-
listenPort: this.port,
245+
listenPort: this.config.port,
251246
common: this.config.chainCommon,
252247
})
253248

@@ -303,8 +298,8 @@ export class RlpxServer extends Server {
303298
})
304299
})
305300

306-
if (this.port) {
307-
this.rlpx.listen(this.port, '0.0.0.0')
301+
if (this.config.port) {
302+
this.rlpx.listen(this.config.port, '0.0.0.0')
308303
}
309304
}
310305
}

packages/client/test/net/server/rlpxserver.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ tape('[RlpxServer]', async (t) => {
179179
const config = new Config({ loglevel: 'error', transports: [] })
180180
const server = new RlpxServer({ config })
181181
server.initDpt()
182-
td.verify((server.dpt as any).bind(server.port, '0.0.0.0'))
182+
td.verify((server.dpt as any).bind(server.config.port, '0.0.0.0'))
183183
server.on('error', (err: any) => t.equals(err, 'err0', 'got error'))
184184
;(server.dpt as any).emit('error', 'err0')
185185
})
@@ -193,7 +193,7 @@ tape('[RlpxServer]', async (t) => {
193193
td.when(RlpxPeer.prototype.accept(rlpxPeer, td.matchers.isA(RlpxServer))).thenResolve()
194194
server.initRlpx()
195195
td.verify(RlpxPeer.capabilities(Array.from((server as any).protocols)))
196-
td.verify(server.rlpx!.listen(server.port, '0.0.0.0'))
196+
td.verify(server.rlpx!.listen(server.config.port, '0.0.0.0'))
197197
server.on('connected', (peer: any) => t.ok(peer instanceof RlpxPeer, 'connected'))
198198
server.on('disconnected', (peer: any) => t.equals(peer.id, '01', 'disconnected'))
199199
server.on('error', (err: Error) => t.equals(err.message, 'err0', 'got error'))

0 commit comments

Comments
 (0)