Skip to content

Commit

Permalink
Fixed #17: Implemented eth_getBlockByNumber()
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenju committed Jun 13, 2018
1 parent dfd8b2a commit 79b7a13
Show file tree
Hide file tree
Showing 9 changed files with 121 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 @@ -93,6 +93,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
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Common = require('ethereumjs-common')
const Logger = require('./logging.js')
const EthNetworkManager = require('./net/EthNetworkManager.js')
const ChainManager = require('./chain/ChainManager.js')
const RPCServer = require('./rpc')

function runClient () {
const cliParser = require('./cliParser.js')
Expand All @@ -16,12 +17,17 @@ function runClient () {

const logger = config.logger
logger.info('ethereumjs-client initialized')
logger.info('Connecting to the %s network', config.common.chainName())
// logger.info('Connecting to the %s network', config.common.chainName())

const PRIVATE_KEY = randomBytes(32)
var nm = new EthNetworkManager(PRIVATE_KEY, config)

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

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

runClient()
36 changes: 36 additions & 0 deletions lib/rpc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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
const { message } = err
response = { code, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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"
}
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 79b7a13

Please sign in to comment.