From 64d8d8abf32c9a89ca1039df3eff1358e9b639f8 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 21:37:14 +0200 Subject: [PATCH 01/28] Adding support for PPS payout method This commit changed a few things in the backend and classes code: * Any _PPS transaction does NOT need to be confirmed * Queries updated for added _PPS transactions * Template updated to properly display these transactions Cronjob * Added pps_payput cron to run payouts based on worker submitted shares * **IMPORTANT**: Can NOT be run with proportional_payout! Addresses #70 --- cronjobs/pps_payout.php | 87 +++++++++++++++++++ public/include/classes/share.class.php | 15 ++++ public/include/classes/transaction.class.php | 18 ++-- .../mmcFE/account/transactions/default.tpl | 5 +- 4 files changed, 118 insertions(+), 7 deletions(-) create mode 100755 cronjobs/pps_payout.php diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php new file mode 100755 index 00000000..a71c42a3 --- /dev/null +++ b/cronjobs/pps_payout.php @@ -0,0 +1,87 @@ +#!/usr/bin/php +can_connect() === true ){ + $dDifficulty = $bitcoin->getdifficulty(); +} else { + verbose("Aborted: " . $bitcoin->can_connect() . "\n"); + exit(1); +} + +// Value per share calculation +$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); + +// Find our last share accounted and last inserted share for PPS calculations +$iPreviousShareId = $setting->getValue('pps_last_share_id'); +$iLastShareId = $share->getLastInsertedShareId(); + +// Check for all new shares, we start one higher as our last accounted share to avoid duplicates +$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); + +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); + +foreach ($aAccountShares as $aData) { + // Take our valid shares and multiply by per share value + $aData['payout'] = $aData['valid'] * $pps_value; + + // Defaults + $aData['fee' ] = 0; + $aData['donation'] = 0; + + // Calculate block fees + if ($config['fees'] > 0) + $aData['fee'] = number_format(round($config['fees'] / 100 * $aData['payout'], 8), 8); + // Calculate donation amount + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 8), 8); + + verbose($aData['id'] . "\t" . + $aData['username'] . "\t" . + $aData['invalid'] . "\t" . + $aData['valid'] . "\t*\t" . + $pps_value . "\t=\t" . + $aData['payout'] . "\t" . + $aData['donation'] . "\t" . + $aData['fee'] . "\t"); + + $strStatus = "OK"; + // Add new credit transaction + if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit_PPS')) + $strStatus = "Transaction Failed"; + // Add new fee debit for this block + if ($aData['fee'] > 0 && $config['fees'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['fee'], 'Fee_PPS')) + $strStatus = "Fee Failed"; + // Add new donation debit + if ($aData['donation'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation_PPS')) + $strStatus = "Donation Failed"; + verbose($strStatus . "\n"); +} + +// Store our last inserted ID for the next run +$setting->setValue('pps_last_share_id', $iLastShareId); + +verbose("\n\n------------------------------------------------------------------------------------\n\n"); +?> diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index b0a3b249..1033f92c 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -44,6 +44,21 @@ class Share { return $this->table; } + /** + * Get last inserted Share ID from Database + * Used for PPS calculations without moving to archive + **/ + public function getLastInsertedShareId() { + $stmt = $this->mysqli->prepare(" + SELECT MAX(id) AS id FROM $this->table + "); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + // Catchall + $this->setErrorMessage('Failed to fetch last inserted share ID'); + return false; + } + /** * Get all valid shares for this round * @param previous_upstream int Previous found share accepted by upstream to limit results diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 020140d3..711e893f 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -142,7 +142,7 @@ class Transaction { ( SELECT sum(t.amount) AS credit FROM $this->table AS t - WHERE t.type = 'Credit' + WHERE t.type IN ('Credit', 'Credit_PPS') ) AS t1, ( SELECT sum(t.amount) AS debit @@ -152,7 +152,7 @@ class Transaction { ( SELECT sum(t.amount) AS other FROM " . $this->table . " AS t - WHERE t.type IN ('Donation','Fee') + WHERE t.type IN ('Donation','Fee','Donation_PPS','Fee_PPS') ) AS t3"); if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) return $dBalance; @@ -176,8 +176,11 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type = 'Credit' - AND b.confirmations >= ? + WHERE + ( + ( t.type = 'Credit' AND b.confirmations >= ? ) OR + ( t.type = 'Credit_PPS' ) + ) AND t.account_id = ? ) AS t1, ( @@ -190,8 +193,11 @@ class Transaction { SELECT sum(t.amount) AS other FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') - AND b.confirmations >= ? + WHERE + ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + ( t.type IN ('Donation_PPS', 'Fee_PPS') ) + ) AND t.account_id = ? ) AS t3 "); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 8f5282e1..dd31723f 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -18,6 +18,9 @@ ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or $TRANSACTIONS[transaction].type == 'Credit_PPS' + or $TRANSACTIONS[transaction].type == 'Fee_PPS' + or $TRANSACTIONS[transaction].type == 'Donation_PPS' or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' )} @@ -27,7 +30,7 @@ {$TRANSACTIONS[transaction].type} {$TRANSACTIONS[transaction].coin_address} {if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} - {$TRANSACTIONS[transaction].amount} + {$TRANSACTIONS[transaction].amount} {/if} {/section} From a3ddf0cfcca9b80a1e3e4d4c434f53fe1653eb6a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:31:35 +0200 Subject: [PATCH 02/28] properly format payout and round it --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index a71c42a3..1dafa288 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = $aData['valid'] * $pps_value; + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); // Defaults $aData['fee' ] = 0; From ad6051df1c0682addb4edebca39c414edee99b15 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:34:02 +0200 Subject: [PATCH 03/28] forgot 8 decimals --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 1dafa288..bef78f2a 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8), 8); // Defaults $aData['fee' ] = 0; From 663c427d4adaf8d96d51e98024c1dd19e83187e4 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:45:53 +0200 Subject: [PATCH 04/28] properly format pps value to 12 digits --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index bef78f2a..e2804595 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -31,7 +31,7 @@ if ( $bitcoin->can_connect() === true ){ } // Value per share calculation -$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); +$pps_value = number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12); // Find our last share accounted and last inserted share for PPS calculations $iPreviousShareId = $setting->getValue('pps_last_share_id'); From 87e721edb89429c2d96af17858d83e8712e9339d Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:48:09 +0200 Subject: [PATCH 05/28] moved table header to the left --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index e2804595..427035d5 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -40,7 +40,7 @@ $iLastShareId = $share->getLastInsertedShareId(); // Check for all new shares, we start one higher as our last accounted share to avoid duplicates $aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); -verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\tPayout\t\tDonation\tFee\t\tStatus\n"); foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value From 531e28cee85f9b96136cfe128f8cfaf4860ee68b Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 21:37:14 +0200 Subject: [PATCH 06/28] Adding support for PPS payout method This commit changed a few things in the backend and classes code: * Any _PPS transaction does NOT need to be confirmed * Queries updated for added _PPS transactions * Template updated to properly display these transactions Cronjob * Added pps_payput cron to run payouts based on worker submitted shares * **IMPORTANT**: Can NOT be run with proportional_payout! Addresses #70 --- cronjobs/pps_payout.php | 87 +++++++++++++++++++ public/include/classes/share.class.php | 15 ++++ public/include/classes/transaction.class.php | 18 ++-- .../mmcFE/account/transactions/default.tpl | 5 +- 4 files changed, 118 insertions(+), 7 deletions(-) create mode 100755 cronjobs/pps_payout.php diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php new file mode 100755 index 00000000..a71c42a3 --- /dev/null +++ b/cronjobs/pps_payout.php @@ -0,0 +1,87 @@ +#!/usr/bin/php +can_connect() === true ){ + $dDifficulty = $bitcoin->getdifficulty(); +} else { + verbose("Aborted: " . $bitcoin->can_connect() . "\n"); + exit(1); +} + +// Value per share calculation +$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); + +// Find our last share accounted and last inserted share for PPS calculations +$iPreviousShareId = $setting->getValue('pps_last_share_id'); +$iLastShareId = $share->getLastInsertedShareId(); + +// Check for all new shares, we start one higher as our last accounted share to avoid duplicates +$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); + +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); + +foreach ($aAccountShares as $aData) { + // Take our valid shares and multiply by per share value + $aData['payout'] = $aData['valid'] * $pps_value; + + // Defaults + $aData['fee' ] = 0; + $aData['donation'] = 0; + + // Calculate block fees + if ($config['fees'] > 0) + $aData['fee'] = number_format(round($config['fees'] / 100 * $aData['payout'], 8), 8); + // Calculate donation amount + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 8), 8); + + verbose($aData['id'] . "\t" . + $aData['username'] . "\t" . + $aData['invalid'] . "\t" . + $aData['valid'] . "\t*\t" . + $pps_value . "\t=\t" . + $aData['payout'] . "\t" . + $aData['donation'] . "\t" . + $aData['fee'] . "\t"); + + $strStatus = "OK"; + // Add new credit transaction + if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit_PPS')) + $strStatus = "Transaction Failed"; + // Add new fee debit for this block + if ($aData['fee'] > 0 && $config['fees'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['fee'], 'Fee_PPS')) + $strStatus = "Fee Failed"; + // Add new donation debit + if ($aData['donation'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation_PPS')) + $strStatus = "Donation Failed"; + verbose($strStatus . "\n"); +} + +// Store our last inserted ID for the next run +$setting->setValue('pps_last_share_id', $iLastShareId); + +verbose("\n\n------------------------------------------------------------------------------------\n\n"); +?> diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index b0a3b249..1033f92c 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -44,6 +44,21 @@ class Share { return $this->table; } + /** + * Get last inserted Share ID from Database + * Used for PPS calculations without moving to archive + **/ + public function getLastInsertedShareId() { + $stmt = $this->mysqli->prepare(" + SELECT MAX(id) AS id FROM $this->table + "); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + // Catchall + $this->setErrorMessage('Failed to fetch last inserted share ID'); + return false; + } + /** * Get all valid shares for this round * @param previous_upstream int Previous found share accepted by upstream to limit results diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 020140d3..711e893f 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -142,7 +142,7 @@ class Transaction { ( SELECT sum(t.amount) AS credit FROM $this->table AS t - WHERE t.type = 'Credit' + WHERE t.type IN ('Credit', 'Credit_PPS') ) AS t1, ( SELECT sum(t.amount) AS debit @@ -152,7 +152,7 @@ class Transaction { ( SELECT sum(t.amount) AS other FROM " . $this->table . " AS t - WHERE t.type IN ('Donation','Fee') + WHERE t.type IN ('Donation','Fee','Donation_PPS','Fee_PPS') ) AS t3"); if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) return $dBalance; @@ -176,8 +176,11 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type = 'Credit' - AND b.confirmations >= ? + WHERE + ( + ( t.type = 'Credit' AND b.confirmations >= ? ) OR + ( t.type = 'Credit_PPS' ) + ) AND t.account_id = ? ) AS t1, ( @@ -190,8 +193,11 @@ class Transaction { SELECT sum(t.amount) AS other FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') - AND b.confirmations >= ? + WHERE + ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + ( t.type IN ('Donation_PPS', 'Fee_PPS') ) + ) AND t.account_id = ? ) AS t3 "); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 8f5282e1..dd31723f 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -18,6 +18,9 @@ ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or $TRANSACTIONS[transaction].type == 'Credit_PPS' + or $TRANSACTIONS[transaction].type == 'Fee_PPS' + or $TRANSACTIONS[transaction].type == 'Donation_PPS' or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' )} @@ -27,7 +30,7 @@ {$TRANSACTIONS[transaction].type} {$TRANSACTIONS[transaction].coin_address} {if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} - {$TRANSACTIONS[transaction].amount} + {$TRANSACTIONS[transaction].amount} {/if} {/section} From dcfbd83270292261e2a0c57964055a6feb57faa8 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:31:35 +0200 Subject: [PATCH 07/28] properly format payout and round it --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index a71c42a3..1dafa288 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = $aData['valid'] * $pps_value; + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); // Defaults $aData['fee' ] = 0; From 5f1e52767e88db2dd8dff843b8463b8b2a9b8ca5 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:34:02 +0200 Subject: [PATCH 08/28] forgot 8 decimals --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 1dafa288..bef78f2a 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8), 8); // Defaults $aData['fee' ] = 0; From 4a36479fe22205d959e80e17e314da294aa5adbe Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:45:53 +0200 Subject: [PATCH 09/28] properly format pps value to 12 digits --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index bef78f2a..e2804595 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -31,7 +31,7 @@ if ( $bitcoin->can_connect() === true ){ } // Value per share calculation -$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); +$pps_value = number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12); // Find our last share accounted and last inserted share for PPS calculations $iPreviousShareId = $setting->getValue('pps_last_share_id'); From b6da195da5954e790d5deb521d2a090094a2c2ef Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:48:09 +0200 Subject: [PATCH 10/28] moved table header to the left --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index e2804595..427035d5 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -40,7 +40,7 @@ $iLastShareId = $share->getLastInsertedShareId(); // Check for all new shares, we start one higher as our last accounted share to avoid duplicates $aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); -verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\tPayout\t\tDonation\tFee\t\tStatus\n"); foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value From c926eabf8dbed1ec13194dbdcc3b745e2526485d Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 21:37:14 +0200 Subject: [PATCH 11/28] Adding support for PPS payout method This commit changed a few things in the backend and classes code: * Any _PPS transaction does NOT need to be confirmed * Queries updated for added _PPS transactions * Template updated to properly display these transactions Cronjob * Added pps_payput cron to run payouts based on worker submitted shares * **IMPORTANT**: Can NOT be run with proportional_payout! Addresses #70 --- cronjobs/pps_payout.php | 87 +++++++++++++++++++ public/include/classes/share.class.php | 15 ++++ public/include/classes/transaction.class.php | 18 ++-- .../mmcFE/account/transactions/default.tpl | 5 +- 4 files changed, 118 insertions(+), 7 deletions(-) create mode 100755 cronjobs/pps_payout.php diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php new file mode 100755 index 00000000..a71c42a3 --- /dev/null +++ b/cronjobs/pps_payout.php @@ -0,0 +1,87 @@ +#!/usr/bin/php +can_connect() === true ){ + $dDifficulty = $bitcoin->getdifficulty(); +} else { + verbose("Aborted: " . $bitcoin->can_connect() . "\n"); + exit(1); +} + +// Value per share calculation +$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); + +// Find our last share accounted and last inserted share for PPS calculations +$iPreviousShareId = $setting->getValue('pps_last_share_id'); +$iLastShareId = $share->getLastInsertedShareId(); + +// Check for all new shares, we start one higher as our last accounted share to avoid duplicates +$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); + +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); + +foreach ($aAccountShares as $aData) { + // Take our valid shares and multiply by per share value + $aData['payout'] = $aData['valid'] * $pps_value; + + // Defaults + $aData['fee' ] = 0; + $aData['donation'] = 0; + + // Calculate block fees + if ($config['fees'] > 0) + $aData['fee'] = number_format(round($config['fees'] / 100 * $aData['payout'], 8), 8); + // Calculate donation amount + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 8), 8); + + verbose($aData['id'] . "\t" . + $aData['username'] . "\t" . + $aData['invalid'] . "\t" . + $aData['valid'] . "\t*\t" . + $pps_value . "\t=\t" . + $aData['payout'] . "\t" . + $aData['donation'] . "\t" . + $aData['fee'] . "\t"); + + $strStatus = "OK"; + // Add new credit transaction + if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit_PPS')) + $strStatus = "Transaction Failed"; + // Add new fee debit for this block + if ($aData['fee'] > 0 && $config['fees'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['fee'], 'Fee_PPS')) + $strStatus = "Fee Failed"; + // Add new donation debit + if ($aData['donation'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation_PPS')) + $strStatus = "Donation Failed"; + verbose($strStatus . "\n"); +} + +// Store our last inserted ID for the next run +$setting->setValue('pps_last_share_id', $iLastShareId); + +verbose("\n\n------------------------------------------------------------------------------------\n\n"); +?> diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index b0a3b249..1033f92c 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -44,6 +44,21 @@ class Share { return $this->table; } + /** + * Get last inserted Share ID from Database + * Used for PPS calculations without moving to archive + **/ + public function getLastInsertedShareId() { + $stmt = $this->mysqli->prepare(" + SELECT MAX(id) AS id FROM $this->table + "); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + // Catchall + $this->setErrorMessage('Failed to fetch last inserted share ID'); + return false; + } + /** * Get all valid shares for this round * @param previous_upstream int Previous found share accepted by upstream to limit results diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index a487e5ec..939684c4 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -143,7 +143,7 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type = 'Credit' + WHERE t.type IN ('Credit', 'Credit_PPS') AND b.confirmations >= " . $this->config['confirmations'] . " ) AS t1, ( @@ -155,7 +155,7 @@ class Transaction { SELECT sum(t.amount) AS other FROM " . $this->table . " AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') + WHERE t.type IN ('Donation','Fee','Donation_PPS','Fee_PPS') AND b.confirmations >= " . $this->config['confirmations'] . " ) AS t3"); if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) @@ -180,8 +180,11 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type = 'Credit' - AND b.confirmations >= ? + WHERE + ( + ( t.type = 'Credit' AND b.confirmations >= ? ) OR + ( t.type = 'Credit_PPS' ) + ) AND t.account_id = ? ) AS t1, ( @@ -194,8 +197,11 @@ class Transaction { SELECT sum(t.amount) AS other FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') - AND b.confirmations >= ? + WHERE + ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + ( t.type IN ('Donation_PPS', 'Fee_PPS') ) + ) AND t.account_id = ? ) AS t3 "); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 88f749c1..c1433f3c 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -19,6 +19,9 @@ ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or $TRANSACTIONS[transaction].type == 'Credit_PPS' + or $TRANSACTIONS[transaction].type == 'Fee_PPS' + or $TRANSACTIONS[transaction].type == 'Donation_PPS' or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' )} @@ -28,7 +31,7 @@ {$TRANSACTIONS[transaction].type} {$TRANSACTIONS[transaction].coin_address} {if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if} - {$TRANSACTIONS[transaction].amount} + {$TRANSACTIONS[transaction].amount} {/if} {/section} From 2c4bf7d4aa237deca0c760f212371b9389bb3c21 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:31:35 +0200 Subject: [PATCH 12/28] properly format payout and round it --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index a71c42a3..1dafa288 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = $aData['valid'] * $pps_value; + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); // Defaults $aData['fee' ] = 0; From e62a3527dbeae989ba9a34c7b211b777df1c188c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:34:02 +0200 Subject: [PATCH 13/28] forgot 8 decimals --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 1dafa288..bef78f2a 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8), 8); // Defaults $aData['fee' ] = 0; From 638e33e52534840afadebd4be6a46f66ddffb79f Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:45:53 +0200 Subject: [PATCH 14/28] properly format pps value to 12 digits --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index bef78f2a..e2804595 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -31,7 +31,7 @@ if ( $bitcoin->can_connect() === true ){ } // Value per share calculation -$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); +$pps_value = number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12); // Find our last share accounted and last inserted share for PPS calculations $iPreviousShareId = $setting->getValue('pps_last_share_id'); From 20c3d771e6e3e88f42015c3b9dbd970bffa4eb21 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:48:09 +0200 Subject: [PATCH 15/28] moved table header to the left --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index e2804595..427035d5 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -40,7 +40,7 @@ $iLastShareId = $share->getLastInsertedShareId(); // Check for all new shares, we start one higher as our last accounted share to avoid duplicates $aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); -verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\tPayout\t\tDonation\tFee\t\tStatus\n"); foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value From ae0252d927153d08e4338fca34c7f26765eae783 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 9 Jun 2013 17:29:38 +0200 Subject: [PATCH 16/28] adding PPS value to sidebard --- public/include/smarty_globals.inc.php | 6 ++++++ public/templates/mmcFE/global/sidebar.tpl | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index fa5c7923..d37e45cc 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -12,12 +12,18 @@ $aRoundShares = $statistics->getRoundShares(); $iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); $iCurrentPoolShareRate = $statistics->getCurrentShareRate(); +if ($bitcoin->can_connect() === true){ + $dDifficulty = $bitcoin->query('getdifficulty'); +} else { + $dDifficulty = 1; +} $aGlobal = array( 'slogan' => $config['website']['slogan'], 'websitename' => $config['website']['name'], 'hashrate' => $iCurrentPoolHashrate, 'sharerate' => $iCurrentPoolShareRate, + 'ppsvalue' => number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12), 'workers' => $iCurrentActiveWorkers, 'roundshares' => $aRoundShares, 'fees' => $config['fees'], diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 21a8e0aa..05f6e00e 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -6,8 +6,17 @@
- - + + + + + + + + + + + From 31de069533811ebb478079de708088ffc2aafd4e Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:28:20 +0200 Subject: [PATCH 17/28] Changed getLockedBalance and added SQL * New SQL file for upgrade includes next changes * Properly calculate getLockedBalance based on shares Further addresses #70 --- public/include/classes/transaction.class.php | 16 +++++++++++----- sql/issue_70_transactions_upgrade.sql | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 sql/issue_70_transactions_upgrade.sql diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 939684c4..4b304797 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -143,8 +143,11 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Credit', 'Credit_PPS') - AND b.confirmations >= " . $this->config['confirmations'] . " + WHERE + ( + ( t.type = 'Credit' AND b.confirmations >= ? ) OR + ( t.type = 'Credit_PPS' ) + ) ) AS t1, ( SELECT sum(t.amount) AS debit @@ -155,10 +158,13 @@ class Transaction { SELECT sum(t.amount) AS other FROM " . $this->table . " AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee','Donation_PPS','Fee_PPS') - AND b.confirmations >= " . $this->config['confirmations'] . " + WHERE + ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + ( t.type IN ('Donation_PPS', 'Fee_PPS') ) + ) ) AS t3"); - if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) + if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $this->config['confirmations'], $this->config['confirmations']) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) return $dBalance; // Catchall $this->setErrorMessage('Unable to find locked credits for all users'); diff --git a/sql/issue_70_transactions_upgrade.sql b/sql/issue_70_transactions_upgrade.sql new file mode 100644 index 00000000..90561397 --- /dev/null +++ b/sql/issue_70_transactions_upgrade.sql @@ -0,0 +1 @@ +ALTER TABLE `transactions` CHANGE `type` `type` ENUM( 'Credit', 'Debit_MP', 'Debit_AP', 'Donation', 'Fee', 'Orphan_Credit', 'Orphan_Fee', 'Orphan_Donation', 'Bonus', 'Orphan_Bonus', 'Credit_PPS', 'Debit_PPS', 'Donation_PPS' ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL; From ea9f6b2c9a88269801c811135ca3155237671575 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:34:20 +0200 Subject: [PATCH 18/28] Adding new config option `payout_system` * Change templates based on the payout system used * Modified sidebar for new PPS method Further addresses #70 --- public/include/config/global.inc.dist.php | 1 + public/include/smarty_globals.inc.php | 1 + public/templates/mmcFE/global/sidebar.tpl | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 306d5261..7e3415f9 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -34,6 +34,7 @@ $config = array( 'slogan' => 'Resistance is futile', 'email' => 'test@example.com', // Mail address used for notifications ), + 'payout_system' => 'pps', // Set your payout here so template changes are activated 'archive_shares' => true, // Store accounted shares in archive table? 'blockexplorer' => 'http://explorer.litecoin.net/search?q=', // URL for block searches, prefixed to each block number 'chaininfo' => 'http://allchains.info', // Link to Allchains for Difficulty information diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index d37e45cc..209f6a71 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -33,6 +33,7 @@ $aGlobal = array( 'blockexplorer' => $config['blockexplorer'], 'chaininfo' => $config['chaininfo'], 'config' => array( + 'payout_system' => $config['payout_system'], 'ap_threshold' => array( 'min' => $config['ap_threshold']['min'], 'max' => $config['ap_threshold']['max'] diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 05f6e00e..e6736e1a 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -13,10 +13,13 @@ +{if $GLOBAL.config.payout_system == 'pps'} +{/if} +{if $GLOBAL.config.payout_system != 'pps'} @@ -28,6 +31,7 @@ +{/if} @@ -43,6 +47,7 @@ +{if $GLOBAL.config.payout_system != 'pps'} @@ -62,6 +67,7 @@ +{/if} From 31e51061f7f6fca8f961377c225c0bf836b6383b Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:53:44 +0200 Subject: [PATCH 19/28] archive/purge accounted PPS shares --- cronjobs/pps_payout.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 427035d5..efdbfc94 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -84,4 +84,22 @@ foreach ($aAccountShares as $aData) { $setting->setValue('pps_last_share_id', $iLastShareId); verbose("\n\n------------------------------------------------------------------------------------\n\n"); + +// Fetch all unaccounted blocks +$aAllBlocks = $block->getAllUnaccounted('ASC'); +if (empty($aAllBlocks)) { + verbose("No new unaccounted blocks found\n"); +} + +// Go through blocks and archive/delete shares that have been accounted for +foreach ($aAllBlocks as $iIndex => $aBlock) { + $dummy = $iIndex - 1; + if ($config['archive_shares'] && $aBlock['share_id'] < $iLastShareId) { + $share->moveArchive($aBlock['share_id'], $aBlock['id'], @$aAllBlocks[$dummy]['share_id']); + } + if ($aBlock['share_id'] < $iLastShareId && !$share->deleteAccountedShares($aBlock['share_id'], @$aAllBlocks[$dummy]['share_id'])) { + verbose("\nERROR : Failed to delete accounted shares from " . $aBlock['share_id'] . " to " . @$aAllBlocks[$dummy]['share_id'] . ", aborting!\n"); + exit(1); + } +} ?> From 4745a2f6f8cbd168bad6c8cd9efeab17aefa7725 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 21:37:14 +0200 Subject: [PATCH 20/28] Adding support for PPS payout method This commit changed a few things in the backend and classes code: * Any _PPS transaction does NOT need to be confirmed * Queries updated for added _PPS transactions * Template updated to properly display these transactions Cronjob * Added pps_payput cron to run payouts based on worker submitted shares * **IMPORTANT**: Can NOT be run with proportional_payout! Addresses #70 --- cronjobs/pps_payout.php | 87 +++++++++++++++++++ public/include/classes/share.class.php | 15 ++++ public/include/classes/transaction.class.php | 28 ++++-- .../mmcFE/account/transactions/default.tpl | 5 +- 4 files changed, 125 insertions(+), 10 deletions(-) create mode 100755 cronjobs/pps_payout.php diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php new file mode 100755 index 00000000..a71c42a3 --- /dev/null +++ b/cronjobs/pps_payout.php @@ -0,0 +1,87 @@ +#!/usr/bin/php +can_connect() === true ){ + $dDifficulty = $bitcoin->getdifficulty(); +} else { + verbose("Aborted: " . $bitcoin->can_connect() . "\n"); + exit(1); +} + +// Value per share calculation +$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); + +// Find our last share accounted and last inserted share for PPS calculations +$iPreviousShareId = $setting->getValue('pps_last_share_id'); +$iLastShareId = $share->getLastInsertedShareId(); + +// Check for all new shares, we start one higher as our last accounted share to avoid duplicates +$aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); + +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); + +foreach ($aAccountShares as $aData) { + // Take our valid shares and multiply by per share value + $aData['payout'] = $aData['valid'] * $pps_value; + + // Defaults + $aData['fee' ] = 0; + $aData['donation'] = 0; + + // Calculate block fees + if ($config['fees'] > 0) + $aData['fee'] = number_format(round($config['fees'] / 100 * $aData['payout'], 8), 8); + // Calculate donation amount + $aData['donation'] = number_format(round($user->getDonatePercent($user->getUserId($aData['username'])) / 100 * ( $aData['payout'] - $aData['fee']), 8), 8); + + verbose($aData['id'] . "\t" . + $aData['username'] . "\t" . + $aData['invalid'] . "\t" . + $aData['valid'] . "\t*\t" . + $pps_value . "\t=\t" . + $aData['payout'] . "\t" . + $aData['donation'] . "\t" . + $aData['fee'] . "\t"); + + $strStatus = "OK"; + // Add new credit transaction + if (!$transaction->addTransaction($aData['id'], $aData['payout'], 'Credit_PPS')) + $strStatus = "Transaction Failed"; + // Add new fee debit for this block + if ($aData['fee'] > 0 && $config['fees'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['fee'], 'Fee_PPS')) + $strStatus = "Fee Failed"; + // Add new donation debit + if ($aData['donation'] > 0) + if (!$transaction->addTransaction($aData['id'], $aData['donation'], 'Donation_PPS')) + $strStatus = "Donation Failed"; + verbose($strStatus . "\n"); +} + +// Store our last inserted ID for the next run +$setting->setValue('pps_last_share_id', $iLastShareId); + +verbose("\n\n------------------------------------------------------------------------------------\n\n"); +?> diff --git a/public/include/classes/share.class.php b/public/include/classes/share.class.php index b0a3b249..1033f92c 100644 --- a/public/include/classes/share.class.php +++ b/public/include/classes/share.class.php @@ -44,6 +44,21 @@ class Share { return $this->table; } + /** + * Get last inserted Share ID from Database + * Used for PPS calculations without moving to archive + **/ + public function getLastInsertedShareId() { + $stmt = $this->mysqli->prepare(" + SELECT MAX(id) AS id FROM $this->table + "); + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + return $result->fetch_object()->id; + // Catchall + $this->setErrorMessage('Failed to fetch last inserted share ID'); + return false; + } + /** * Get all valid shares for this round * @param previous_upstream int Previous found share accepted by upstream to limit results diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index d12d14ee..f1c9907c 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -131,8 +131,10 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Credit','Bonus') - AND b.confirmations >= " . $this->config['confirmations'] . " + WHERE ( + ( t.type IN ('Credit','Bonus') AND b.confirmations >= ? ) OR + t.type = 'Credit_PPS' + ) ) AS t1, ( SELECT sum(t.amount) AS debit @@ -143,10 +145,12 @@ class Transaction { SELECT sum(t.amount) AS other FROM " . $this->table . " AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') - AND b.confirmations >= " . $this->config['confirmations'] . " + WHERE ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + t.type IN ('Donation_PPS','Fee_PPS') + ) ) AS t3"); - if ($this->checkStmt($stmt) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) + if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $this->config['confirmations'], $this->config['confirmations']) && $stmt->execute() && $stmt->bind_result($dBalance) && $stmt->fetch()) return $dBalance; // Catchall $this->setErrorMessage('Unable to find locked credits for all users'); @@ -168,8 +172,11 @@ class Transaction { SELECT sum(t.amount) AS credit FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Credit','Bonus') - AND b.confirmations >= ? + WHERE + ( + ( t.type IN ('Credit','Bonus') AND b.confirmations >= ? ) OR + ( t.type = 'Credit_PPS' ) + ) AND t.account_id = ? ) AS t1, ( @@ -182,8 +189,11 @@ class Transaction { SELECT sum(t.amount) AS other FROM $this->table AS t LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.type IN ('Donation','Fee') - AND b.confirmations >= ? + WHERE + ( + ( t.type IN ('Donation','Fee') AND b.confirmations >= ? ) OR + ( t.type IN ('Donation_PPS', 'Fee_PPS') ) + ) AND t.account_id = ? ) AS t3 "); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 2e6ae074..d2393450 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -19,6 +19,9 @@ (($TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus')and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Donation' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) or ($TRANSACTIONS[transaction].type == 'Fee' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + or $TRANSACTIONS[transaction].type == 'Credit_PPS' + or $TRANSACTIONS[transaction].type == 'Fee_PPS' + or $TRANSACTIONS[transaction].type == 'Donation_PPS' or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' )} @@ -28,7 +31,7 @@ - + {/if} {/section} From 4acf1194184a4753dc01c1dd9adae7ef117efa94 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:31:35 +0200 Subject: [PATCH 21/28] properly format payout and round it --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index a71c42a3..1dafa288 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = $aData['valid'] * $pps_value; + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); // Defaults $aData['fee' ] = 0; From 3f8a1ea0f7a86ee247f2b5b9a1ab581f0c64ca8c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:34:02 +0200 Subject: [PATCH 22/28] forgot 8 decimals --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 1dafa288..bef78f2a 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -44,7 +44,7 @@ verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\ foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value - $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8)); + $aData['payout'] = number_format(round($aData['valid'] * $pps_value, 8), 8); // Defaults $aData['fee' ] = 0; From cb3e02e896f37cbef2ac55d239edb4cd5862ac90 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:45:53 +0200 Subject: [PATCH 23/28] properly format pps value to 12 digits --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index bef78f2a..e2804595 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -31,7 +31,7 @@ if ( $bitcoin->can_connect() === true ){ } // Value per share calculation -$pps_value = 50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']); +$pps_value = number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12); // Find our last share accounted and last inserted share for PPS calculations $iPreviousShareId = $setting->getValue('pps_last_share_id'); From 41b35a2d962240408669b6d9a611e02841e4785a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 6 Jun 2013 23:48:09 +0200 Subject: [PATCH 24/28] moved table header to the left --- cronjobs/pps_payout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index e2804595..427035d5 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -40,7 +40,7 @@ $iLastShareId = $share->getLastInsertedShareId(); // Check for all new shares, we start one higher as our last accounted share to avoid duplicates $aAccountShares = $share->getSharesForAccounts($iPreviousShareId + 1, $iLastShareId); -verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\t\tPayout\t\tDonation\tFee\t\tStatus\n"); +verbose("ID\tUsername\tInvalid\tValid\t\tPPS Value\t\tPayout\t\tDonation\tFee\t\tStatus\n"); foreach ($aAccountShares as $aData) { // Take our valid shares and multiply by per share value From 666fde91b6d0a9863cc57e39aeff9b62947fae4f Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 9 Jun 2013 17:29:38 +0200 Subject: [PATCH 25/28] adding PPS value to sidebard --- public/include/smarty_globals.inc.php | 6 ++++++ public/templates/mmcFE/global/sidebar.tpl | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index fa5c7923..d37e45cc 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -12,12 +12,18 @@ $aRoundShares = $statistics->getRoundShares(); $iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); $iCurrentPoolShareRate = $statistics->getCurrentShareRate(); +if ($bitcoin->can_connect() === true){ + $dDifficulty = $bitcoin->query('getdifficulty'); +} else { + $dDifficulty = 1; +} $aGlobal = array( 'slogan' => $config['website']['slogan'], 'websitename' => $config['website']['name'], 'hashrate' => $iCurrentPoolHashrate, 'sharerate' => $iCurrentPoolShareRate, + 'ppsvalue' => number_format(round(50 / (pow(2,32) * $dDifficulty) * pow(2, $config['difficulty']), 12) ,12), 'workers' => $iCurrentActiveWorkers, 'roundshares' => $aRoundShares, 'fees' => $config['fees'], diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 21a8e0aa..05f6e00e 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -6,8 +6,17 @@
- - + + + + + + + + + + + From e8f8b2f5f444efacdd3a8482efb66eccfaae7e80 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:28:20 +0200 Subject: [PATCH 26/28] Changed getLockedBalance and added SQL * New SQL file for upgrade includes next changes * Properly calculate getLockedBalance based on shares Further addresses #70 --- public/include/classes/transaction.class.php | 2 +- sql/issue_70_transactions_upgrade.sql | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 sql/issue_70_transactions_upgrade.sql diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index f1c9907c..79d5ba66 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -133,7 +133,7 @@ class Transaction { LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id WHERE ( ( t.type IN ('Credit','Bonus') AND b.confirmations >= ? ) OR - t.type = 'Credit_PPS' + ( t.type = 'Credit_PPS' ) ) ) AS t1, ( diff --git a/sql/issue_70_transactions_upgrade.sql b/sql/issue_70_transactions_upgrade.sql new file mode 100644 index 00000000..90561397 --- /dev/null +++ b/sql/issue_70_transactions_upgrade.sql @@ -0,0 +1 @@ +ALTER TABLE `transactions` CHANGE `type` `type` ENUM( 'Credit', 'Debit_MP', 'Debit_AP', 'Donation', 'Fee', 'Orphan_Credit', 'Orphan_Fee', 'Orphan_Donation', 'Bonus', 'Orphan_Bonus', 'Credit_PPS', 'Debit_PPS', 'Donation_PPS' ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL; From b782857c41cc52147f5d14ea9a0a561e6ee3f746 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:34:20 +0200 Subject: [PATCH 27/28] Adding new config option `payout_system` * Change templates based on the payout system used * Modified sidebar for new PPS method Further addresses #70 --- public/include/config/global.inc.dist.php | 1 + public/include/smarty_globals.inc.php | 1 + public/templates/mmcFE/global/sidebar.tpl | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index f1cb0cbe..3d329645 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -35,6 +35,7 @@ $config = array( 'email' => 'test@example.com', // Mail address used for notifications ), 'block_bonus' => 0, + 'payout_system' => 'pps', // Set your payout here so template changes are activated 'archive_shares' => true, // Store accounted shares in archive table? 'blockexplorer' => 'http://explorer.litecoin.net/search?q=', // URL for block searches, prefixed to each block number 'chaininfo' => 'http://allchains.info', // Link to Allchains for Difficulty information diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index d37e45cc..209f6a71 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -33,6 +33,7 @@ $aGlobal = array( 'blockexplorer' => $config['blockexplorer'], 'chaininfo' => $config['chaininfo'], 'config' => array( + 'payout_system' => $config['payout_system'], 'ap_threshold' => array( 'min' => $config['ap_threshold']['min'], 'max' => $config['ap_threshold']['max'] diff --git a/public/templates/mmcFE/global/sidebar.tpl b/public/templates/mmcFE/global/sidebar.tpl index 05f6e00e..e6736e1a 100644 --- a/public/templates/mmcFE/global/sidebar.tpl +++ b/public/templates/mmcFE/global/sidebar.tpl @@ -13,10 +13,13 @@ +{if $GLOBAL.config.payout_system == 'pps'} +{/if} +{if $GLOBAL.config.payout_system != 'pps'} @@ -28,6 +31,7 @@ +{/if} @@ -43,6 +47,7 @@ +{if $GLOBAL.config.payout_system != 'pps'} @@ -62,6 +67,7 @@ +{/if} From b4a09f22eebda4b794c0a65c82df997b22eb2c34 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 11:53:44 +0200 Subject: [PATCH 28/28] archive/purge accounted PPS shares --- cronjobs/pps_payout.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cronjobs/pps_payout.php b/cronjobs/pps_payout.php index 427035d5..efdbfc94 100755 --- a/cronjobs/pps_payout.php +++ b/cronjobs/pps_payout.php @@ -84,4 +84,22 @@ foreach ($aAccountShares as $aData) { $setting->setValue('pps_last_share_id', $iLastShareId); verbose("\n\n------------------------------------------------------------------------------------\n\n"); + +// Fetch all unaccounted blocks +$aAllBlocks = $block->getAllUnaccounted('ASC'); +if (empty($aAllBlocks)) { + verbose("No new unaccounted blocks found\n"); +} + +// Go through blocks and archive/delete shares that have been accounted for +foreach ($aAllBlocks as $iIndex => $aBlock) { + $dummy = $iIndex - 1; + if ($config['archive_shares'] && $aBlock['share_id'] < $iLastShareId) { + $share->moveArchive($aBlock['share_id'], $aBlock['id'], @$aAllBlocks[$dummy]['share_id']); + } + if ($aBlock['share_id'] < $iLastShareId && !$share->deleteAccountedShares($aBlock['share_id'], @$aAllBlocks[$dummy]['share_id'])) { + verbose("\nERROR : Failed to delete accounted shares from " . $aBlock['share_id'] . " to " . @$aAllBlocks[$dummy]['share_id'] . ", aborting!\n"); + exit(1); + } +} ?>