diff --git a/config/app.default.php b/config/app.default.php index c06599e..7fbc837 100644 --- a/config/app.default.php +++ b/config/app.default.php @@ -222,16 +222,16 @@ return [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, - 'host' => 'localhost', + 'host' => 'chainquery.lbry.io:3600', /** * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', - 'username' => 'my_app', - 'password' => 'secret', - 'database' => 'my_app', + 'username' => 'marcdeb', + 'password' => '3885fc6d0b71befe72a327f47f36c0911057', + 'database' => 'lbry', 'encoding' => 'utf8', 'timezone' => 'UTC', 'flags' => [], @@ -257,7 +257,7 @@ return [ */ //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'], - 'url' => env('DATABASE_URL', null), + 'url' => env('DATABASE_URL', 'chainquery.lbry.io:3600'), ], /** diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index cc5d715..f144f0c 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -14,6 +14,7 @@ use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\LabelAlignment; use Endroid\QrCode\QrCode; + class MainController extends AppController { public static $rpcurl; @@ -92,21 +93,23 @@ class MainController extends AppController { $lbcUsdPrice = $this->_getLatestPrice(); $this->set('lbcUsdPrice', $lbcUsdPrice); - $blocks = $this->Blocks->find()->select(['Chainwork', 'Confirmations', 'Difficulty', 'Hash', 'Height', 'TransactionHashes', 'BlockTime', 'BlockSize'])-> - order(['Height' => 'desc'])->limit(6)->toArray(); + $blocks = $this->Blocks->find()->select(['chainwork', 'confirmations', 'difficulty', 'hash', 'height', 'transaction_hashes', 'block_time', 'block_size'])->order(['height' => 'desc'])->limit(6)->toArray(); for ($i = 0; $i < count($blocks); $i++) { - $tx_hashes = json_decode($blocks[$i]->TransactionHashes); - $blocks[$i]->TransactionCount = count($tx_hashes); + $tx_hashes = json_decode($blocks[$i]->transaction_hashes, true); + $blocks[$i]->transaction_count = count($tx_hashes); } // hash rate $hashRate = $this->_formatHashRate($this->_gethashrate()); // recent claims - $claims = $this->Claims->find()->select(['TransactionHash', 'Name', 'Vout', 'ClaimId', 'ClaimType', 'Author', 'Title', 'Description', 'ContentType', - 'IsNSFW', 'Language', 'ThumbnailUrl', 'Created'])-> - distinct(['Claims.ClaimId'])-> - contain(['Publisher' => ['fields' => ['Name']]])->order(['Claims.Created' => 'DESC'])->limit(5)->toArray(); + $claims = $this->Claims->find()->select(['transaction_hash_id', 'name', 'vout', 'claim_id', 'claim_type', 'author', 'title', 'description', 'content_type', 'is_nsfw', 'language', 'thumbnail_url', 'created_at'])-> + distinct(['Claims.claim_id'])->order(['Claims.created_at' => 'DESC'])->limit(5)->toArray(); + + foreach($claims as $claim) { + $publisher = $this->Claims->find()->select(['name'])->where(['claim_id' => $claim->publisher_id])->first(); + $claim->publisher = $publisher; + } $this->set('recentBlocks', $blocks); $this->set('recentClaims', $claims); @@ -399,10 +402,10 @@ class MainController extends AppController { } $offset = ($page - 1) * $pageLimit; - $currentBlock = $this->Blocks->find()->select(['Height'])->order(['Height' => 'DESC'])->first(); + $currentBlock = $this->Blocks->find()->select(['height'])->order(['height' => 'DESC'])->first(); $blocks = $this->Blocks->find()->select( - ['Height', 'Difficulty', 'TransactionHashes', 'BlockSize', 'Nonce', 'BlockTime'] - )->offset($offset)->limit($pageLimit)->order(['Height' => 'DESC'])->toArray(); + ['height', 'difficulty', 'transaction_hashes', 'block_size', 'nonce', 'block_time'] + )->offset($offset)->limit($pageLimit)->order(['height' => 'DESC'])->toArray(); $this->set('currentBlock', $currentBlock); $this->set('blocks', $blocks); $this->set('pageLimit', $pageLimit); @@ -416,28 +419,13 @@ class MainController extends AppController { return $this->redirect('/'); } - $block = $this->Blocks->find()->where(['Height' => $height])->first(); + $block = $this->Blocks->find()->where(['height' => $height])->first(); if (!$block) { return $this->redirect('/'); } - try { - // update the block confirmations - $req = ['method' => 'getblock', 'params' => [$block->Hash]]; - $response = self::curl_json_post(self::$rpcurl, json_encode($req)); - $json = json_decode($response); - $rpc_block = $json->result; - if (isset($rpc_block->confirmations)) { - $block->Confirmations = $rpc_block->confirmations; - $conn = ConnectionManager::get('default'); - $conn->execute('UPDATE Blocks SET Confirmations = ? WHERE Id = ?', [$rpc_block->confirmations, $block->Id]); - } - } catch (\Exception $e) { - // try again next time - } - // Get the basic block transaction info - $txs = $this->Transactions->find()->select(['InputCount', 'OutputCount', 'Hash', 'Value', 'Version'])->where(['BlockHash' => $block->Hash])->toArray(); + $txs = $this->Transactions->find()->select(['input_count', 'output_count', 'hash', 'version'])->where(['block_hash_id' => $block->hash])->toArray(); $this->set('block', $block); $this->set('blockTxs', $txs); @@ -449,32 +437,27 @@ class MainController extends AppController { $this->loadModel('Transactions'); $this->loadModel('Inputs'); $this->loadModel('Outputs'); + $this->loadModel('Addresses'); $this->loadModel('Claims'); $sourceAddress = $this->request->query('address'); $tx = $this->Transactions->find()->select( - ['Id', 'BlockHash', 'InputCount', 'OutputCount', 'Hash', 'Value', 'TransactionTime', 'TransactionSize', 'Created', 'Version', 'LockTime', 'Raw'])->where(['Hash' => $hash])->first(); + ['id', 'block_hash_id', 'input_count', 'output_count', 'hash', 'transaction_time', 'transaction_size', 'created_at', 'version', 'lock_time', 'raw'])->where(['hash' => $hash])->first(); if (!$tx) { return $this->redirect('/'); } - if ($tx->TransactionSize == 0) { - $tx->TransactionSize = (strlen($tx->Raw) / 2); - $conn = ConnectionManager::get('default'); - $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(); - $confirmations = $block ? (($maxBlock->Height - $block->Height) + 1) : '0'; - $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(); + $block = $this->Blocks->find()->select(['confirmations', 'height'])->where(['hash' => $tx->block_hash_id])->first(); + $confirmations = $block->confirmations; + $inputs = $this->Inputs->find()->where(['transaction_id' => $tx->id])->order(['prevout_n' => 'asc'])->toArray(); + $inputAddresses = $this->Addresses->find()->where(['input_address_id'])->toArray(); + $outputs = $this->Outputs->find()->where(['transaction_id' => $tx->id])->order(['vout' => 'asc'])->toArray(); for ($i = 0; $i < count($outputs); $i++) { - $outputs[$i]->IsClaim = (strpos($outputs[$i]->ScriptPubKeyAsm, 'CLAIM') > -1); - $outputs[$i]->IsSupportClaim = (strpos($outputs[$i]->ScriptPubKeyAsm, 'SUPPORT_CLAIM') > -1); - $outputs[$i]->IsUpdateClaim = (strpos($outputs[$i]->ScriptPubKeyAsm, 'UPDATE_CLAIM') > -1); - $claim = $this->Claims->find()->where(['TransactionHash' => $tx->Hash, 'Vout' => $outputs[$i]->Vout])->first(); + $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]->IsUpdateClaim = (strpos($outputs[$i]->script_pub_key_asm, 'UPDATE_CLAIM') > -1); + $claim = $this->Claims->find()->where(['transaction_hash_id' => $tx->hash, 'vout' => $outputs[$i]->vout])->first(); $outputs[$i]->Claim = $claim; } @@ -482,10 +465,10 @@ class MainController extends AppController { $totalOut = 0; $fee = 0; foreach ($inputs as $in) { - $totalIn = bcadd($totalIn, $in->Value, 8); + $totalIn = bcadd($totalIn, $in->value, 8); } foreach ($outputs as $out) { - $totalOut = bcadd($totalOut, $out->Value, 8); + $totalOut = bcadd($totalOut, $out->value, 8); } $fee = bcsub($totalIn, $totalOut, 8); diff --git a/src/Model/Entity/Block.php b/src/Model/Entity/Block.php index 7858813..8f66eac 100644 --- a/src/Model/Entity/Block.php +++ b/src/Model/Entity/Block.php @@ -5,6 +5,9 @@ namespace App\Model\Entity; use Cake\ORM\Entity; class Block extends Entity { + public function target() { + + } } diff --git a/src/Model/Entity/Claim.php b/src/Model/Entity/Claim.php index 18a5734..d12c480 100644 --- a/src/Model/Entity/Claim.php +++ b/src/Model/Entity/Claim.php @@ -6,7 +6,7 @@ use Cake\ORM\Entity; class Claim extends Entity { function getLbryLink() { - $link = $this->Name; + $link = $this->name; if (isset($this->Publisher->Name)) { $link = $this->Publisher->Name . '/' . $link; } @@ -15,21 +15,21 @@ class Claim extends Entity { } function getExplorerLink() { - $link = '/claims/' . $this->ClaimId; + $link = '/claims/' . $this->claim_id; return $link; } function getContentTag() { $ctTag = null; - if (substr($this->ContentType, 0, 5) === 'audio') { + if (substr($this->content_type, 0, 5) === 'audio') { $ctTag = 'audio'; - } else if (substr($this->ContentType, 0, 5) === 'video') { + } else if (substr($this->content_type, 0, 5) === 'video') { $ctTag = 'video'; - } else if (substr($this->ContentType, 0, 5) === 'image') { + } else if (substr($this->content_type, 0, 5) === 'image') { $ctTag = 'image'; } - if (!$ctTag && $this->ClaimType == 1) { + if (!$ctTag && $this->claim_type == 1) { $ctTag = 'identity'; } return $ctTag; @@ -37,10 +37,10 @@ class Claim extends Entity { function getAutoThumbText() { $autoThumbText = ''; - if ($this->ClaimType == 1) { - $autoThumbText = strtoupper(substr($this->Name, 1, min( strlen($this->Name), 3 ))); + if ($this->claim_type == 1) { + $autoThumbText = strtoupper(substr($this->name, 1, min( strlen($this->name), 3 ))); } else { - $str = (strlen(trim($this->Title)) > 0) ? $this->Title : $this->Name; + $str = (strlen(trim($this->title)) > 0) ? $this->title : $this->name; $autoThumbText = strtoupper(substr($str, 0, min (strlen($str), 2 ))); } return $autoThumbText; diff --git a/src/Model/Entity/ClaimStream.php b/src/Model/Entity/ClaimStream.php deleted file mode 100644 index 6a51882..0000000 --- a/src/Model/Entity/ClaimStream.php +++ /dev/null @@ -1,11 +0,0 @@ - \ No newline at end of file diff --git a/src/Model/Entity/Transaction.php b/src/Model/Entity/Transaction.php index 631f08c..b491ed7 100644 --- a/src/Model/Entity/Transaction.php +++ b/src/Model/Entity/Transaction.php @@ -3,8 +3,19 @@ namespace App\Model\Entity; use Cake\ORM\Entity; +use Cake\ORM\TableRegistry; + class Transaction extends Entity { + public function value() { + $OutputModel = TableRegistry::get('Outputs'); + $outputs = $OutputModel->find()->select(['value'])->where(['transaction_id' => $this->id])->toArray(); + $value = 0; + foreach($outputs as $o) { + $value += $o->value; + } + return $value; + } } diff --git a/src/Model/Table/AddressesTable.php b/src/Model/Table/AddressesTable.php index 1876136..daa47f2 100644 --- a/src/Model/Table/AddressesTable.php +++ b/src/Model/Table/AddressesTable.php @@ -8,8 +8,8 @@ class AddressesTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Addresses'); + $this->primaryKey('id'); + $this->table('address'); $this->addBehavior('SimpleAudit'); } diff --git a/src/Model/Table/BlocksTable.php b/src/Model/Table/BlocksTable.php index 0b2a8ed..bc58288 100644 --- a/src/Model/Table/BlocksTable.php +++ b/src/Model/Table/BlocksTable.php @@ -8,8 +8,8 @@ class BlocksTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Blocks'); + $this->primaryKey('id'); + $this->table('block'); $this->addBehavior('SimpleAudit'); } diff --git a/src/Model/Table/ClaimStreamsTable.php b/src/Model/Table/ClaimStreamsTable.php deleted file mode 100644 index eb77edf..0000000 --- a/src/Model/Table/ClaimStreamsTable.php +++ /dev/null @@ -1,18 +0,0 @@ -primaryKey('Id'); - $this->table('ClaimStreams'); - - //$this->addBehavior('SimpleAudit'); - } -} - -?> \ No newline at end of file diff --git a/src/Model/Table/ClaimsTable.php b/src/Model/Table/ClaimsTable.php index 4177faa..9f826da 100644 --- a/src/Model/Table/ClaimsTable.php +++ b/src/Model/Table/ClaimsTable.php @@ -8,25 +8,17 @@ class ClaimsTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Claims'); + $this->primaryKey('id'); + $this->table('claim'); //$this->addBehavior('SimpleAudit'); $this->addAssociations([ 'belongsTo' => [ - 'Publisher' => [ + 'publisher' => [ 'className' => 'App\Model\Table\ClaimsTable', - 'foreignKey' => 'PublisherId', - 'bindingKey' => 'ClaimId', - 'propertyName' => 'Publisher' - ] - ], - 'hasOne' => [ - 'Stream' => [ - 'className' => 'App\Model\Table\ClaimStreamsTable', - 'foreignKey' => 'Id', - 'bindingKey' => 'Id', - 'propertyName' => 'Stream' + 'foreignKey' => 'publisher_id', + 'bindingKey' => 'claim_id', + 'propertyName' => 'publisher' ] ] ]); diff --git a/src/Model/Table/InputsTable.php b/src/Model/Table/InputsTable.php index c1c06e7..58776b6 100644 --- a/src/Model/Table/InputsTable.php +++ b/src/Model/Table/InputsTable.php @@ -8,19 +8,19 @@ class InputsTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Inputs'); + $this->primaryKey('id'); + $this->table('input'); $this->addBehavior('SimpleAudit'); $this->addAssociations([ 'belongsToMany' => [ - 'InputAddresses' => [ + 'input_addresses' => [ 'className' => 'App\Model\Table\AddressesTable', - 'joinTable' => 'InputsAddresses', - 'foreignKey' => 'InputId', - 'targetForeignKey' => 'AddressId', - 'propertyName' => 'InputAddresses' + 'joinTable' => 'input_addresses', + 'foreignKey' => 'input_id', + 'targetForeignKey' => 'address_id', + 'propertyName' => 'input_addresses' ] ] ]); diff --git a/src/Model/Table/OutputsTable.php b/src/Model/Table/OutputsTable.php index 1840aa7..dbb58fc 100644 --- a/src/Model/Table/OutputsTable.php +++ b/src/Model/Table/OutputsTable.php @@ -8,26 +8,26 @@ class OutputsTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Outputs'); + $this->primaryKey('id'); + $this->table('output'); $this->addBehavior('SimpleAudit'); $this->addAssociations([ 'belongsTo' => [ - 'SpendInput' => [ + 'spend_input' => [ 'className' => 'App\Model\Table\InputsTable', - 'foreignKey' => 'SpentByInputId', - 'propertyName' => 'SpendInput' + 'foreignKey' => 'spent_by_input_id', + 'propertyName' => 'spend_input' ] ], 'belongsToMany' => [ - 'OutputAddresses' => [ + 'output_addresses' => [ 'className' => 'App\Model\Table\AddressesTable', - 'joinTable' => 'OutputsAddresses', - 'foreignKey' => 'OutputId', - 'targetForeignKey' => 'AddressId', - 'propertyName' => 'OutputAddresses' + 'joinTable' => 'output_addresses', + 'foreignKey' => 'output_id', + 'targetForeignKey' => 'address_id', + 'propertyName' => 'output_addresses' ] ] ]); diff --git a/src/Model/Table/TransactionsTable.php b/src/Model/Table/TransactionsTable.php index 613512d..7d1a180 100644 --- a/src/Model/Table/TransactionsTable.php +++ b/src/Model/Table/TransactionsTable.php @@ -8,8 +8,8 @@ class TransactionsTable extends Table { public function initialize(array $config) { parent::initialize($config); - $this->primaryKey('Id'); - $this->table('Transactions'); + $this->primaryKey('id'); + $this->table('transaction'); $this->addBehavior('SimpleAudit'); } diff --git a/src/Template/Main/blocks.ctp b/src/Template/Main/blocks.ctp index 38b8543..3c1512f 100644 --- a/src/Template/Main/blocks.ctp +++ b/src/Template/Main/blocks.ctp @@ -21,20 +21,20 @@ end(); ?> - assign('title', 'Block Height ' . $block->Height) ?> + assign('title', 'Block Height ' . $block->height) ?>