From ffc39f855d742deaeaf6bc2e64e0b25b01b55912 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 07:33:24 +0100 Subject: [PATCH 1/9] Addressing #853 with a fix and proposed change --- public/include/classes/transaction.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 61072e29..a8e77d1b 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -39,9 +39,9 @@ class Transaction extends Base { LEFT JOIN " . $this->block->getTableName() . " AS b ON b.id = t.block_id SET t.archived = 1 - WHERE ( t.account_id = ? AND t.id <= ? AND b.confirmations >= ? ) - OR ( t.account_id = ? AND t.id <= ? AND t.type IN ( 'Credit_PPS', 'Donation_PPS', 'Fee_PPS', 'TXFee', 'Debit_MP', 'Debit_AP' ) )"); - if ($this->checkStmt($stmt) && $stmt->bind_param('iiiii', $account_id, $txid, $this->config['confirmations'], $account_id, $txid) && $stmt->execute()) + WHERE archived = 0 AND t.account_id = ? AND t.id <= ? + "); + if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $account_id, $txid) && $stmt->execute()) return true; return $this->sqlError(); } From b0cdbd54bcfce09fc5dc662c75a66a12d40eb7ae Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 07:43:09 +0100 Subject: [PATCH 2/9] [FIX] Removed blocks JOIN on archive query --- public/include/classes/transaction.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index a8e77d1b..0dad5fe6 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -36,8 +36,6 @@ class Transaction extends Base { public function setArchived($account_id, $txid) { $stmt = $this->mysqli->prepare(" UPDATE $this->table AS t - LEFT JOIN " . $this->block->getTableName() . " AS b - ON b.id = t.block_id SET t.archived = 1 WHERE archived = 0 AND t.account_id = ? AND t.id <= ? "); From 78ae3174d80bd782b7e4c13cb80ba5b454f08e69 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 08:11:16 +0100 Subject: [PATCH 3/9] [UPDATE] Use proper transaction ID boundaries --- public/include/classes/transaction.class.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 0dad5fe6..1d90373a 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -34,12 +34,14 @@ class Transaction extends Base { * @param bool boolean True or False **/ public function setArchived($account_id, $txid) { - $stmt = $this->mysqli->prepare(" - UPDATE $this->table AS t - SET t.archived = 1 - WHERE archived = 0 AND t.account_id = ? AND t.id <= ? - "); - if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $account_id, $txid) && $stmt->execute()) + // Fetch last archived transaction for user + $stmt = $this->mysqli->prepare("SELECT IFNULL(MAX(id), 0) AS id FROM $this->table WHERE archived = 1 AND account_id = ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) + $last_id = $result->fetch_object()->id; + $this->debug->append('Found last archived transaction: ' . $last_id); + // Update all transactions, mark as archived for user previous to $txid and higher than last archived transaction + $stmt = $this->mysqli->prepare("UPDATE $this->table SET archived = 1 WHERE archived = 0 AND account_id = ? AND id <= ? AND id > ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param('iii', $account_id, $txid, $last_id) && $stmt->execute()) return true; return $this->sqlError(); } From f80826ff71dd79cd1b8de2b350909423ab8d8920 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 08:21:11 +0100 Subject: [PATCH 4/9] [UPDATE] Cache transaction summaries --- public/include/classes/transaction.class.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 1d90373a..8d8c718e 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -52,9 +52,10 @@ class Transaction extends Base { * @return data array type and total **/ public function getTransactionSummary($account_id=NULL) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $sql = " SELECT - SUM(t.amount) AS total, t.type AS type + SUM(t.amount) AS total, t.type AS type FROM transactions AS t LEFT OUTER JOIN blocks AS b ON b.id = t.block_id @@ -79,7 +80,8 @@ class Transaction extends Base { while ($row = $result->fetch_assoc()) { $aData[$row['type']] = $row['total']; } - return $aData; + // Cache data for a while, query takes long on many rows + return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData, 60); } return $this->sqlError(); } @@ -277,6 +279,7 @@ class Transaction extends Base { } $transaction = new Transaction(); +$transaction->setMemcache($memcache); $transaction->setDebug($debug); $transaction->setMysql($mysqli); $transaction->setConfig($config); From 0c1d1010f8b15ade42b4a7add825f3fee856356e Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 09:29:36 +0100 Subject: [PATCH 5/9] [FIX] Re-added executable bit on findblock --- cronjobs/findblock.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 cronjobs/findblock.php diff --git a/cronjobs/findblock.php b/cronjobs/findblock.php old mode 100644 new mode 100755 From 09cf8217300b6b39321536741c228d46e7085bc4 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 09:30:21 +0100 Subject: [PATCH 6/9] [FIX] Honor confirmation status --- public/include/classes/transaction.class.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 8d8c718e..fbccd467 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -40,8 +40,15 @@ class Transaction extends Base { $last_id = $result->fetch_object()->id; $this->debug->append('Found last archived transaction: ' . $last_id); // Update all transactions, mark as archived for user previous to $txid and higher than last archived transaction - $stmt = $this->mysqli->prepare("UPDATE $this->table SET archived = 1 WHERE archived = 0 AND account_id = ? AND id <= ? AND id > ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param('iii', $account_id, $txid, $last_id) && $stmt->execute()) + $stmt = $this->mysqli->prepare(" + UPDATE $this->table AS t + LEFT JOIN " . $this->block->getTableName() . " AS b + ON b.id = t.block_id + SET archived = 1 + WHERE t.archived = 0 AND t.account_id = ? AND t.id <= ? AND t.id > ? AND (b.confirmations >= ? OR b.confirmations IS NULL) + "); + if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $account_id, $txid, $last_id, $this->config['confirmations']) && $stmt->execute()) + var_dump($stmt); return true; return $this->sqlError(); } From 4819b6819f6b67c47596f55510916293cd8f135a Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 09:31:54 +0100 Subject: [PATCH 7/9] [FIX] Remove debug output --- public/include/classes/transaction.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index fbccd467..90153961 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -48,7 +48,6 @@ class Transaction extends Base { WHERE t.archived = 0 AND t.account_id = ? AND t.id <= ? AND t.id > ? AND (b.confirmations >= ? OR b.confirmations IS NULL) "); if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $account_id, $txid, $last_id, $this->config['confirmations']) && $stmt->execute()) - var_dump($stmt); return true; return $this->sqlError(); } From 038d398344e1913c1b9172199c16c2060961ccad Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 09:53:40 +0100 Subject: [PATCH 8/9] [FIX] Wrong TX ID boundaries * We must ignore the last debit transaction to find the proper TX ID that has been archived This should now complete the fix for #853 - needs live testing but I am confident it will work as intended. Deploying to my live FST pool for testing. --- public/include/classes/transaction.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 90153961..d82f30ce 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -34,8 +34,9 @@ class Transaction extends Base { * @param bool boolean True or False **/ public function setArchived($account_id, $txid) { - // Fetch last archived transaction for user - $stmt = $this->mysqli->prepare("SELECT IFNULL(MAX(id), 0) AS id FROM $this->table WHERE archived = 1 AND account_id = ?"); + // Fetch last archived transaction for user, we must exclude our Debits though! There might be unarchived/archived + // records before our last payout + $stmt = $this->mysqli->prepare("SELECT IFNULL(MAX(id), 0) AS id FROM $this->table WHERE archived = 1 AND account_id = ? AND type NOT IN ('Debit_MP','Debit_AP')"); if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) $last_id = $result->fetch_object()->id; $this->debug->append('Found last archived transaction: ' . $last_id); From 55190b7d08a09fce60e070b3ef2e1fb036cee984 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Wed, 20 Nov 2013 11:13:11 +0100 Subject: [PATCH 9/9] [FIX] Also exclude TXFee records for last ID --- public/include/classes/transaction.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index d82f30ce..ecf93357 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -36,7 +36,7 @@ class Transaction extends Base { public function setArchived($account_id, $txid) { // Fetch last archived transaction for user, we must exclude our Debits though! There might be unarchived/archived // records before our last payout - $stmt = $this->mysqli->prepare("SELECT IFNULL(MAX(id), 0) AS id FROM $this->table WHERE archived = 1 AND account_id = ? AND type NOT IN ('Debit_MP','Debit_AP')"); + $stmt = $this->mysqli->prepare("SELECT IFNULL(MAX(id), 0) AS id FROM $this->table WHERE archived = 1 AND account_id = ? AND type NOT IN ('Debit_MP','Debit_AP','TXFee')"); if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) $last_id = $result->fetch_object()->id; $this->debug->append('Found last archived transaction: ' . $last_id);