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

Commit 02e420c

Browse files
holgerd77ryanio
authored andcommitted
Added default members to Config, use defaults for CLI, lightserv -> horizontal integration
1 parent ddaee2e commit 02e420c

File tree

7 files changed

+46
-29
lines changed

7 files changed

+46
-29
lines changed

bin/cli.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const args = require('yargs')
3030
syncmode: {
3131
describe: 'Blockchain sync mode',
3232
choices: ['light', 'fast'],
33-
default: 'fast',
33+
default: Config.SYNCMODE_DEFAULT,
3434
},
3535
lightserv: {
3636
describe: 'Serve light peer requests',
3737
boolean: true,
38-
default: false,
38+
default: Config.LIGHTSERV_DEFAULT,
3939
},
4040
datadir: {
4141
describe: 'Data directory for the blockchain',
@@ -68,12 +68,12 @@ const args = require('yargs')
6868
minPeers: {
6969
describe: 'Peers needed before syncing',
7070
number: true,
71-
default: 2,
71+
default: Config.MINPEERS_DEFAULT,
7272
},
7373
maxPeers: {
7474
describe: 'Maximum peers to sync with',
7575
number: true,
76-
default: 25,
76+
default: Config.MINPEERS_DEFAULT,
7777
},
7878
params: {
7979
describe: 'Path to chain parameters json file',
@@ -85,7 +85,7 @@ const logger = getLogger({ loglevel: args.loglevel })
8585

8686
async function runNode(options: any) {
8787
logger.info('Initializing Ethereumjs client...')
88-
if (options.lightserv) {
88+
if (options.config.lightserv) {
8989
logger.info(`Serving light peer requests`)
9090
}
9191
const node = new Node(options)
@@ -130,6 +130,7 @@ async function run() {
130130
common,
131131
logger,
132132
syncmode: args.syncmode,
133+
lightserv: args.lightserv,
133134
minPeers: args.minPeers,
134135
maxPeers: args.maxPeers,
135136
})
@@ -153,7 +154,6 @@ async function run() {
153154
const options = {
154155
config,
155156
servers,
156-
lightserv: args.lightserv,
157157
db: level(dataDir),
158158
rpcport: args.rpcport,
159159
rpcaddr: args.rpcaddr,

lib/config.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,37 @@ export interface Options {
66
/**
77
* Specify the chain and hardfork by passing a Common instance.
88
*
9-
* If not provided this defaults to chain `mainnet` and hardfork `chainstart`
9+
* Default: chain `mainnet` and hardfork `chainstart`
1010
*/
1111
common?: Common
1212
/**
1313
* The logger instance with the log level set (winston)
14+
*
15+
* Default: Logger with loglevel 'debug'
1416
*/
1517
logger?: Logger
1618
/**
1719
* Synchronization mode ('fast' or 'light')
20+
*
21+
* Default: 'fast'
1822
*/
1923
syncmode?: string
24+
/**
25+
* Serve light peer requests
26+
*
27+
* Default: false
28+
*/
29+
lightserv?: boolean
2030
/**
2131
* Number of peers needed before syncing
32+
*
33+
* Default: 2
2234
*/
2335
minPeers?: number
2436
/**
2537
* Maximum peers allowed
38+
*
39+
* Default: 25
2640
*/
2741
maxPeers?: number
2842
}
@@ -31,21 +45,29 @@ export class Config {
3145
public common: Common
3246
public logger: Logger
3347
public syncmode: string
48+
public lightserv: boolean
3449
public minPeers: number
3550
public maxPeers: number
3651

52+
public static readonly COMMON_DEFAULT = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
53+
public static readonly LOGGER_DEFAULT = defaultLogger
54+
public static readonly SYNCMODE_DEFAULT = 'fast'
55+
public static readonly LIGHTSERV_DEFAULT = false
56+
public static readonly MINPEERS_DEFAULT = 2
57+
public static readonly MAXPEERS_DEFAULT = 25
58+
59+
3760
constructor(options: Options = {}) {
3861
// Initialize Common with an explicit 'chainstart' HF set until
3962
// hardfork awareness is implemented within the library
4063
// Also a fix for https://github.com/ethereumjs/ethereumjs-vm/issues/757
4164

4265
// 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
66+
this.common = options.common ? options.common : Config.COMMON_DEFAULT
67+
this.logger = options.logger ? options.logger : Config.LOGGER_DEFAULT
68+
this.syncmode = options.syncmode ? options.syncmode : Config.SYNCMODE_DEFAULT
69+
this.lightserv = options.lightserv ? options.lightserv : Config.LIGHTSERV_DEFAULT
70+
this.minPeers = options.minPeers ? options.minPeers : Config.MINPEERS_DEFAULT
71+
this.maxPeers = options.maxPeers ? options.maxPeers : Config.MAXPEERS_DEFAULT
5072
}
5173
}

lib/node.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export default class Node extends events.EventEmitter {
2121
* @param {Object} options constructor parameters
2222
* @param {Config} [options.config] Client configuration
2323
* @param {LevelDB} [options.db=null] blockchain database
24+
<<<<<<< HEAD
25+
=======
26+
* @param {Server[]} [options.servers=[]] list of servers to use
27+
>>>>>>> Added default members to Config, use defaults for CLI, lightserv -> horizontal integration
2428
* @param {Object[]} [options.bootnodes] list of bootnodes to use for discovery
2529
* @param {string[]} [options.clientFilter] list of supported clients
2630
* @param {number} [options.refreshInterval] how often to discover new peers

lib/service/fastethereumservice.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,22 @@ import { LesProtocol } from '../net/protocol/lesprotocol'
55
import { Peer } from '../net/peer/peer'
66
import { BoundProtocol } from '../net/protocol'
77

8-
const defaultOptions = {
9-
lightserv: false,
10-
}
11-
128
/**
139
* Ethereum service
1410
* @memberof module:service
1511
*/
1612
export class FastEthereumService extends EthereumService {
17-
public lightserv: any
13+
1814
/**
1915
* Create new ETH service
2016
* @param {Object} options constructor parameters
2117
* @param {Config} [options.config] Client configuration
2218
* @param {Server[]} options.servers servers to run service on
23-
* @param {boolean} [options.lightserv=false] serve LES requests
2419
* @param {Chain} [options.chain] blockchain
2520
* @param {number} [options.interval] sync retry interval
2621
*/
2722
constructor(options?: any) {
2823
super(options)
29-
options = { ...defaultOptions, ...options }
30-
this.lightserv = options.lightserv
3124
this.init()
3225
}
3326

@@ -53,7 +46,7 @@ export class FastEthereumService extends EthereumService {
5346
timeout: this.timeout,
5447
}),
5548
]
56-
if (this.lightserv) {
49+
if (this.config.lightserv) {
5750
protocols.push(
5851
new LesProtocol({
5952
config: this.config,
@@ -106,7 +99,7 @@ export class FastEthereumService extends EthereumService {
10699
* @param peer peer
107100
*/
108101
async handleLes(message: any, peer: Peer): Promise<void> {
109-
if (message.name === 'GetBlockHeaders' && this.lightserv) {
102+
if (message.name === 'GetBlockHeaders' && this.config.lightserv) {
110103
const { reqId, block, max, skip, reverse } = message.data
111104
const bv = this.flow.handleRequest(peer, message.name, max)
112105
if (bv < 0) {

test/integration/fastethereumservice.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ tape('[Integration:FastEthereumService]', async (t) => {
1212
const server = new MockServer()
1313
const chain = new MockChain()
1414
const service = new FastEthereumService({
15-
config: new Config(),
15+
config: new Config({ lightserv: true }),
1616
servers: [server],
17-
lightserv: true,
1817
chain,
1918
})
2019
await service.open()

test/integration/lightsync.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ tape('[Integration:LightSync]', async (t) => {
2222
const service =
2323
options.syncmode === 'fast'
2424
? new FastEthereumService({
25-
config: new Config({ minPeers: 1 }),
25+
config: new Config({ lightserv: true, minPeers: 1 }),
2626
servers: [server],
27-
lightserv: true,
2827
interval: options.interval || 10,
2928
chain,
3029
})

test/service/fastethereumservice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tape.skip('[FastEthereumService]', (t) => {
3838
let service = new FastEthereumService({ config: new Config() })
3939
t.ok(service.protocols[0] instanceof EthProtocol, 'fast protocols')
4040
t.notOk(service.protocols[1], 'no light protocol')
41-
service = new FastEthereumService({ lightserv: true })
41+
service = new FastEthereumService({ config: new Config({ lightserv: true }) })
4242
t.ok(service.protocols[0] instanceof EthProtocol, 'fast protocols')
4343
t.ok(service.protocols[1] instanceof LesProtocol, 'lightserv protocols')
4444
t.end()

0 commit comments

Comments
 (0)