From 640c6a4426356f45617c5c400b214febc3454a67 Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 11:52:34 +0100 Subject: [PATCH 01/14] Create liquid_payout.php Add support for a cronjob to send liquid assets. Maintains a set reserve and sends only when a wallet address is set; and the threshold is set in the config. --- cronjobs/liquid_payout.php | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cronjobs/liquid_payout.php diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php new file mode 100644 index 00000000..f357c766 --- /dev/null +++ b/cronjobs/liquid_payout.php @@ -0,0 +1,57 @@ +#!/usr/bin/php +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; +} +} else { + $dBalance = 0; + $dNewmint = -1; + $log->logError('Unable to connect to wallet RPC service'); +} + +// Fetch locked balance from transactions +$dLockedBalance = $transaction->getLockedBalance(); +$log->logDebug("The Locked Wallet Balance for Users is: " .$dLockedBalance. "\n"); + +// 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"); + +// Send Liquid Balance +$send = $dBalance - $dFloat ; +$log->logInfo("Final Sending Amount is : " .$send. "\n"); + +if($send > $dThreshold){ + if($sendAddress !== ''){ + $bitcoin->sendtoaddress($sendAddress, $send); + } + else { + $log->logInfo("No wallet address set\n"); + } +} +else{ + $log->logInfo("Final Sending Amount Not Exceed threshold : " .$send. "\n"); +} +?> + From 26d138a637a689920a0b4c3c1fa4811564cc0f98 Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 11:54:08 +0100 Subject: [PATCH 02/14] Update run-crons.sh added liquid_payout.php --- cronjobs/run-crons.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/run-crons.sh b/cronjobs/run-crons.sh index 759d1879..7dc1c262 100755 --- a/cronjobs/run-crons.sh +++ b/cronjobs/run-crons.sh @@ -10,7 +10,7 @@ PHP_BIN=$( which php ) # List of cruns to execute -CRONS="findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php tickerupdate.php notifications.php statistics.php token_cleanup.php archive_cleanup.php" +CRONS="liquid_payout.php findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php tickerupdate.php notifications.php statistics.php token_cleanup.php archive_cleanup.php" # Output additional runtime information VERBOSE="0" From 9f69a0b9f74233b5283cf83f0d526f4949d88803 Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 11:54:35 +0100 Subject: [PATCH 03/14] Update run-payout.sh added liquid_payout.php --- cronjobs/run-payout.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/run-payout.sh b/cronjobs/run-payout.sh index d14a56bd..6fc26158 100755 --- a/cronjobs/run-payout.sh +++ b/cronjobs/run-payout.sh @@ -10,7 +10,7 @@ PHP_BIN=$( which php ) # List of cruns to execute -CRONS="findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php" +CRONS="liquid_payout.php findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php" # Output additional runtime information VERBOSE="0" From a37ebd2db891e372aaf77b6d03933c0b68407bea Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 11:55:04 +0100 Subject: [PATCH 04/14] Update version.inc.php changed config to 0.0.2 to support liquid_payout.php cron --- public/include/version.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/version.inc.php b/public/include/version.inc.php index 026707ea..e3f284f6 100644 --- a/public/include/version.inc.php +++ b/public/include/version.inc.php @@ -1,7 +1,7 @@ Date: Fri, 10 Jan 2014 11:58:31 +0100 Subject: [PATCH 05/14] Update global.inc.dist.php added config variabled for the liquid_payout.php cronjob --- public/include/config/global.inc.dist.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 704d01eb..8d02bc8e 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -7,7 +7,7 @@ if (!defined('SECURITY')) die('Hacking attempt'); * This is used in the version check to ensure you run the latest version of the configuration file. * Once you upgraded your config, change the version here too. **/ -$config['version'] = '0.0.1'; +$config['version'] = '0.0.2'; // Our include directory for additional features define('INCLUDE_DIR', BASEPATH . 'include'); @@ -76,6 +76,21 @@ $config['wallet']['host'] = 'localhost:19334'; $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. + * + **/ + +$config['coldwallet']['address'] = 'RPzoUrjk8eScysjrhrSJY9Bg4AgpwYDLiP'; +$config['coldwallet']['reserve'] = 1.1; +$config['coldwallet']['threshold'] = 1; + + /** * Lock account after maximum failed logins * From 75f35d80b143d535d74fdcb9eacbcd0534224907 Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 12:18:28 +0100 Subject: [PATCH 06/14] Update liquid_payout.php formatting --- cronjobs/liquid_payout.php | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index f357c766..7c088f26 100644 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -8,21 +8,22 @@ 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(); + $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; + // 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; + } } -} else { - $dBalance = 0; +else { + $dBalance = 0; $dNewmint = -1; $log->logError('Unable to connect to wallet RPC service'); } @@ -43,15 +44,15 @@ $send = $dBalance - $dFloat ; $log->logInfo("Final Sending Amount is : " .$send. "\n"); if($send > $dThreshold){ - if($sendAddress !== ''){ - $bitcoin->sendtoaddress($sendAddress, $send); - } - else { - $log->logInfo("No wallet address set\n"); - } -} -else{ - $log->logInfo("Final Sending Amount Not Exceed threshold : " .$send. "\n"); + if($sendAddress !== ''){ + $bitcoin->sendtoaddress($sendAddress, $send); + } + else { + $log->logInfo("No wallet address set\n"); + } +} +else { + $log->logInfo("Final Sending Amount Not Exceed threshold : " .$send. "\n"); } ?> From fca7bd7b3d64816a9a745b1a0b5bd5d223bd5f9b Mon Sep 17 00:00:00 2001 From: myrinx Date: Fri, 10 Jan 2014 12:32:48 +0100 Subject: [PATCH 07/14] Update global.inc.dist.php removed wallet address :) --- public/include/config/global.inc.dist.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 8d02bc8e..3cc85842 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -86,7 +86,7 @@ $config['wallet']['password'] = 'testnet'; * **/ -$config['coldwallet']['address'] = 'RPzoUrjk8eScysjrhrSJY9Bg4AgpwYDLiP'; +$config['coldwallet']['address'] = ''; $config['coldwallet']['reserve'] = 1.1; $config['coldwallet']['threshold'] = 1; From 90a8404bab75915f2aeb036ebdd47b7a333baeed Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 13:51:30 +0100 Subject: [PATCH 08/14] [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']) { From 0b43c66e67b5025bac4b71ac435d828baee3beed Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 14:46:52 +0100 Subject: [PATCH 09/14] [CLEANUP] Removed newline --- public/include/config/global.inc.dist.php | 1 - 1 file changed, 1 deletion(-) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 714356a1..ddabfca7 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -95,7 +95,6 @@ $config['wallet']['password'] = 'testnet'; * reserve : 50 * threshold : 25 **/ - $config['coldwallet']['address'] = ''; $config['coldwallet']['reserve'] = 50; $config['coldwallet']['threshold'] = 5; From 07b90dbc58131e992bc646ef2b0139dbadb6070a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 15:47:57 +0100 Subject: [PATCH 10/14] [FIX] Config option detection --- cronjobs/liquid_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index 8979b270..662841b3 100755 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -7,7 +7,7 @@ chdir(dirname(__FILE__)); require_once('shared.inc.php'); // Simple configuration check -if (empty($config['coldwallet'])) { +if (unset($config['coldwallet'])) { $log->logFatal('Missing config option: coldwallet'); $monitoring->endCronjob($cron_name, 'E0075', 1, true); } From c29bda4e5d0ab231b1fed6ad098b0448e174812a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 15:51:36 +0100 Subject: [PATCH 11/14] [FIX] Config option detection --- cronjobs/liquid_payout.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index 662841b3..53f5d2db 100755 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -7,7 +7,7 @@ chdir(dirname(__FILE__)); require_once('shared.inc.php'); // Simple configuration check -if (unset($config['coldwallet'])) { +if (!is_array($config['coldwallet'])) { $log->logFatal('Missing config option: coldwallet'); $monitoring->endCronjob($cron_name, 'E0075', 1, true); } @@ -66,4 +66,3 @@ if($send > $dThreshold) { $log->logDebug('Final sending amount not exceeding threshold: ' . $send); } ?> - From ead36cfd078a415b560e05e96f046583f9b899e6 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 16:06:31 +0100 Subject: [PATCH 12/14] [FIX] End cronjob too --- cronjobs/liquid_payout.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index 53f5d2db..bdc37c6b 100755 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -65,4 +65,7 @@ if($send > $dThreshold) { } else { $log->logDebug('Final sending amount not exceeding threshold: ' . $send); } + +// Cron cleanup and monitoring +require_once('cron_end.inc.php'); ?> From 187721f48cb4d6df57efbc25bef3ce1684195fcd Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 16:14:17 +0100 Subject: [PATCH 13/14] [FIX] Catch new jsonRPC exception --- cronjobs/liquid_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index bdc37c6b..cd628252 100755 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -55,7 +55,7 @@ if($send > $dThreshold) { if (!empty($sendAddress)) { try { $bitcoin->sendtoaddress($sendAddress, $send); - } catch (BitcoinClientException $e) { + } catch (Exception $e) { $log->logError('Failed to send coins to address, skipping liquid assets payout'); } $log->logInfo('Sent out ' . $send . ' liquid assets'); From 6aefea6b6fe1a43d1d42f78c2894a142278c9f01 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 10 Jan 2014 16:41:05 +0100 Subject: [PATCH 14/14] [FIX] Better error handling --- cronjobs/liquid_payout.php | 3 ++- public/include/config/error_codes.inc.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cronjobs/liquid_payout.php b/cronjobs/liquid_payout.php index cd628252..a0c9be13 100755 --- a/cronjobs/liquid_payout.php +++ b/cronjobs/liquid_payout.php @@ -56,7 +56,8 @@ if($send > $dThreshold) { try { $bitcoin->sendtoaddress($sendAddress, $send); } catch (Exception $e) { - $log->logError('Failed to send coins to address, skipping liquid assets payout'); + $log->logFatal('Failed to send coins to address, skipping liquid assets payout:' . $e->getMessage()); + $monitoring->endCronjob($cron_name, 'E0077', 1, true); } $log->logInfo('Sent out ' . $send . ' liquid assets'); } else { diff --git a/public/include/config/error_codes.inc.php b/public/include/config/error_codes.inc.php index c9164058..c57e9e3c 100644 --- a/public/include/config/error_codes.inc.php +++ b/public/include/config/error_codes.inc.php @@ -74,4 +74,5 @@ $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'; +$aErrorCodes['E0077'] = 'RPC method or connection failed'; ?>