Kvazar Crawler Engine that works with official Kevacoin API without node installation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.8 KiB

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