From e30466980b4e14a1c522b728dfe177fe0cc62927 Mon Sep 17 00:00:00 2001 From: ghost Date: Sat, 1 Jul 2023 17:46:13 +0300 Subject: [PATCH] update semaphore ID --- crawler.php | 246 ++++++++++++++++++++++++++-------------------------- 1 file changed, 122 insertions(+), 124 deletions(-) diff --git a/crawler.php b/crawler.php index 557790b..60a104e 100644 --- a/crawler.php +++ b/crawler.php @@ -1,189 +1,187 @@ getLastBlock(); - $blockTotal = $kevaCoin->getblockcount(); +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'); - if (false === $blockTotal) { - echo "API connection error.\n"; - exit; - } +$db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD); +$kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD); - $response = []; +$blockLast = $db->getLastBlock(); +$blockTotal = $kevaCoin->getblockcount(); - if (CRAWLER_DEBUG) { - echo "scanning blockhain...\n"; - } +if (false === $blockTotal) { + 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) { - echo "database is up to date.\n"; - } +for ($blockCurrent = ($blockLast + 1); $blockCurrent <= $blockLast + STEP_BLOCK_LIMIT; $blockCurrent++) { - 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); - } - } + if (!$blockId = $db->getBlock($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) { - echo sprintf("could not read the transaction %s. skipped.\n", $transaction); - } + $db->setLostTransactions($blockId, $lostTransactions); - 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])); - $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 (!$nameSpaceId = $db->getNameSpace($hash)) { + $nameSpaceId = $db->addNameSpace($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 (CRAWLER_DEBUG) { + echo sprintf("add namespace %s\n", $hash); + } + } - 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) { - echo sprintf("add new key/value %s\n", $transactionRaw['txid']); - } + $db->setDataKeyDeleted($nameSpaceId, $key, false); - } else { + if (CRAWLER_DEBUG) { + echo sprintf("add new key/value %s\n", $transactionRaw['txid']); + } - $db->setDataKeyDeleted($nameSpaceId, $key, true); + } else { - if (CRAWLER_DEBUG) { - echo sprintf("delete key %s from namespace %s\n", $key, $hash); - } + $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); }