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

Commit 6a762a3

Browse files
committed
Type options
1 parent fb44030 commit 6a762a3

Some content is hidden

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

59 files changed

+725
-709
lines changed

bin/cli.ts

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

3-
const chains = require('@ethereumjs/common/dist/chains').chains
3+
import { Server as RPCServer } from 'jayson'
4+
import Common from '@ethereumjs/common'
45
import { parseParams } from '../lib/util'
56
import Node from '../lib/node'
6-
import { Server as RPCServer } from 'jayson'
77
import { Config } from '../lib/config'
8-
import Common from '@ethereumjs/common'
8+
import { Logger } from '../lib/logging'
99
import { RPCManager } from '../lib/rpc'
10-
import { Logger } from 'winston'
1110
const level = require('level')
1211
const path = require('path')
1312
const fs = require('fs-extra')
13+
const chains = require('@ethereumjs/common/dist/chains').chains
1414

1515
const networks = Object.entries(chains.names)
1616

lib/blockchain/chain.ts

Lines changed: 6 additions & 7 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.
@@ -79,7 +79,7 @@ export interface GenesisBlockParams {
7979
export class Chain extends EventEmitter {
8080
public config: Config
8181

82-
public db: any
82+
public db: LevelUp
8383
public blockchain: Blockchain
8484
public opened: boolean
8585

@@ -99,18 +99,18 @@ 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 = options.config
105+
this.config = options.config ?? new Config()
106106

107107
this.blockchain =
108108
options.blockchain ??
109109
new Blockchain({
110110
db: options.db,
111+
common: this.config.common,
111112
validateBlocks: false,
112113
validatePow: false,
113-
common: this.config.common,
114114
})
115115

116116
this.db = this.blockchain.db
@@ -249,8 +249,7 @@ export class Chain extends EventEmitter {
249249

250250
/**
251251
* Insert new blocks into blockchain
252-
* @param {Block[]} blocks list of blocks to add
253-
* @return {Promise<void>}
252+
* @param {Block[]} blocks list of blocks to add
254253
*/
255254
async putBlocks(blocks: Block[]): Promise<void> {
256255
if (blocks.length === 0) {

lib/config.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,93 @@
11
const os = require('os')
22
import Common from '@ethereumjs/common'
3-
import { Logger } from 'winston'
4-
import { defaultLogger, getLogger } from './logging'
3+
import { getLogger, Logger } from './logging'
54
import { Libp2pServer, RlpxServer } from './net/server'
65
import { parseTransports } from './util'
76

8-
export interface Options {
7+
export interface ConfigOptions {
98
/**
109
* Specify the chain and hardfork by passing a Common instance.
1110
*
1211
* Default: chain 'mainnet' and hardfork 'chainstart'
1312
*/
1413
common?: Common
14+
1515
/**
1616
* Synchronization mode ('fast' or 'light')
1717
*
1818
* Default: 'fast'
1919
*/
2020
syncmode?: string
21+
2122
/**
2223
* Serve light peer requests
2324
*
2425
* Default: `false`
2526
*/
2627
lightserv?: boolean
28+
2729
/**
2830
* Root data directory for the blockchain
2931
*/
3032
datadir?: string
33+
3134
/**
3235
* Network transports ('rlpx' and/or 'libp2p')
3336
*
3437
* Default: `['rlpx:port=30303', 'libp2p']`
3538
*/
3639
transports?: string[]
40+
3741
/**
3842
* Transport servers (RLPx or Libp2p)
3943
* Use `transports` option, only used for testing purposes
4044
*
4145
* Default: servers created from `transports` option
4246
*/
4347
servers?: (RlpxServer | Libp2pServer)[]
48+
4449
/**
4550
* Enable the JSON-RPC server
4651
*
4752
* Default: false
4853
*/
4954
rpc?: boolean
55+
5056
/**
5157
* HTTP-RPC server listening port
5258
*
5359
* Default: 8545
5460
*/
5561
rpcport?: number
62+
5663
/**
5764
* HTTP-RPC server listening interface
5865
*/
5966
rpcaddr?: string
67+
6068
/**
6169
* Logging verbosity
6270
*
6371
* Choices: ['error', 'warn', 'info', 'debug']
6472
* Default: 'info'
6573
*/
6674
loglevel?: string
75+
6776
/**
6877
* A custom winston logger can be provided
6978
* if setting logging verbosity is not sufficient
7079
*
7180
* Default: Logger with loglevel 'info'
7281
*/
7382
logger?: Logger
83+
7484
/**
7585
* Number of peers needed before syncing
7686
*
7787
* Default: `2`
7888
*/
7989
minPeers?: number
90+
8091
/**
8192
* Maximum peers allowed
8293
*
@@ -116,7 +127,7 @@ export class Config {
116127

117128
public readonly servers: (RlpxServer | Libp2pServer)[] = []
118129

119-
constructor(options: Options = {}) {
130+
constructor(options: ConfigOptions = {}) {
120131
// TODO: map chainParams (and lib/util.parseParams) to new Common format
121132
this.common = options.common ?? Config.COMMON_DEFAULT
122133
this.syncmode = options.syncmode ?? Config.SYNCMODE_DEFAULT
@@ -138,16 +149,7 @@ export class Config {
138149
// Logger option takes precedence
139150
this.logger = options.logger
140151
} else {
141-
// Use the default logger to keep the logger deactivation in the test working
142-
// along these lines in the test files
143-
// import { defaultLogger } from '../../lib/logging'
144-
// defaultLogger.silent = true
145-
// TODO: find a more generic solution here
146-
if (this.loglevel === 'info') {
147-
this.logger = defaultLogger
148-
} else {
149-
this.logger = getLogger({ loglevel: this.loglevel })
150-
}
152+
this.logger = getLogger({ loglevel: this.loglevel })
151153
}
152154

153155
if (options.servers) {

lib/logging.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import chalk from 'chalk'
2-
import { createLogger, format, transports } from 'winston'
2+
import { createLogger, format, transports, Logger as WinstonLogger } from 'winston'
33
const { combine, timestamp, label, printf } = format
44

5+
export type Logger = WinstonLogger
6+
57
const levelColors: any = {
68
error: 'red',
79
warn: 'yellow',

lib/net/peer/libp2ppeer.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import { Peer } from './peer'
21
import PeerId from 'peer-id'
32
import PeerInfo from 'peer-info'
4-
import { Libp2pNode } from './libp2pnode'
3+
import { Multiaddrs, MultiaddrsLike } from '../../types'
4+
import { parseMultiaddrs } from '../../util'
55
import { Libp2pSender } from '../protocol/libp2psender'
6+
import { Peer, PeerOptions } from './peer'
7+
import { Libp2pNode } from './libp2pnode'
68

7-
const defaultOptions = {
8-
multiaddrs: ['/ip4/0.0.0.0/tcp/0'],
9-
key: null,
10-
bootnodes: [],
9+
export interface Libp2pPeerOptions extends Omit<PeerOptions, 'address' | 'transport'> {
10+
/* Multiaddrs to listen on (can be a comma separated string or list) */
11+
multiaddrs?: MultiaddrsLike
1112
}
1213

1314
/**
1415
* Libp2p peer
1516
* @memberof module:net/peer
1617
* @example
1718
*
18-
* const { Libp2pPeer } = require('./lib/net/peer')
19+
* import { Libp2pPeer } from './lib/net/peer'
1920
* import { Chain } from './lib/blockchain'
20-
* const { EthProtocol } = require('./lib/net/protocol')
21+
* import { EthProtocol } from './lib/net/protocol'
2122
*
2223
* const chain = new Chain()
2324
* const protocols = [ new EthProtocol({ chain })]
@@ -31,31 +32,22 @@ const defaultOptions = {
3132
* .connect()
3233
*/
3334
export class Libp2pPeer extends Peer {
34-
private multiaddrs: string | string[]
35+
private multiaddrs: Multiaddrs
3536
private connected: boolean
3637

3738
/**
3839
* Create new libp2p peer
39-
* @param {Object} options constructor parameters
40-
* @param {string} options.id peer id
41-
* @param {multiaddr[]} options.multiaddrs multiaddrs to listen on (can be
42-
* a comma separated string or list)
43-
* @param {Protocols[]} [options.protocols=[]] supported protocols
40+
* @param {Libp2pPeerOptions}
4441
*/
45-
constructor(options: any) {
46-
super({ ...options, transport: 'libp2p' })
47-
options = { ...defaultOptions, ...options }
42+
constructor(options: Libp2pPeerOptions = {}) {
43+
options.multiaddrs = options.multiaddrs
44+
? parseMultiaddrs(options.multiaddrs)
45+
: ['/ip4/0.0.0.0/tcp/0']
46+
47+
super({ ...options, transport: 'libp2p', address: options.multiaddrs[0] })
4848

4949
this.multiaddrs = options.multiaddrs
50-
this.server = null
5150
this.connected = false
52-
this.init()
53-
}
54-
55-
init() {
56-
if (typeof this.multiaddrs === 'string') {
57-
this.multiaddrs = this.multiaddrs.split(',')
58-
}
5951
this.address = this.multiaddrs.map((ma: string) => ma.split('/ipfs')[0]).join(',')
6052
}
6153

lib/net/peer/peer.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import * as events from 'events'
2-
import { Protocol } from '../protocol/protocol'
3-
import { BoundProtocol, Sender } from '../protocol'
2+
import { Protocol, BoundProtocol, Sender } from '../protocol'
3+
import { Server } from '../server'
44
import { Config } from '../../config'
55

6-
const defaultOptions = {
7-
inbound: false,
8-
server: null,
9-
protocols: [],
6+
export interface PeerOptions {
7+
/* Config */
8+
config?: Config
9+
10+
/* Peer id */
11+
id?: string
12+
13+
/* Peer address */
14+
address: string
15+
16+
/* Transport name */
17+
transport: string
18+
19+
/* Pass true if peer initiated connection (default: false) */
20+
inbound?: boolean
21+
22+
/* Supported protocols */
23+
protocols?: Protocol[]
24+
25+
/* Server */
26+
server?: Server
1027
}
1128

1229
/**
@@ -15,42 +32,34 @@ const defaultOptions = {
1532
*/
1633
export class Peer extends events.EventEmitter {
1734
public config: Config
18-
1935
public id: string
20-
protected transport: string
21-
protected protocols: any[]
22-
private _idle: boolean
2336
public address: string
2437
public inbound: boolean
25-
public bound: Map<string, any>
26-
public server: any
38+
public server: Server | undefined
39+
public bound: Map<string, BoundProtocol>
40+
protected transport: string
41+
protected protocols: Protocol[]
42+
private _idle: boolean
2743

2844
// Dynamically bound protocol properties
2945
public les: BoundProtocol | undefined
3046
public eth: BoundProtocol | undefined
3147

3248
/**
3349
* Create new peer
34-
* @param {Object} options constructor parameters
35-
* @param {string} options.id peer id
36-
* @param {string} [options.address] peer address
37-
* @param {boolean} [options.inbound] true if peer initiated connection
38-
* @param {string} [options.transport] transport name
39-
* @param {Protocols[]} [options.protocols=[]] supported protocols
50+
* @param {PeerOptions}
4051
*/
41-
constructor(options: any) {
52+
constructor(options: PeerOptions) {
4253
super()
4354

44-
options = { ...defaultOptions, ...options }
45-
this.config = options.config
55+
this.config = options.config ?? new Config()
4656

47-
this.id = options.id
57+
this.id = options.id ?? ''
4858
this.address = options.address
49-
this.inbound = options.inbound
5059
this.transport = options.transport
51-
this.protocols = options.protocols
60+
this.inbound = options.inbound ?? false
61+
this.protocols = options.protocols ?? []
5262
this.bound = new Map()
53-
this.server = options.server
5463

5564
this._idle = true
5665
}

0 commit comments

Comments
 (0)