Skip to content

Commit 05366ae

Browse files
authored
Merge pull request #1087 from ethereumjs/client-devp2p-connection-improvements
Client & dDevp2p connection improvements
2 parents 39d7edb + aea0bfa commit 05366ae

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

packages/client/lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class Config {
126126
public static readonly RPCPORT_DEFAULT = 8545
127127
public static readonly RPCADDR_DEFAULT = 'localhost'
128128
public static readonly LOGLEVEL_DEFAULT = 'info'
129-
public static readonly MINPEERS_DEFAULT = 2
129+
public static readonly MINPEERS_DEFAULT = 1
130130
public static readonly MAXPEERS_DEFAULT = 25
131131
public static readonly DNSADDR_DEFAULT = '8.8.8.8'
132132

packages/client/lib/net/protocol/rlpxsender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Sender } from './sender'
2-
import { rlp } from 'ethereumjs-util'
32
import { ETH as Devp2pETH, LES as Devp2pLES } from '@ethereumjs/devp2p'
43

54
/**
@@ -45,7 +44,8 @@ export class RlpxSender extends Sender {
4544
*/
4645
sendMessage(code: number, data: any) {
4746
try {
48-
this.sender._send(code, rlp.encode(data))
47+
//@ts-ignore "type number is not assignable to type never"
48+
this.sender.sendMessage(code, data)
4949
} catch (err) {
5050
this.emit('error', err)
5151
}

packages/client/lib/net/server/rlpxserver.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ export class RlpxServer extends Server {
122122
await super.start()
123123
this.initDpt()
124124
this.initRlpx()
125-
await this.bootstrap()
126125
this.started = true
126+
// Boostrapping is technically not needed for a server start
127+
// (this is a repeated process) and setting `started` to `true`
128+
// before allows other services to resolve earlier and makes
129+
// the sync pick-up more reliable
130+
await this.bootstrap()
127131

128132
return true
129133
}

packages/client/test/net/protocol/rlpxsender.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { EventEmitter } from 'events'
22
import tape from 'tape-catch'
33
import td from 'testdouble'
4-
import * as rlp from 'rlp'
54
import { ETH as Devp2pETH } from '@ethereumjs/devp2p'
65
import { RlpxSender } from '../../../lib/net/protocol'
76

@@ -21,7 +20,7 @@ tape('[RlpxSender]', (t) => {
2120
const rlpxProtocol = td.object() as any
2221
const sender = new RlpxSender(rlpxProtocol)
2322
sender.sendMessage(1, 5)
24-
td.verify(rlpxProtocol._send(1, rlp.encode(5)))
23+
td.verify(rlpxProtocol.sendMessage(1, 5))
2524
td.reset()
2625
t.pass('message sent')
2726
t.end()

packages/devp2p/src/les/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export class LES extends EventEmitter {
158158
this._handleStatus()
159159
}
160160

161-
sendMessage(code: LES.MESSAGE_CODES, reqId: number, payload: any) {
161+
/**
162+
*
163+
* @param code Message code
164+
* @param payload Payload (including reqId, e.g. `[1, [437000, 1, 0, 0]]`)
165+
*/
166+
sendMessage(code: LES.MESSAGE_CODES, payload: any) {
162167
const debugMsg = `Send ${this.getMsgPrefix(code)} message to ${
163168
this._peer._socket.remoteAddress
164169
}:${this._peer._socket.remotePort}`
@@ -197,7 +202,7 @@ export class LES extends EventEmitter {
197202
throw new Error(`Unknown code ${code}`)
198203
}
199204

200-
this._send(code, rlp.encode([reqId, payload]))
205+
this._send(code, rlp.encode(payload))
201206
}
202207

203208
getMsgPrefix(msgCode: LES.MESSAGE_CODES) {

packages/devp2p/src/rlpx/rlpx.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,15 @@ export class RLPx extends EventEmitter {
266266

267267
_refillConnections() {
268268
if (!this._isAlive()) return
269-
debug(
270-
`refill connections.. (selector ${this._refillIntervalSelectionCounter}) peers: ${
271-
this._peers.size
272-
}, queue size: ${this._peersQueue.length}, open slots: ${this._getOpenSlots()}`
273-
)
269+
if (this._refillIntervalSelectionCounter === 0) {
270+
debug(
271+
`Restart connection refill .. with selector ${
272+
this._refillIntervalSelectionCounter
273+
} peers: ${this._peers.size}, queue size: ${
274+
this._peersQueue.length
275+
}, open slots: ${this._getOpenSlots()}`
276+
)
277+
}
274278
// Rotating selection counter going in loop from 0..9
275279
this._refillIntervalSelectionCounter = (this._refillIntervalSelectionCounter + 1) % 10
276280

packages/devp2p/test/integration/les-simulator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test('LES: send valid message', async (t) => {
8585
opts.status1 = Object.assign({}, status)
8686
opts.onOnceStatus0 = function (rlpxs: any, les: any) {
8787
t.equal(les.getVersion(), 2, 'should use les2 as protocol version')
88-
les.sendMessage(devp2p.LES.MESSAGE_CODES.GET_BLOCK_HEADERS, 1, [437000, 1, 0, 0])
88+
les.sendMessage(devp2p.LES.MESSAGE_CODES.GET_BLOCK_HEADERS, [1, [437000, 1, 0, 0]])
8989
t.pass('should send GET_BLOCK_HEADERS message')
9090
}
9191
opts.onOnMsg1 = function (rlpxs: any, eth: any, code: any) {
@@ -104,7 +104,7 @@ test('LES: send unknown message code', async (t) => {
104104
opts.status1 = Object.assign({}, status)
105105
opts.onOnceStatus0 = function (rlpxs: any, les: any) {
106106
try {
107-
les.sendMessage(0x55, 1, [])
107+
les.sendMessage(0x55, [1, []])
108108
} catch (err) {
109109
const msg = 'Error: Unknown code 85'
110110
t.equal(err.toString(), msg, `should emit error: ${msg}`)

0 commit comments

Comments
 (0)