Browse Source

update semaphore ID

sqlite
ghost 11 months ago
parent
commit
bfc73fd8bd
  1. 248
      crawler.php

248
crawler.php

@ -1,189 +1,187 @@ @@ -1,189 +1,187 @@
<?php
$semaphore = sem_get(1);
$semaphore = sem_get(crc32('kvazar-network.crawler_api_node'), 1);
if (false !== sem_acquire($semaphore, 1)) {
if (false === sem_acquire($semaphore, true)) {
require_once(__DIR__ . '/config.php');
require_once(__DIR__ . '/library/sqlite.php');
require_once(__DIR__ . '/library/api.php');
require_once(__DIR__ . '/library/hash.php');
require_once(__DIR__ . '/library/base58.php');
require_once(__DIR__ . '/library/base58check.php');
require_once(__DIR__ . '/library/crypto.php');
require_once(__DIR__ . '/library/helper.php');
$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
$api = new API();
echo "database locked by the another process..." . PHP_EOL;
exit;
}
$blockLast = $db->getLastBlock();
$blockTotal = $api->getblockcount();
require_once(__DIR__ . '/config.php');
require_once(__DIR__ . '/library/sqlite.php');
require_once(__DIR__ . '/library/api.php');
require_once(__DIR__ . '/library/hash.php');
require_once(__DIR__ . '/library/base58.php');
require_once(__DIR__ . '/library/base58check.php');
require_once(__DIR__ . '/library/crypto.php');
require_once(__DIR__ . '/library/helper.php');
if (false === $blockTotal) {
echo "API connection error.\n";
exit;
}
$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
$api = new API();
$response = [];
$blockLast = $db->getLastBlock();
$blockTotal = $api->getblockcount();
if (CRAWLER_DEBUG) {
echo "scanning blockhain...\n";
}
if (false === $blockTotal) {
echo "API connection error.\n";
exit;
}
for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) {
$response = [];
if ($blockCurrent > $blockTotal) {
if (CRAWLER_DEBUG) {
echo "scanning blockhain...\n";
}
if (CRAWLER_DEBUG) {
echo "database is up to date.\n";
}
for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) {
break;
}
if ($blockCurrent > $blockTotal) {
if (CRAWLER_DEBUG) {
echo sprintf("reading block %s\n", $blockCurrent);
echo "database is up to date.\n";
}
if (!$blockHash = $api->getblockhash($blockCurrent)) {
break;
}
if (CRAWLER_DEBUG) {
echo "could not read the block hash. waiting for reconnect.\n";
}
if (CRAWLER_DEBUG) {
echo sprintf("reading block %s\n", $blockCurrent);
}
break;
if (!$blockHash = $api->getblockhash($blockCurrent)) {
if (CRAWLER_DEBUG) {
echo "could not read the block hash. waiting for reconnect.\n";
}
if (!$blockData = $api->getblock($blockHash)) {
break;
}
if (CRAWLER_DEBUG) {
echo "could not read the block data. waiting for reconnect.\n";
}
if (!$blockData = $api->getblock($blockHash)) {
break;
if (CRAWLER_DEBUG) {
echo "could not read the block data. waiting for reconnect.\n";
}
if (!$blockId = $db->getBlock($blockCurrent)) {
$blockId = $db->addBlock($blockCurrent);
break;
}
if (CRAWLER_DEBUG) {
echo sprintf("add block %s\n", $blockCurrent);
}
}
if (!$blockId = $db->getBlock($blockCurrent)) {
$blockId = $db->addBlock($blockCurrent);
$lostTransactions = 0;
if (CRAWLER_DEBUG) {
echo sprintf("add block %s\n", $blockCurrent);
}
}
foreach ($blockData['tx'] as $transaction) {
$lostTransactions = 0;
if (!$transactionRaw = $api->getrawtransaction($transaction)) {
foreach ($blockData['tx'] as $transaction) {
$lostTransactions++;
if (!$transactionRaw = $api->getrawtransaction($transaction)) {
$db->setLostTransactions($blockId, $lostTransactions);
$lostTransactions++;
if (CRAWLER_DEBUG) {
echo sprintf("could not read the transaction %s. skipped.\n", $transaction);
}
$db->setLostTransactions($blockId, $lostTransactions);
break;
if (CRAWLER_DEBUG) {
echo sprintf("could not read the transaction %s. skipped.\n", $transaction);
}
foreach($transactionRaw['vout'] as $vout) {
break;
}
$asmArray = explode(' ', $vout['scriptPubKey']['asm']);
foreach($transactionRaw['vout'] as $vout) {
if (in_array($asmArray[0], ['OP_KEVA_NAMESPACE', 'OP_KEVA_PUT', 'OP_KEVA_DELETE'])) {
$asmArray = explode(' ', $vout['scriptPubKey']['asm']);
$hash = Base58Check::encode($asmArray[1], false , 0 , false);
if (in_array($asmArray[0], ['OP_KEVA_NAMESPACE', 'OP_KEVA_PUT', 'OP_KEVA_DELETE'])) {
switch ($asmArray[0]) {
$hash = Base58Check::encode($asmArray[1], false , 0 , false);
case 'OP_KEVA_DELETE':
switch ($asmArray[0]) {
$key = filterString(decodeString($asmArray[2]));
$value = '';
case 'OP_KEVA_DELETE':
break;
$key = filterString(decodeString($asmArray[2]));
$value = '';
case 'OP_KEVA_NAMESPACE':
break;
$key = '_KEVA_NS_';
$value = filterString(decodeString($asmArray[2]));
case 'OP_KEVA_NAMESPACE':
break;
$key = '_KEVA_NS_';
$value = filterString(decodeString($asmArray[2]));
default:
break;
$key = filterString(decodeString($asmArray[2]));
$value = filterString(decodeString($asmArray[3]));
}
default:
if (!$nameSpaceId = $db->getNameSpace($hash)) {
$nameSpaceId = $db->addNameSpace($hash);
$key = filterString(decodeString($asmArray[2]));
$value = filterString(decodeString($asmArray[3]));
}
if (CRAWLER_DEBUG) {
echo sprintf("add namespace %s\n", $hash);
}
}
if (!$nameSpaceId = $db->getNameSpace($hash)) {
$nameSpaceId = $db->addNameSpace($hash);
if (!$dataId = $db->getData($transactionRaw['txid'])) {
$dataId = $db->addData($blockId,
$nameSpaceId,
$transactionRaw['time'],
$transactionRaw['size'],
$transactionRaw['txid'],
$key,
$value,
($key == '_KEVA_NS_'),
empty($value));
if (CRAWLER_DEBUG) {
echo sprintf("add namespace %s\n", $hash);
}
}
if ($value) {
if (!$dataId = $db->getData($transactionRaw['txid'])) {
$dataId = $db->addData($blockId,
$nameSpaceId,
$transactionRaw['time'],
$transactionRaw['size'],
$transactionRaw['txid'],
$key,
$value,
($key == '_KEVA_NS_'),
empty($value));
$db->setDataKeyDeleted($nameSpaceId, $key, false);
if ($value) {
if (CRAWLER_DEBUG) {
echo sprintf("add new key/value %s\n", $transactionRaw['txid']);
}
$db->setDataKeyDeleted($nameSpaceId, $key, false);
} else {
if (CRAWLER_DEBUG) {
echo sprintf("add new key/value %s\n", $transactionRaw['txid']);
}
$db->setDataKeyDeleted($nameSpaceId, $key, true);
} else {
if (CRAWLER_DEBUG) {
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
}
$db->setDataKeyDeleted($nameSpaceId, $key, true);
if (CRAWLER_DEBUG) {
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
}
}
}
if (CRAWLER_DEBUG) {
$response[] = [
'blocktotal'=> $blockTotal,
'block' => $blockCurrent,
'blockhash' => $transactionRaw['blockhash'],
'txid' => $transactionRaw['txid'],
'version' => $transactionRaw['version'],
'size' => $transactionRaw['size'],
'time' => $transactionRaw['time'],
'blocktime' => $transactionRaw['blocktime'],
'namehash' => $hash,
'key' => $key,
'value' => $value
];
}
if (CRAWLER_DEBUG) {
$response[] = [
'blocktotal'=> $blockTotal,
'block' => $blockCurrent,
'blockhash' => $transactionRaw['blockhash'],
'txid' => $transactionRaw['txid'],
'version' => $transactionRaw['version'],
'size' => $transactionRaw['size'],
'time' => $transactionRaw['time'],
'blocktime' => $transactionRaw['blocktime'],
'namehash' => $hash,
'key' => $key,
'value' => $value
];
}
}
}
}
}
// Debug
if (CRAWLER_DEBUG) {
echo "scanning completed.\n";
# print_r($response);
}
sem_release($semaphore);
} else {
echo "database locked by the another process...\n";
}
// Debug
if (CRAWLER_DEBUG) {
echo "scanning completed.\n";
# print_r($response);
}
Loading…
Cancel
Save