@@ -3,12 +3,13 @@ import Common from '@ethereumjs/common'
33import { Logger } from 'winston'
44import { defaultLogger } from './logging'
55import { Libp2pServer , RlpxServer } from './net/server'
6+ import { parseTransports } from './util'
67
78export interface Options {
89 /**
910 * Specify the chain and hardfork by passing a Common instance.
1011 *
11- * Default: chain ` mainnet` and hardfork ` chainstart`
12+ * Default: chain ' mainnet' and hardfork ' chainstart'
1213 */
1314 common ?: Common
1415 /**
@@ -17,12 +18,6 @@ export interface Options {
1718 * Default: Logger with loglevel 'debug'
1819 */
1920 logger ?: Logger
20- /**
21- * Transport servers (RLPx or Libp2p)
22- *
23- * Default: []
24- */
25- servers ?: ( RlpxServer | Libp2pServer ) [ ]
2621 /**
2722 * Synchronization mode ('fast' or 'light')
2823 *
@@ -32,45 +27,60 @@ export interface Options {
3227 /**
3328 * Serve light peer requests
3429 *
35- * Default: false
30+ * Default: ` false`
3631 */
3732 lightserv ?: boolean
3833 /**
3934 * Root data directory for the blockchain
4035 */
4136 datadir ?: string
37+ /**
38+ * Network transports ('rlpx' and/or 'libp2p')
39+ *
40+ * Default: `['rlpx:port=30303', 'libp2p']`
41+ */
42+ transports ?: string [ ]
43+ /**
44+ * Transport servers (RLPx or Libp2p)
45+ * Use `transports` option, only used for testing purposes
46+ *
47+ * Default: servers created from `transports` option
48+ */
49+ servers ?: ( RlpxServer | Libp2pServer ) [ ]
4250 /**
4351 * Number of peers needed before syncing
4452 *
45- * Default: 2
53+ * Default: `2`
4654 */
4755 minPeers ?: number
4856 /**
4957 * Maximum peers allowed
5058 *
51- * Default: 25
59+ * Default: `25`
5260 */
5361 maxPeers ?: number
5462}
5563
5664export class Config {
65+ public static readonly COMMON_DEFAULT = new Common ( { chain : 'mainnet' , hardfork : 'chainstart' } )
66+ public static readonly LOGGER_DEFAULT = defaultLogger
67+ public static readonly SYNCMODE_DEFAULT = 'fast'
68+ public static readonly LIGHTSERV_DEFAULT = false
69+ public static readonly DATADIR_DEFAULT = `${ os . homedir ( ) } /Library/Ethereum`
70+ public static readonly TRANSPORTS_DEFAULT = [ 'rlpx:port=30303' , 'libp2p' ]
71+ public static readonly MINPEERS_DEFAULT = 2
72+ public static readonly MAXPEERS_DEFAULT = 25
73+
5774 public common : Common
5875 public logger : Logger
59- public servers : ( RlpxServer | Libp2pServer ) [ ]
6076 public syncmode : string
6177 public lightserv : boolean
6278 public datadir : string
79+ public transports : string [ ]
6380 public minPeers : number
6481 public maxPeers : number
6582
66- public static readonly COMMON_DEFAULT = new Common ( { chain : 'mainnet' , hardfork : 'chainstart' } )
67- public static readonly LOGGER_DEFAULT = defaultLogger
68- public static readonly SERVERS_DEFAULT = [ ]
69- public static readonly SYNCMODE_DEFAULT = 'fast'
70- public static readonly LIGHTSERV_DEFAULT = false
71- public static readonly DATADIR_DEFAULT = `${ os . homedir ( ) } /Library/Ethereum`
72- public static readonly MINPEERS_DEFAULT = 2
73- public static readonly MAXPEERS_DEFAULT = 25
83+ private servers : ( RlpxServer | Libp2pServer ) [ ] = [ ]
7484
7585 constructor ( options : Options = { } ) {
7686 // Initialize Common with an explicit 'chainstart' HF set until
@@ -80,12 +90,14 @@ export class Config {
8090 // TODO: map chainParams (and lib/util.parseParams) to new Common format
8191 this . common = options . common ?? Config . COMMON_DEFAULT
8292 this . logger = options . logger ?? Config . LOGGER_DEFAULT
83- this . servers = options . servers ?? Config . SERVERS_DEFAULT
8493 this . syncmode = options . syncmode ?? Config . SYNCMODE_DEFAULT
8594 this . lightserv = options . lightserv ?? Config . LIGHTSERV_DEFAULT
95+ this . transports = options . transports ?? Config . TRANSPORTS_DEFAULT
8696 this . datadir = options . datadir ?? Config . DATADIR_DEFAULT
8797 this . minPeers = options . minPeers ?? Config . MINPEERS_DEFAULT
8898 this . maxPeers = options . maxPeers ?? Config . MAXPEERS_DEFAULT
99+
100+ this . servers = options . servers ? options . servers : this . getTransportServers ( )
89101 }
90102
91103 /**
@@ -100,4 +112,21 @@ export class Config {
100112 const dataDir = `${ this . datadir } /${ networkDirName } ethereumjs/${ syncDirName } `
101113 return dataDir
102114 }
115+
116+ /**
117+ * Returns the transport servers created from the `transports` option
118+ */
119+ getTransportServers ( ) : ( RlpxServer | Libp2pServer ) [ ] {
120+ if ( this . servers . length === 0 ) {
121+ this . servers = parseTransports ( this . transports ) . map ( ( t ) => {
122+ if ( t . name === 'rlpx' ) {
123+ t . options . bootnodes = t . options . bootnodes || this . common . bootstrapNodes ( )
124+ return new RlpxServer ( { config : this , ...t . options } )
125+ } else {
126+ return new Libp2pServer ( { config : this , ...t . options } )
127+ }
128+ } )
129+ }
130+ return this . servers
131+ }
103132}
0 commit comments