commit
54cb936b00
107
lib/node.js
107
lib/node.js
@ -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;
|
||||
|
12
package.json
12
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "eth-net-intelligence-api",
|
||||
"description": "Ethereum Network Intelligence API",
|
||||
"version": "0.0.17git",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"main": "./app.js",
|
||||
"directories": {
|
||||
@ -12,12 +12,12 @@
|
||||
"chalk": "^1.0.0",
|
||||
"debounce": "1.0.0",
|
||||
"debug": "2.2.0",
|
||||
"lodash": "3.10.1",
|
||||
"primus": "3.2.1",
|
||||
"primus-emit": "0.1.2",
|
||||
"lodash": "4.1.0",
|
||||
"primus": "4.0.5",
|
||||
"primus-emit": "1.0.0",
|
||||
"primus-spark-latency": "0.1.1",
|
||||
"web3": "0.12.2",
|
||||
"ws": "0.8.0"
|
||||
"web3": "0.15.1",
|
||||
"ws": "^1.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node ./app.js"
|
||||
|
Loading…
Reference in New Issue
Block a user