From 74b54c03b0b2db1e6dfd2e446b76ffa071db4365 Mon Sep 17 00:00:00 2001 From: cubedro Date: Mon, 6 Apr 2015 21:11:22 +0300 Subject: [PATCH] added registrar --- app.js | 2 +- lib/node.js | 71 +++++++++++++++++++- lib/registrar.js | 169 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 lib/registrar.js diff --git a/app.js b/app.js index 47018a6..e8cc01a 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,5 @@ var sleep = require('sleep'); -sleep.sleep(15); +// sleep.sleep(15); var nodeModel = require('./lib/node'); diff --git a/lib/node.js b/lib/node.js index 83c676a..0f01ec0 100644 --- a/lib/node.js +++ b/lib/node.js @@ -3,6 +3,7 @@ var _ = require('lodash'); var os = require('os'); var shelljs = require('shelljs'); var debounce = require('debounce'); +var registrar = require('./registrar.js'); var Primus = require('primus'), Emitter = require('primus-emit'), @@ -14,6 +15,8 @@ var ETH_VERSION, API_VERSION; var INSTANCE_NAME = process.env.INSTANCE_NAME; +var Contract = null; + web3.setProvider(new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080'))); Socket = Primus.createSocket({ @@ -90,6 +93,9 @@ function Node() this.blocks = []; + this._Registrar = null; + this._knownMiners = []; + this._socket = null; this.pendingFilter = false; this.chainFilter = false; @@ -300,8 +306,31 @@ Node.prototype.gasSpendingChart = function() }); } +Node.prototype.getMinerName = function(miner) +{ + var result = _.find(this._knownMiners, {miner: miner}); + + if(result !== undefined) + { + return result.name; + } + else + { + var name = this._Registrar.name(miner); + + if(name.length > 0) + { + this._knownMiners.push({miner: miner, name: name}); + return name; + } + } + + return false; +} + Node.prototype.minersChart = function() { + var self = this; var miners = _.countBy(this.blocks, function(block) { return block.miner; @@ -309,8 +338,10 @@ Node.prototype.minersChart = function() var minersArray = []; - _.forEach(miners, function(cnt, miner) { - minersArray.push({miner: miner, blocks: cnt}); + _.forEach(miners, function(cnt, miner) + { + var name = self.getMinerName(miner); + minersArray.push({miner: miner, name: name, blocks: cnt}); }); var minersArray = _.sortBy(minersArray, 'blocks').reverse(); @@ -437,8 +468,15 @@ Node.prototype.emit = function(message, payload) } } +Node.prototype.installContract = function() +{ + Contract = web3.eth.contract(registrar.desc); + this._Registrar = new Contract(registrar.address); +} + Node.prototype.init = function() { + this.installContract(); this.update(); this.setWatches(); } @@ -458,4 +496,33 @@ Node.prototype.stop = function() web3.reset(); } +function directICAP(address) +{ + if (address[0] != 0) + return ''; + + var d = toBase36(address); + + while(d.length < 30) + d = "0" + d; + + return d; + // return iban("XE", d); +} + +function toBase36(hex) +{ + var c_alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + var a = hex.length; + var ret = ''; + + for(; a > 0; a = a/36){ + ret = c_alphabet[a%36] + ret; + console.log('a: ', a); + console.log('ret: ', ret); + } + + return ret; +} + module.exports = Node; \ No newline at end of file diff --git a/lib/registrar.js b/lib/registrar.js new file mode 100644 index 0000000..df0eb46 --- /dev/null +++ b/lib/registrar.js @@ -0,0 +1,169 @@ +var address = "0xc6d9d2cd449a754c494264e1809c50e34d64562b"; +var registrar = [ + { + "constant":true, + "inputs": + [ + { + "name":"_owner", + "type":"address" + } + ], + "name":"name", + "outputs": + [ + { + "name":"o_name", + "type":"bytes32" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_name", + "type":"bytes32" + } + ], + "name":"owner", + "outputs": + [ + { + "name":"o_owner", + "type":"address" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_name", + "type":"bytes32" + } + ], + "name":"getAddress", + "outputs": + [ + { + "name":"o_owner", + "type":"address" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_name", + "type":"bytes32" + } + ], + "name":"content", + "outputs": + [ + { + "name":"o_content", + "type":"bytes32" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_name", + "type":"bytes32" + } + ], + "name":"addr", + "outputs": + [ + { + "name":"o_address", + "type":"address" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_name", + "type":"bytes32" + } + ], + "name":"subRegistrar", + "outputs": + [ + { + "name":"o_subRegistrar", + "type":"address" + } + ], + "type":"function" + }, + { + "constant":true, + "inputs": + [ + { + "name":"_owner", + "type":"address" + } + ], + "name":"getName", + "outputs": + [ + { + "name":"o_name", + "type":"bytes32" + } + ], + "type":"function" + }, + { + "anonymous":false, + "inputs": + [ + { + "indexed":true, + "name":"name", + "type":"bytes32" + } + ], + "name":"Changed", + "type":"event" + }, + { + "anonymous":false, + "inputs": + [ + { + "indexed":true, + "name":"name", + "type":"bytes32" + }, + { + "indexed":true, + "name":"addr", + "type":"address" + } + ], + "name":"PrimaryChanged", + "type":"event" + } +]; + +module.exports = { desc: registrar, address: address}; \ No newline at end of file