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