crawler-full-node/crawler.php

188 lines
4.9 KiB
PHP
Raw Normal View History

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