better confirmations calculation

This commit is contained in:
Akinwale Ariwodola 2018-07-26 12:08:15 +01:00
parent ad4615cc5a
commit 667097f35d
2 changed files with 25 additions and 19 deletions

View file

@ -1,3 +0,0 @@
#!/bin/sh
cd /home/lbry/explorer.lbry.io/cron
php -d extension=pthreads.so blockstuff.php

View file

@ -462,7 +462,9 @@ class MainController extends AppController {
$conn->execute('UPDATE Transactions SET TransactionSize = ? WHERE Id = ?', [$tx->TransactionSize, $tx->Id]); $conn->execute('UPDATE Transactions SET TransactionSize = ? WHERE Id = ?', [$tx->TransactionSize, $tx->Id]);
} }
$maxBlock = $this->Blocks->find()->select(['Height'])->order(['Height' => 'desc'])->first();
$block = $this->Blocks->find()->select(['Confirmations', 'Height'])->where(['Hash' => $tx->BlockHash])->first(); $block = $this->Blocks->find()->select(['Confirmations', 'Height'])->where(['Hash' => $tx->BlockHash])->first();
$confirmations = $block ? max(1, $maxBlock->Height - $block->Height) : '0';
$inputs = $this->Inputs->find()->contain(['InputAddresses'])->where(['TransactionId' => $tx->Id])->order(['PrevoutN' => 'asc'])->toArray(); $inputs = $this->Inputs->find()->contain(['InputAddresses'])->where(['TransactionId' => $tx->Id])->order(['PrevoutN' => 'asc'])->toArray();
$outputs = $this->Outputs->find()->contain(['OutputAddresses', 'SpendInput' => ['fields' => ['Id', 'TransactionHash', 'PrevoutN', 'PrevoutHash']]])->where(['Outputs.TransactionId' => $tx->Id])->order(['Vout' => 'asc'])->toArray(); $outputs = $this->Outputs->find()->contain(['OutputAddresses', 'SpendInput' => ['fields' => ['Id', 'TransactionHash', 'PrevoutN', 'PrevoutHash']]])->where(['Outputs.TransactionId' => $tx->Id])->order(['Vout' => 'asc'])->toArray();
for ($i = 0; $i < count($outputs); $i++) { for ($i = 0; $i < count($outputs); $i++) {
@ -484,7 +486,7 @@ class MainController extends AppController {
$this->set('tx', $tx); $this->set('tx', $tx);
$this->set('block', $block); $this->set('block', $block);
$this->set('confirmations', $block ? number_format($block->Confirmations, 0, '', ',') : '0'); $this->set('confirmations', $confirmations);
$this->set('fee', $fee); $this->set('fee', $fee);
$this->set('inputs', $inputs); $this->set('inputs', $inputs);
$this->set('outputs', $outputs); $this->set('outputs', $outputs);
@ -526,6 +528,7 @@ class MainController extends AppController {
public function address($addr = null) { public function address($addr = null) {
set_time_limit(0); set_time_limit(0);
$this->loadModel('Blocks');
$this->loadModel('Addresses'); $this->loadModel('Addresses');
$this->loadModel('Transactions'); $this->loadModel('Transactions');
$this->loadModel('Inputs'); $this->loadModel('Inputs');
@ -554,7 +557,6 @@ class MainController extends AppController {
$tagRequestAmount = '25.' . rand(11111111, 99999999); $tagRequestAmount = '25.' . rand(11111111, 99999999);
} }
$address = $this->Addresses->find()->where(['Address' => $addr])->first(); $address = $this->Addresses->find()->where(['Address' => $addr])->first();
if (!$address) { if (!$address) {
if (strlen($addr) === 34) { if (strlen($addr) === 34) {
@ -593,13 +595,18 @@ class MainController extends AppController {
$stmt = $conn->execute('SELECT A.TotalReceived, A.TotalSent, A.Balance FROM Addresses A WHERE A.Id = ?', [$address->Id]); $stmt = $conn->execute('SELECT A.TotalReceived, A.TotalSent, A.Balance FROM Addresses A WHERE A.Id = ?', [$address->Id]);
$totals = $stmt->fetch(\PDO::FETCH_OBJ); $totals = $stmt->fetch(\PDO::FETCH_OBJ);
$stmt = $conn->execute(sprintf('SELECT T.Id, T.Hash, T.InputCount, T.OutputCount, T.Value, ' . $currentBlock = $this->Blocks->find()->select(['Height'])->order(['Height' => 'desc'])->first();
'TA.DebitAmount, TA.CreditAmount, ' . $currentHeight = $currentBlock ? intval($currentBlock->Height) : 0;
'B.Height, B.Confirmations, IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime ' .
'FROM Transactions T ' . $stmt = $conn->execute(sprintf(
'LEFT JOIN Blocks B ON T.BlockHash = B.Hash ' . 'SELECT T.Id, T.Hash, T.InputCount, T.OutputCount, T.Value, ' .
'RIGHT JOIN (SELECT TransactionId, DebitAmount, CreditAmount FROM TransactionsAddresses ' . ' TA.DebitAmount, TA.CreditAmount, ' .
' WHERE AddressId = ? ORDER BY TransactionTime DESC LIMIT %d, %d) TA ON TA.TransactionId = T.Id', $offset, $pageLimit), [$addressId]); ' B.Height, (CASE WHEN B.Height IS NOT NULL THEN GREATEST(1, ' . $currentHeight . ' - B.Height) ELSE NULL END) AS Confirmations, ' .
' IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime ' .
'FROM Transactions T ' .
'LEFT JOIN Blocks B ON T.BlockHash = B.Hash ' .
'RIGHT JOIN (SELECT TransactionId, DebitAmount, CreditAmount FROM TransactionsAddresses ' .
' WHERE AddressId = ? ORDER BY TransactionTime DESC LIMIT %d, %d) TA ON TA.TransactionId = T.Id', $offset, $pageLimit), [$addressId]);
$recentTxs = $stmt->fetchAll(\PDO::FETCH_OBJ); $recentTxs = $stmt->fetchAll(\PDO::FETCH_OBJ);
$totalRecvAmount = $totals->TotalReceived == 0 ? '0' : $totals->TotalReceived + 0; $totalRecvAmount = $totals->TotalReceived == 0 ? '0' : $totals->TotalReceived + 0;
@ -682,6 +689,7 @@ class MainController extends AppController {
return 'N/A'; return 'N/A';
} }
} }
private function _gettxoutsetinfo() { private function _gettxoutsetinfo() {
$req = ['method' => 'gettxoutsetinfo', 'params' => []]; $req = ['method' => 'gettxoutsetinfo', 'params' => []];
try { try {
@ -694,6 +702,7 @@ class MainController extends AppController {
return 'N/A'; return 'N/A';
} }
} }
public function apistatus() { public function apistatus() {
$this->autoRender = false; $this->autoRender = false;
$this->loadModel('Blocks'); $this->loadModel('Blocks');