mirror of
https://github.com/kvazar-network/crawler-full-node.git
synced 2025-01-22 12:54:14 +00:00
update semaphore ID
This commit is contained in:
parent
64e4bf7a3c
commit
e30466980b
258
crawler.php
258
crawler.php
@ -1,189 +1,187 @@
|
||||
<?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');
|
||||
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');
|
||||
echo "database locked by the another process...\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
|
||||
$kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD);
|
||||
require_once(__DIR__ . '/config.php');
|
||||
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');
|
||||
|
||||
$blockLast = $db->getLastBlock();
|
||||
$blockTotal = $kevaCoin->getblockcount();
|
||||
$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
|
||||
$kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD);
|
||||
|
||||
if (false === $blockTotal) {
|
||||
echo "API connection error.\n";
|
||||
exit;
|
||||
}
|
||||
$blockLast = $db->getLastBlock();
|
||||
$blockTotal = $kevaCoin->getblockcount();
|
||||
|
||||
$response = [];
|
||||
if (false === $blockTotal) {
|
||||
echo "API connection error.\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
if (CRAWLER_DEBUG) {
|
||||
echo "scanning blockhain...\n";
|
||||
}
|
||||
$response = [];
|
||||
|
||||
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) {
|
||||
echo "database is up to date.\n";
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
if ($blockCurrent > $blockTotal) {
|
||||
|
||||
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) {
|
||||
echo "could not read the block hash. waiting for reconnect.\n";
|
||||
}
|
||||
if (CRAWLER_DEBUG) {
|
||||
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) {
|
||||
echo "could not read the block data. waiting for reconnect.\n";
|
||||
}
|
||||
if (!$blockData = $kevaCoin->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);
|
||||
}
|
||||
}
|
||||
|
||||
$lostTransactions = 0;
|
||||
|
||||
foreach ($blockData['tx'] as $transaction) {
|
||||
|
||||
if (!$transactionRaw = $kevaCoin->getrawtransaction($transaction)) {
|
||||
|
||||
$lostTransactions++;
|
||||
|
||||
$db->setLostTransactions($blockId, $lostTransactions);
|
||||
if (!$blockId = $db->getBlock($blockCurrent)) {
|
||||
$blockId = $db->addBlock($blockCurrent);
|
||||
|
||||
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 = $kevaCoin->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]));
|
||||
$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 (!$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 (!$nameSpaceId = $db->getNameSpace($hash)) {
|
||||
$nameSpaceId = $db->addNameSpace($hash);
|
||||
|
||||
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) {
|
||||
echo sprintf("delete key %s from namespace %s\n", $key, $hash);
|
||||
}
|
||||
$db->setDataKeyDeleted($nameSpaceId, $key, false);
|
||||
|
||||
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) {
|
||||
$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…
x
Reference in New Issue
Block a user