Browse Source

update semaphore ID

sqlite
ghost 1 year ago
parent
commit
e30466980b
  1. 246
      crawler.php

246
crawler.php

@ -1,189 +1,187 @@
<?php <?php
$semaphore = sem_get(1); $semaphore = sem_get(crc32('kvazar-network.crawler_full_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...\n";
require_once(__DIR__ . '/library/sqlite.php'); exit;
require_once(__DIR__ . '/library/kevacoin.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);
$kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD);
$blockLast = $db->getLastBlock(); require_once(__DIR__ . '/config.php');
$blockTotal = $kevaCoin->getblockcount(); require_once(__DIR__ . '/library/sqlite.php');
require_once(__DIR__ . '/library/kevacoin.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) { $db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
echo "API connection error.\n"; $kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD);
exit;
}
$response = []; $blockLast = $db->getLastBlock();
$blockTotal = $kevaCoin->getblockcount();
if (CRAWLER_DEBUG) { if (false === $blockTotal) {
echo "scanning blockhain...\n"; 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) { for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) {
echo "database is up to date.\n";
}
break; if ($blockCurrent > $blockTotal) {
}
if (CRAWLER_DEBUG) { if (CRAWLER_DEBUG) {
echo sprintf("reading block %s\n", $blockCurrent); echo "database is up to date.\n";
} }
if (!$blockHash = $kevaCoin->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 = $kevaCoin->getblockhash($blockCurrent)) {
if (CRAWLER_DEBUG) {
echo "could not read the block hash. waiting for reconnect.\n";
} }
if (!$blockData = $kevaCoin->getblock($blockHash)) { break;
}
if (CRAWLER_DEBUG) { if (!$blockData = $kevaCoin->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; if (CRAWLER_DEBUG) {
echo sprintf("add block %s\n", $blockCurrent);
}
}
foreach ($blockData['tx'] as $transaction) { $lostTransactions = 0;
if (!$transactionRaw = $kevaCoin->getrawtransaction($transaction)) { foreach ($blockData['tx'] as $transaction) {
$lostTransactions++; if (!$transactionRaw = $kevaCoin->getrawtransaction($transaction)) {
$db->setLostTransactions($blockId, $lostTransactions); $lostTransactions++;
if (CRAWLER_DEBUG) { $db->setLostTransactions($blockId, $lostTransactions);
echo sprintf("could not read the transaction %s. skipped.\n", $transaction);
}
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])); 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'])) { if (CRAWLER_DEBUG) {
$dataId = $db->addData($blockId, echo sprintf("add namespace %s\n", $hash);
$nameSpaceId, }
$transactionRaw['time'], }
$transactionRaw['size'],
$transactionRaw['txid'],
$key,
$value,
($key == '_KEVA_NS_'),
empty($value));
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) { $db->setDataKeyDeleted($nameSpaceId, $key, false);
echo sprintf("add new key/value %s\n", $transactionRaw['txid']);
}
} else { if (CRAWLER_DEBUG) {
echo sprintf("add new key/value %s\n", $transactionRaw['txid']);
}
$db->setDataKeyDeleted($nameSpaceId, $key, true); } else {
if (CRAWLER_DEBUG) { $db->setDataKeyDeleted($nameSpaceId, $key, true);
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
} 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 // Debug
if (CRAWLER_DEBUG) { if (CRAWLER_DEBUG) {
echo "scanning completed.\n"; echo "scanning completed.\n";
# print_r($response); # print_r($response);
}
sem_release($semaphore);
} else {
echo "database locked by the another process...\n";
} }

Loading…
Cancel
Save