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-03-05 14:10:50 -08:00
|
|
|
module.exports = function(logger, poolConfig){
|
2014-03-03 12:51:11 -08:00
|
|
|
|
2014-03-06 12:46:01 -08:00
|
|
|
var internalConfig = poolConfig.shareProcessing.internal;
|
|
|
|
var redisConfig = internalConfig.redis;
|
2014-03-05 14:10:50 -08:00
|
|
|
var coin = poolConfig.coin.name;
|
2014-03-03 12:51:11 -08:00
|
|
|
|
2014-03-22 23:16:06 -07:00
|
|
|
var logSystem = 'Shares';
|
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
var connection;
|
2014-03-04 12:24:02 -08:00
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
function connect(){
|
2014-03-05 14:31:32 -08:00
|
|
|
|
|
|
|
var reconnectTimeout;
|
|
|
|
|
2014-03-06 12:46:01 -08:00
|
|
|
connection = redis.createClient(redisConfig.port, redisConfig.host);
|
2014-03-05 14:31:32 -08:00
|
|
|
connection.on('ready', function(){
|
|
|
|
clearTimeout(reconnectTimeout);
|
2014-03-22 23:16:06 -07:00
|
|
|
logger.debug(logSystem, 'redis', 'Successfully connected to redis database');
|
2014-03-05 14:31:32 -08:00
|
|
|
});
|
2014-03-05 14:10:50 -08:00
|
|
|
connection.on('error', function(err){
|
2014-03-22 23:16:06 -07:00
|
|
|
logger.error(logSystem, 'redis', 'Redis client had an error: ' + JSON.stringify(err))
|
2014-03-05 14:10:50 -08:00
|
|
|
});
|
|
|
|
connection.on('end', function(){
|
2014-03-22 23:16:06 -07:00
|
|
|
logger.error(logSystem, 'redis', 'Connection to redis database as been ended');
|
|
|
|
logger.warning(logSystem, 'redis', 'Trying reconnection in 3 seconds...');
|
2014-03-05 14:31:32 -08:00
|
|
|
reconnectTimeout = setTimeout(function(){
|
|
|
|
connect();
|
|
|
|
}, 3000);
|
2014-03-04 12:24:02 -08:00
|
|
|
});
|
2014-03-05 14:10:50 -08:00
|
|
|
}
|
|
|
|
connect();
|
2014-03-03 12:51:11 -08:00
|
|
|
|
|
|
|
|
2014-03-12 23:37:27 -07:00
|
|
|
|
2014-03-05 14:10:50 -08: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 = [];
|
|
|
|
|
|
|
|
if (isValidShare){
|
|
|
|
redisCommands.push(['hincrby', coin + '_shares:roundCurrent', shareData.worker, shareData.difficulty]);
|
|
|
|
redisCommands.push(['hincrby', coin + '_stats', 'validShares', 1]);
|
|
|
|
|
|
|
|
/* Stores share diff, worker, and unique value with a score that is the timestamp. Unique value ensures it
|
|
|
|
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. */
|
2014-03-23 13:18:54 -07:00
|
|
|
var dateNow = Date.now();
|
|
|
|
redisCommands.push(['zadd', coin + '_hashrate', dateNow / 1000 | 0, [shareData.difficulty, shareData.worker, dateNow].join(':')]);
|
2014-03-13 16:20:57 -07:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
redisCommands.push(['hincrby', coin + '_stats', 'invalidShares', 1]);
|
|
|
|
}
|
2014-03-03 12:51:11 -08:00
|
|
|
|
2014-03-07 14:04:14 -08:00
|
|
|
if (isValidBlock){
|
2014-03-13 16:20:57 -07:00
|
|
|
redisCommands.push(['rename', coin + '_shares:roundCurrent', coin + '_shares:round' + shareData.height]);
|
2014-03-27 15:29:43 -07:00
|
|
|
redisCommands.push(['sadd', coin + '_blocksPending', [shareData.solution, shareData.tx, shareData.height, shareData.reward].join(':')]);
|
2014-03-13 16:20:57 -07:00
|
|
|
redisCommands.push(['hincrby', coin + '_stats', 'validBlocks', 1]);
|
2014-03-07 14:04:14 -08:00
|
|
|
}
|
2014-03-13 16:20:57 -07:00
|
|
|
else if (shareData.solution){
|
|
|
|
redisCommands.push(['hincrby', coin + '_stats', 'invalidBlocks', 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.multi(redisCommands).exec(function(err, replies){
|
|
|
|
if (err)
|
2014-03-22 23:46:59 -07:00
|
|
|
logger.error(logSystem, 'redis', 'Error with share processor multi ' + JSON.stringify(err));
|
2014-03-22 23:16:06 -07:00
|
|
|
else
|
2014-03-22 23:46:59 -07:00
|
|
|
logger.debug(logSystem, 'redis', 'Share data and stats recorded');
|
2014-03-13 16:20:57 -07:00
|
|
|
});
|
|
|
|
|
2014-03-07 14:04:14 -08:00
|
|
|
|
2014-03-04 00:33:55 -08:00
|
|
|
};
|
2014-03-01 17:19:10 -08:00
|
|
|
|
2014-03-04 00:33:55 -08:00
|
|
|
};
|