Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #69 from aunyks/master
Browse files Browse the repository at this point in the history
Add net_version and net_listening RPC methods
  • Loading branch information
vpulim authored Nov 15, 2018
2 parents 3df77fd + c420f30 commit 6b4446c
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rpc/modules/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const moduleList = ['eth', 'web3']
const moduleList = ['eth', 'web3', 'net']

moduleList.forEach(mod => {
module.exports[mod] = require(`./${mod}`)
Expand Down
22 changes: 22 additions & 0 deletions lib/rpc/modules/net.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { middleware } = require('../validation')

class Net {
constructor (node) {
const service = node.services.find(s => s.name === 'eth')
this._chain = service.chain
this._node = node

this.version = middleware(this.version.bind(this), 0, [])
this.listening = middleware(this.listening.bind(this), 0, [])
}

version (params, cb) {
cb(null, `${this._node.common.chainId()}`)
}

listening (params, cb) {
cb(null, this._node.opened)
}
}

module.exports = Net
81 changes: 81 additions & 0 deletions test/rpc/net/listening.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const test = require('tape')

const request = require('supertest')
const Common = require('ethereumjs-common')
const { startRPC, closeRPC, createManager } = require('../helpers')
const blockChain = require('../blockChainStub.js')
const Chain = require('../../../lib/blockchain/chain.js')

function createNode (opened, commonChain = new Common('mainnet')) {
let chain = new Chain({ blockchain: blockChain({}) })
chain.opened = true
return {
services: [{ name: 'eth', chain: chain }],
common: commonChain,
opened
}
}

test('call net_listening while listening', t => {
const manager = createManager(createNode(true))
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_listening',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body
if (typeof result !== 'boolean') {
throw new Error('Result should be a boolean, but is not')
}

if (result !== true) {
throw new Error('Not listening, when it should be')
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

test('call net_listening while not listening', t => {
const manager = createManager(createNode(false))
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_listening',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body
if (typeof result !== 'boolean') {
throw new Error('Result should be a boolean, but is not')
}

if (result !== false) {
throw new Error('Listening, when it not should be')
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})
166 changes: 166 additions & 0 deletions test/rpc/net/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
const test = require('tape')

const request = require('supertest')
const Common = require('ethereumjs-common')
const { startRPC, closeRPC, createManager } = require('../helpers')
const blockChain = require('../blockChainStub.js')
const Chain = require('../../../lib/blockchain/chain.js')

function createNode (commonChain = new Common('mainnet')) {
let chain = new Chain({ blockchain: blockChain({}) })
chain.opened = true
return {
services: [{ name: 'eth', chain: chain }],
common: commonChain
}
}

test('call net_version on ropsten', t => {
const manager = createManager(createNode(new Common('ropsten')))
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body

if (typeof result !== 'string') {
throw new Error('Result should be a string, but is not')
}

if (result.length === 0) {
throw new Error('Empty result string')
}

if (result !== '3') {
throw new Error(`Incorrect chain ID. Expected: 3, Received: ${result}`)
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

test('call net_version on mainnet', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body

if (typeof result !== 'string') {
throw new Error('Result should be a string, but is not')
}

if (result.length === 0) {
throw new Error('Empty result string')
}

if (result !== '1') {
throw new Error(`Incorrect chain ID. Expected: 1, Received: ${result}`)
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

test('call net_version on rinkeby', t => {
const manager = createManager(createNode(new Common('rinkeby')))
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body

if (typeof result !== 'string') {
throw new Error('Result should be a string, but is not')
}

if (result.length === 0) {
throw new Error('Empty result string')
}

if (result !== '4') {
throw new Error(`Incorrect chain ID. Expected: 4, Received: ${result}`)
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

test('call net_version on kovan', t => {
const manager = createManager(createNode(new Common('kovan')))
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 1
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
const { result } = res.body

if (typeof result !== 'string') {
throw new Error('Result should be a string, but is not')
}

if (result.length === 0) {
throw new Error('Empty result string')
}

if (result !== '42') {
throw new Error(
`Incorrect chain ID. Expected: 42, Received: ${result}`
)
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
})

0 comments on commit 6b4446c

Please sign in to comment.