Skip to content
This repository was archived by the owner on Dec 10, 2020. It is now read-only.

Commit c689480

Browse files
holgerd77ryanio
authored andcommitted
Replaced getTransportServers() with direct servers access, switched to readonly for all Config properties
1 parent 1b226e2 commit c689480

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

lib/config.ts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export interface Options {
6262
}
6363

6464
export class Config {
65+
// Initialize Common with an explicit 'chainstart' HF set until
66+
// hardfork awareness is implemented within the library
67+
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
6568
public static readonly COMMON_DEFAULT = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
6669
public static readonly LOGGER_DEFAULT = defaultLogger
6770
public static readonly SYNCMODE_DEFAULT = 'fast'
@@ -71,22 +74,18 @@ export class Config {
7174
public static readonly MINPEERS_DEFAULT = 2
7275
public static readonly MAXPEERS_DEFAULT = 25
7376

74-
public common: Common
75-
public logger: Logger
76-
public syncmode: string
77-
public lightserv: boolean
78-
public datadir: string
79-
public transports: string[]
80-
public minPeers: number
81-
public maxPeers: number
77+
public readonly common: Common
78+
public readonly logger: Logger
79+
public readonly syncmode: string
80+
public readonly lightserv: boolean
81+
public readonly datadir: string
82+
public readonly transports: string[]
83+
public readonly minPeers: number
84+
public readonly maxPeers: number
8285

83-
private servers: (RlpxServer | Libp2pServer)[] = []
86+
public readonly servers: (RlpxServer | Libp2pServer)[] = []
8487

8588
constructor(options: Options = {}) {
86-
// Initialize Common with an explicit 'chainstart' HF set until
87-
// hardfork awareness is implemented within the library
88-
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
89-
9089
// TODO: map chainParams (and lib/util.parseParams) to new Common format
9190
this.common = options.common ?? Config.COMMON_DEFAULT
9291
this.logger = options.logger ?? Config.LOGGER_DEFAULT
@@ -97,7 +96,20 @@ export class Config {
9796
this.minPeers = options.minPeers ?? Config.MINPEERS_DEFAULT
9897
this.maxPeers = options.maxPeers ?? Config.MAXPEERS_DEFAULT
9998

100-
this.servers = options.servers ? options.servers : this.getTransportServers()
99+
if (options.servers) {
100+
// Servers option takes precedence
101+
this.servers = options.servers
102+
} else {
103+
// Otherwise parse transports from transports option
104+
this.servers = parseTransports(this.transports).map((t) => {
105+
if (t.name === 'rlpx') {
106+
t.options.bootnodes = t.options.bootnodes || this.common.bootstrapNodes()
107+
return new RlpxServer({ config: this, ...t.options })
108+
} else {
109+
return new Libp2pServer({ config: this, ...t.options })
110+
}
111+
})
112+
}
101113
}
102114

103115
/**
@@ -112,21 +124,4 @@ export class Config {
112124
const dataDir = `${this.datadir}/${networkDirName}ethereumjs/${syncDirName}`
113125
return dataDir
114126
}
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-
}
132127
}

lib/net/peerpool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class PeerPool extends EventEmitter {
5555
if (this.opened) {
5656
return false
5757
}
58-
this.config.getTransportServers().map((s) => {
58+
this.config.servers.map((s) => {
5959
s.on('connected', (peer: Peer) => {
6060
this.connected(peer)
6161
})
@@ -196,7 +196,7 @@ export class PeerPool extends EventEmitter {
196196
if (this.size === 0) {
197197
this.noPeerPeriods += 1
198198
if (this.noPeerPeriods >= 3) {
199-
const promises = this.config.getTransportServers().map(async (server: any) => {
199+
const promises = this.config.servers.map(async (server: any) => {
200200
if (server.bootstrap) {
201201
this.config.logger.info('Retriggering bootstrap.')
202202
await server.bootstrap()

lib/service/service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class Service extends events.EventEmitter {
6969
return false
7070
}
7171
const protocols = this.protocols
72-
this.config.getTransportServers().map((s) => s.addProtocols(protocols))
72+
this.config.servers.map((s) => s.addProtocols(protocols))
7373
if (this.pool) {
7474
this.pool.on('banned', (peer: Peer) => this.config.logger.debug(`Peer banned: ${peer}`))
7575
this.pool.on('error', (error: Error) => this.emit('error', error))
@@ -100,7 +100,7 @@ export class Service extends events.EventEmitter {
100100
if (this.running) {
101101
return false
102102
}
103-
await Promise.all(this.config.getTransportServers().map((s) => s.start()))
103+
await Promise.all(this.config.servers.map((s) => s.start()))
104104
this.running = true
105105
this.config.logger.info(`Started ${this.name} service.`)
106106
}

0 commit comments

Comments
 (0)