diff --git a/src/index.php b/src/index.php index 5569226..36a913c 100644 --- a/src/index.php +++ b/src/index.php @@ -149,7 +149,7 @@ if (isset($argv[1])) // Is valid crawl delay? $seconds = (int) $argv[1]; - if ($seconds > 1) { + if ($seconds >= 1) { $delay = $seconds; } } @@ -158,277 +158,268 @@ if (isset($argv[1])) // Begin crawler while (true) { - // Get total blocks to crawl - if (!$blocks = $kevacoin->getBlockCount()) + if ($blocks = $kevacoin->getBlockCount()) { - // Dump errors - if ($error = $kevacoin->getError()) { - var_dump( - $error - ); - } - - echo sprintf( - "Blocks not found!\r", - $delay - ); - } - - // Begin block index - for ($block = $state + 1; $block <= $blocks; $block++) - { - // Debug progress - echo sprintf( - "%d/%d\r", - $block, - $blocks - ); - - // Get block hash - if (!$hash = $kevacoin->getBlockHash($block)) + // Begin block index + for ($block = $state + 1; $block <= $blocks; $block++) { - var_dump( - $kevacoin->getError() + // Debug progress + printf( + "%d/%d\r", + $block, + $blocks ); - exit( - sprintf( - _('Could not receive "%d" block hash!'), - $block - ) - ); - } - - // Get block data - if (!$data = $kevacoin->getBlock($hash)) - { - var_dump( - $kevacoin->getError() - ); - - exit( - sprintf( - _('Could not receive "%d" block data by hash "%s"!'), - $block, - $hash - ) - ); - } - - if (!isset($data['tx'])) - { - exit( - sprintf( - _('Could not receive tx data in block "%d"!'), - $transaction, - $block - ) - ); - } - - // Reset memory pool of processed transactions for each new block - $transactions = []; - - // Process each transaction in block - foreach ((array) $data['tx'] as $transaction) - { - // Validate each transaction - if (!$raw = $kevacoin->getRawTransaction($transaction)) + // Get block hash + if (!$hash = $kevacoin->getBlockHash($block)) { - continue; + var_dump( + $kevacoin->getError() + ); + + exit( + sprintf( + _('Could not receive "%d" block hash!'), + $block + ) + ); } - if (empty($raw['txid'])) + // Get block data + if (!$data = $kevacoin->getBlock($hash)) + { + var_dump( + $kevacoin->getError() + ); + + exit( + sprintf( + _('Could not receive "%d" block data by hash "%s"!'), + $block, + $hash + ) + ); + } + + if (!isset($data['tx'])) { exit( sprintf( - _('Could not receive txid of transaction "%s" in block "%d"!'), + _('Could not receive tx data in block "%d"!'), $transaction, $block ) ); } - if (!isset($raw['vout'])) + // Reset memory pool of processed transactions for each new block + $transactions = []; + + // Process each transaction in block + foreach ((array) $data['tx'] as $transaction) { - exit( - sprintf( - _('Could not receive vout of transaction "%s" in block "%d"!'), - $transaction, - $block - ) - ); - } + // Validate each transaction + if (!$raw = $kevacoin->getRawTransaction($transaction)) + { + continue; + } - if (empty($raw['time'])) - { - exit( - sprintf( - _('Could not receive time of transaction "%s" in block "%d"!'), - $transaction, - $block - ) - ); - } - - if (empty($raw['size'])) - { - exit( - sprintf( - _('Could not receive size of transaction "%s" in block "%d"!'), - $transaction, - $block - ) - ); - } - - // Parse transaction data - foreach((array) $raw['vout'] as $vout) { - - if (!$vout || empty($vout['scriptPubKey']) || empty($vout['scriptPubKey']['asm'])) + if (empty($raw['txid'])) { exit( sprintf( - _('Invalid vout transaction "%s" in block "%d"!'), + _('Could not receive txid of transaction "%s" in block "%d"!'), $transaction, $block ) ); } - // Parse space-separated fragments to array - $asm = explode( - ' ', - $vout['scriptPubKey']['asm'] - ); - - // Operation ID required to continue - if (empty($asm[0])) + if (!isset($raw['vout'])) { - continue; + exit( + sprintf( + _('Could not receive vout of transaction "%s" in block "%d"!'), + $transaction, + $block + ) + ); } - // Detect key / value - switch ($asm[0]) { + if (empty($raw['time'])) + { + exit( + sprintf( + _('Could not receive time of transaction "%s" in block "%d"!'), + $transaction, + $block + ) + ); + } - case 'OP_KEVA_PUT': - case 'OP_KEVA_NAMESPACE': + if (empty($raw['size'])) + { + exit( + sprintf( + _('Could not receive size of transaction "%s" in block "%d"!'), + $transaction, + $block + ) + ); + } - // Namespace info required to continue - if (empty($asm[1])) - { - exit( - sprintf( - _('Undefined namespace of transaction "%s" in block "%d"!'), - $transaction, - $block - ) - ); - } - - // Decode namespace - $namespace = \Kvazar\Crypto\Base58::encode( - $asm[1], - false, - 0, - false - ); - - // Find natively decoded data in block by namespace - // https://github.com/kvazar-network/crawler/issues/1 - // @TODO complete \Kvazar\Crypto\Kevacoin to decode tx faster - foreach ((array) $kevacoin->kevaFilter( - $namespace - ) as $record) - { - if ( - // Filter current block only - $record['height'] === $block - && - // Skip processed transaction - !in_array($record['txid'], $transactions) - ) { - // Register new transaction - $index->add( - $raw['time'], - $raw['size'], - $block, - $namespace, - $record['txid'], - $asm[0], - $record['key'], - $record['value'] - ); - - // Register processed transaction - $transactions[] = $record['txid']; - } - } - - // Find namespace alias (not always return by kevaFilter) - if ($record = $kevacoin->kevaGet($namespace, '_KEVA_NS_')) - { - if ( - // Filter current block only - $record['height'] === $block - && - // Skip processed transaction - !in_array($record['txid'], $transactions) - ) { - // Register new transaction - $index->add( - $raw['time'], - $raw['size'], - $block, - $namespace, - $record['txid'], - $asm[0], - $record['key'], - $record['value'] - ); - - // Register processed transaction - $transactions[] = $record['txid']; - } - } - - break; - - // @TODO not in use at this moment - case 'OP_KEVA_DELETE': - case 'OP_HASH160': - case 'OP_RETURN': - case 'OP_DUP': - case 'OP_NOP': - - break; - - default: + // Parse transaction data + foreach((array) $raw['vout'] as $vout) { + if (!$vout || empty($vout['scriptPubKey']) || empty($vout['scriptPubKey']['asm'])) + { exit( sprintf( - _('Unknown operation "%s" of transaction "%s" in block "%d"!'), - $asm[0], + _('Invalid vout transaction "%s" in block "%d"!'), $transaction, $block ) ); + } + + // Parse space-separated fragments to array + $asm = explode( + ' ', + $vout['scriptPubKey']['asm'] + ); + + // Operation ID required to continue + if (empty($asm[0])) + { + continue; + } + + // Detect key / value + switch ($asm[0]) { + + case 'OP_KEVA_PUT': + case 'OP_KEVA_NAMESPACE': + + // Namespace info required to continue + if (empty($asm[1])) + { + exit( + sprintf( + _('Undefined namespace of transaction "%s" in block "%d"!'), + $transaction, + $block + ) + ); + } + + // Decode namespace + $namespace = \Kvazar\Crypto\Base58::encode( + $asm[1], + false, + 0, + false + ); + + // Find natively decoded data in block by namespace + // https://github.com/kvazar-network/crawler/issues/1 + // @TODO complete \Kvazar\Crypto\Kevacoin to decode tx faster + foreach ((array) $kevacoin->kevaFilter( + $namespace + ) as $record) + { + if ( + // Filter current block only + $record['height'] === $block + && + // Skip processed transaction + !in_array($record['txid'], $transactions) + ) { + // Register new transaction + $index->add( + $raw['time'], + $raw['size'], + $block, + $namespace, + $record['txid'], + $asm[0], + $record['key'], + $record['value'] + ); + + // Register processed transaction + $transactions[] = $record['txid']; + } + } + + // Find namespace alias (not always return by kevaFilter) + if ($record = $kevacoin->kevaGet($namespace, '_KEVA_NS_')) + { + if ( + // Filter current block only + $record['height'] === $block + && + // Skip processed transaction + !in_array($record['txid'], $transactions) + ) { + // Register new transaction + $index->add( + $raw['time'], + $raw['size'], + $block, + $namespace, + $record['txid'], + $asm[0], + $record['key'], + $record['value'] + ); + + // Register processed transaction + $transactions[] = $record['txid']; + } + } + + break; + + // @TODO not in use at this moment + case 'OP_KEVA_DELETE': + case 'OP_HASH160': + case 'OP_RETURN': + case 'OP_DUP': + case 'OP_NOP': + + break; + + default: + + exit( + sprintf( + _('Unknown operation "%s" of transaction "%s" in block "%d"!'), + $asm[0], + $transaction, + $block + ) + ); + } } } + + // Update current block state + file_put_contents( + __DIR__ . '/../.state', + $block + ); + } + } + else + { + // Dump errors + if ($error = $kevacoin->getError()) + { + var_dump( + $error + ); } - - // Update current block state - file_put_contents( - __DIR__ . '/../.state', - $block - ); } - - // Dump results - echo sprintf( - "Crawl queue completed, await %d seconds for new blocks...\r", - $delay - ); sleep($delay); }