From 4e284895a8547403d5f8a8cf41ae61537f3fc8fb Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Mon, 10 Jun 2013 10:11:56 +0200 Subject: [PATCH] Adding support for block finder bonus * Added new configuration option `block_bonus`, see `global.inc.dist.php`, default 0 * Added new transaction types: `Bonus` and `Orphan_Bonus` * Changes transaction table structure, added upgrade SQL * Changed findblock cron to credit bonus to finder * Modified transactions class to reflect changes Fixes #148 --- cronjobs/findblock.php | 18 ++++--- public/include/classes/transaction.class.php | 50 +++++++------------ public/include/config/global.inc.dist.php | 1 + .../mmcFE/account/transactions/default.tpl | 15 +++--- sql/issue_148_transactions_upgrade.sql | 1 + 5 files changed, 41 insertions(+), 44 deletions(-) create mode 100644 sql/issue_148_transactions_upgrade.sql diff --git a/cronjobs/findblock.php b/cronjobs/findblock.php index ead4b61c..b5fdca28 100755 --- a/cronjobs/findblock.php +++ b/cronjobs/findblock.php @@ -102,6 +102,9 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { $strStatus = "Finder Failed"; if (!$block->setShares($aBlock['id'], $iRoundShares)) $strStatus = "Shares Failed"; + if ($config['block_bonus'] > 0 && !$transaction->addTransaction($iAccountId, $config['block_bonus'], 'Bonus', $aBlock['id'])) { + $strStatus = "Bonus Failed"; + } verbose( $aBlock['id'] . "\t\t" @@ -115,13 +118,16 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { // Notify users $aAccounts = $notification->getNotificationAccountIdByType('new_block'); - foreach ($aAccounts as $account_id) { - $aMailData['height'] = $aBlock['height']; - $aMailData['subject'] = 'New Block'; - $aMailData['email'] = $user->getUserEmail($user->getUserName($account_id)); - $aMailData['shares'] = $iRoundShares; - $notification->sendNotification($account_id, 'new_block', $aMailData); + if (is_array($aAccounts)) { + foreach ($aAccounts as $account_id) { + $aMailData['height'] = $aBlock['height']; + $aMailData['subject'] = 'New Block'; + $aMailData['email'] = $user->getUserEmail($user->getUserName($account_id)); + $aMailData['shares'] = $iRoundShares; + $notification->sendNotification($account_id, 'new_block', $aMailData); + } } + break; } } ?> diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index a487e5ec..d12d14ee 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -55,35 +55,23 @@ class Transaction { **/ public function setOrphan($block_id) { $this->debug->append("STA " . __METHOD__, 4); - $stmt = $this->mysqli->prepare(" - UPDATE $this->table - SET type = 'Orphan_Credit' - WHERE type = 'Credit' - AND block_id = ? - "); - if (!($this->checkStmt($stmt) && $stmt->bind_param('i', $block_id) && $stmt->execute())) { - $this->debug->append("Failed to set orphan credit transactions for $block_id"); - return false; - } - $stmt = $this->mysqli->prepare(" - UPDATE $this->table - SET type = 'Orphan_Fee' - WHERE type = 'Fee' - AND block_id = ? - "); - if (!($this->checkStmt($stmt) && $stmt->bind_param('i', $block_id) && $stmt->execute())) { - $this->debug->append("Failed to set orphan fee transactions for $block_id"); - return false; - } - $stmt = $this->mysqli->prepare(" - UPDATE $this->table - SET type = 'Orphan_Donation' - WHERE type = 'Donation' - AND block_id = ? - "); - if (!($this->checkStmt($stmt) && $stmt->bind_param('i', $block_id) && $stmt->execute())) { - $this->debug->append("Failed to set orphan donation transactions for $block_id"); - return false; + $aOrphans = array( + 'Credit' => 'Orphan_Credit', + 'Fee' => 'Orphan_Fee', + 'Donation' => 'Orphan_Donation', + 'Bonus' => 'Orphan_Bonus' + ); + foreach ($aOrphans as $from => $to) { + $stmt = $this->mysqli->prepare(" + UPDATE $this->table + SET type = '$to' + WHERE type = '$from' + AND block_id = ? + "); + if (!($this->checkStmt($stmt) && $stmt->bind_param('i', $block_id) && $stmt->execute())) { + $this->debug->append("Failed to set orphan $from => $to transactions for $block_id"); + return false; + } } return true; } @@ -143,7 +131,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','Bonus') AND b.confirmations >= " . $this->config['confirmations'] . " ) AS t1, ( @@ -180,7 +168,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','Bonus') AND b.confirmations >= ? AND t.account_id = ? ) AS t1, diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 306d5261..f1cb0cbe 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 ), + 'block_bonus' => 0, '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/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index 88f749c1..2e6ae074 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -16,7 +16,7 @@ {section transaction $TRANSACTIONS} {if ( - ($TRANSACTIONS[transaction].type == 'Credit' and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations) + (($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 == 'Debit_AP' @@ -28,7 +28,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} @@ -58,7 +58,7 @@ {section transaction $TRANSACTIONS} {if ( - $TRANSACTIONS[transaction].type == 'Credit' && $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations + ($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) )} @@ -68,9 +68,9 @@ {$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 $TRANSACTIONS[transaction].type == Credit} + {if $TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus'} {assign var="credits" value="`$credits+$TRANSACTIONS[transaction].amount`"} {else} {assign var="debits" value="`$debits+$TRANSACTIONS[transaction].amount`"} @@ -106,6 +106,7 @@ $TRANSACTIONS[transaction].type == 'Orphan_Credit' or $TRANSACTIONS[transaction].type == 'Orphan_Donation' or $TRANSACTIONS[transaction].type == 'Orphan_Fee' + or $TRANSACTIONS[transaction].type == 'Orphan_Bonus' )} {$TRANSACTIONS[transaction].id} @@ -113,9 +114,9 @@ {$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 $TRANSACTIONS[transaction].type == Orphan_Credit} + {if $TRANSACTIONS[transaction].type == 'Orphan_Credit' or $TRANSACTIONS[transaction].type == 'Orphan_Bonus'} {assign var="orphan_credits" value="`$orphan_credits+$TRANSACTIONS[transaction].amount`"} {else} {assign var="orphan_debits" value="`$orphan_debits+$TRANSACTIONS[transaction].amount`"} diff --git a/sql/issue_148_transactions_upgrade.sql b/sql/issue_148_transactions_upgrade.sql new file mode 100644 index 00000000..2c0a47a7 --- /dev/null +++ b/sql/issue_148_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' ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;