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

Commit 000b8d9

Browse files
authored
Merge branch 'master' into digitize-diagram
2 parents bd67be0 + 9b32bd3 commit 000b8d9

File tree

11 files changed

+56
-12
lines changed

11 files changed

+56
-12
lines changed

lib/net/peerpool.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PeerPool extends EventEmitter {
4343
this.logger = options.logger
4444
this.maxPeers = options.maxPeers
4545
this.pool = new Map()
46+
this.noPeerPeriods = 0
4647
this.init()
4748
}
4849

@@ -63,6 +64,7 @@ class PeerPool extends EventEmitter {
6364
server.on('disconnected', (peer) => { this.disconnected(peer) })
6465
})
6566
this.opened = true
67+
this._statusCheckInterval = setInterval(await this._statusCheck.bind(this), 20000)
6668
}
6769

6870
/**
@@ -72,6 +74,7 @@ class PeerPool extends EventEmitter {
7274
async close () {
7375
this.pool.clear()
7476
this.opened = false
77+
clearInterval(this._statusCheckInterval)
7578
}
7679

7780
/**
@@ -185,6 +188,29 @@ class PeerPool extends EventEmitter {
185188
}
186189
}
187190
}
191+
192+
/**
193+
* Peer pool status check on a repeated interval
194+
*/
195+
async _statusCheck () {
196+
if (this.size === 0) {
197+
this.noPeerPeriods += 1
198+
if (this.noPeerPeriods >= 3) {
199+
const promises = this.servers.map(async server => {
200+
if (server.bootstrap) {
201+
this.logger.info('Retriggering bootstrap.')
202+
await server.bootstrap()
203+
}
204+
})
205+
await Promise.all(promises)
206+
this.noPeerPeriods = 0
207+
} else {
208+
this.logger.info('Looking for suited peers...')
209+
}
210+
} else {
211+
this.noPeerPeriods = 0
212+
}
213+
}
188214
}
189215

190216
module.exports = PeerPool

lib/net/protocol/ethprotocol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class EthProtocol extends Protocol {
4646
* Create eth protocol
4747
* @param {Object} options constructor parameters
4848
* @param {Chain} options.chain blockchain
49-
* @param {number} [options.timeout=5000] handshake timeout in ms
49+
* @param {number} [options.timeout=8000] handshake timeout in ms
5050
* @param {Logger} [options.logger] logger instance
5151
*/
5252
constructor (options) {

lib/net/protocol/lesprotocol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class LesProtocol extends Protocol {
6565
* @param {Chain} options.chain blockchain
6666
* @param {FlowControl} [options.flow] flow control manager. if undefined,
6767
* header serving will be disabled
68-
* @param {number} [options.timeout=5000] handshake timeout in ms
68+
* @param {number} [options.timeout=8000] handshake timeout in ms
6969
* @param {Logger} [options.logger] logger instance
7070
*/
7171
constructor (options) {

lib/net/protocol/protocol.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { defaultLogger } = require('../../logging')
66

77
const defaultOptions = {
88
logger: defaultLogger,
9-
timeout: 5000
9+
timeout: 8000
1010
}
1111

1212
/**
@@ -28,7 +28,7 @@ class Protocol extends EventEmitter {
2828
/**
2929
* Create new protocol
3030
* @param {Object} options constructor parameters
31-
* @param {number} [options.timeout=5000] handshake timeout in ms
31+
* @param {number} [options.timeout=8000] handshake timeout in ms
3232
* @param {Logger} [options.logger] logger instance
3333
*/
3434
constructor (options) {

lib/net/server/rlpxserver.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,25 @@ class RlpxServer extends Server {
9191
this.initDpt()
9292
this.initRlpx()
9393

94-
this.bootnodes.map(node => {
94+
await this.bootstrap()
95+
96+
this.started = true
97+
}
98+
99+
/**
100+
* Bootstrap bootnode peers from the network
101+
* @return {Promise}
102+
*/
103+
async bootstrap () {
104+
const promises = this.bootnodes.map(node => {
95105
const bootnode = {
96106
address: node.ip,
97107
udpPort: node.port,
98108
tcpPort: node.port
99109
}
100110
return this.dpt.bootstrap(bootnode).catch(e => this.error(e))
101111
})
102-
103-
this.started = true
112+
await Promise.all(promises)
104113
}
105114

106115
/**

lib/service/ethereumservice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const defaultOptions = {
99
lightserv: false,
1010
common: new Common('mainnet', 'chainstart'),
1111
minPeers: 3,
12-
timeout: 5000,
12+
timeout: 8000,
1313
interval: 1000
1414
}
1515

lib/service/service.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ class Service extends EventEmitter {
109109
}
110110

111111
/**
112-
* Start service
112+
* Stop service
113113
* @return {Promise}
114114
*/
115115
async stop () {
116+
if (this.opened) {
117+
await this.close()
118+
}
116119
this.running = false
117120
this.logger.info(`Stopped ${this.name} service.`)
118121
}

lib/sync/fetcher/fetcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { defaultLogger } = require('../../logging')
88
const defaultOptions = {
99
common: new Common('mainnet', 'chainstart'),
1010
logger: defaultLogger,
11-
timeout: 5000,
11+
timeout: 8000,
1212
interval: 1000,
1313
banTime: 60000,
1414
maxQueue: 16,

test/blockchain/chain.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tape('[Chain]', t => {
4343
t.equal(chain.genesis.hash.toString('hex'),
4444
chain.blocks.latest.hash().toString('hex'),
4545
'get chain.block.latest')
46-
chain.close()
46+
await chain.close()
4747
t.end()
4848
})
4949

@@ -80,6 +80,7 @@ tape('[Chain]', t => {
8080
await chain.getTd(block.hash())
8181
t.ok(chain.opened, 'chain should open if getTd() called')
8282
t.equal(await chain.open(), false, 'skip open if already opened')
83+
await chain.close()
8384
t.end()
8485
})
8586

@@ -89,6 +90,7 @@ tape('[Chain]', t => {
8990
t.notOk(await chain.putBlocks(), 'add undefined block')
9091
t.notOk(await chain.putBlocks(null), 'add null block')
9192
t.notOk(await chain.putBlocks([]), 'add empty block list')
93+
await chain.close()
9294
t.end()
9395
})
9496

@@ -98,6 +100,7 @@ tape('[Chain]', t => {
98100
t.notOk(await chain.putHeaders(), 'add undefined header')
99101
t.notOk(await chain.putHeaders(null), 'add null header')
100102
t.notOk(await chain.putHeaders([]), 'add empty header list')
103+
await chain.close()
101104
t.end()
102105
})
103106

@@ -112,7 +115,7 @@ tape('[Chain]', t => {
112115
await chain.putBlocks([block])
113116
t.equal(chain.blocks.td.toString(16), '4abcdffff', 'get chain.td')
114117
t.equal(chain.blocks.height.toString(10), '1', 'get chain.height')
115-
chain.close()
118+
await chain.close()
116119
t.end()
117120
})
118121
})

test/integration/peerpool.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tape('[Integration:PeerPool]', async (t) => {
1818

1919
async function destroy (server, pool) {
2020
await server.stop()
21+
await pool.close()
2122
}
2223

2324
t.test('should open', async (t) => {

0 commit comments

Comments
 (0)