Skip to content

Commit 8c656da

Browse files
authored
Merge pull request #1147 from ethereumjs/local-client-connectivity
Client: local client connections for debugging
2 parents 2c455a7 + 8861290 commit 8c656da

File tree

7 files changed

+93
-22
lines changed

7 files changed

+93
-22
lines changed

packages/client/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,61 @@ to help contributors better understand how the project is organized.
337337

338338
## Developer
339339

340+
### Debugging
341+
342+
#### Networking
343+
344+
##### Local Connection: EthereumJS <- EthereumJS
345+
346+
For debugging on networking issues there are two custom npm start scripts with appropriate settings.
347+
348+
Start a first client listening on the default port and using the default data directory with:
349+
350+
```shell
351+
DEBUG=devp2p:* npm run client:start:dev1
352+
DEBUG=devp2p:* npm run client:start:dev1 -- --datadir=datadir-dev1
353+
```
354+
355+
Then take the enode address from the started client instance (use `127.0.0.1` for the IP address) and start a second client with:
356+
357+
```shell
358+
DEBUG=devp2p:* npm run client:start:dev2 -- --bootnodes=enode://[DEV1_NODE_ID]@127.0.0.1:30303
359+
```
360+
361+
This second client is using './datadir-dev2' for its data directory.
362+
363+
##### Local Connection: EthereumJS <- Geth
364+
365+
To connect Geth to a running EthereumJS instance start a client with:
366+
367+
```shell
368+
DEBUG=devp2p:* npm run client:start:dev1
369+
```
370+
371+
Then connect with your Geth instance via:
372+
373+
```shell
374+
geth --maxpeers=1 --bootnodes=enode://[DEV1_NODE_ID]@127.0.0.1:30303
375+
```
376+
377+
Depending on your use case you might want to turn off your internet connection to not allow further incoming connections on your Geth instance in case the EthereumJS connection gets disconnected.
378+
379+
##### Local Connection: Geth <- EthereumJS
380+
381+
Start your Geth instance:
382+
383+
```shell
384+
geth [--syncmode=full] [--verbosity=5]
385+
```
386+
387+
Note that you might want to turn off your internet connection to limit on the Geth discovery process (setting the `--nodiscover` flag won't work since this also disallows our local client from connecting).
388+
389+
Then connect with your EthereumJS instance via:
390+
391+
```shell
392+
DEBUG=devp2p:* npm run client:start:dev2 -- --bootnodes=enode://[GETH_NODE_ID]@127.0.0.1:30303
393+
```
394+
340395
### Diagram Updates
341396

342397
To update the structure diagram files in the root folder open the `client.drawio` file in [draw.io](https://draw.io/), make your changes, and open a PR with the updated files. Export `svg` and `png` with `border` `width=20` and `transparency=false`. For `png` go to "Advanced" and select `300 DPI`.

packages/client/bin/cli.ts

Lines changed: 7 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,
@@ -176,6 +180,8 @@ async function run() {
176180

177181
const common = new Common({ chain, hardfork: 'chainstart' })
178182
const datadir = args.datadir ?? Config.DATADIR_DEFAULT
183+
const configDirectory = `${datadir}/${common.chainName()}/config`
184+
fs.ensureDirSync(configDirectory)
179185
const key = await Config.getClientKey(datadir, common)
180186
const config = new Config({
181187
common,
@@ -185,6 +191,7 @@ async function run() {
185191
key,
186192
transports: args.transports,
187193
bootnodes: args.bootnodes ? parseMultiaddrs(args.bootnodes) : undefined,
194+
port: args.port,
188195
multiaddrs: args.multiaddrs ? parseMultiaddrs(args.multiaddrs) : undefined,
189196
rpc: args.rpc,
190197
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/peerpool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class PeerPool extends EventEmitter {
204204
this.noPeerPeriods += 1
205205
if (this.noPeerPeriods >= 3) {
206206
const promises = this.config.servers.map(async (server) => {
207-
if (server instanceof RlpxServer) {
207+
if (server instanceof RlpxServer && server.discovery) {
208208
this.config.logger.info('Restarting RLPx server: bootstrap')
209209
await server.stop()
210210
await server.start()
@@ -215,11 +215,11 @@ export class PeerPool extends EventEmitter {
215215
} else {
216216
let tablesize: number | undefined = 0
217217
this.config.servers.forEach((server) => {
218-
if (server instanceof RlpxServer) {
218+
if (server instanceof RlpxServer && server.discovery) {
219219
tablesize = server.dpt?.getPeers().length
220+
this.config.logger.info(`Looking for suited peers: peertablesize=${tablesize}`)
220221
}
221222
})
222-
this.config.logger.info(`Looking for suited peers: peertablesize=${tablesize}`)
223223
}
224224
} else {
225225
this.noPeerPeriods = 0

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

Lines changed: 12 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,7 @@ const ignoredErrors = new RegExp(
4946
export class RlpxServer extends Server {
5047
private peers: Map<string, RlpxPeer> = new Map()
5148

52-
public port: number
49+
public discovery: boolean
5350
private clientFilter: string[]
5451

5552
public rlpx: Devp2pRLPx | null = null
@@ -65,7 +62,7 @@ export class RlpxServer extends Server {
6562

6663
// TODO: get the external ip from the upnp service
6764
this.ip = '::'
68-
this.port = options.port ?? 30303
65+
this.discovery = options.config.discV4 || options.config.discDns
6966
this.clientFilter = options.clientFilter ?? [
7067
'go1.5',
7168
'go1.6',
@@ -97,17 +94,17 @@ export class RlpxServer extends Server {
9794
enode: undefined,
9895
id: undefined,
9996
ip: this.ip,
100-
listenAddr: `[${this.ip}]:${this.port}`,
101-
ports: { discovery: this.port, listener: this.port },
97+
listenAddr: `[${this.ip}]:${this.config.port}`,
98+
ports: { discovery: this.config.port, listener: this.config.port },
10299
}
103100
}
104101
const id = this.rlpx._id.toString('hex')
105102
return {
106-
enode: `enode://${id}@[${this.ip}]:${this.port}`,
103+
enode: `enode://${id}@[${this.ip}]:${this.config.port}`,
107104
id: id,
108105
ip: this.ip,
109-
listenAddr: `[${this.ip}]:${this.port}`,
110-
ports: { discovery: this.port, listener: this.port },
106+
listenAddr: `[${this.ip}]:${this.config.port}`,
107+
ports: { discovery: this.config.port, listener: this.config.port },
111108
}
112109
}
113110

@@ -230,8 +227,8 @@ export class RlpxServer extends Server {
230227

231228
this.dpt.on('error', (e: Error) => this.error(e))
232229

233-
if (this.port) {
234-
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')
235232
}
236233
}
237234

@@ -245,7 +242,7 @@ export class RlpxServer extends Server {
245242
maxPeers: this.config.maxPeers,
246243
capabilities: RlpxPeer.capabilities(Array.from(this.protocols)),
247244
remoteClientIdFilter: this.clientFilter,
248-
listenPort: this.port,
245+
listenPort: this.config.port,
249246
common: this.config.chainCommon,
250247
})
251248

@@ -301,8 +298,8 @@ export class RlpxServer extends Server {
301298
})
302299
})
303300

304-
if (this.port) {
305-
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')
306303
}
307304
}
308305
}

packages/client/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"prepublishOnly": "npm run build && npm run test",
2020
"bundle": "webpack",
2121
"client:start": "ts-node bin/cli.ts",
22+
"client:start:dev1": "npm run client:start -- --discDns=false --discV4=false --bootnodes",
23+
"client:start:dev2": "npm run client:start -- --discDns=false --discV4=false --transports=rlpx --port=30304 --datadir=datadir-dev2",
2224
"coverage": "nyc npm run test && nyc report --reporter=lcov",
2325
"docs:build": "typedoc --tsconfig tsconfig.prod.json",
2426
"lint": "ethereumjs-config-lint",

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)