z-nomp/libs/shareProcessor.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

var redis = require('redis');
var Stratum = require('stratum-pool');
/*
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:
*/
module.exports = function(logger, poolConfig){
2014-03-03 12:51:11 -08:00
var internalConfig = poolConfig.shareProcessing.internal;
var redisConfig = internalConfig.redis;
var coin = poolConfig.coin.name;
2014-03-03 12:51:11 -08:00
var connection;
2014-03-04 12:24:02 -08:00
function connect(){
var reconnectTimeout;
connection = redis.createClient(redisConfig.port, redisConfig.host);
connection.on('ready', function(){
clearTimeout(reconnectTimeout);
logger.debug('redis', 'Successfully connected to redis database');
});
connection.on('error', function(err){
logger.error('redis', 'Redis client had an error: ' + JSON.stringify(err))
});
connection.on('end', function(){
logger.error('redis', 'Connection to redis database as been ended');
logger.warning('redis', 'Trying reconnection in 3 seconds...');
reconnectTimeout = setTimeout(function(){
connect();
}, 3000);
2014-03-04 12:24:02 -08:00
});
}
connect();
2014-03-03 12:51:11 -08:00
this.handleShare = function(isValidShare, isValidBlock, shareData){
2014-03-04 12:24:02 -08:00
if (!isValidShare) return;
2014-03-03 12:51:11 -08:00
2014-03-11 12:12:46 -07:00
connection.hincrby(['shares_' + coin + ':roundCurrent', shareData.worker, shareData.difficulty], function(error, result){
2014-03-04 12:24:02 -08:00
if (error)
logger.error('redis', 'Could not store worker share')
2014-03-04 12:24:02 -08:00
});
2014-03-03 12:51:11 -08:00
2014-03-07 14:04:14 -08:00
if (isValidBlock){
2014-03-11 12:12:46 -07:00
connection.rename('shares_' + coin + ':roundCurrent', 'shares_' + coin + ':round' + shareData.height, function(result){
console.log('rename result: ' + result);
});
2014-03-09 19:31:58 -07:00
connection.sadd(['blocks_' + coin, shareData.tx + ':' + shareData.height], function(error, result){
2014-03-07 14:04:14 -08:00
if (error)
logger.error('redis', 'Could not store block data');
});
}
};
};