[Enhance] PPS / Payout Crons - Improved logging and and improved fail safes

[Optimize] Removed Coin Daemon / RPC Address Validation
This commit is contained in:
Neozonz 2014-01-20 22:20:02 -05:00
parent f20719720d
commit 08a3f12368
2 changed files with 67 additions and 41 deletions

View File

@ -29,7 +29,7 @@ if ($setting->getValue('disable_payouts') == 1) {
$log->logInfo(" payouts disabled via admin panel");
$monitoring->endCronjob($cron_name, 'E0009', 0, true, false);
}
$log->logInfo("Starting Payout...");
if ($bitcoin->can_connect() !== true) {
$log->logFatal(" unable to connect to RPC server, exiting");
$monitoring->endCronjob($cron_name, 'E0006', 1, true);
@ -37,10 +37,15 @@ if ($bitcoin->can_connect() !== true) {
if ($setting->getValue('disable_manual_payouts') != 1) {
// Fetch outstanding payout requests
$aPayouts = $oPayout->getUnprocessedPayouts();
if (!$aPayouts = $oPayout->getUnprocessedPayouts()) {
$log->logFatal("\tFailed Processing Manual Payment Queue...");
$monitoring->endCronjob($cron_name, 'E0050', 1, true);
}
if (count($aPayouts > 0)) $log->logDebug(" found " . count($aPayouts) . " queued manual payout requests");
if (count($aPayouts) > 0) {
$log->logInfo("\tStarting Manual Payments...");
$log->logInfo("\tAccount ID\tUsername\tBalance\t\tCoin Address");
foreach ($aPayouts as $aData) {
$aBalance = $transaction->getBalance($aData['account_id']);
@ -50,21 +55,11 @@ if ($setting->getValue('disable_manual_payouts') != 1) {
if ($dBalance > $config['txfee_manual']) {
// To ensure we don't run this transaction again, lets mark it completed
if (!$oPayout->setProcessed($aData['id'])) {
$log->logFatal('unable to mark transactions ' . $aData['id'] . ' as processed.');
$log->logFatal('unable to mark transactions ' . $aData['id'] . ' as processed. ERROR: ' . $oPayout->getCronError());
$monitoring->endCronjob($cron_name, 'E0010', 1, true);
}
$log->logInfo("\t" . $aData['account_id'] . "\t\t" . $aData['username'] . "\t" . $dBalance . "\t\t" . $aData['coin_address']);
try {
$aStatus = $bitcoin->validateaddress($aData['coin_address']);
if (!$aStatus['isvalid']) {
$log->logError('Skipping payment. Failed to verify coin address: ' . $aData['coin_address'] . ' ERROR: ' . $e->getMessage());
continue;
}
} catch (Exception $e) {
$log->logError('Skipping payment. RPC ERROR: ' . $e->getMessage());
continue;
}
try {
$txid = $bitcoin->sendtoaddress($aData['coin_address'], $dBalance - $config['txfee_manual']);
} catch (Exception $e) {
@ -75,22 +70,25 @@ if ($setting->getValue('disable_manual_payouts') != 1) {
if ($transaction->addTransaction($aData['account_id'], $dBalance - $config['txfee_manual'], 'Debit_MP', NULL, $aData['coin_address'], $txid) && $transaction->addTransaction($aData['account_id'], $config['txfee_manual'], 'TXFee', NULL, $aData['coin_address'])) {
// Mark all older transactions as archived
if (!$transaction->setArchived($aData['account_id'], $transaction->insert_id))
$log->logError('Failed to mark transactions for #' . $aData['account_id'] . ' prior to #' . $transaction->insert_id . ' as archived');
$log->logError('Failed to mark transactions for #' . $aData['account_id'] . ' prior to #' . $transaction->insert_id . ' as archived. ERROR: ' . $transaction->getCronError());
// Notify user via mail
$aMailData['email'] = $user->getUserEmail($user->getUserName($aData['account_id']));
$aMailData['subject'] = 'Manual Payout Completed';
$aMailData['amount'] = $dBalance - $config['txfee_manual'];
$aMailData['payout_id'] = $aData['id'];
if (!$notification->sendNotification($aData['account_id'], 'manual_payout', $aMailData))
$log->logError('Failed to send notification email to users address: ' . $aMailData['email']);
$log->logError('Failed to send notification email to users address: ' . $aMailData['email'] . 'ERROR: ' . $notification->getCronError());
// Recheck the users balance to make sure it is now 0
$aBalance = $transaction->getBalance($aData['account_id']);
if (!$aBalance = $transaction->getBalance($aData['account_id']) {
$log->logFatal('Failed to fetch balance for account ' . $aData['account_id'] . '. ERROR: ' . $transaction->getCronError());
$monitoring->endCronjob($cron_name, 'E0065', 1, true);
}
if ($aBalance['confirmed'] > 0) {
$log->logFatal('User has a remaining balance of ' . $aBalance['confirmed'] . ' after a successful payout!');
$monitoring->endCronjob($cron_name, 'E0065', 1, true);
}
} else {
$log->logFatal('Failed to add new Debit_MP transaction in database for user ' . $user->getUserName($aData['account_id']));
$log->logFatal('Failed to add new Debit_MP transaction in database for user ' . $user->getUserName($aData['account_id']) . ' ERROR: ' . $notification->getCronError()););
$monitoring->endCronjob($cron_name, 'E0064', 1, true);
}
}
@ -103,11 +101,15 @@ if ($setting->getValue('disable_manual_payouts') != 1) {
if ($setting->getValue('disable_auto_payouts') != 1) {
// Fetch all users balances
$users = $transaction->getAPQueue();
if (!$users = $transaction->getAPQueue()) {
$log->logFatal("\tFailed Processing Auto Payment Payment Queue. ERROR: " . $share->getCronError());
$monitoring->endCronjob($cron_name, 'E0050', 1, true);
}
if (count($users) > 0) $log->logDebug(" found " . count($users) . " queued payout(s)");
// Go through users and run transactions
if (! empty($users)) {
$log->logInfo("\tStarting Auto Payments...");
$log->logInfo("\tUserID\tUsername\tBalance\tThreshold\tAddress");
foreach ($users as $aUserData) {
@ -116,18 +118,6 @@ if ($setting->getValue('disable_auto_payouts') != 1) {
// Only run if balance meets threshold and can pay the potential transaction fee
if ($dBalance > $aUserData['ap_threshold'] && $dBalance > $config['txfee_auto']) {
// Validate address against RPC
try {
$aStatus = $bitcoin->validateaddress($aUserData['coin_address']);
if (!$aStatus['isvalid']) {
$log->logError('Skipping payment. Failed to verify coin address: ' . $aData['coin_address'] . ' ERROR: ' . $e->getMessage());
continue;
}
} catch (Exception $e) {
$log->logError('Skipping payment. RPC ERROR: ' . $e->getMessage());
continue;
}
// Send balance, fees are reduced later by RPC Server
try {
$txid = $bitcoin->sendtoaddress($aUserData['coin_address'], $dBalance - $config['txfee_auto']);
@ -140,13 +130,13 @@ if ($setting->getValue('disable_auto_payouts') != 1) {
if ($transaction->addTransaction($aUserData['id'], $dBalance - $config['txfee_auto'], 'Debit_AP', NULL, $aUserData['coin_address'], $txid) && $transaction->addTransaction($aUserData['id'], $config['txfee_auto'], 'TXFee', NULL, $aUserData['coin_address'])) {
// Mark all older transactions as archived
if (!$transaction->setArchived($aUserData['id'], $transaction->insert_id))
$log->logError('Failed to mark transactions for user #' . $aUserData['id'] . ' prior to #' . $transaction->insert_id . ' as archived');
$log->logError('Failed to mark transactions for user #' . $aUserData['id'] . ' prior to #' . $transaction->insert_id . ' as archived. ERROR: ' . $transaction->getCronError());
// Notify user via mail
$aMailData['email'] = $user->getUserEmail($user->getUserName($aUserData['id']));
$aMailData['subject'] = 'Auto Payout Completed';
$aMailData['amount'] = $dBalance - $config['txfee_auto'];
if (!$notification->sendNotification($aUserData['id'], 'auto_payout', $aMailData))
$log->logError('Failed to send notification email to users address: ' . $aMailData['email']);
$log->logError('Failed to send notification email to users address: ' . $aMailData['email'] . ' ERROR: ' . $notification->getCronError());
// Recheck the users balance to make sure it is now 0
$aBalance = $transaction->getBalance($aUserData['id']);
if ($aBalance['confirmed'] > 0) {
@ -154,17 +144,19 @@ if ($setting->getValue('disable_auto_payouts') != 1) {
$monitoring->endCronjob($cron_name, 'E0065', 1, true);
}
} else {
$log->logFatal('Failed to add new Debit_AP transaction in database for user ' . $user->getUserName($aUserData['id']));
$log->logFatal('Failed to add new Debit_AP transaction in database for user ' . $user->getUserName($aUserData['id']) . ' ERROR: ' . $notification->getCronError());
$monitoring->endCronjob($cron_name, 'E0064', 1, true);
}
}
}
} else {
$log->logDebug(" no user has configured their AP > 0");
$log->logDebug("Users have not configured their AP > 0");
}
} else {
$log->logDebug("Auto payouts disabled via admin panel");
}
$log->logInfo("\tCompleted Payouts");
// Cron cleanup and monitoring
require_once('cron_end.inc.php');
?>

View File

@ -31,6 +31,7 @@ if ($config['payout_system'] != 'pps') {
$log->logInfo("Please activate this cron in configuration via payout_system = pps\n");
exit(0);
}
$log->logInfo("Starting PPS Payout...");
// Fetch all transactions since our last block
if ( $bitcoin->can_connect() === true ){
@ -55,27 +56,45 @@ if ($config['pps']['reward']['type'] == 'blockavg' && $block->getBlockCount() >
if ($config['pps']['reward']['type'] == 'block') {
if ($aLastBlock = $block->getLast()) {
$pps_reward = $aLastBlock['amount'];
$log->logInfo("PPS value (Last Block): " . $pps_reward);
} else {
$pps_reward = $config['pps']['reward']['default'];
$log->logInfo("PPS value (Default): " . $pps_reward);
}
} else {
$pps_reward = $config['pps']['reward']['default'];
$log->logInfo("PPS value (Default): " . $pps_reward);
}
}
// Per-share value to be paid out to users
$pps_value = round($pps_reward / (pow(2, $config['target_bits']) * $dDifficulty), 12);
$log->logInfo("PPS value: " . $pps_value);
// Find our last share accounted and last inserted share for PPS calculations
$iPreviousShareId = $setting->getValue('pps_last_share_id');
$iLastShareId = $share->getLastInsertedShareId();
$log->logInfo("PPS Previous Share ID: " . $iLastShareId);
if (!$iPreviousShareId = $setting->getValue('pps_last_share_id');
$log->logError("Failed to fetch Previous Share ID. ERROR: " . $setting->getCronError());
}
$log->logInfo("PPS Last Share ID: " . $iPreviousShareId);
if (!$iLastShareId = $share->getLastInsertedShareId();
$log->logError("Failed to fetch Last Inserted PPS Share ID. ERROR: " . $share->getCronError());
}
// Check for all new shares, we start one higher as our last accounted share to avoid duplicates
$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId);
$log->logInfo("Query getSharesForAccounts... starting...");
if (!$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId)) {
$log->logError("Failed to fetch Account Shares. ERROR: " . $share->getCronError());
}
$log->logInfo("Query Completed...");
if (!empty($aAccountShares)) {
// Info for this payout
$log->logInfo("PPS reward type: " . $config['pps']['reward']['type'] . ", amount: " . $pps_reward . "\tdifficulty: " . $dDifficulty . "\tPPS value: " . $pps_value);
$log->logInfo("Running through accounts to process shares...");
$log->logInfo("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\tPayout\t\tDonation\tFee");
}
@ -123,18 +142,24 @@ foreach ($aAccountShares as $aData) {
}
// Store our last inserted ID for the next run
$setting->setValue('pps_last_share_id', $iLastShareId);
$log->logInfo("\tFetching Last Share ID...");
if (!$setting->setValue('pps_last_share_id', $iLastShareId)) {
$log->logError("Failed to fetch Last Share ID. ERROR: " . $setting->getCronError());
}
// Fetch all unaccounted blocks
$log->logInfo("\tFetching unaccounted blocks.");
$aAllBlocks = $block->getAllUnaccounted('ASC');
if (empty($aAllBlocks)) {
$log->logDebug("No new unaccounted blocks found");
$log->logInfo("\tNo new blocks.");
// No monitoring event here, not fatal for PPS
}
// Go through blocks and archive/delete shares that have been accounted for
foreach ($aAllBlocks as $iIndex => $aBlock) {
// If we are running through more than one block, check for previous share ID
$log->logInfo("\tProcess each block for Previous Share ID.");
$iLastBlockShare = @$aAllBlocks[$iIndex - 1]['share_id'] ? @$aAllBlocks[$iIndex - 1]['share_id'] : 0;
if (!is_numeric($aBlock['share_id'])) {
$log->logFatal("Block " . $aBlock['height'] . " has no share_id associated with it, not going to continue");
@ -144,27 +169,36 @@ foreach ($aAllBlocks as $iIndex => $aBlock) {
exit(1);
}
// Per account statistics
$aAccountShares = $share->getSharesForAccounts(@$iLastBlockShare, $aBlock['share_id']);
$log->logInfo("\tStarting to store fresh user statistics...");
if (!$aAccountShares = $share->getSharesForAccounts(@$iLastBlockShare, $aBlock['share_id'])) {
$log->logError("Failed to Account Shares. ERROR: " . $share->getCronError());
}
foreach ($aAccountShares as $key => $aData) {
if (!$statistics->updateShareStatistics($aData, $aBlock['id']))
$log->logError("Failed to update stats for this block on : " . $aData['username'] . ': ' . $statistics->getCronError());
$log->logError("Failed to update statistics for Block " . $aBlock['id'] . "for" . $aData['username'] . ' ERROR: ' . $statistics->getCronError());
}
// Move shares to archive
$log->logInfo("\tMove shares to archive...");
if ($aBlock['share_id'] < $iLastShareId) {
if (!$share->moveArchive($aBlock['share_id'], $aBlock['id'], @$iLastBlockShare))
$log->logError("Failed to copy shares to archive: " . $share->getCronError() . ': ' . $share->getCronError());
}
// Delete shares
$log->logInfo("\tDelete accounted shares...");
if ($aBlock['share_id'] < $iLastShareId && !$share->deleteAccountedShares($aBlock['share_id'], $iLastBlockShare)) {
$log->logFatal("Failed to delete accounted shares from " . $aBlock['share_id'] . " to " . $iLastBlockShare . ", aborting! Error: " . $share->getCronError());
$monitoring->endCronjob($cron_name, 'E0016', 1, true);
}
// Mark this block as accounted for
$log->logInfo("\tMark Block as accounted");
if (!$block->setAccounted($aBlock['id'])) {
$log->logFatal("Failed to mark block as accounted! Aborting! Error: " . $block->getCronError());
$monitoring->endCronjob($cron_name, 'E0014', 1, true);
}
}
$log->logInfo("Completed PPS Payout");
require_once('cron_end.inc.php');
?>