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');
@ -866,14 +875,14 @@ class MainController extends AppController {
public function apiutxosupply() { public function apiutxosupply() {
$this->autoRender = false; $this->autoRender = false;
$this->loadModel('Addresses'); $this->loadModel('Addresses');
$circulating = 0; $circulating = 0;
$reservedcommunity = 0; $reservedcommunity = 0;
$reservedoperational = 0; $reservedoperational = 0;
$reservedinstitutional = 0; $reservedinstitutional = 0;
$reservedtotal = 0; $reservedtotal = 0;
$circulating = 0; $circulating = 0;
$txoutsetinfo = $this->_gettxoutsetinfo(); $txoutsetinfo = $this->_gettxoutsetinfo();
$reservedcommunity = $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'rFLUohPG4tP3gZHYoyhvADCtrDMiaYb7Qd'])->first(); $reservedcommunity = $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'rFLUohPG4tP3gZHYoyhvADCtrDMiaYb7Qd'])->first();
$reservedoperational = $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'r9PGXsejVJb9ZfMf3QVdDEJCzxkd9JLxzL'])->first(); $reservedoperational = $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'r9PGXsejVJb9ZfMf3QVdDEJCzxkd9JLxzL'])->first();
@ -888,15 +897,15 @@ class MainController extends AppController {
$this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bZja2VyhAC84a9hMwT8dwTU6rDRXowrjxH'])->first() + $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bZja2VyhAC84a9hMwT8dwTU6rDRXowrjxH'])->first() +
$this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bMvUBo1h5WS46ThHtmfmXftz3z33VHL7wc'])->first() + $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bMvUBo1h5WS46ThHtmfmXftz3z33VHL7wc'])->first() +
$this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bMgqQqYfwzWWYBk5o5dBMXtCndVAoeqy6h'])->first(); $this->Addresses->find()->select(['Balance'])->where(['Address =' => 'bMgqQqYfwzWWYBk5o5dBMXtCndVAoeqy6h'])->first();
$reservedtotal = $reservedcommunity->Balance + $reservedoperational->Balance + $reservedinstitutional->Balance + $reservedaux->Balance; $reservedtotal = $reservedcommunity->Balance + $reservedoperational->Balance + $reservedinstitutional->Balance + $reservedaux->Balance;
$circulating = $txoutsetinfo->total_amount - $reservedtotal; $circulating = $txoutsetinfo->total_amount - $reservedtotal;
return $this->_jsonResponse(['success' => true, 'utxosupply' => ['total' => $txoutsetinfo->total_amount, 'circulating' => $circulating]]); return $this->_jsonResponse(['success' => true, 'utxosupply' => ['total' => $txoutsetinfo->total_amount, 'circulating' => $circulating]]);
} }
private static function curl_json_post($url, $data, $headers = []) { private static function curl_json_post($url, $data, $headers = []) {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);