Skip to content

Commit

Permalink
WIP: implementation idea
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenju committed Jun 12, 2018
1 parent dfd8b2a commit 6d917a5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/cliParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var parser = require('yargs')
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
})
.option('rpc', {
describe: 'Enable the JSON-RPC server'
})
.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 @@ -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 @@ -22,6 +23,11 @@ function runClient () {
var nm = new EthNetworkManager(PRIVATE_KEY, config)

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

if (config.rpc) {
const rpc = new RPCServer(cm._blockchain)
rpc.listen(8545)
}
}

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

class RPCServer {
constructor (chain) {
this._chain = chain;
this._server = http.createServer(this._handle.bind(this));
this.modules = {};

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

_handle (req, res) {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', async () => {
let result = {};
body = JSON.parse(Buffer.concat(body).toString());
const [moduleName, methodName] = body.method.split('_')

try {
result = await this.modules[moduleName][methodName](body.params);
} catch (e) {
console.error(e);
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(result));
});

}

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

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

getBlockByNumber (blockNumber, fullTransaction = false) {
throw 'not implement yet';
}
}

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 };

0 comments on commit 6d917a5

Please sign in to comment.