rlpx-simulator.js
1 const async = require('async') 2 const test = require('tape') 3 const util = require('./util.js') 4 const Peer = require('../../src/rlpx/peer.js') 5 6 test('RLPX: add working node', async (t) => { 7 const rlpxs = util.initTwoPeerRLPXSetup(null, null) 8 9 rlpxs[0].on('peer:added', function (peer) { 10 t.equal(peer._port, 30306, 'should have added peer on peer:added after successful handshake') 11 t.equal(rlpxs[0].getPeers().length, 1, 'peer list length should be 1') 12 t.equal(rlpxs[0]._getOpenSlots(), 9, 'should have maxPeers - 1 open slots left') 13 util.destroyRLPXs(rlpxs) 14 t.end() 15 }) 16 }) 17 18 test('RLPX: ban node with missing tcp port', async (t) => { 19 const rlpxs = util.initTwoPeerRLPXSetup(null, null) 20 rlpxs[0].on('peer:added', function () { 21 const peer = { id: Buffer.from('abcd', 'hex'), address: '127.0.0.1', udpPort: 30308, tcpPort: null } 22 t.notOk(rlpxs[0]._dpt._banlist.has(peer), 'should not be in ban list before bad peer discovered') 23 rlpxs[0]._dpt.emit('peer:new', peer) 24 t.ok(rlpxs[0]._dpt._banlist.has(peer), 'should be in ban list after bad peer discovered') 25 util.destroyRLPXs(rlpxs) 26 t.end() 27 }) 28 }) 29 30 test('RLPX: remove node', async (t) => { 31 const rlpxs = util.initTwoPeerRLPXSetup(null, null) 32 33 async.series([ 34 function (cb) { 35 rlpxs[0].on('peer:added', function (peer) { 36 rlpxs[0].disconnect(peer._remoteId) 37 cb(null) 38 }) 39 }, 40 function (cb) { 41 rlpxs[0].on('peer:removed', function (peer, reason, disconnectWe) { 42 t.equal(reason, Peer.DISCONNECT_REASONS.CLIENT_QUITTING, 'should close with CLIENT_QUITTING disconnect reason') 43 t.equal(rlpxs[0]._getOpenSlots(), 10, 'should have maxPeers open slots left') 44 cb(null) 45 }) 46 } 47 ], 48 function (err, results) { 49 if (err) { 50 t.fail('An unexpected error occured.') 51 } 52 util.destroyRLPXs(rlpxs) 53 t.end() 54 }) 55 }) 56 57 test('RLPX: test peer queue / refill connections', async (t) => { 58 const rlpxs = util.getTestRLPXs(3, 1, null) 59 60 const peer = { address: util.localhost, udpPort: util.basePort + 1, tcpPort: util.basePort + 1 } 61 rlpxs[0]._dpt.addPeer(peer) 62 63 async.series([ 64 function (cb) { 65 rlpxs[0].once('peer:added', function (peer) { 66 t.equal(rlpxs[0]._peersQueue.length, 0, 'peers queue should contain no peers') 67 const peer2 = { address: util.localhost, udpPort: util.basePort + 2, tcpPort: util.basePort + 2 } 68 rlpxs[0]._dpt.addPeer(peer2) 69 cb(null) 70 }) 71 }, 72 function (cb) { 73 rlpxs[0].once('peer:added', function (peer) { 74 // FIXME: values not as expected 75 // t.equal(rlpxs[0]._peersQueue.length, 1, 'peers queue should contain one peer') 76 cb(null) 77 }) 78 } 79 ], 80 function (err, results) { 81 if (err) { 82 t.fail('An unexpected error occured.') 83 } 84 util.destroyRLPXs(rlpxs) 85 t.end() 86 }) 87 })