22
33require ( './utils/logger.js' ) ;
44
5+ var http = require ( 'http' ) ;
56var os = require ( 'os' ) ;
67var Web3 = require ( 'web3' ) ;
78var web3 ;
@@ -10,6 +11,7 @@ var _ = require('lodash');
1011var debounce = require ( 'debounce' ) ;
1112var pjson = require ( './../package.json' ) ;
1213var chalk = require ( 'chalk' ) ;
14+ var poolOptions = null ;
1315
1416var Primus = require ( 'primus' ) ,
1517 Emitter = require ( 'primus-emit' ) ,
@@ -91,6 +93,10 @@ function Node ()
9193 transactions : [ ] ,
9294 uncles : [ ]
9395 } ,
96+ pool : {
97+ hashrate : 0 ,
98+ minersTotal : 0
99+ } ,
94100 syncing : false ,
95101 uptime : 0
96102 } ;
@@ -126,6 +132,7 @@ function Node ()
126132 this . _timeOffset = null ;
127133
128134 this . startWeb3Connection ( ) ;
135+ this . setupPoolOptions ( ) ;
129136
130137 return this ;
131138}
@@ -140,6 +147,18 @@ Node.prototype.startWeb3Connection = function()
140147 this . checkWeb3Connection ( ) ;
141148}
142149
150+ Node . prototype . setupPoolOptions = function ( )
151+ {
152+ if ( ! process . env . NODE_TYPE || process . env . NODE_TYPE != 'pool' )
153+ return ;
154+
155+ poolOptions = {
156+ hostname : ( process . env . API_HOST || 'localhost' ) ,
157+ port : ( process . env . API_PORT || 8080 ) ,
158+ path : ( process . env . API_URL || '/api/stats' )
159+ } ;
160+ }
161+
143162Node . prototype . checkWeb3Connection = function ( )
144163{
145164 var self = this ;
@@ -498,6 +517,23 @@ Node.prototype.getStats = function(forced)
498517 syncing : function ( callback )
499518 {
500519 web3 . eth . getSyncing ( callback ) ;
520+ } ,
521+ pool : function ( callback )
522+ {
523+ if ( poolOptions == null ) {
524+ callback ( null , { hashrate : 0 , minersTotal : 0 } ) ;
525+ return ;
526+ }
527+ http . get ( poolOptions , function ( res ) {
528+ if ( res . statusCode != 200 ) {
529+ console . error ( 'Status:' , res . statusCode ) ;
530+ }
531+
532+ res . on ( 'data' , function ( data ) {
533+ var result = JSON . parse ( data ) ;
534+ callback ( null , result ) ;
535+ } ) ;
536+ } ) ;
501537 }
502538 } ,
503539 function ( err , results )
@@ -525,6 +561,11 @@ Node.prototype.getStats = function(forced)
525561 self . stats . hashrate = results . hashrate ;
526562 self . stats . gasPrice = results . gasPrice . toString ( 10 ) ;
527563
564+ // setup pool stats
565+ self . stats . pool = results . pool ;
566+ // sum hashares
567+ self . stats . hashrate += results . pool . hashrate ;
568+
528569 if ( results . syncing !== false ) {
529570 var sync = results . syncing ;
530571
@@ -662,6 +703,7 @@ Node.prototype.prepareStats = function ()
662703 hashrate : this . stats . hashrate ,
663704 peers : this . stats . peers ,
664705 gasPrice : this . stats . gasPrice ,
706+ pool : this . stats . pool ,
665707 uptime : this . stats . uptime
666708 }
667709 } ;
0 commit comments