added isSyncing functionality

This commit is contained in:
cubedro 2016-02-01 22:38:31 +02:00
parent 7d351badfe
commit 2ca054ba5d
2 changed files with 79 additions and 30 deletions

View File

@ -3,7 +3,8 @@
require('./utils/logger.js');
var os = require('os');
var web3 = require('web3');
var Web3 = require('web3');
var web3;
var async = require('async');
var _ = require('lodash');
var debounce = require('debounce');
@ -90,6 +91,7 @@ function Node ()
transactions: [],
uncles: []
},
syncing: false,
uptime: 0
};
@ -132,7 +134,7 @@ Node.prototype.startWeb3Connection = function()
{
console.info('Starting web3 connection');
web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8545')) );
web3 = new Web3(new Web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8545')));
this.checkWeb3Connection();
}
@ -143,21 +145,15 @@ Node.prototype.checkWeb3Connection = function()
if (!this._web3)
{
try {
var eth_version = web3.version.client;
if(web3.isConnected()) {
console.success('Web3 connection established');
if( !_.isUndefined(eth_version) )
{
console.success(eth_version);
console.success('Web3 connection established');
this._web3 = true;
this.init();
this._web3 = true;
this.init();
return true;
}
return true;
}
catch (err)
else
{
if(this._connection_attempts < MAX_CONNECTION_ATTEMPTS)
{
@ -189,7 +185,7 @@ Node.prototype.reconnectWeb3 = function()
try
{
web3.reset();
web3.reset(true);
}
catch (err)
{
@ -340,7 +336,7 @@ Node.prototype.getInfo = function()
try {
this.info.coinbase = web3.eth.coinbase;
this.info.node = web3.version.client;
this.info.node = web3.version.node;
this.info.net = web3.version.network;
this.info.protocol = web3.toDecimal(web3.version.ethereum);
this.info.api = web3.version.api;
@ -497,6 +493,10 @@ Node.prototype.getStats = function(forced)
gasPrice: function (callback)
{
web3.eth.getGasPrice(callback);
},
syncing: function (callback)
{
web3.eth.getSyncing(callback);
}
},
function (err, results)
@ -523,6 +523,19 @@ Node.prototype.getStats = function(forced)
self.stats.mining = results.mining;
self.stats.hashrate = results.hashrate;
self.stats.gasPrice = results.gasPrice.toString(10);
if(results.syncing !== false) {
var sync = results.syncing;
var progress = sync.currentBlock - sync.startingBlock;
var total = sync.highestBlock - sync.startingBlock;
sync.progress = progress/total;
self.stats.syncing = sync;
} else {
self.stats.syncing = false;
}
}
else {
self.setInactive();
@ -643,6 +656,7 @@ Node.prototype.prepareStats = function ()
id: this.id,
stats: {
active: this.stats.active,
syncing: this.stats.syncing,
mining: this.stats.mining,
hashrate: this.stats.hashrate,
peers: this.stats.peers,
@ -669,7 +683,10 @@ Node.prototype.sendStatsUpdate = function (force)
{
if( this.changed() || force ) {
console.stats("wsc", "Sending", chalk.reset.blue((force ? "forced" : "changed")), chalk.bold.white("update"));
this.emit('stats', this.prepareStats());
var stats = this.prepareStats();
console.info(stats);
this.emit('stats', stats);
// this.emit('stats', this.prepareStats());
}
}
@ -686,6 +703,49 @@ Node.prototype.setWatches = function()
{
var self = this;
this.setFilters();
this.updateInterval = setInterval( function(){
self.getStats();
}, UPDATE_INTERVAL);
if( !this.pingInterval )
{
this.pingInterval = setInterval( function(){
self.ping();
}, PING_INTERVAL);
}
web3.eth.isSyncing(function(error, sync) {
if(!error) {
if(sync === true) {
web3.reset(true);
console.info("SYNC STARTED:", sync);
} else if(sync) {
var synced = sync.currentBlock - sync.startingBlock;
var total = sync.highestBlock - sync.startingBlock;
sync.progress = synced/total;
self.stats.syncing = sync;
if(self._lastBlock !== sync.currentBlock) {
self._latestQueue.push(sync.currentBlock);
}
console.info("SYNC UPDATE:", sync);
} else {
console.info("SYNC STOPPED:", sync);
}
} else {
self.stats.syncing = false;
self.setFilters();
console.error("SYNC ERROR", error);
}
});
}
Node.prototype.setFilters = function()
{
var self = this;
this._latestQueue = async.queue(function (hash, callback)
{
var timeString = 'Got block ' + chalk.reset.red(hash) + chalk.reset.bold.white(' in') + chalk.reset.green('');
@ -808,17 +868,6 @@ Node.prototype.setWatches = function()
console.error("Couldn't set up pending filter");
console.error(err);
}
this.updateInterval = setInterval( function(){
self.getStats();
}, UPDATE_INTERVAL);
if( !this.pingInterval )
{
this.pingInterval = setInterval( function(){
self.ping();
}, PING_INTERVAL);
}
}
Node.prototype.init = function()
@ -844,7 +893,7 @@ Node.prototype.stop = function()
if(this.pingInterval)
clearInterval(this.pingInterval);
web3.reset();
web3.reset(false);
}
module.exports = Node;

View File

@ -1,7 +1,7 @@
{
"name": "eth-net-intelligence-api",
"description": "Ethereum Network Intelligence API",
"version": "0.0.17git",
"version": "0.0.18",
"private": true,
"main": "./app.js",
"directories": {