added registrar

This commit is contained in:
cubedro 2015-04-06 21:11:22 +03:00
parent c2e8dfdf33
commit 74b54c03b0
3 changed files with 239 additions and 3 deletions

2
app.js
View File

@ -1,5 +1,5 @@
var sleep = require('sleep');
sleep.sleep(15);
// sleep.sleep(15);
var nodeModel = require('./lib/node');

View File

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

169
lib/registrar.js Normal file
View File

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