forked from ethereumjs/ethereumjs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #17: Implemented eth_getBlockByNumber()
- Loading branch information
Showing
9 changed files
with
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,10 @@ class ChainManager { | |
}) | ||
} | ||
} | ||
|
||
getBlockchain () { | ||
return this._blockchain | ||
} | ||
} | ||
|
||
module.exports = ChainManager |
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
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,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 |
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,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 |
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,3 @@ | ||
const Eth = require('./eth') | ||
|
||
module.exports = { Eth } |
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,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 |
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,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() | ||
}) | ||
}) |