Skip to content

Commit

Permalink
Issue #17: Implemented eth_getBlockByNumber()
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenju committed Jun 25, 2018
1 parent beedc72 commit de79b53
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/chain/ChainManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class ChainManager {
})
}
}

getBlockchain () {
return this._blockchain
}
}

module.exports = ChainManager
7 changes: 7 additions & 0 deletions lib/cliParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ var parser = require('yargs')
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
})
.option('rpc', {
describe: 'Enable the JSON-RPC server'
})
.option('rpcport', {
describe: 'HTTP-RPC server listening port',
default: 8545
})
.locale('en_EN')

parser.getClientConfig = function () {
Expand Down
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Logger = require('./logging.js')
const EthNetworkManager = require('./net/EthNetworkManager.js')
const ChainManager = require('./chain/ChainManager.js')
const DBManager = require('./chain/DBManager.js')
const RPCServer = require('./rpc')

function runClient () {
const cliParser = require('./cliParser.js')
Expand All @@ -31,6 +32,11 @@ function runClient () {
})

var cm = new ChainManager(config, nm, bc) // eslint-disable-line

if (config.rpc) {
const rpc = new RPCServer(cm.getBlockchain())
rpc.listen(config.rpcport)
}
}

runClient()
35 changes: 35 additions & 0 deletions lib/rpc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const http = require('http')
const RPCManager = require('./rpc-manager')

class RPCServer {
constructor (chain) {
this._manager = new RPCManager(chain)
this._server = http.createServer(this._requestHandler.bind(this))
}

listen (port) {
this._server.listen(port)
}

_requestHandler (req, res) {
let body = []
req.on('data', chunk => body.push(chunk))
req.on('end', () => {
body = JSON.parse(Buffer.concat(body).toString())
this._manager.execute(body, (err, result) => {
let response = result
let code = 200

if (err) {
code = 406
response = { code, message: err.message }
}

res.writeHead(code, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(response))
})
})
}
}

module.exports = RPCServer
15 changes: 15 additions & 0 deletions lib/rpc/modules/eth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Eth {
constructor (chain) {
this._chain = chain
}

getBlockByNumber (params, cb) {
let [blockNumber] = params
blockNumber = Number.parseInt(blockNumber, 16)
this._chain.getBlock(blockNumber, (err, block) => {
cb(err, block.toJSON(true))
})
}
}

module.exports = Eth
3 changes: 3 additions & 0 deletions lib/rpc/modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Eth = require('./eth')

module.exports = { Eth }
21 changes: 21 additions & 0 deletions lib/rpc/rpc-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const modules = require('./modules')

class RPCServer {
constructor (chain) {
this._chain = chain
this._modules = {}

// should be 'Eth', 'EVM', etc.
const moduleList = ['Eth']
moduleList.forEach(moduleName => {
this._modules[moduleName.toLowerCase()] = new modules[moduleName](this._chain)
})
}

execute (req, cb) {
const [moduleName, methodName] = req.method.split('_')
this._modules[moduleName][methodName](req.params, cb)
}
}

module.exports = RPCServer
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"coverage": "nyc npm run test && nyc report --reporter=text-lcov > .nyc_output/lcov.info",
"coveralls": "npm run coverage && coveralls <.nyc_output/lcov.info",
"lint": "standard",
"test": "npm run lint && node tests/"
"test": "npm run lint && tape tests/*.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"coveralls": "^3.0.0",
"nyc": "~11.6.0",
"sinon": "^6.0.0",
"standard": "~11.0.1",
"tape": "~4.9.0",
"tmp": "~0.0.33"
Expand Down
27 changes: 27 additions & 0 deletions tests/rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const test = require('tape')
const sinon = require('sinon')
const Manager = require('../lib/rpc/rpc-manager')

test('eth_getBlockByNumber', t => {
const block = {
toJSON: sinon.stub().returns({})
}
const blockchain = {
getBlock: sinon.stub().yields(null, block)
}
const manager = new Manager(blockchain)
const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByNumber',
params: ['0x1', true],
id: 1
}

t.false(blockchain.getBlock.called)
manager.execute(req, (err, block) => {
t.error(err)
t.true(blockchain.getBlock.called)
t.equal(blockchain.getBlock.firstCall.args[0], 1)
t.end()
})
})

0 comments on commit de79b53

Please sign in to comment.