Fixing manual payout race condition

* Mark manual payout active
* Run payout logics
* Reset manual payout

This ensures only one manual transaction can be run at a time.
If any users starts a manual payout others have to wait until the site
completed loading and finished the transaction process.

As long as we don't have too many users doing a manual payout at the
same time this should not be an issue. Best for users is using auto
payouts anyway.

This addresses #149
This commit is contained in:
Sebastian Grewe 2013-06-10 10:38:05 +02:00
parent 463f199040
commit b1f3c9bece

View File

@ -13,45 +13,51 @@ if ( ! $user->checkPin($_SESSION['USERDATA']['id'], $_POST['authPin']) && $_POST
} else { } else {
switch ($_POST['do']) { switch ($_POST['do']) {
case 'cashOut': case 'cashOut':
$continue = true; if ($setting->getValue('manual_payout_active') == 1) {
$dBalance = $transaction->getBalance($_SESSION['USERDATA']['id']); $_SESSION['POPUP'][] = array('CONTENT' => 'A manual payout is in progress. Please try again later.', 'TYPE' => 'errormsg');
$sCoinAddress = $user->getCoinAddress($_SESSION['USERDATA']['id']); } else {
// Ensure we can cover the potential transaction fee of 0.1 LTC with the balance $setting->setValue('manual_payout_active', 1);
if ($dBalance > 0.1) { $continue = true;
if ($bitcoin->can_connect() === true) { $dBalance = $transaction->getBalance($_SESSION['USERDATA']['id']);
try { $sCoinAddress = $user->getCoinAddress($_SESSION['USERDATA']['id']);
$bitcoin->validateaddress($sCoinAddress); // Ensure we can cover the potential transaction fee of 0.1 LTC with the balance
} catch (BitcoinClientException $e) { if ($dBalance > 0.1) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Invalid payment address: ' . $sUserSendAddress, 'TYPE' => 'errormsg'); if ($bitcoin->can_connect() === true) {
$continue = false;
}
if ($continue == true) {
// Send balance to address, mind 0.1 fee for transaction!
try { try {
if ($setting->getValue('auto_payout_active') == 0) { $bitcoin->validateaddress($sCoinAddress);
$bitcoin->sendtoaddress($sCoinAddress, $dBalance);
} else {
$_SESSION['POPUP'][] = array('CONTENT' => 'Auto-payout active, please contact site support immidiately to revoke invalid transactions.', 'TYPE' => 'errormsg');
$continue = false;
}
} catch (BitcoinClientException $e) { } catch (BitcoinClientException $e) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Failed to send LTC, please contact site support immidiately', 'TYPE' => 'errormsg'); $_SESSION['POPUP'][] = array('CONTENT' => 'Invalid payment address: ' . $sUserSendAddress, 'TYPE' => 'errormsg');
$continue = false; $continue = false;
} }
if ($continue == true) {
// Send balance to address, mind 0.1 fee for transaction!
try {
if ($setting->getValue('auto_payout_active') == 0) {
$bitcoin->sendtoaddress($sCoinAddress, $dBalance);
} else {
$_SESSION['POPUP'][] = array('CONTENT' => 'Auto-payout active, please contact site support immidiately to revoke invalid transactions.', 'TYPE' => 'errormsg');
$continue = false;
}
} catch (BitcoinClientException $e) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Failed to send LTC, please contact site support immidiately', 'TYPE' => 'errormsg');
$continue = false;
}
}
// Set balance to 0, add to paid out, insert to ledger
if ($continue == true && $transaction->addTransaction($_SESSION['USERDATA']['id'], $dBalance, 'Debit_MP', NULL, $sCoinAddress)) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Transaction completed', 'TYPE' => 'success');
$aMailData['email'] = $user->getUserEmail($user->getUserName($_SESSION['USERDATA']['id']));
$aMailData['amount'] = $dBalance;
$aMailData['subject'] = 'Manual Payout Completed';
$notification->sendNotification($_SESSION['USERDATA']['id'], 'manual_payout', $aMailData);
}
} else {
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to litecoind RPC service', 'TYPE' => 'errormsg');
} }
// Set balance to 0, add to paid out, insert to ledger
if ($continue == true && $transaction->addTransaction($_SESSION['USERDATA']['id'], $dBalance, 'Debit_MP', NULL, $sCoinAddress)) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Transaction completed', 'TYPE' => 'success');
$aMailData['email'] = $user->getUserEmail($user->getUserName($_SESSION['USERDATA']['id']));
$aMailData['amount'] = $dBalance;
$aMailData['subject'] = 'Manual Payout Completed';
$notification->sendNotification($_SESSION['USERDATA']['id'], 'manual_payout', $aMailData);
}
} else { } else {
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to litecoind RPC service', 'TYPE' => 'errormsg'); $_SESSION['POPUP'][] = array('CONTENT' => 'Insufficient funds, you need more than 0.1 LTC to cover transaction fees', 'TYPE' => 'errormsg');
} }
} else { $setting->setValue('manual_payout_active', 0);
$_SESSION['POPUP'][] = array('CONTENT' => 'Insufficient funds, you need more than 0.1 LTC to cover transaction fees', 'TYPE' => 'errormsg');
} }
break; break;