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

Commit e6581d2

Browse files
authored
Merge pull request #161 from ethereumjs/integrate-singleton-config-class
New Config Class
2 parents 5be9aa8 + 6fd730d commit e6581d2

File tree

7 files changed

+54
-25
lines changed

7 files changed

+54
-25
lines changed

bin/cli.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env node
22

3-
const Common = require('@ethereumjs/common').default
43
const chains = require('@ethereumjs/common/dist/chains').chains
54
const { getLogger } = require('../lib/logging')
65
const { parseParams, parseTransports } = require('../lib/util')
76
const { fromName: serverFromName } = require('../lib/net/server')
87
import Node from '../lib/node'
98
import { Server as RPCServer } from 'jayson'
9+
import { Config } from '../lib/config'
1010
const RPCManager = require('../lib/rpc')
1111
const level = require('level')
1212
const os = require('os')
@@ -95,7 +95,7 @@ async function runNode(options: any) {
9595
node.on('synchronized', () => {
9696
logger.info('Synchronized')
9797
})
98-
logger.info(`Connecting to network: ${options.common.chainName()}`)
98+
logger.info(`Connecting to network: ${options.config.common.chainName()}`)
9999
await node.open()
100100
logger.info('Synchronizing blockchain...')
101101
await node.start()
@@ -128,16 +128,11 @@ async function run() {
128128
// eslint-disable-next-line @typescript-eslint/no-unused-vars
129129
const chainParams = args.params ? await parseParams(args.params) : args.network
130130

131-
// Initialize Common with an explicit 'chainstart' HF set until
132-
// hardfork awareness is implemented within the library
133-
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
134-
135-
// TODO: map chainParams (and lib/util.parseParams) to new Common format
136-
const common = new Common({ chain: args.network, hardfork: 'chainstart' })
131+
const config = new Config()
137132
const servers = parseTransports(args.transports).map((t: any) => {
138133
const Server = serverFromName(t.name)
139134
if (t.name === 'rlpx') {
140-
t.options.bootnodes = t.options.bootnodes || common.bootstrapNodes()
135+
t.options.bootnodes = t.options.bootnodes || config.common.bootstrapNodes()
141136
}
142137
return new Server({ logger, ...t.options })
143138
})
@@ -147,7 +142,7 @@ async function run() {
147142
logger.info(`Data directory: ${dataDir}`)
148143

149144
const options = {
150-
common,
145+
config,
151146
logger,
152147
servers,
153148
syncmode: args.syncmode,

lib/config.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Common from '@ethereumjs/common'
2+
3+
export interface Options {
4+
/**
5+
* Specify the chain and hardfork by passing a Common instance.
6+
*
7+
* If not provided this defaults to chain `mainnet` and hardfork `chainstart`
8+
*/
9+
common?: Common
10+
}
11+
12+
export class Config {
13+
public common!: Common
14+
15+
static instance: Config
16+
17+
constructor(options: Options = {}) {
18+
if (Config.instance) {
19+
return Config.instance
20+
}
21+
22+
// Initialize Common with an explicit 'chainstart' HF set until
23+
// hardfork awareness is implemented within the library
24+
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
25+
26+
// TODO: map chainParams (and lib/util.parseParams) to new Common format
27+
this.common = options.common || new Common({ chain: 'mainnet', hardfork: 'chainstart' })
28+
29+
Config.instance = this
30+
}
31+
}

lib/node.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as events from 'events'
22
import { FastEthereumService, LightEthereumService } from './service'
33
import { defaultLogger } from './logging'
4+
import { Config } from './config'
45

56
const defaultOptions = {
67
minPeers: 3,
@@ -15,8 +16,9 @@ const defaultOptions = {
1516
* @memberof module:node
1617
*/
1718
export default class Node extends events.EventEmitter {
19+
public config: Config
20+
1821
public logger: any
19-
public common: any
2022
public servers: any
2123
public syncmode: any
2224
public services: any
@@ -27,8 +29,8 @@ export default class Node extends events.EventEmitter {
2729
/**
2830
* Create new node
2931
* @param {Object} options constructor parameters
32+
* @param {Config} [options.config] Client configuration
3033
* @param {Logger} [options.logger] Logger instance
31-
* @param {Common} [options.common] common parameters
3234
* @param {LevelDB} [options.db=null] blockchain database
3335
* @param {string} [options.syncmode=light] synchronization mode ('fast' or 'light')
3436
* @param {boolean} [options.lightserv=false] serve LES requests
@@ -42,9 +44,8 @@ export default class Node extends events.EventEmitter {
4244
constructor(options: any) {
4345
super()
4446
options = { ...defaultOptions, ...options }
45-
47+
this.config = options.config || new Config()
4648
this.logger = options.logger
47-
this.common = options.common
4849
this.servers = options.servers
4950
this.syncmode = options.syncmode
5051
this.services = [
@@ -53,15 +54,15 @@ export default class Node extends events.EventEmitter {
5354
servers: this.servers,
5455
logger: this.logger,
5556
lightserv: options.lightserv,
56-
common: options.common,
57+
config: this.config,
5758
minPeers: options.minPeers,
5859
maxPeers: options.maxPeers,
5960
db: options.db,
6061
})
6162
: new LightEthereumService({
6263
servers: this.servers,
6364
logger: this.logger,
64-
common: options.common,
65+
config: this.config,
6566
minPeers: options.minPeers,
6667
maxPeers: options.maxPeers,
6768
db: options.db,

lib/service/ethereumservice.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Service } from './service'
22
import { FlowControl } from '../net/protocol/flowcontrol'
33
import { Chain } from '../blockchain'
4-
import Common from '@ethereumjs/common'
4+
import { Config } from '../config'
55

66
const defaultOptions = {
77
lightserv: false,
8-
common: new Common({ chain: 'mainnet', hardfork: 'chainstart' }),
98
minPeers: 3,
109
timeout: 8000,
1110
interval: 1000,
@@ -16,9 +15,10 @@ const defaultOptions = {
1615
* @memberof module:service
1716
*/
1817
export class EthereumService extends Service {
18+
public config: Config
19+
1920
public flow: FlowControl
2021
public chain: Chain
21-
public common: Common
2222
public minPeers: number
2323
public interval: number
2424
public timeout: number
@@ -27,10 +27,10 @@ export class EthereumService extends Service {
2727
/**
2828
* Create new ETH service
2929
* @param {Object} options constructor parameters
30+
* @param {Config} [options.config] Client configuration
3031
* @param {Server[]} options.servers servers to run service on
3132
* @param {Chain} [options.chain] blockchain
3233
* @param {LevelDB} [options.db=null] blockchain database
33-
* @param {Common} [options.common] ethereum network name
3434
* @param {number} [options.minPeers=3] number of peers needed before syncing
3535
* @param {number} [options.maxPeers=25] maximum peers allowed
3636
* @param {number} [options.timeout] protocol timeout
@@ -41,9 +41,10 @@ export class EthereumService extends Service {
4141
options = { ...defaultOptions, ...options }
4242
super(options)
4343

44+
this.config = options.config || new Config()
45+
4446
this.flow = new FlowControl(options)
4547
this.chain = options.chain || new Chain(options)
46-
this.common = options.common
4748
this.minPeers = options.minPeers
4849
this.interval = options.interval
4950
this.timeout = options.timeout

lib/service/fastethereumservice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export class FastEthereumService extends EthereumService {
1818
/**
1919
* Create new ETH service
2020
* @param {Object} options constructor parameters
21+
* @param {Config} [options.config] Client configuration
2122
* @param {Server[]} options.servers servers to run service on
2223
* @param {boolean} [options.lightserv=false] serve LES requests
2324
* @param {Chain} [options.chain] blockchain
24-
* @param {Common} [options.common] ethereum network name
2525
* @param {number} [options.minPeers=3] number of peers needed before syncing
2626
* @param {number} [options.maxPeers=25] maximum peers allowed
2727
* @param {number} [options.interval] sync retry interval
@@ -40,7 +40,7 @@ export class FastEthereumService extends EthereumService {
4040
logger: this.logger,
4141
pool: this.pool,
4242
chain: this.chain,
43-
common: this.common,
43+
common: this.config.common,
4444
minPeers: this.minPeers,
4545
interval: this.interval,
4646
})

lib/service/lightethereumservice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export class LightEthereumService extends EthereumService {
1111
/**
1212
* Create new ETH service
1313
* @param {Object} options constructor parameters
14+
* @param {Config} [options.config] Client configuration
1415
* @param {Server[]} options.servers servers to run service on
1516
* @param {Chain} [options.chain] blockchain
16-
* @param {Common} [options.common] ethereum network name
1717
* @param {number} [options.minPeers=3] number of peers needed before syncing
1818
* @param {number} [options.maxPeers=25] maximum peers allowed
1919
* @param {number} [options.interval] sync retry interval
@@ -30,7 +30,7 @@ export class LightEthereumService extends EthereumService {
3030
logger: this.logger,
3131
pool: this.pool,
3232
chain: this.chain,
33-
common: this.common,
33+
common: this.config.common,
3434
minPeers: this.minPeers,
3535
flow: this.flow,
3636
interval: this.interval,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"@types/node": "^14.11.5",
9494
"@types/tape": "^4.13.0",
9595
"@types/tape-catch": "^1.0.0",
96+
"eslint": "^6.8.0",
9697
"file-replace-loader": "^1.2.0",
9798
"husky": "^2.1.0",
9899
"json-to-markdown": "^1.0.4",

0 commit comments

Comments
 (0)