Kvazar Crawler Engine that works with Kevacoin node
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.

190 lines
4.9 KiB

3 years ago
<?php
$semaphore = sem_get(1);
3 years ago
if (false !== sem_acquire($semaphore, 1)) {
3 years ago
require_once('config.php');
require_once('library/sqlite.php');
require_once('library/kevacoin.php');
require_once('library/hash.php');
require_once('library/base58.php');
require_once('library/base58check.php');
require_once('library/crypto.php');
require_once('library/helper.php');
3 years ago
$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD);
$kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD);
3 years ago
$blockLast = $db->getLastBlock();
$blockTotal = $kevaCoin->getblockcount();
3 years ago
if (!$blockTotal) {
echo "API connection error.\n";
exit;
}
$response = [];
3 years ago
if (CRAWLER_DEBUG) {
echo "scanning blockhain...\n";
}
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.\n";
3 years ago
}
break;
}
3 years ago
if (CRAWLER_DEBUG) {
echo sprintf("reading block %s\n", $blockCurrent);
}
3 years ago
if (!$blockHash = $kevaCoin->getblockhash($blockCurrent)) {
3 years ago
if (CRAWLER_DEBUG) {
echo "could not read the block hash. waiting for reconnect.\n";
}
3 years ago
break;
}
3 years ago
if (!$blockData = $kevaCoin->getblock($blockHash)) {
3 years ago
if (CRAWLER_DEBUG) {
echo "could not read the block data. waiting for reconnect.\n";
}
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\n", $blockCurrent);
}
}
$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);
}
break;
}
foreach($transactionRaw['vout'] as $vout) {
$asmArray = explode(' ', $vout['scriptPubKey']['asm']);
if (in_array($asmArray[0], ['OP_KEVA_NAMESPACE', 'OP_KEVA_PUT', 'OP_KEVA_DELETE'])) {
$hash = Base58Check::encode($asmArray[1], false , 0 , false);
switch ($asmArray[0]) {
case 'OP_KEVA_DELETE':
$key = filterString(decodeString($asmArray[2]));
$value = '';
break;
case 'OP_KEVA_NAMESPACE':
$key = '_KEVA_NS_';
$value = filterString(decodeString($asmArray[2]));
break;
default:
$key = filterString(decodeString($asmArray[2]));
$value = filterString(decodeString($asmArray[3]));
}
if (!$nameSpaceId = $db->getNameSpace($hash)) {
$nameSpaceId = $db->addNameSpace($hash);
3 years ago
if (CRAWLER_DEBUG) {
echo sprintf("add namespace %s\n", $hash);
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));
if ($value) {
$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);
}
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.\n";
# print_r($response);
3 years ago
}
sem_release($semaphore);
} else {
echo "database locked by the another process...\n";
3 years ago
}