2014-03-01 17:19:10 -08:00
var redis = require ( 'redis' ) ;
2014-03-06 12:46:01 -08:00
var Stratum = require ( 'stratum-pool' ) ;
2014-03-20 15:25:59 -07:00
2014-03-06 12:46:01 -08:00
/ *
This module deals with handling shares when in internal payment processing mode . It connects to a redis
database and inserts shares with the database structure of :
key : coin _name + ':' + block _height
value : a hash with . .
key :
* /
2014-03-01 17:19:10 -08:00
2014-05-12 10:15:17 -07:00
2014-03-05 14:10:50 -08:00
module . exports = function ( logger , poolConfig ) {
2014-03-03 12:51:11 -08:00
2014-05-02 14:59:46 -07:00
var redisConfig = poolConfig . redis ;
2014-04-08 11:23:48 -07:00
var coin = poolConfig . coin . name ;
2014-03-03 12:51:11 -08:00
2014-05-07 11:22:32 -07:00
2014-03-30 16:04:54 -07:00
var forkId = process . env . forkId ;
var logSystem = 'Pool' ;
var logComponent = coin ;
var logSubCat = 'Thread ' + ( parseInt ( forkId ) + 1 ) ;
2017-04-29 14:54:26 -07:00
2014-03-30 16:04:54 -07:00
var connection = redis . createClient ( redisConfig . port , redisConfig . host ) ;
2017-09-04 15:24:43 -07:00
if ( redisConfig . password ) {
2017-07-01 17:21:18 -07:00
connection . auth ( redisConfig . password ) ;
2017-09-04 15:24:43 -07:00
}
2014-03-30 16:04:54 -07:00
connection . on ( 'ready' , function ( ) {
logger . debug ( logSystem , logComponent , logSubCat , 'Share processing setup with redis (' + redisConfig . host +
':' + redisConfig . port + ')' ) ;
} ) ;
connection . on ( 'error' , function ( err ) {
logger . error ( logSystem , logComponent , logSubCat , 'Redis client had an error: ' + JSON . stringify ( err ) )
} ) ;
connection . on ( 'end' , function ( ) {
2016-02-22 05:53:21 -08:00
logger . error ( logSystem , logComponent , logSubCat , 'Connection to redis database has been ended' ) ;
2014-03-30 16:04:54 -07:00
} ) ;
2014-05-12 10:15:17 -07:00
connection . info ( function ( error , response ) {
if ( error ) {
logger . error ( logSystem , logComponent , logSubCat , 'Redis version check failed' ) ;
return ;
}
var parts = response . split ( '\r\n' ) ;
var version ;
var versionString ;
for ( var i = 0 ; i < parts . length ; i ++ ) {
if ( parts [ i ] . indexOf ( ':' ) !== - 1 ) {
var valParts = parts [ i ] . split ( ':' ) ;
if ( valParts [ 0 ] === 'redis_version' ) {
versionString = valParts [ 1 ] ;
version = parseFloat ( versionString ) ;
break ;
}
}
}
if ( ! version ) {
logger . error ( logSystem , logComponent , logSubCat , 'Could not detect redis version - but be super old or broken' ) ;
}
else if ( version < 2.6 ) {
logger . error ( logSystem , logComponent , logSubCat , "You're using redis version " + versionString + " the minimum required version is 2.6. Follow the damn usage instructions..." ) ;
}
} ) ;
2014-03-03 12:51:11 -08:00
2017-04-29 12:44:04 -07:00
this . handleShare = function ( isValidShare , isValidBlock , shareData ) {
2014-03-04 12:24:02 -08:00
2014-03-13 16:20:57 -07:00
var redisCommands = [ ] ;
2017-04-29 12:44:04 -07:00
if ( isValidShare ) {
2014-05-09 16:43:11 -07:00
redisCommands . push ( [ 'hincrbyfloat' , coin + ':shares:roundCurrent' , shareData . worker , shareData . difficulty ] ) ;
redisCommands . push ( [ 'hincrby' , coin + ':stats' , 'validShares' , 1 ] ) ;
2017-05-05 21:19:02 -07:00
} else {
2014-05-09 16:43:11 -07:00
redisCommands . push ( [ 'hincrby' , coin + ':stats' , 'invalidShares' , 1 ] ) ;
2014-03-13 16:20:57 -07:00
}
2017-05-05 21:19:02 -07:00
2014-05-13 08:30:54 -07:00
/ * S t o r e s s h a r e d i f f , w o r k e r , a n d u n i q u e v a l u e w i t h a s c o r e t h a t i s t h e t i m e s t a m p . U n i q u e v a l u e e n s u r e s i t
doesn ' t overwrite an existing entry , and timestamp as score lets us query shares from last X minutes to
generate hashrate for each worker and pool . * /
var dateNow = Date . now ( ) ;
var hashrateData = [ isValidShare ? shareData . difficulty : - shareData . difficulty , shareData . worker , dateNow ] ;
redisCommands . push ( [ 'zadd' , coin + ':hashrate' , dateNow / 1000 | 0 , hashrateData . join ( ':' ) ] ) ;
2014-03-03 12:51:11 -08:00
2014-03-07 14:04:14 -08:00
if ( isValidBlock ) {
2014-05-09 16:48:19 -07:00
redisCommands . push ( [ 'rename' , coin + ':shares:roundCurrent' , coin + ':shares:round' + shareData . height ] ) ;
2017-04-29 12:44:04 -07:00
redisCommands . push ( [ 'rename' , coin + ':shares:timesCurrent' , coin + ':shares:times' + shareData . height ] ) ;
2017-01-31 15:42:46 -08:00
redisCommands . push ( [ 'sadd' , coin + ':blocksPending' , [ shareData . blockHash , shareData . txHash , shareData . height , shareData . worker , dateNow ] . join ( ':' ) ] ) ;
2017-04-30 13:10:05 -07:00
redisCommands . push ( [ 'hincrby' , coin + ':stats' , 'validBlocks' , 1 ] ) ;
2014-03-07 14:04:14 -08:00
}
2014-04-02 12:01:05 -07:00
else if ( shareData . blockHash ) {
2014-05-09 16:43:11 -07:00
redisCommands . push ( [ 'hincrby' , coin + ':stats' , 'invalidBlocks' , 1 ] ) ;
2014-03-13 16:20:57 -07:00
}
connection . multi ( redisCommands ) . exec ( function ( err , replies ) {
if ( err )
2014-03-30 16:04:54 -07:00
logger . error ( logSystem , logComponent , logSubCat , 'Error with share processor multi ' + JSON . stringify ( err ) ) ;
2014-03-13 16:20:57 -07:00
} ) ;
2014-03-04 00:33:55 -08:00
} ;
2014-03-01 17:19:10 -08:00
2014-04-06 20:11:53 -07:00
} ;