From 90a8404bab75915f2aeb036ebdd47b7a333baeed Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 13:51:30 +0100 Subject: [PATCH] [CLEANUP] Code cleanup, error checking Fixes #1315 once merged. --- cronjobs/liquid_payout.php | 77 +++++++++++-------- public/include/config/error_codes.inc.php | 1 + public/include/config/global.inc.dist.php | 29 ++++--- public/include/pages/admin/monitoring.inc.php | 2 +- 4 files changed, 65 insertions(+), 44 deletions(-) mode change 100644 => 100755 cronjobs/liquid_payout.php diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php old mode 100644 new mode 100755 index 7c088f26..8979b270 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -6,53 +6,64 @@ chdir(dirname(__FILE__)); // Include all settings and classes require_once('shared.inc.php'); -// Check For RPC Connection -if ($bitcoin->can_connect() === true){ - $dBalance = $bitcoin->getbalance(); - // Check Wallet Balance - $log->logDebug("The Wallet Balance is " .$dBalance. "\n"); - $dGetInfo = $bitcoin->getinfo(); - - // Check for POS Mint - - if (is_array($dGetInfo) && array_key_exists('newmint', $dGetInfo)) { - $dNewmint = $dGetInfo['newmint']; - $log->logDebug("Current Mint is: " .$dNewmint); - } else { - $dNewmint = -1; - } +// Simple configuration check +if (empty($config['coldwallet'])) { + $log->logFatal('Missing config option: coldwallet'); + $monitoring->endCronjob($cron_name, 'E0075', 1, true); } -else { - $dBalance = 0; - $dNewmint = -1; - $log->logError('Unable to connect to wallet RPC service'); + +// Check RPC connection +if ($bitcoin->can_connect() !== true) { + $log->logFatal('Unable to connect to RPC server, exiting'); + $monitoring->endCronjob($cron_name, 'E0006', 1, true); +} else { + // Check Wallet Balance + $dBalance = $bitcoin->getbalance(); + $log->logDebug('The wallet balance is: ' .$dBalance); + + // Do we have anything available at all? + if (! ($dBalance > 0)) { + $log->logInfo('No coins available in wallet'); + $monitoring->endCronjob($cron_name, 'E0076', 0, true, false); + } + + // Check for POS Mint + $dGetInfo = $bitcoin->getinfo(); + if (is_array($dGetInfo) && array_key_exists('newmint', $dGetInfo)) { + $dNewmint = $dGetInfo['newmint']; + $log->logDebug('Current Mint is: ' . $dNewmint); + } else { + $dNewmint = -1; + } } // Fetch locked balance from transactions $dLockedBalance = $transaction->getLockedBalance(); -$log->logDebug("The Locked Wallet Balance for Users is: " .$dLockedBalance. "\n"); +$log->logDebug('The locked wallet balance for users is: ' . $dLockedBalance); // Fetch Final Wallet Balance after Transfer $dFloat = $dLockedBalance + $config['coldwallet']['reserve']; $dThreshold = $config['coldwallet']['threshold']; -$sendAddress = $config['coldwallet']['address']; - -$log->logDebug("The Locked Wallet Balance & Float amounts to: " .$dFloat. "\n"); +$log->logDebug('The locked wallet balance + reserves amounts to: ' . $dFloat); // Send Liquid Balance +$sendAddress = $config['coldwallet']['address']; $send = $dBalance - $dFloat ; -$log->logInfo("Final Sending Amount is : " .$send. "\n"); +$log->logDebug('Final Sending Amount is : ' . $send); -if($send > $dThreshold){ - if($sendAddress !== ''){ - $bitcoin->sendtoaddress($sendAddress, $send); +if($send > $dThreshold) { + if (!empty($sendAddress)) { + try { + $bitcoin->sendtoaddress($sendAddress, $send); + } catch (BitcoinClientException $e) { + $log->logError('Failed to send coins to address, skipping liquid assets payout'); + } + $log->logInfo('Sent out ' . $send . ' liquid assets'); + } else { + $log->logDebug('No wallet address set'); } - else { - $log->logInfo("No wallet address set\n"); - } -} -else { - $log->logInfo("Final Sending Amount Not Exceed threshold : " .$send. "\n"); +} else { + $log->logDebug('Final sending amount not exceeding threshold: ' . $send); } ?> diff --git a/public/include/config/error_codes.inc.php b/public/include/config/error_codes.inc.php index a157f14b..c9164058 100644 --- a/public/include/config/error_codes.inc.php +++ b/public/include/config/error_codes.inc.php @@ -73,4 +73,5 @@ $aErrorCodes['E0072'] = 'Worker names must be alphanumeric'; $aErrorCodes['E0073'] = 'Worker name is too long; try entering a shorter name'; $aErrorCodes['E0074'] = 'Failed deleting expired tokens'; $aErrorCodes['E0075'] = 'Upgrade required'; +$aErrorCodes['E0076'] = 'No coins in wallet available'; ?> diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 3cc85842..714356a1 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -77,19 +77,28 @@ $config['wallet']['username'] = 'testnet'; $config['wallet']['password'] = 'testnet'; /** - * Added support for payout of liquid assets - * - * address: the address of the wallet to the address you'd like to receive the coins in - * * reserve: is the amount you'd like to remain in the wallet. Recommended is at least 1 block value - * threshold: is the amount of coins you'd like to send per batch minimum. once exceeded this is sent - * to the address of the cold wallet. - * + * Payout of liquid assets + * + * Explanation: + * Running pools, especially those with active fees, will build up a good + * amount of liquid assets that can be used by pool operators. If you wish + * to automaitcally send your assets to a offline wallet, set your account + * address, reserves and thresholds here. + * + * Options: + * address : The address of the wallet to the address you'd like to receive the coins in + * reserve : The amount you'd like to remain in the wallet. Recommended is at least 1 block value + * threshold : The amount of coins you'd like to send per batch minimum. Once exceeded, this is sent + * to the offline wallet address specified. + * Default: + * addresss : empty + * reserve : 50 + * threshold : 25 **/ $config['coldwallet']['address'] = ''; -$config['coldwallet']['reserve'] = 1.1; -$config['coldwallet']['threshold'] = 1; - +$config['coldwallet']['reserve'] = 50; +$config['coldwallet']['threshold'] = 5; /** * Lock account after maximum failed logins diff --git a/public/include/pages/admin/monitoring.inc.php b/public/include/pages/admin/monitoring.inc.php index 9b8f795c..08efaee4 100644 --- a/public/include/pages/admin/monitoring.inc.php +++ b/public/include/pages/admin/monitoring.inc.php @@ -10,7 +10,7 @@ if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) { } // Default crons to monitor -$aCrons = array('statistics','payouts','token_cleanup','archive_cleanup','blockupdate','findblock','notifications','tickerupdate'); +$aCrons = array('statistics','payouts','token_cleanup','archive_cleanup','blockupdate','findblock','notifications','tickerupdate','liquid_payout'); // Special cases, only add them if activated switch ($config['payout_system']) {