2014-03-09 19:31:58 -07:00
|
|
|
var redis = require('redis');
|
2014-03-11 18:56:19 -07:00
|
|
|
var async = require('async');
|
2014-03-09 19:31:58 -07:00
|
|
|
|
|
|
|
var Stratum = require('stratum-pool');
|
2014-04-26 15:24:06 -07:00
|
|
|
var util = require('stratum-pool/lib/util.js');
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-03-09 19:31:58 -07:00
|
|
|
module.exports = function(logger){
|
|
|
|
|
|
|
|
var poolConfigs = JSON.parse(process.env.pools);
|
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
var enabledPools = [];
|
2014-03-09 19:31:58 -07:00
|
|
|
|
|
|
|
Object.keys(poolConfigs).forEach(function(coin) {
|
2014-03-30 16:04:54 -07:00
|
|
|
var poolOptions = poolConfigs[coin];
|
2014-05-02 14:59:46 -07:00
|
|
|
if (poolOptions.paymentProcessing &&
|
|
|
|
poolOptions.paymentProcessing.enabled)
|
2014-03-30 16:04:54 -07:00
|
|
|
enabledPools.push(coin);
|
2014-03-09 19:31:58 -07:00
|
|
|
});
|
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
async.filter(enabledPools, function(coin, callback){
|
|
|
|
SetupForPool(logger, poolConfigs[coin], function(setupResults){
|
|
|
|
callback(setupResults);
|
|
|
|
});
|
|
|
|
}, function(coins){
|
|
|
|
coins.forEach(function(coin){
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
var poolOptions = poolConfigs[coin];
|
2014-05-02 14:59:46 -07:00
|
|
|
var processingConfig = poolOptions.paymentProcessing;
|
2014-03-30 16:04:54 -07:00
|
|
|
var logSystem = 'Payments';
|
|
|
|
var logComponent = coin;
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
logger.debug(logSystem, logComponent, 'Payment processing setup to run every '
|
|
|
|
+ processingConfig.paymentInterval + ' second(s) with daemon ('
|
|
|
|
+ processingConfig.daemon.user + '@' + processingConfig.daemon.host + ':' + processingConfig.daemon.port
|
2014-05-02 14:59:46 -07:00
|
|
|
+ ') and redis (' + poolOptions.redis.host + ':' + poolOptions.redis.port + ')');
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function SetupForPool(logger, poolOptions, setupFinished){
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-26 14:08:34 -07:00
|
|
|
|
2014-03-30 16:04:54 -07:00
|
|
|
var coin = poolOptions.coin.name;
|
2014-05-02 14:59:46 -07:00
|
|
|
var processingConfig = poolOptions.paymentProcessing;
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-03-22 23:16:06 -07:00
|
|
|
var logSystem = 'Payments';
|
|
|
|
var logComponent = coin;
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var daemon = new Stratum.daemon.interface([processingConfig.daemon]);
|
|
|
|
var redisClient = redis.createClient(poolOptions.redis.port, poolOptions.redis.host);
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var magnitude;
|
|
|
|
var minPaymentSatoshis;
|
|
|
|
var coinPrecision;
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var paymentInterval;
|
2014-03-30 16:04:54 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
async.parallel([
|
2014-03-30 16:04:54 -07:00
|
|
|
function(callback){
|
2014-05-02 14:59:46 -07:00
|
|
|
daemon.cmd('validateaddress', [poolOptions.address], function(result) {
|
|
|
|
if (result.error){
|
|
|
|
logger.error(logSystem, logComponent, 'Error with payment processing daemon ' + JSON.stringify(result.error));
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else if (!result.response || !result.response.ismine) {
|
|
|
|
logger.error(logSystem, logComponent,
|
2014-03-31 10:29:36 -07:00
|
|
|
'Daemon does not own pool address - payment processing can not be done with this daemon, '
|
2014-05-02 14:59:46 -07:00
|
|
|
+ JSON.stringify(result.response));
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else{
|
2014-03-30 16:04:54 -07:00
|
|
|
callback()
|
2014-05-02 14:59:46 -07:00
|
|
|
}
|
|
|
|
}, true);
|
2014-03-30 16:04:54 -07:00
|
|
|
},
|
|
|
|
function(callback){
|
2014-05-02 14:59:46 -07:00
|
|
|
daemon.cmd('getbalance', [], function(result){
|
|
|
|
if (result.error){
|
|
|
|
callback(true);
|
2014-03-30 16:04:54 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
try {
|
|
|
|
var d = result.data.split('result":')[1].split(',')[0].split('.')[1];
|
|
|
|
magnitude = parseInt('10' + new Array(d.length).join('0'));
|
|
|
|
minPaymentSatoshis = parseInt(processingConfig.minimumPayment * magnitude);
|
|
|
|
coinPrecision = magnitude.toString().length - 1;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
logger.error(logSystem, logComponent, 'Error detecting number of satoshis in a coin, cannot do payment processing');
|
|
|
|
callback(true);
|
2014-03-30 16:04:54 -07:00
|
|
|
}
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
}, true, true);
|
2014-03-30 16:04:54 -07:00
|
|
|
}
|
|
|
|
], function(err){
|
|
|
|
if (err){
|
|
|
|
setupFinished(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
paymentInterval = setInterval(function(){
|
2014-03-30 16:04:54 -07:00
|
|
|
try {
|
|
|
|
processPayments();
|
|
|
|
} catch(e){
|
|
|
|
throw e;
|
2014-03-09 19:31:58 -07:00
|
|
|
}
|
2014-03-30 16:04:54 -07:00
|
|
|
}, processingConfig.paymentInterval * 1000);
|
|
|
|
setTimeout(processPayments, 100);
|
|
|
|
setupFinished(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-03-09 19:31:58 -07:00
|
|
|
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var satoshisToCoins = function(satoshis){
|
|
|
|
return parseFloat((satoshis / magnitude).toFixed(coinPrecision));
|
2014-03-26 23:56:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Deal with numbers in smallest possible units (satoshis) as much as possible. This greatly helps with accuracy
|
|
|
|
when rounding and whatnot. When we are storing numbers for only humans to see, store in whole coin units. */
|
2014-03-12 15:33:29 -07:00
|
|
|
|
2014-03-11 18:56:19 -07:00
|
|
|
var processPayments = function(){
|
2014-03-27 15:29:43 -07:00
|
|
|
|
|
|
|
var startPaymentProcess = Date.now();
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var timeSpentRPC = 0;
|
|
|
|
var timeSpentRedis = 0;
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var startTimeRedis;
|
|
|
|
var startTimeRPC;
|
|
|
|
|
|
|
|
var startRedisTimer = function(){ startTimeRedis = Date.now() };
|
|
|
|
var endRedisTimer = function(){ timeSpentRedis += Date.now() - startTimeRedis };
|
|
|
|
|
|
|
|
var startRPCTimer = function(){ startTimeRPC = Date.now(); };
|
|
|
|
var endRPCTimer = function(){ timeSpentRPC += Date.now() - startTimeRedis };
|
|
|
|
|
|
|
|
async.waterfall([
|
2014-04-04 18:02:52 -07:00
|
|
|
|
2014-03-12 15:33:29 -07:00
|
|
|
/* Call redis to get an array of rounds - which are coinbase transactions and block heights from submitted
|
|
|
|
blocks. */
|
2014-03-11 18:56:19 -07:00
|
|
|
function(callback){
|
2014-03-11 21:57:03 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
startRedisTimer();
|
|
|
|
redisClient.multi([
|
|
|
|
['hgetall', coin + '_balances'],
|
|
|
|
['smembers', coin + '_blocksPending']
|
|
|
|
]).exec(function(error, results){
|
|
|
|
endRedisTimer();
|
2014-03-11 20:47:14 -07:00
|
|
|
|
2014-03-11 18:56:19 -07:00
|
|
|
if (error){
|
2014-04-04 18:02:52 -07:00
|
|
|
logger.error(logSystem, logComponent, 'Could not get blocks from redis ' + JSON.stringify(error));
|
2014-05-02 14:59:46 -07:00
|
|
|
callback(true);
|
2014-03-11 18:56:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var workers = {};
|
|
|
|
for (var w in results[0]){
|
|
|
|
workers[w] = {balance: parseInt(results[0][w])};
|
2014-03-11 18:56:19 -07:00
|
|
|
}
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var rounds = results[1].map(function(r){
|
2014-03-12 17:09:12 -07:00
|
|
|
var details = r.split(':');
|
2014-03-22 19:08:33 -07:00
|
|
|
return {
|
2014-04-02 12:01:05 -07:00
|
|
|
blockHash: details[0],
|
2014-03-27 15:29:43 -07:00
|
|
|
txHash: details[1],
|
|
|
|
height: details[2],
|
2014-03-22 19:08:33 -07:00
|
|
|
serialized: r
|
|
|
|
};
|
2014-03-11 18:56:19 -07:00
|
|
|
});
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
callback(null, workers, rounds);
|
2014-03-11 18:56:19 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-12 15:33:29 -07:00
|
|
|
/* Does a batch rpc call to daemon with all the transaction hashes to see if they are confirmed yet.
|
|
|
|
It also adds the block reward amount to the round object - which the daemon gives also gives us. */
|
2014-05-02 14:59:46 -07:00
|
|
|
function(workers, rounds, callback){
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-03-12 17:09:12 -07:00
|
|
|
var batchRPCcommand = rounds.map(function(r){
|
|
|
|
return ['gettransaction', [r.txHash]];
|
|
|
|
});
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-04-23 22:59:53 -07:00
|
|
|
batchRPCcommand.push(['getaccount', [poolOptions.address]]);
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
startRPCTimer();
|
2014-03-11 18:56:19 -07:00
|
|
|
daemon.batchCmd(batchRPCcommand, function(error, txDetails){
|
2014-05-02 14:59:46 -07:00
|
|
|
endRPCTimer();
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-03-11 20:47:14 -07:00
|
|
|
if (error || !txDetails){
|
2014-05-02 14:59:46 -07:00
|
|
|
logger.error(logSystem, logComponent, 'Check finished - daemon rpc error with batch gettransactions '
|
|
|
|
+ JSON.stringify(error));
|
|
|
|
callback(true);
|
2014-03-11 20:47:14 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:59:53 -07:00
|
|
|
var addressAccount;
|
|
|
|
|
2014-03-27 11:57:56 -07:00
|
|
|
txDetails.forEach(function(tx, i){
|
2014-04-23 22:59:53 -07:00
|
|
|
|
|
|
|
if (i === txDetails.length - 1){
|
|
|
|
addressAccount = tx.result;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-27 15:29:43 -07:00
|
|
|
var round = rounds[i];
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (tx.error && tx.error.code === -5){
|
|
|
|
logger.error(logSystem, logComponent, 'Daemon reports invalid transaction ' + round.txHash + ' '
|
|
|
|
+ JSON.stringify(tx.error));
|
|
|
|
return;
|
2014-03-27 11:57:56 -07:00
|
|
|
}
|
|
|
|
else if (tx.error || !tx.result){
|
2014-05-02 14:59:46 -07:00
|
|
|
logger.error(logSystem, logComponent, 'Odd error with gettransaction ' + round.txHash + ' '
|
|
|
|
+ JSON.stringify(tx));
|
|
|
|
return;
|
2014-03-15 17:58:28 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
else if (round.blockHash !== tx.result.blockhash){
|
|
|
|
logger.error(logSystem, logComponent, 'Daemon reports blockhash ' + tx.result.blockhash
|
|
|
|
+ ' for tx ' + round.txHash + ' is not the one we have stored: ' + round.blockHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (!(tx.result.details instanceof Array)){
|
|
|
|
logger.error(logSystem, logComponent, 'Details array missing from transaction '
|
|
|
|
+ round.txHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var generationTx = tx.result.details.filter(function(tx){
|
|
|
|
return tx.address === poolOptions.address;
|
|
|
|
})[0];
|
|
|
|
|
2014-05-04 01:21:15 -07:00
|
|
|
|
|
|
|
if (!generationTx && tx.result.details.length === 1){
|
|
|
|
generationTx = tx.results.details[0];
|
|
|
|
}
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (!generationTx){
|
|
|
|
logger.error(logSystem, logComponent, 'Missing output details to pool address for transaction '
|
|
|
|
+ round.txHash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
round.category = generationTx.category;
|
|
|
|
if (round.category === 'generate') {
|
2014-05-04 01:21:15 -07:00
|
|
|
round.reward = generationTx.amount || generationTx.value;
|
2014-03-27 15:29:43 -07:00
|
|
|
}
|
2014-03-15 17:58:28 -07:00
|
|
|
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
});
|
|
|
|
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-03-26 23:56:36 -07:00
|
|
|
//Filter out all rounds that are immature (not confirmed or orphaned yet)
|
2014-03-22 19:08:33 -07:00
|
|
|
rounds = rounds.filter(function(r){
|
2014-03-27 15:29:43 -07:00
|
|
|
switch (r.category) {
|
|
|
|
case 'generate':
|
|
|
|
return true;
|
|
|
|
case 'orphan':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-11 18:56:19 -07:00
|
|
|
});
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
callback(null, workers, rounds, addressAccount);
|
|
|
|
|
2014-03-11 18:56:19 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-11 20:47:14 -07:00
|
|
|
|
2014-03-12 15:33:29 -07:00
|
|
|
/* Does a batch redis call to get shares contributed to each round. Then calculates the reward
|
|
|
|
amount owned to each miner for each round. */
|
2014-05-02 14:59:46 -07:00
|
|
|
function(workers, rounds, addressAccount, callback){
|
2014-03-11 18:56:19 -07:00
|
|
|
|
2014-03-12 15:33:29 -07:00
|
|
|
|
2014-03-12 17:09:12 -07:00
|
|
|
var shareLookups = rounds.map(function(r){
|
|
|
|
return ['hgetall', coin + '_shares:round' + r.height]
|
|
|
|
});
|
2014-03-12 15:33:29 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
startRedisTimer();
|
2014-03-12 17:09:12 -07:00
|
|
|
redisClient.multi(shareLookups).exec(function(error, allWorkerShares){
|
2014-05-02 14:59:46 -07:00
|
|
|
endRedisTimer();
|
|
|
|
|
2014-03-11 18:56:19 -07:00
|
|
|
if (error){
|
2014-05-02 14:59:46 -07:00
|
|
|
callback('Check finished - redis error with multi get rounds share');
|
2014-03-11 18:56:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-03-11 20:47:14 -07:00
|
|
|
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-03-22 19:08:33 -07:00
|
|
|
rounds.forEach(function(round, i){
|
|
|
|
var workerShares = allWorkerShares[i];
|
2014-03-12 15:33:29 -07:00
|
|
|
|
2014-03-27 15:29:43 -07:00
|
|
|
if (!workerShares){
|
|
|
|
logger.error(logSystem, logComponent, 'No worker shares for round: '
|
2014-04-02 12:01:05 -07:00
|
|
|
+ round.height + ' blockHash: ' + round.blockHash);
|
2014-03-27 15:29:43 -07:00
|
|
|
return;
|
2014-03-11 20:47:14 -07:00
|
|
|
}
|
2014-03-20 15:25:59 -07:00
|
|
|
|
2014-03-27 15:29:43 -07:00
|
|
|
switch (round.category){
|
|
|
|
case 'orphan':
|
|
|
|
/* Each block that gets orphaned, all the shares go into the current round so that
|
|
|
|
miners still get a reward for their work. This seems unfair to those that just
|
|
|
|
started mining during this current round, but over time it balances out and rewards
|
|
|
|
loyal miners. */
|
2014-05-02 14:59:46 -07:00
|
|
|
round.workerShares = workerShares;
|
2014-03-27 15:29:43 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'generate':
|
|
|
|
/* We found a confirmed block! Now get the reward for it and calculate how much
|
|
|
|
we owe each miner based on the shares they submitted during that block round. */
|
2014-05-02 14:59:46 -07:00
|
|
|
var reward = parseInt(round.reward * magnitude);
|
2014-03-27 15:29:43 -07:00
|
|
|
|
|
|
|
var totalShares = Object.keys(workerShares).reduce(function(p, c){
|
2014-04-24 13:53:06 -07:00
|
|
|
return p + parseFloat(workerShares[c])
|
2014-03-27 15:29:43 -07:00
|
|
|
}, 0);
|
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
for (var workerAddress in workerShares){
|
|
|
|
var percent = parseFloat(workerShares[workerAddress]) / totalShares;
|
2014-03-27 15:29:43 -07:00
|
|
|
var workerRewardTotal = Math.floor(reward * percent);
|
2014-05-02 14:59:46 -07:00
|
|
|
var worker = workers[workerAddress] = (workers[workerAddress] || {});
|
|
|
|
worker.reward = (worker.reward || 0) + workerRewardTotal;
|
2014-03-27 15:29:43 -07:00
|
|
|
}
|
|
|
|
break;
|
2014-03-22 19:08:33 -07:00
|
|
|
}
|
|
|
|
});
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
callback(null, workers, rounds, addressAccount);
|
2014-03-11 20:47:14 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-11 21:01:33 -07:00
|
|
|
|
2014-03-11 20:47:14 -07:00
|
|
|
/* Calculate if any payments are ready to be sent and trigger them sending
|
2014-03-11 21:57:03 -07:00
|
|
|
Get balance different for each address and pass it along as object of latest balances such as
|
|
|
|
{worker1: balance1, worker2, balance2}
|
|
|
|
when deciding the sent balance, it the difference should be -1*amount they had in db,
|
|
|
|
if not sending the balance, the differnce should be +(the amount they earned this round)
|
|
|
|
*/
|
2014-05-02 14:59:46 -07:00
|
|
|
function(workers, rounds, addressAccount, callback) {
|
2014-03-11 21:01:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var trySend = function (withholdPercent) {
|
|
|
|
var addressAmounts = {};
|
|
|
|
var totalSent = 0;
|
|
|
|
for (var w in workers) {
|
|
|
|
var worker = workers[w];
|
|
|
|
worker.balance = worker.balance || 0;
|
|
|
|
worker.reward = worker.reward || 0;
|
|
|
|
var toSend = (worker.balance + worker.reward) * (1 - withholdPercent);
|
|
|
|
if (toSend >= minPaymentSatoshis) {
|
|
|
|
totalSent += toSend;
|
|
|
|
var address = worker.address = (worker.address || getProperAddress(w));
|
|
|
|
worker.sent = addressAmounts[address] = satoshisToCoins(toSend);
|
|
|
|
worker.balanceChange = Math.min(worker.balance, toSend) * -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
worker.balanceChange = Math.max(toSend - worker.balance, 0);
|
|
|
|
worker.sent = 0;
|
|
|
|
}
|
2014-03-20 15:25:59 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
|
|
|
|
if (Object.keys(addressAmounts).length === 0){
|
|
|
|
callback(null, workers, rounds);
|
|
|
|
return;
|
2014-03-20 15:25:59 -07:00
|
|
|
}
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
daemon.cmd('sendmany', [addressAccount || '', addressAmounts], function (result) {
|
|
|
|
if (result.error && result.error.code === -6) {
|
|
|
|
var higherPercent = withholdPercent + 0.01;
|
|
|
|
console.log('asdfasdfsadfasdf');
|
|
|
|
logger.warning(logSystem, logComponent, 'Not enough funds to send out payments, decreasing rewards by '
|
|
|
|
+ (higherPercent * 100) + '% and retrying');
|
|
|
|
trySend(higherPercent);
|
|
|
|
}
|
|
|
|
else if (result.error) {
|
2014-05-03 09:29:31 -07:00
|
|
|
logger.error(logSystem, logComponent, 'Error trying to send payments with RPC sendmany '
|
2014-05-02 14:59:46 -07:00
|
|
|
+ JSON.stringify(result.error));
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.debug(logSystem, logComponent, 'Sent out a total of ' + (totalSent / magnitude)
|
|
|
|
+ ' to ' + Object.keys(addressAmounts).length + ' workers');
|
|
|
|
if (withholdPercent > 0) {
|
|
|
|
logger.warning(logSystem, logComponent, 'Had to withhold ' + (withholdPercent * 100)
|
|
|
|
+ '% of reward from miners to cover transaction fees. '
|
|
|
|
+ 'Fund pool wallet with coins to prevent this from happening');
|
2014-03-22 19:08:33 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
callback(null, workers, rounds);
|
2014-03-20 15:25:59 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
}, true, true);
|
|
|
|
};
|
|
|
|
trySend(0);
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
},
|
|
|
|
function(workers, rounds, callback){
|
|
|
|
|
|
|
|
var totalPaid = 0;
|
|
|
|
|
|
|
|
var balanceUpdateCommands = [];
|
|
|
|
var workerPayoutsCommand = [];
|
|
|
|
|
|
|
|
for (var w in workers) {
|
|
|
|
var worker = workers[w];
|
|
|
|
if (worker.balanceChange !== 0){
|
|
|
|
balanceUpdateCommands.push([
|
|
|
|
'hincrby',
|
|
|
|
coin + '_balances',
|
|
|
|
w,
|
|
|
|
worker.balanceChange
|
|
|
|
]);
|
2014-03-20 15:25:59 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
if (worker.sent !== 0){
|
|
|
|
workerPayoutsCommand.push(['hincrbyfloat', coin + '_payouts', w, worker.sent]);
|
|
|
|
totalPaid += worker.sent;
|
2014-03-20 15:25:59 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
}
|
2014-03-20 15:25:59 -07:00
|
|
|
|
|
|
|
|
2014-03-27 15:29:43 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var movePendingCommands = [];
|
|
|
|
var roundsToDelete = [];
|
|
|
|
var orphanMergeCommands = [];
|
2014-03-20 15:25:59 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
rounds.forEach(function(r){
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
switch(r.category){
|
|
|
|
case 'orphan':
|
|
|
|
movePendingCommands.push(['smove', coin + '_blocksPending', coin + '_blocksOrphaned', r.serialized]);
|
|
|
|
var workerShares = r.workerShares;
|
|
|
|
Object.keys(workerShares).forEach(function(worker){
|
|
|
|
orphanMergeCommands.push(['hincrby', coin + '_shares:roundCurrent',
|
|
|
|
worker, workerShares[worker]]);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'generate':
|
|
|
|
movePendingCommands.push(['smove', coin + '_blocksPending', coin + '_blocksConfirmed', r.serialized]);
|
|
|
|
roundsToDelete.push(coin + '_shares:round' + r.height);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-12 17:09:12 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
});
|
2014-03-20 15:25:59 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
var finalRedisCommands = [];
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (movePendingCommands.length > 0)
|
|
|
|
finalRedisCommands = finalRedisCommands.concat(movePendingCommands);
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (orphanMergeCommands.length > 0)
|
|
|
|
finalRedisCommands = finalRedisCommands.concat(orphanMergeCommands);
|
2014-03-20 15:25:59 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (balanceUpdateCommands.length > 0)
|
|
|
|
finalRedisCommands = finalRedisCommands.concat(balanceUpdateCommands);
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (workerPayoutsCommand.length > 0)
|
|
|
|
finalRedisCommands = finalRedisCommands.concat(workerPayoutsCommand);
|
2014-04-02 13:43:58 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (roundsToDelete.length > 0)
|
|
|
|
finalRedisCommands.push(['del'].concat(roundsToDelete));
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (totalPaid !== 0)
|
|
|
|
finalRedisCommands.push(['hincrbyfloat', coin + '_stats', 'totalPaid', totalPaid]);
|
2014-03-22 19:08:33 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
if (finalRedisCommands.length === 0){
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2014-03-09 19:31:58 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
startRedisTimer();
|
|
|
|
redisClient.multi(finalRedisCommands).exec(function(error, results){
|
|
|
|
endRedisTimer();
|
2014-04-02 03:56:13 -07:00
|
|
|
if (error){
|
2014-05-02 14:59:46 -07:00
|
|
|
clearInterval(paymentInterval);
|
|
|
|
logger.error(logSystem, logComponent,
|
|
|
|
'Payments sent but could not update redis. ' + JSON.stringify(error)
|
|
|
|
+ ' Disabling payment processing to prevent possible double-payouts. The redis commands in '
|
|
|
|
+ coin + '_finalRedisCommands.txt must be ran manually');
|
|
|
|
fs.writeFile(coin + '_finalRedisCommands.txt', JSON.stringify(finalRedisCommands), function(err){
|
|
|
|
logger.error('Could not write finalRedisCommands.txt, you are fucked.');
|
|
|
|
});
|
2014-04-02 03:56:13 -07:00
|
|
|
}
|
2014-05-02 14:59:46 -07:00
|
|
|
callback();
|
2014-04-02 03:56:13 -07:00
|
|
|
});
|
2014-03-11 18:56:19 -07:00
|
|
|
}
|
2014-03-27 15:29:43 -07:00
|
|
|
|
2014-05-02 14:59:46 -07:00
|
|
|
], function(){
|
2014-03-27 15:29:43 -07:00
|
|
|
|
|
|
|
var paymentProcessTime = Date.now() - startPaymentProcess;
|
2014-05-02 14:59:46 -07:00
|
|
|
logger.debug(logSystem, logComponent, 'Finished interval - time spent: '
|
|
|
|
+ paymentProcessTime + 'ms total, ' + timeSpentRedis + 'ms redis, '
|
|
|
|
+ timeSpentRPC + 'ms daemon RPC');
|
2014-03-27 15:29:43 -07:00
|
|
|
|
2014-03-09 19:31:58 -07:00
|
|
|
});
|
2014-03-11 18:56:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-26 15:24:06 -07:00
|
|
|
var getProperAddress = function(address){
|
|
|
|
if (address.length === 40){
|
|
|
|
return util.addressFromEx(poolOptions.address, address);
|
|
|
|
}
|
|
|
|
else return address;
|
|
|
|
};
|
|
|
|
|
2014-03-20 16:05:13 -07:00
|
|
|
|
2014-05-03 09:29:31 -07:00
|
|
|
}
|