/ test / integration / eth-simulator.js
eth-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.ETH.eth63,
 12    devp2p.ETH.eth62
 13  ]
 14  
 15  const status = {
 16    networkId: CHAIN_ID,
 17    td: devp2p._util.int2buffer(GENESIS_TD),
 18    bestHash: GENESIS_HASH,
 19    genesisHash: GENESIS_HASH
 20  }
 21  
 22  // FIXME: Handle unhandled promises directly
 23  process.on('unhandledRejection', (reason, p) => {})
 24  
 25  test('ETH: 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('ETH: send status message (NetworkId mismatch)', async (t) => {
 38    let opts = {}
 39    opts.status0 = Object.assign({}, status)
 40    let status1 = Object.assign({}, status)
 41    status1['networkId'] = 2
 42    opts.status1 = status1
 43    opts.onPeerError0 = function (err, rlpxs) {
 44      const msg = 'NetworkId mismatch: 01 / 02'
 45      t.equal(err.message, msg, `should emit error: ${msg}`)
 46      util.destroyRLPXs(rlpxs)
 47      t.end()
 48    }
 49    util.twoPeerMsgExchange(t, capabilities, opts)
 50  })
 51  
 52  test('ETH: send status message (Genesis block mismatch)', async (t) => {
 53    let opts = {}
 54    opts.status0 = Object.assign({}, status)
 55    let status1 = Object.assign({}, status)
 56    status1['genesisHash'] = Buffer.alloc(32)
 57    opts.status1 = status1
 58    opts.onPeerError0 = function (err, rlpxs) {
 59      const msg = 'Genesis block mismatch: d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3 / 0000000000000000000000000000000000000000000000000000000000000000'
 60      t.equal(err.message, msg, `should emit error: ${msg}`)
 61      util.destroyRLPXs(rlpxs)
 62      t.end()
 63    }
 64    util.twoPeerMsgExchange(t, capabilities, opts)
 65  })
 66  
 67  test('ETH: send allowed eth63', async (t) => {
 68    let opts = {}
 69    opts.status0 = Object.assign({}, status)
 70    opts.status1 = Object.assign({}, status)
 71    opts.onOnceStatus0 = function (rlpxs, eth) {
 72      t.equal(eth.getVersion(), 63, 'should use eth63 as protocol version')
 73      eth.sendMessage(devp2p.ETH.MESSAGE_CODES.NEW_BLOCK_HASHES, [ 437000, 1, 0, 0 ])
 74      t.pass('should send NEW_BLOCK_HASHES message')
 75    }
 76    opts.onOnMsg1 = function (rlpxs, eth, code, payload) {
 77      if (code === devp2p.ETH.MESSAGE_CODES.NEW_BLOCK_HASHES) {
 78        t.pass('should receive NEW_BLOCK_HASHES message')
 79        util.destroyRLPXs(rlpxs)
 80        t.end()
 81      }
 82    }
 83    util.twoPeerMsgExchange(t, capabilities, opts)
 84  })
 85  
 86  test('ETH: send allowed eth62', async (t) => {
 87    let cap = [
 88      devp2p.ETH.eth62
 89    ]
 90    let opts = {}
 91    opts.status0 = Object.assign({}, status)
 92    opts.status1 = Object.assign({}, status)
 93    opts.onOnceStatus0 = function (rlpxs, eth) {
 94      eth.sendMessage(devp2p.ETH.MESSAGE_CODES.NEW_BLOCK_HASHES, [ 437000, 1, 0, 0 ])
 95      t.pass('should send NEW_BLOCK_HASHES message')
 96    }
 97    opts.onOnMsg1 = function (rlpxs, eth, code, payload) {
 98      if (code === devp2p.ETH.MESSAGE_CODES.NEW_BLOCK_HASHES) {
 99        t.pass('should receive NEW_BLOCK_HASHES message')
100        util.destroyRLPXs(rlpxs)
101        t.end()
102      }
103    }
104    util.twoPeerMsgExchange(t, cap, opts)
105  })
106  
107  test('ETH: send not-allowed eth62', async (t) => {
108    let cap = [
109      devp2p.ETH.eth62
110    ]
111    let opts = {}
112    opts.status0 = Object.assign({}, status)
113    opts.status1 = Object.assign({}, status)
114    opts.onOnceStatus0 = function (rlpxs, eth) {
115      try {
116        eth.sendMessage(devp2p.ETH.MESSAGE_CODES.GET_NODE_DATA, [])
117      } catch (err) {
118        const msg = 'Error: Code 13 not allowed with version 62'
119        t.equal(err.toString(), msg, `should emit error: ${msg}`)
120        util.destroyRLPXs(rlpxs)
121        t.end()
122      }
123    }
124    util.twoPeerMsgExchange(t, cap, opts)
125  })
126  
127  test('ETH: send unknown message code', async (t) => {
128    let opts = {}
129    opts.status0 = Object.assign({}, status)
130    opts.status1 = Object.assign({}, status)
131    opts.onOnceStatus0 = function (rlpxs, eth) {
132      try {
133        eth.sendMessage(0x55, [])
134      } catch (err) {
135        const msg = 'Error: Unknown code 85'
136        t.equal(err.toString(), msg, `should emit error: ${msg}`)
137        util.destroyRLPXs(rlpxs)
138        t.end()
139      }
140    }
141    util.twoPeerMsgExchange(t, capabilities, opts)
142  })
143  
144  test('ETH: invalid status send', async (t) => {
145    let opts = {}
146    opts.status0 = Object.assign({}, status)
147    opts.status1 = Object.assign({}, status)
148    opts.onOnceStatus0 = function (rlpxs, eth) {
149      try {
150        eth.sendMessage(devp2p.ETH.MESSAGE_CODES.STATUS, [])
151      } catch (err) {
152        const msg = 'Error: Please send status message through .sendStatus'
153        t.equal(err.toString(), msg, `should emit error: ${msg}`)
154        util.destroyRLPXs(rlpxs)
155        t.end()
156      }
157    }
158    util.twoPeerMsgExchange(t, capabilities, opts)
159  })