/ test / integration / les-simulator.js
les-simulator.js
  1  const test = require('tape')
  2  const devp2p = require('../../src')
  3  const util = require('./util.js')
  4  
  5  const CHAIN_ID = 1
  6  
  7  const GENESIS_TD = 17179869184
  8  const GENESIS_HASH = Buffer.from('d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3', 'hex')
  9  
 10  var capabilities = [
 11    devp2p.LES.les2
 12  ]
 13  
 14  const status = {
 15    networkId: CHAIN_ID,
 16    headTd: devp2p._util.int2buffer(GENESIS_TD), // total difficulty in genesis block
 17    headHash: GENESIS_HASH,
 18    headNum: devp2p._util.int2buffer(0),
 19    genesisHash: GENESIS_HASH
 20  }
 21  
 22  // FIXME: Handle unhandled promises directly
 23  process.on('unhandledRejection', (reason, p) => {})
 24  
 25  test('LES: send status message (successful)', async (t) => {
 26    let opts = {}
 27    opts.status0 = Object.assign({}, status)
 28    opts.status1 = Object.assign({}, status)
 29    opts.onOnceStatus0 = function (rlpxs, eth) {
 30      t.pass('should receive echoing status message and welcome connection')
 31      util.destroyRLPXs(rlpxs)
 32      t.end()
 33    }
 34    util.twoPeerMsgExchange(t, capabilities, opts)
 35  })
 36  
 37  test('LES: send status message (modified announceType)', async (t) => {
 38    let opts = {}
 39    opts.status0 = Object.assign({}, status)
 40    opts.status0['announceType'] = 0
 41    opts.status1 = Object.assign({}, status)
 42    opts.status1['announceType'] = 0
 43    opts.onOnceStatus0 = function (rlpxs, eth) {
 44      t.pass('should receive echoing status message and welcome connection')
 45      util.destroyRLPXs(rlpxs)
 46      t.end()
 47    }
 48    util.twoPeerMsgExchange(t, capabilities, opts)
 49  })
 50  
 51  test('LES: send status message (NetworkId mismatch)', async (t) => {
 52    let opts = {}
 53    opts.status0 = Object.assign({}, status)
 54    let status1 = Object.assign({}, status)
 55    status1['networkId'] = 2
 56    opts.status1 = status1
 57    opts.onPeerError0 = function (err, rlpxs) {
 58      const msg = 'NetworkId mismatch: 01 / 02'
 59      t.equal(err.message, msg, `should emit error: ${msg}`)
 60      util.destroyRLPXs(rlpxs)
 61      t.end()
 62    }
 63    util.twoPeerMsgExchange(t, capabilities, opts)
 64  })
 65  
 66  test('ETH: send status message (Genesis block mismatch)', async (t) => {
 67    let opts = {}
 68    opts.status0 = Object.assign({}, status)
 69    let status1 = Object.assign({}, status)
 70    status1['genesisHash'] = Buffer.alloc(32)
 71    opts.status1 = status1
 72    opts.onPeerError0 = function (err, rlpxs) {
 73      const msg = 'Genesis block mismatch: d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3 / 0000000000000000000000000000000000000000000000000000000000000000'
 74      t.equal(err.message, msg, `should emit error: ${msg}`)
 75      util.destroyRLPXs(rlpxs)
 76      t.end()
 77    }
 78    util.twoPeerMsgExchange(t, capabilities, opts)
 79  })
 80  
 81  test('LES: send valid message', async (t) => {
 82    let opts = {}
 83    opts.status0 = Object.assign({}, status)
 84    opts.status1 = Object.assign({}, status)
 85    opts.onOnceStatus0 = function (rlpxs, les) {
 86      t.equal(les.getVersion(), 2, 'should use les2 as protocol version')
 87      les.sendMessage(devp2p.LES.MESSAGE_CODES.GET_BLOCK_HEADERS, 1, [ 437000, 1, 0, 0 ])
 88      t.pass('should send GET_BLOCK_HEADERS message')
 89    }
 90    opts.onOnMsg1 = function (rlpxs, eth, code, payload) {
 91      if (code === devp2p.LES.MESSAGE_CODES.GET_BLOCK_HEADERS) {
 92        t.pass('should receive GET_BLOCK_HEADERS message')
 93        util.destroyRLPXs(rlpxs)
 94        t.end()
 95      }
 96    }
 97    util.twoPeerMsgExchange(t, capabilities, opts)
 98  })
 99  
100  test('LES: send unknown message code', async (t) => {
101    let opts = {}
102    opts.status0 = Object.assign({}, status)
103    opts.status1 = Object.assign({}, status)
104    opts.onOnceStatus0 = function (rlpxs, les) {
105      try {
106        les.sendMessage(0x55, 1, [])
107      } catch (err) {
108        const msg = 'Error: Unknown code 85'
109        t.equal(err.toString(), msg, `should emit error: ${msg}`)
110        util.destroyRLPXs(rlpxs)
111        t.end()
112      }
113    }
114    util.twoPeerMsgExchange(t, capabilities, opts)
115  })
116  
117  test('LES: invalid status send', async (t) => {
118    let opts = {}
119    opts.status0 = Object.assign({}, status)
120    opts.status1 = Object.assign({}, status)
121    opts.onOnceStatus0 = function (rlpxs, les) {
122      try {
123        les.sendMessage(devp2p.ETH.MESSAGE_CODES.STATUS, 1, [])
124      } catch (err) {
125        const msg = 'Error: Please send status message through .sendStatus'
126        t.equal(err.toString(), msg, `should emit error: ${msg}`)
127        util.destroyRLPXs(rlpxs)
128        t.end()
129      }
130    }
131    util.twoPeerMsgExchange(t, capabilities, opts)
132  })