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,9 +595,14 @@ 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();
$currentHeight = $currentBlock ? intval($currentBlock->Height) : 0;
$stmt = $conn->execute(sprintf(
'SELECT T.Id, T.Hash, T.InputCount, T.OutputCount, T.Value, ' .
' TA.DebitAmount, TA.CreditAmount, ' . ' TA.DebitAmount, TA.CreditAmount, ' .
'B.Height, B.Confirmations, IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime ' . ' 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 ' . 'FROM Transactions T ' .
'LEFT JOIN Blocks B ON T.BlockHash = B.Hash ' . 'LEFT JOIN Blocks B ON T.BlockHash = B.Hash ' .
'RIGHT JOIN (SELECT TransactionId, DebitAmount, CreditAmount FROM TransactionsAddresses ' . 'RIGHT JOIN (SELECT TransactionId, DebitAmount, CreditAmount FROM TransactionsAddresses ' .
@ -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');