Crash fix #70

Merged
akinwale merged 2 commits from crash-fix into master 2019-10-21 17:41:59 +02:00

View file

@ -136,6 +136,10 @@ class MainController extends AppController {
$count = $stmt->fetch(\PDO::FETCH_OBJ); $count = $stmt->fetch(\PDO::FETCH_OBJ);
$numClaims = $count->Total; $numClaims = $count->Total;
$stmt = $conn->execute('SELECT MAX(id) AS MaxId FROM claim');
$res = $stmt->fetch(\PDO::FETCH_OBJ);
$maxClaimId = $res->MaxId;
$numPages = ceil($numClaims / $pageLimit); $numPages = ceil($numClaims / $pageLimit);
if ($page < 1) { if ($page < 1) {
$page = 1; $page = 1;
@ -144,14 +148,22 @@ class MainController extends AppController {
$page = $numPages; $page = $numPages;
} }
$offset = ($page - 1) * $pageLimit; $startLimitId = $maxClaimId - ($page * $pageLimit);
$claims = $this->Claims->find()->select($this->Claims)->select(['publisher' => 'C.name'])->leftJoin(['C' => 'claim'], ['C.claim_id = Claims.publisher_id'])->order(['Claims.created_at' => 'DESC'])->offset($offset)->limit($pageLimit)->toArray(); $endLimitId = $startLimitId + $pageLimit;
if ($endLimitId > $maxClaimId) {
$endLimitId = $maxClaimId;
}
$claims = $this->Claims->find()->select($this->Claims)->
select(['publisher' => 'C.name'])->leftJoin(['C' => 'claim'], ['C.claim_id = Claims.publisher_id'])->
where(['Claims.id >' => $startLimitId, 'Claims.id <=' => $endLimitId])->
order(['Claims.id' => 'DESC'])->toArray();
for ($i = 0; $i < count($claims); $i++) { for ($i = 0; $i < count($claims); $i++) {
if ($canConvert && $claims[$i]->fee > 0 && $claims[$i]->fee_currency == 'USD') { if ($canConvert && $claims[$i]->fee > 0 && $claims[$i]->fee_currency == 'USD') {
$claims[$i]->price = $claims[$i]->fee / $priceInfo->price; $claims[$i]->price = $claims[$i]->fee / $priceInfo->price;
} }
if (isset($claims[$i]->Stream)) { if (isset($claims[$i]->Stream)) {
$json = json_decode($claims[$i]->Stream->Stream); $json = json_decode($claims[$i]->Stream->Stream);
if (isset($json->metadata->license)) { if (isset($json->metadata->license)) {
@ -226,7 +238,7 @@ class MainController extends AppController {
} }
$transactions = $this->Transactions->find()->select(['Transactions.id', 'Transactions.hash', 'Transactions.value', 'Transactions.input_count', 'Transactions.output_count', 'Transactions.transaction_time', 'Transactions.created_at'])->order(['Transactions.created_at' => 'desc'])->limit(10)->toArray(); $transactions = $this->Transactions->find()->select(['Transactions.id', 'Transactions.hash', 'Transactions.value', 'Transactions.input_count', 'Transactions.output_count', 'Transactions.transaction_time', 'Transactions.created_at'])->order(['Transactions.created_at' => 'desc'])->limit(10)->toArray();
$this->set('blocks', $blocks); $this->set('blocks', $blocks);
$this->set('txs', $transactions); $this->set('txs', $transactions);
} }
@ -359,21 +371,21 @@ class MainController extends AppController {
else { else {
$last_block = $this->Blocks->find()->select(['height'])->order(['height' => 'desc'])->first(); $last_block = $this->Blocks->find()->select(['height'])->order(['height' => 'desc'])->first();
$confirmations = $last_block->height - $block->height + 1; $confirmations = $last_block->height - $block->height + 1;
} }
$inputs = $this->Inputs->find()->where(['transaction_id' => $tx->id])->order(['prevout_n' => 'asc'])->toArray(); $inputs = $this->Inputs->find()->where(['transaction_id' => $tx->id])->order(['prevout_n' => 'asc'])->toArray();
foreach($inputs as $input) { foreach($inputs as $input) {
$inputAddresses = $this->Addresses->find()->select(['id', 'address'])->where(['id' => $input->input_address_id])->toArray(); $inputAddresses = $this->Addresses->find()->select(['id', 'address'])->where(['id' => $input->input_address_id])->toArray();
$input->input_addresses = $inputAddresses; $input->input_addresses = $inputAddresses;
} }
$outputs = $this->Outputs->find()->select($this->Outputs)->select(['spend_input_hash' => 'I.transaction_hash', 'spend_input_id' => 'I.id'])->where(['Outputs.transaction_id' => $tx->id])->leftJoin(['I' => 'input'], ['I.id = Outputs.spent_by_input_id'])->order(['Outputs.vout' => 'asc'])->toArray(); $outputs = $this->Outputs->find()->select($this->Outputs)->select(['spend_input_hash' => 'I.transaction_hash', 'spend_input_id' => 'I.id'])->where(['Outputs.transaction_id' => $tx->id])->leftJoin(['I' => 'input'], ['I.id = Outputs.spent_by_input_id'])->order(['Outputs.vout' => 'asc'])->toArray();
for ($i = 0; $i < count($outputs); $i++) { for ($i = 0; $i < count($outputs); $i++) {
$outputs[$i]->IsClaim = (strpos($outputs[$i]->script_pub_key_asm, 'CLAIM') > -1); $outputs[$i]->IsClaim = (strpos($outputs[$i]->script_pub_key_asm, 'CLAIM') > -1);
$outputs[$i]->IsSupportClaim = (strpos($outputs[$i]->script_pub_key_asm, 'SUPPORT_CLAIM') > -1); $outputs[$i]->IsSupportClaim = (strpos($outputs[$i]->script_pub_key_asm, 'SUPPORT_CLAIM') > -1);
$outputs[$i]->IsUpdateClaim = (strpos($outputs[$i]->script_pub_key_asm, 'UPDATE_CLAIM') > -1); $outputs[$i]->IsUpdateClaim = (strpos($outputs[$i]->script_pub_key_asm, 'UPDATE_CLAIM') > -1);
$claim = $this->Claims->find()->select(['id', 'claim_id', 'claim_address', 'vout', 'transaction_hash_id'])->where(['transaction_hash_id' => $tx->hash, 'vout' => $outputs[$i]->vout])->first(); $claim = $this->Claims->find()->select(['id', 'claim_id', 'claim_address', 'vout', 'transaction_hash_id'])->where(['transaction_hash_id' => $tx->hash, 'vout' => $outputs[$i]->vout])->first();
$outputs[$i]->Claim = $claim; $outputs[$i]->Claim = $claim;
$output_address = trim($outputs[$i]->address_list, '[""]'); $output_address = trim($outputs[$i]->address_list, '[""]');
if(!$output_address && $claim) { if(!$output_address && $claim) {
$output_address = $claim->claim_address; $output_address = $claim->claim_address;
@ -477,7 +489,7 @@ class MainController extends AppController {
$canTag = true; $canTag = true;
$transactionAddresses = $this->TransactionAddresses->find()->where(['address_id' => $address->id])->toArray(); $transactionAddresses = $this->TransactionAddresses->find()->where(['address_id' => $address->id])->toArray();
$numTransactions = count($transactionAddresses); $numTransactions = count($transactionAddresses);
$all = $this->request->query('all'); $all = $this->request->query('all');
if ($all === 'true') { if ($all === 'true') {
$offset = 0; $offset = 0;
@ -495,7 +507,7 @@ class MainController extends AppController {
$offset = ($page - 1) * $pageLimit; $offset = ($page - 1) * $pageLimit;
} }
$stmt = $conn->execute(sprintf( $stmt = $conn->execute(sprintf(
'SELECT T.id, T.hash, T.input_count, T.output_count, T.block_hash_id, ' . 'SELECT T.id, T.hash, T.input_count, T.output_count, T.block_hash_id, ' .
' TA.debit_amount, TA.credit_amount, ' . ' TA.debit_amount, TA.credit_amount, ' .
@ -552,7 +564,7 @@ class MainController extends AppController {
echo $qrCode->writeString(); echo $qrCode->writeString();
exit(0); exit(0);
} }
public function apiblocksize($timePeriod = '24h') { public function apiblocksize($timePeriod = '24h') {
$this->autoRender = false; $this->autoRender = false;
@ -587,7 +599,7 @@ class MainController extends AppController {
$resultSet[$price->TimePeriod]['AvgUSD'] = (float) $price->AvgUSD; $resultSet[$price->TimePeriod]['AvgUSD'] = (float) $price->AvgUSD;
} }
*/ */
$conn = ConnectionManager::get('default'); $conn = ConnectionManager::get('default');
// get avg block sizes for the time period // get avg block sizes for the time period
$stmt = $conn->execute("SELECT AVG(block_size) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') AS TimePeriod " . $stmt = $conn->execute("SELECT AVG(block_size) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') AS TimePeriod " .
@ -602,7 +614,7 @@ class MainController extends AppController {
return $this->_jsonResponse(['success' => true, 'data' => $resultSet]); return $this->_jsonResponse(['success' => true, 'data' => $resultSet]);
} }
public function apirealtimeblocks() { public function apirealtimeblocks() {
// Load 10 blocks // Load 10 blocks
$this->autoRender = false; $this->autoRender = false;
@ -616,7 +628,7 @@ class MainController extends AppController {
$this->_jsonResponse(['success' => true, 'blocks' => $blocks]); $this->_jsonResponse(['success' => true, 'blocks' => $blocks]);
} }
public function apirealtimetx() { public function apirealtimetx() {
// Load 10 transactions // Load 10 transactions
$this->autoRender = false; $this->autoRender = false;
@ -625,7 +637,7 @@ class MainController extends AppController {
$this->_jsonResponse(['success' => true, 'txs' => $txs]); $this->_jsonResponse(['success' => true, 'txs' => $txs]);
} }
protected function _gettxoutsetinfo() { protected function _gettxoutsetinfo() {
$now = new \DateTime('now', new \DateTimeZone('UTC')); $now = new \DateTime('now', new \DateTimeZone('UTC'));
$txOutSetInfo = new \stdClass(); $txOutSetInfo = new \stdClass();
@ -775,12 +787,12 @@ class MainController extends AppController {
if (!isset($base58address)) { if (!isset($base58address)) {
return $this->_jsonError('Base58 address not specified.', 400); return $this->_jsonError('Base58 address not specified.', 400);
} }
$address = $this->Addresses->find()->select(['id', 'balance'])->where(['address' => $base58address])->first(); $address = $this->Addresses->find()->select(['id', 'balance'])->where(['address' => $base58address])->first();
if (!$address) { if (!$address) {
return $this->_jsonError('Could not find address.', 400); return $this->_jsonError('Could not find address.', 400);
} }
return $this->_jsonResponse(['success' => true, ['balance' => ['confirmed' => $address->balance, 'unconfirmed' => 0]]]); return $this->_jsonResponse(['success' => true, ['balance' => ['confirmed' => $address->balance, 'unconfirmed' => 0]]]);
} }
@ -840,20 +852,20 @@ class MainController extends AppController {
$circulating = 0; $circulating = 0;
$txoutsetinfo = $this->_gettxoutsetinfo(); $txoutsetinfo = $this->_gettxoutsetinfo();
$reservedcommunity = ['rFLUohPG4tP3gZHYoyhvADCtrDMiaYb7Qd']; $reservedcommunity = ['rFLUohPG4tP3gZHYoyhvADCtrDMiaYb7Qd'];
$reservedoperational = ['r9PGXsejVJb9ZfMf3QVdDEJCzxkd9JLxzL']; $reservedoperational = ['r9PGXsejVJb9ZfMf3QVdDEJCzxkd9JLxzL'];
$reservedinstitutional = ['r9srwX7DEN7Mex3a8oR1mKSqQmLBizoJvi']; $reservedinstitutional = ['r9srwX7DEN7Mex3a8oR1mKSqQmLBizoJvi'];
$reservedaux = ['bRo4FEeqqxY7nWFANsZsuKEWByEgkvz8Qt', 'bU2XUzckfpdEuQNemKvhPT1gexQ3GG3SC2', 'bay3VA6YTQBL4WLobbG7CthmoGeUKXuXkD', 'bLPbiXBp6Vr3NSnsHzDsLNzoy5o36re9Cz', 'bMvUBo1h5WS46ThHtmfmXftz3z33VHL7wc', 'bVUrbCK8hcZ5XWti7b9eNxKEBxzc1rr393', 'bZja2VyhAC84a9hMwT8dwTU6rDRXowrjxH', 'bCrboXVztuSbZzVToCWSsu1pEr2oxKHu9v', 'bMgqQqYfwzWWYBk5o5dBMXtCndVAoeqy6h']; $reservedaux = ['bRo4FEeqqxY7nWFANsZsuKEWByEgkvz8Qt', 'bU2XUzckfpdEuQNemKvhPT1gexQ3GG3SC2', 'bay3VA6YTQBL4WLobbG7CthmoGeUKXuXkD', 'bLPbiXBp6Vr3NSnsHzDsLNzoy5o36re9Cz', 'bMvUBo1h5WS46ThHtmfmXftz3z33VHL7wc', 'bVUrbCK8hcZ5XWti7b9eNxKEBxzc1rr393', 'bZja2VyhAC84a9hMwT8dwTU6rDRXowrjxH', 'bCrboXVztuSbZzVToCWSsu1pEr2oxKHu9v', 'bMgqQqYfwzWWYBk5o5dBMXtCndVAoeqy6h'];
$allAddresses = array_merge($reservedcommunity, $reservedoperational, $reservedinstitutional, $reservedaux); $allAddresses = array_merge($reservedcommunity, $reservedoperational, $reservedinstitutional, $reservedaux);
$reservedtotal = $this->Addresses->find()->select(['id', 'balance' => 'SUM(balance)'])->where(['Addresses.address IN' => $allAddresses])->first(); $reservedtotal = $this->Addresses->find()->select(['id', 'balance' => 'SUM(balance)'])->where(['Addresses.address IN' => $allAddresses])->first();
$circulating = $txoutsetinfo->total_amount - ($reservedtotal->balance); $circulating = $txoutsetinfo->total_amount - ($reservedtotal->balance);
return $this->_jsonResponse(['success' => true, 'utxosupply' => ['total' => $txoutsetinfo->total_amount, 'circulating' => $circulating]]); return $this->_jsonResponse(['success' => true, 'utxosupply' => ['total' => $txoutsetinfo->total_amount, 'circulating' => $circulating]]);
} }
protected function _formatHashRate($value) { protected function _formatHashRate($value) {
if ($value === 'N/A') { if ($value === 'N/A') {
return $value; return $value;