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

Commit eb977b4

Browse files
holgerd77evertonfraga
authored andcommitted
Added Config singleton class to unify configuration parameter access and instantiation
1 parent 5be9aa8 commit eb977b4

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
14+
public common!: Common
15+
16+
static instance: Config
17+
18+
constructor(options: Options = {}) {
19+
if (Config.instance) {
20+
return Config.instance
21+
}
22+
23+
// Initialize Common with an explicit 'chainstart' HF set until
24+
// hardfork awareness is implemented within the library
25+
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
26+
27+
// TODO: map chainParams (and lib/util.parseParams) to new Common format
28+
this.common = options.common || new Common({ chain: 'mainnet', hardfork: 'chainstart' })
29+
30+
Config.instance = this
31+
}
32+
33+
34+
35+
}

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+
common: this.config.common,
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+
common: this.config.common,
6566
minPeers: options.minPeers,
6667
maxPeers: options.maxPeers,
6768
db: options.db,

lib/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { LevelUp } from 'levelup'
2+
import { Logger } from 'winston'
3+
import Common from '@ethereumjs/common'
4+
5+
export interface Options {
6+
/**
7+
* Specify the chain and hardfork by passing a Common instance.
8+
*
9+
* If not provided this defaults to chain `mainnet` and hardfork `chainstart`
10+
*/
11+
common?: Common,
12+
/**
13+
* Database to store blocks and metadata. Should be an abstract-leveldown compliant store.
14+
*/
15+
db?: LevelUp
16+
/**
17+
* Logger (winston) with log level used
18+
*/
19+
logger?: Logger,
20+
}

0 commit comments

Comments
 (0)