Skip to content

Commit 18a323b

Browse files
committed
client: split cli transports option for bootnodes and multiaddrs
1 parent e8bdbf6 commit 18a323b

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

packages/client/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ listener. The second client will use libp2p to connect to the first client.
213213
Run the first client and start downloading blocks:
214214

215215
```
216-
ethereumjs --syncmode full --lightserv true --datadir first --network rinkeby --transports rlpx libp2p:multiaddrs=/ip4/127.0.0.1/tcp/50505/ws
216+
ethereumjs --syncmode full --lightserv true --datadir first --network rinkeby --transports rlpx libp2p --multiaddrs /ip4/127.0.0.1/tcp/50505/ws
217217
```
218218

219219
Output:
@@ -230,10 +230,10 @@ Copy the libp2p URL from the output. In this example, the url is `/ip4/127.0.0.1
230230
Wait until a few thousand blocks are downloaded and then run the second client in a new terminal, using the url above to connect to the first client:
231231

232232
<pre>
233-
ethereumjs --syncmode light --network rinkeby --datadir second --transports libp2p:multiaddrs=/ip4/0.0.0.0/tcp/50506,bootnodes=<b>/ip4/127.0.0.1/tcp/50505/ws/p2p/QmYAuYxw6QX1x5aafs6g3bUrPbMDifP5pDun3N9zbVLpEa</b>
233+
ethereumjs --syncmode light --network rinkeby --datadir second --transports libp2p --multiaddrs /ip4/0.0.0.0/tcp/50506 --bootnodes=<b>/ip4/127.0.0.1/tcp/50505/ws/p2p/QmYAuYxw6QX1x5aafs6g3bUrPbMDifP5pDun3N9zbVLpEa</b>
234234
</pre>
235235

236-
Notice that we have to run the second client on port 50506 using the `multiaddrs=/ip4/0.0.0.0/tcp/50506` libp2p option to avoid port conflicts.
236+
Notice that we have to run the second client on port 50506 using the `--multiaddrs /ip4/0.0.0.0/tcp/50506` libp2p option to avoid port conflicts.
237237

238238
### Example 2: Light sync from within a browser
239239

packages/client/bin/cli.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Server as RPCServer } from 'jayson/promise'
44
import Common from '@ethereumjs/common'
5-
import { parseParams } from '../lib/util'
5+
import { parseParams, parseMultiaddrs } from '../lib/util'
66
import EthereumClient from '../lib/client'
77
import { Config } from '../lib/config'
88
import { Logger } from '../lib/logging'
@@ -46,6 +46,14 @@ const args = require('yargs')
4646
default: Config.TRANSPORTS_DEFAULT,
4747
array: true,
4848
},
49+
bootnodes: {
50+
describe: 'Network bootnodes',
51+
array: true,
52+
},
53+
multiaddrs: {
54+
describe: 'Network multiaddrs',
55+
array: true,
56+
},
4957
rpc: {
5058
describe: 'Enable the JSON-RPC server',
5159
boolean: true,
@@ -173,6 +181,8 @@ async function run() {
173181
lightserv: args.lightserv,
174182
datadir: args.datadir,
175183
transports: args.transports,
184+
bootnodes: args.bootnodes ? parseMultiaddrs(args.bootnodes) : undefined,
185+
multiaddrs: args.multiaddrs ? parseMultiaddrs(args.multiaddrs) : undefined,
176186
rpc: args.rpc,
177187
rpcport: args.rpcport,
178188
rpcaddr: args.rpcaddr,
@@ -203,4 +213,5 @@ async function run() {
203213
})
204214
}
205215

206-
run().catch((err) => logger!.error(err))
216+
// eslint-disable-next-line no-console
217+
run().catch((err) => logger?.error(err) ?? console.error(err))

packages/client/lib/config.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Common from '@ethereumjs/common'
22
import VM from '@ethereumjs/vm'
3+
import Multiaddr from 'multiaddr'
34
import { getLogger, Logger } from './logging'
45
import { Libp2pServer, RlpxServer } from './net/server'
5-
import { parseMultiaddrs, parseTransports } from './util'
6+
import { parseTransports } from './util'
67

78
export interface ConfigOptions {
89
/**
@@ -46,6 +47,18 @@ export interface ConfigOptions {
4647
*/
4748
transports?: string[]
4849

50+
/**
51+
* Network bootnodes
52+
* (e.g. [email protected] or /ip4/127.0.0.1/tcp/50505/p2p/QmABC)
53+
*/
54+
bootnodes?: Multiaddr[]
55+
56+
/**
57+
* Network multiaddrs for libp2p
58+
* (e.g. /ip4/127.0.0.1/tcp/50505/p2p/QmABC)
59+
*/
60+
multiaddrs?: Multiaddr[]
61+
4962
/**
5063
* Transport servers (RLPx or Libp2p)
5164
* Use `transports` option, only used for testing purposes
@@ -160,6 +173,8 @@ export class Config {
160173
public readonly lightserv: boolean
161174
public readonly datadir: string
162175
public readonly transports: string[]
176+
public readonly bootnodes?: Multiaddr[]
177+
public readonly multiaddrs?: Multiaddr[]
163178
public readonly rpc: boolean
164179
public readonly rpcport: number
165180
public readonly rpcaddr: string
@@ -181,6 +196,8 @@ export class Config {
181196
this.vm = options.vm
182197
this.lightserv = options.lightserv ?? Config.LIGHTSERV_DEFAULT
183198
this.transports = options.transports ?? Config.TRANSPORTS_DEFAULT
199+
this.bootnodes = options.bootnodes
200+
this.multiaddrs = options.multiaddrs
184201
this.datadir = options.datadir ?? Config.DATADIR_DEFAULT
185202
this.rpc = options.rpc ?? Config.RPC_DEFAULT
186203
this.rpcport = options.rpcport ?? Config.RPCPORT_DEFAULT
@@ -217,25 +234,20 @@ export class Config {
217234
'Config initialization with both servers and transports options not allowed'
218235
)
219236
}
220-
221237
// Servers option takes precedence
222238
this.servers = options.servers
223239
} else {
224240
// Otherwise parse transports from transports option
225241
this.servers = parseTransports(this.transports).map((t) => {
226-
// format multiaddrs as multiaddr[]
227-
if (t.options.multiaddrs) {
228-
t.options.multiaddrs = parseMultiaddrs(t.options.multiaddrs) as any
229-
}
230242
if (t.name === 'rlpx') {
231-
if (!t.options.bootnodes) {
232-
t.options.bootnodes = this.chainCommon.bootstrapNodes()
233-
}
234-
t.options.bootnodes = parseMultiaddrs(t.options.bootnodes) as any
235-
t.options.dnsNetworks = options.dnsNetworks ?? this.chainCommon.dnsNetworks()
236-
return new RlpxServer({ config: this, ...t.options })
243+
const bootnodes = this.bootnodes ?? this.chainCommon.bootstrapNodes()
244+
const dnsNetworks = options.dnsNetworks ?? this.chainCommon.dnsNetworks()
245+
return new RlpxServer({ config: this, bootnodes, dnsNetworks })
237246
} else {
238-
return new Libp2pServer({ config: this, ...t.options })
247+
// t.name === 'libp2p'
248+
const multiaddrs = this.multiaddrs
249+
const bootnodes = this.bootnodes
250+
return new Libp2pServer({ config: this, multiaddrs, bootnodes })
239251
}
240252
})
241253
}

packages/client/lib/util/parse.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { SecureTrie as Trie } from 'merkle-patricia-tree'
55
import { Account, BN, keccak, rlp, toBuffer, unpadBuffer, isHexPrefixed } from 'ethereumjs-util'
66
import { MultiaddrLike } from '../types'
77

8+
/**
9+
* Parses multiaddrs and bootnodes to multiaddr format.
10+
*/
811
export function parseMultiaddrs(input: MultiaddrLike): multiaddr[] {
912
if (!input) {
1013
return []

0 commit comments

Comments
 (0)