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

Commit ddaee2e

Browse files
holgerd77ryanio
authored andcommitted
Moved Config to be a simple class with a dedicated entry point instead of using the singleton pattern
1 parent 7b1035c commit ddaee2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+202
-153
lines changed

bin/cli.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { fromName: serverFromName } = require('../lib/net/server')
77
import Node from '../lib/node'
88
import { Server as RPCServer } from 'jayson'
99
import { Config } from '../lib/config'
10+
import Common from '@ethereumjs/common'
1011
const RPCManager = require('../lib/rpc')
1112
const level = require('level')
1213
const os = require('os')
@@ -82,7 +83,7 @@ const args = require('yargs')
8283
.locale('en_EN').argv
8384
const logger = getLogger({ loglevel: args.loglevel })
8485

85-
async function runNode(options: any, config: Config) {
86+
async function runNode(options: any) {
8687
logger.info('Initializing Ethereumjs client...')
8788
if (options.lightserv) {
8889
logger.info(`Serving light peer requests`)
@@ -95,7 +96,7 @@ async function runNode(options: any, config: Config) {
9596
node.on('synchronized', () => {
9697
logger.info('Synchronized')
9798
})
98-
logger.info(`Connecting to network: ${config.common.chainName()}`)
99+
logger.info(`Connecting to network: ${options.config.common.chainName()}`)
99100
await node.open()
100101
logger.info('Synchronizing blockchain...')
101102
await node.start()
@@ -114,13 +115,6 @@ function runRpcServer(node: any, options: any) {
114115
}
115116

116117
async function run() {
117-
const config = new Config({
118-
logger,
119-
syncmode: args.syncmode,
120-
minPeers: args.minPeers,
121-
maxPeers: args.maxPeers,
122-
})
123-
124118
const syncDirName = args.syncmode === 'light' ? 'lightchaindata' : 'chaindata'
125119
// give network id precedence over network name
126120
if (args.networkId) {
@@ -131,6 +125,15 @@ async function run() {
131125
}
132126
const networkDirName = args.network === 'mainnet' ? '' : `${args.network}/`
133127

128+
const common = new Common({ chain: args.network, hardfork: 'chainstart' })
129+
const config = new Config({
130+
common,
131+
logger,
132+
syncmode: args.syncmode,
133+
minPeers: args.minPeers,
134+
maxPeers: args.maxPeers,
135+
})
136+
134137
// TODO: see todo below wrt resolving chain param parsing
135138
// eslint-disable-next-line @typescript-eslint/no-unused-vars
136139
const chainParams = args.params ? await parseParams(args.params) : args.network
@@ -140,21 +143,22 @@ async function run() {
140143
if (t.name === 'rlpx') {
141144
t.options.bootnodes = t.options.bootnodes || config.common.bootstrapNodes()
142145
}
143-
return new Server({ logger, ...t.options })
146+
return new Server({ config, ...t.options })
144147
})
145148
const dataDir = `${args.datadir}/${networkDirName}ethereumjs/${syncDirName}`
146149

147150
fs.ensureDirSync(dataDir)
148151
logger.info(`Data directory: ${dataDir}`)
149152

150153
const options = {
154+
config,
151155
servers,
152156
lightserv: args.lightserv,
153157
db: level(dataDir),
154158
rpcport: args.rpcport,
155159
rpcaddr: args.rpcaddr,
156160
}
157-
const node = await runNode(options, config)
161+
const node = await runNode(options)
158162
const server = args.rpc ? runRpcServer(node, options) : null
159163

160164
process.on('SIGINT', async () => {

lib/blockchain/chain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ChainOptions {
1212
/**
1313
* Client configuration instance
1414
*/
15-
config?: Config
15+
config: Config
1616

1717
/**
1818
* Database to store blocks and metadata. Should be an abstract-leveldown compliant store.
@@ -99,10 +99,10 @@ export class Chain extends EventEmitter {
9999
* Create new chain
100100
* @param {ChainOptions} options
101101
*/
102-
constructor(options: ChainOptions = {}) {
102+
constructor(options: ChainOptions) {
103103
super()
104104

105-
this.config = new Config()
105+
this.config = options.config
106106

107107
this.blockchain =
108108
options.blockchain ||

lib/config.ts

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,24 @@ export interface Options {
2828
}
2929

3030
export class Config {
31-
public common!: Common
32-
public logger!: Logger
33-
public syncmode!: string
34-
public minPeers!: number
35-
public maxPeers!: number
36-
37-
static instance: Config
31+
public common: Common
32+
public logger: Logger
33+
public syncmode: string
34+
public minPeers: number
35+
public maxPeers: number
3836

3937
constructor(options: Options = {}) {
40-
if (!Config.instance) {
41-
Config.instance = this
42-
43-
// Set default values on first initialization
44-
45-
// Initialize Common with an explicit 'chainstart' HF set until
46-
// hardfork awareness is implemented within the library
47-
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
48-
49-
// TODO: map chainParams (and lib/util.parseParams) to new Common format
50-
Config.instance.common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
51-
Config.instance.logger = defaultLogger
52-
Config.instance.syncmode = 'fast'
53-
Config.instance.minPeers = 3
54-
Config.instance.maxPeers = 25
55-
}
56-
57-
if (options.common) {
58-
Config.instance.common = options.common
59-
}
60-
if (options.logger) {
61-
Config.instance.logger = options.logger
62-
}
63-
if (options.syncmode) {
64-
Config.instance.syncmode = options.syncmode
65-
}
66-
if (options.minPeers) {
67-
Config.instance.minPeers = options.minPeers
68-
}
69-
if (options.maxPeers) {
70-
Config.instance.maxPeers = options.maxPeers
71-
}
72-
73-
return Config.instance
38+
// Initialize Common with an explicit 'chainstart' HF set until
39+
// hardfork awareness is implemented within the library
40+
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
41+
42+
// TODO: map chainParams (and lib/util.parseParams) to new Common format
43+
this.common = options.common
44+
? options.common
45+
: new Common({ chain: 'mainnet', hardfork: 'chainstart' })
46+
this.logger = options.logger ? options.logger : defaultLogger
47+
this.syncmode = options.syncmode ? options.syncmode : 'fast'
48+
this.minPeers = options.minPeers ? options.minPeers : 3
49+
this.maxPeers = options.maxPeers ? options.maxPeers : 25
7450
}
7551
}

lib/net/peer/peer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Peer extends events.EventEmitter {
4141
constructor(options: any) {
4242
super()
4343

44-
this.config = new Config()
44+
this.config = options.config
4545

4646
options = { ...defaultOptions, ...options }
4747

lib/net/peerpool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class PeerPool extends EventEmitter {
3939
constructor(options: any) {
4040
super()
4141

42-
this.config = new Config()
42+
this.config = options.config
4343

4444
options = { ...defaultOptions, ...options }
4545

lib/net/protocol/boundprotocol.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class BoundProtocol extends EventEmitter {
2323
/**
2424
* Create bound protocol
2525
* @param {Object} options constructor parameters
26+
* @param {Config} [options.config] Client configuration
2627
* @param {Protocol} options.protocol protocol to bind
2728
* @param {Peer} options.peer peer that protocol is bound to
2829
* @param {Sender} options.sender message sender
@@ -31,7 +32,7 @@ export class BoundProtocol extends EventEmitter {
3132
constructor(options: any) {
3233
super()
3334

34-
this.config = new Config()
35+
this.config = options.config
3536

3637
this.protocol = options.protocol
3738
this.peer = options.peer

lib/net/protocol/ethprotocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class EthProtocol extends Protocol {
5555
/**
5656
* Create eth protocol
5757
* @param {Object} options constructor parameters
58+
* @param {Config} [options.config] Client configuration
5859
* @param {Chain} options.chain blockchain
5960
* @param {number} [options.timeout=8000] handshake timeout in ms
6061
*/

lib/net/protocol/lesprotocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class LesProtocol extends Protocol {
6767
/**
6868
* Create les protocol
6969
* @param {Object} options constructor parameters
70+
* @param {Config} [options.config] Client configuration
7071
* @param {Chain} options.chain blockchain
7172
* @param {FlowControl} [options.flow] flow control manager. if undefined,
7273
* header serving will be disabled

lib/net/protocol/protocol.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ export class Protocol extends EventEmitter {
4343
/**
4444
* Create new protocol
4545
* @param {Object} options constructor parameters
46+
* @param {Config} [options.config] Client configuration
4647
* @param {number} [options.timeout=8000] handshake timeout in ms
4748
*/
4849
constructor(options?: any) {
4950
super()
5051

51-
this.config = new Config()
52+
this.config = options.config
5253

5354
options = { ...defaultOptions, ...options }
5455
this.timeout = options.timeout
@@ -170,6 +171,7 @@ export class Protocol extends EventEmitter {
170171
*/
171172
async bind(peer: Peer, sender: Sender): Promise<BoundProtocol> {
172173
const bound = new BoundProtocol({
174+
config: this.config,
173175
protocol: this,
174176
peer: peer,
175177
sender: sender,

lib/net/server/libp2pserver.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class Libp2pServer extends Server {
2626
/**
2727
* Create new DevP2P/RLPx server
2828
* @param {Object} options constructor parameters
29+
* @param {Config} [options.config] Client configuration
2930
* @param {Object[]} [options.bootnodes] list of bootnodes to use for discovery (can be
3031
* a comma separated string or list)
3132
* @param {multiaddr[]} [options.multiaddrs] multiaddrs to listen on (can be
@@ -225,6 +226,7 @@ export class Libp2pServer extends Server {
225226

226227
createPeer(peerInfo: any) {
227228
const peer = new Libp2pPeer({
229+
config: this.config,
228230
id: peerInfo.id.toB58String(),
229231
multiaddrs: peerInfo.multiaddrs.toArray().map((ma: any) => ma.toString()),
230232
protocols: Array.from(this.protocols),

0 commit comments

Comments
 (0)