This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from aunyks/master
Add net_version and net_listening RPC methods
- Loading branch information
Showing
4 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |