changed web3 to async

This commit is contained in:
cubedro 2015-05-26 21:45:33 +03:00
parent 6df36c6ce4
commit b28332596b

View File

@ -89,6 +89,7 @@ function Node ()
this._lastStats = JSON.stringify(this.stats);
this._lastFetch = 0;
this._startBlockFetch = 0;
this._tries = 0;
this._down = 0;
@ -364,26 +365,37 @@ Node.prototype.formatBlock = function (block)
Node.prototype.getStatsBlock = function ()
{
var self = this;
if(this._socket)
this._lastStats = JSON.stringify(this.stats);
if(this._web3)
{
var start = _.now();
var block = this.getBlock();
this._startBlockFetch = _.now();
web3.eth.getBlock('latest', false, function(error, result) {
self.sendStatsBlock(error, result);
});
}
}
if( block )
Node.prototype.sendStatsBlock = function (error, block)
{
if( !error )
{
this.stats.block = block;
console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - start, 'ms'));
if( _.isUndefined(this.stats.block.number) || (!_.isUndefined(block.number) && this.stats.block.number != block.number) )
{
this.stats.block = this.formatBlock(block);
console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms'));
this.sendUpdate();
}
console.warn("==>", "Got same block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(_.now() - this._startBlockFetch, 'ms'));
}
else
{
console.error("xx>", "getStatsBlock couldn't fetch block...");
console.log(block);
}
console.error("xx>", error);
}
}
@ -410,106 +422,24 @@ Node.prototype.getStats = function(forced)
},
peers: function (callback)
{
async.nextTick(function () {
var peers = null;
var error = null;
try {
peers = web3.toDecimal(web3.net.peerCount);
}
catch (err) {
console.error('xx>', 'PeerCount failed: ', err);
error = err;
}
callback(error, peers);
});
web3.net.getPeerCount(callback);
},
pending: function (callback)
{
async.nextTick(function () {
try {
web3.eth.getBlockTransactionCount('pending', callback);
}
catch (err) {
console.error('xx>', 'Pending failed: ', err);
callback(err, null);
}
});
},
mining: function (callback)
{
async.nextTick(function () {
var mining = null;
var error = null;
try {
mining = web3.eth.mining;
}
catch (err) {
console.error('xx>', 'Mining failed: ', err);
error = err;
}
callback(error, mining);
});
web3.eth.getMining(callback);
},
hashrate: function (callback)
{
if(self.stats.mining) {
async.nextTick(function () {
var hashrate = null;
var error = null;
try {
hashrate = web3.eth.hashrate;
}
catch (err) {
console.error('xx>', 'Hashrate failed: ', err);
error = err;
}
callback(error, hashrate);
});
}
else {
callback(null, 0);
}
web3.eth.getHashrate(callback);
},
gasPrice: function (callback)
{
async.nextTick(function () {
var gasPrice = null;
var error = null;
try {
gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10);
web3.eth.getGasPrice(callback);
}
catch (err) {
console.error('xx>', 'gasPrice failed: ', err);
error = err;
}
callback(error, gasPrice);
});
},
// minerName: function (callback)
// {
// async.nextTick(function () {
// var minerName = null;
// var error = null;
// try {
// minerName = self.getMinerName(self.stats.block.miner);
// }
// catch (err) {
// console.error('xx>', 'minerName failed: ', err);
// error = err;
// }
// callback(error, minerName);
// });
// }
},
function (err, results)
{
@ -531,11 +461,11 @@ Node.prototype.getStats = function(forced)
if(results.peers !== null)
{
self.stats.active = true;
self.stats.peers = results.peers;
self.stats.peers = web3.toDecimal(results.peers);
self.stats.pending = results.pending;
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice;
self.stats.gasPrice = web3.toBigNumber(results.gasPrice).toString(10);
}
else {
self.setInactive();
@ -599,33 +529,6 @@ Node.prototype.getHistory = function (range)
});
}
Node.prototype.getMinerName = function(miner)
{
var result = _.find(this._knownMiners, { miner: miner });
if (result !== undefined)
{
return result.name;
}
else
{
if (this._Registrar !== null)
{
var name = this._Registrar.name(miner);
if(name.length > 0)
{
this._knownMiners.push({ miner: miner, name: name });
return name;
}
}
this._knownMiners.push({ miner: miner, name: false });
}
return false;
}
Node.prototype.updateBlock = function()
{
this.getStatsBlock();