update semaphore ID

This commit is contained in:
ghost 2023-07-07 11:48:38 +03:00
parent e0831b59f5
commit bfc73fd8bd

View File

@ -1,189 +1,187 @@
<?php <?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'); echo "database locked by the another process..." . PHP_EOL;
require_once(__DIR__ . '/library/sqlite.php'); exit;
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); require_once(__DIR__ . '/config.php');
$api = new API(); 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');
$blockLast = $db->getLastBlock(); $db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
$blockTotal = $api->getblockcount(); $api = new API();
if (false === $blockTotal) { $blockLast = $db->getLastBlock();
echo "API connection error.\n"; $blockTotal = $api->getblockcount();
exit;
}
$response = []; if (false === $blockTotal) {
echo "API connection error.\n";
exit;
}
if (CRAWLER_DEBUG) { $response = [];
echo "scanning blockhain...\n";
}
for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) { if (CRAWLER_DEBUG) {
echo "scanning blockhain...\n";
}
if ($blockCurrent > $blockTotal) { for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) {
if (CRAWLER_DEBUG) { if ($blockCurrent > $blockTotal) {
echo "database is up to date.\n";
}
break;
}
if (CRAWLER_DEBUG) { 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) { if (CRAWLER_DEBUG) {
echo "could not read the block hash. waiting for reconnect.\n"; 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) { if (!$blockData = $api->getblock($blockHash)) {
echo "could not read the block data. waiting for reconnect.\n";
}
break; if (CRAWLER_DEBUG) {
echo "could not read the block data. waiting for reconnect.\n";
} }
if (!$blockId = $db->getBlock($blockCurrent)) { break;
$blockId = $db->addBlock($blockCurrent); }
if (CRAWLER_DEBUG) { if (!$blockId = $db->getBlock($blockCurrent)) {
echo sprintf("add block %s\n", $blockCurrent); $blockId = $db->addBlock($blockCurrent);
}
}
$lostTransactions = 0;
foreach ($blockData['tx'] as $transaction) {
if (!$transactionRaw = $api->getrawtransaction($transaction)) {
$lostTransactions++;
$db->setLostTransactions($blockId, $lostTransactions);
if (CRAWLER_DEBUG) { if (CRAWLER_DEBUG) {
echo sprintf("could not read the transaction %s. skipped.\n", $transaction); echo sprintf("add block %s\n", $blockCurrent);
} }
}
break; $lostTransactions = 0;
foreach ($blockData['tx'] as $transaction) {
if (!$transactionRaw = $api->getrawtransaction($transaction)) {
$lostTransactions++;
$db->setLostTransactions($blockId, $lostTransactions);
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])); case 'OP_KEVA_DELETE':
$value = '';
break; $key = filterString(decodeString($asmArray[2]));
$value = '';
case 'OP_KEVA_NAMESPACE': break;
$key = '_KEVA_NS_'; case 'OP_KEVA_NAMESPACE':
$value = filterString(decodeString($asmArray[2]));
break; $key = '_KEVA_NS_';
$value = filterString(decodeString($asmArray[2]));
default: break;
$key = filterString(decodeString($asmArray[2])); default:
$value = filterString(decodeString($asmArray[3]));
}
if (!$nameSpaceId = $db->getNameSpace($hash)) { $key = filterString(decodeString($asmArray[2]));
$nameSpaceId = $db->addNameSpace($hash); $value = filterString(decodeString($asmArray[3]));
}
if (CRAWLER_DEBUG) { if (!$nameSpaceId = $db->getNameSpace($hash)) {
echo sprintf("add namespace %s\n", $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 ($value) {
$db->setDataKeyDeleted($nameSpaceId, $key, false);
if (CRAWLER_DEBUG) { if (CRAWLER_DEBUG) {
echo sprintf("add new key/value %s\n", $transactionRaw['txid']); echo sprintf("add namespace %s\n", $hash);
} }
}
} else { 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, true); if ($value) {
if (CRAWLER_DEBUG) { $db->setDataKeyDeleted($nameSpaceId, $key, false);
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
} if (CRAWLER_DEBUG) {
echo sprintf("add new key/value %s\n", $transactionRaw['txid']);
}
} else {
$db->setDataKeyDeleted($nameSpaceId, $key, true);
if (CRAWLER_DEBUG) {
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
} }
} }
}
if (CRAWLER_DEBUG) { if (CRAWLER_DEBUG) {
$response[] = [ $response[] = [
'blocktotal'=> $blockTotal, 'blocktotal'=> $blockTotal,
'block' => $blockCurrent, 'block' => $blockCurrent,
'blockhash' => $transactionRaw['blockhash'], 'blockhash' => $transactionRaw['blockhash'],
'txid' => $transactionRaw['txid'], 'txid' => $transactionRaw['txid'],
'version' => $transactionRaw['version'], 'version' => $transactionRaw['version'],
'size' => $transactionRaw['size'], 'size' => $transactionRaw['size'],
'time' => $transactionRaw['time'], 'time' => $transactionRaw['time'],
'blocktime' => $transactionRaw['blocktime'], 'blocktime' => $transactionRaw['blocktime'],
'namehash' => $hash, 'namehash' => $hash,
'key' => $key, 'key' => $key,
'value' => $value 'value' => $value
]; ];
}
} }
} }
} }
} }
}
// Debug
if (CRAWLER_DEBUG) { // Debug
echo "scanning completed.\n"; if (CRAWLER_DEBUG) {
# print_r($response); echo "scanning completed.\n";
} # print_r($response);
sem_release($semaphore);
} else {
echo "database locked by the another process...\n";
} }