Skip to content

Commit

Permalink
devp2p -> connection reliability: distribute network traffic on DPT a…
Browse files Browse the repository at this point in the history
…dditions of new neighbour peers
  • Loading branch information
holgerd77 committed Jan 14, 2021
1 parent 06f31ea commit c110b70
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
16 changes: 10 additions & 6 deletions packages/devp2p/src/dpt/dpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class DPT extends EventEmitter {
})
this._server.once('listening', () => this.emit('listening'))
this._server.once('close', () => this.emit('close'))
this._server.on('peers', (peers, remote) => this._onServerPeers(peers))
this._server.on('peers', (peers) => this._onServerPeers(peers))
this._server.on('error', (err) => this.emit('error', err))

const refreshIntervalSubdivided = Math.floor((options.refreshInterval ?? ms('60s')) / 10)
Expand Down Expand Up @@ -115,12 +115,16 @@ export class DPT extends EventEmitter {
}

_onServerPeers(peers: PeerInfo[]): void {
const ms = 0
const DIFF_TIME_MS = 200
let ms = 0
for (const peer of peers) {
this.addPeer(peer).catch((error) => {
this.emit('error', error )
})
}
setTimeout(() => {
this.addPeer(peer).catch((error) => {
this.emit('error', error)
})
}, ms)
ms += DIFF_TIME_MS
}
}

async bootstrap(peer: PeerInfo): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/devp2p/src/dpt/kbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class KBucket extends EventEmitter {
this.emit('removed', peer)
})

this._kbucket.on('ping', (oldPeers: PeerInfo[], newPeer: PeerInfo | undefined) => {
this._kbucket.on('ping', (oldPeers: PeerInfo[], newPeer: PeerInfo | undefined) => {
this.emit('ping', oldPeers, newPeer)
})
}
Expand Down
13 changes: 8 additions & 5 deletions packages/devp2p/test/integration/dpt-simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('DPT: remove node', async (t) => {
async.series(
[
function (cb) {
dpts[0].on('peer:added', function (peer: any) {
dpts[0].on('peer:added', function (peer) {
dpts[0].removePeer(peer)
cb(null)
})
Expand Down Expand Up @@ -64,13 +64,13 @@ test('DPT: ban node', async (t) => {
async.series(
[
function (cb) {
dpts[0].on('peer:added', function (peer: any) {
dpts[0].on('peer:added', function (peer) {
dpts[0].banPeer(peer)
cb(null)
})
},
function (cb) {
dpts[0].on('peer:removed', function (peer: any) {
dpts[0].on('peer:removed', function (peer) {
t.equal(dpts[0].banlist.has(peer), true, 'ban-list should contain peer')
t.equal(
dpts[0].getPeers().length,
Expand Down Expand Up @@ -150,11 +150,14 @@ test('DPT: simulate bootstrap', async (t) => {
}

await delay(250)
util.destroyDPTs(dpts)

// dpts.forEach((dpt, i) => console.log(`${i}:${dpt.getPeers().length}`))
for (const dpt of dpts)
for (const dpt of dpts) {
t.equal(dpt.getPeers().length, numDPTs, 'Peers should be distributed to all DPTs')
}
await delay(1000)

util.destroyDPTs(dpts)

t.end()
})
4 changes: 2 additions & 2 deletions packages/devp2p/test/integration/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Common from '@ethereumjs/common'
export const localhost = '127.0.0.1'
export const basePort = 30306

export function getTestDPTs(numDPTs: any) {
export function getTestDPTs(numDPTs: number) {
const dpts = []

for (let i = 0; i < numDPTs; ++i) {
Expand All @@ -30,7 +30,7 @@ export function initTwoPeerDPTSetup() {
return dpts
}

export function destroyDPTs(dpts: any) {
export function destroyDPTs(dpts: DPT[]) {
for (const dpt of dpts) dpt.destroy()
}

Expand Down

0 comments on commit c110b70

Please sign in to comment.