mirror of
https://github.com/kvazar-network/crawler.git
synced 2025-09-07 20:41:52 +00:00
fix update loop, remove debug messages
This commit is contained in:
parent
116cce4fb7
commit
a4984e892d
449
src/index.php
449
src/index.php
@ -149,7 +149,7 @@ if (isset($argv[1]))
|
|||||||
// Is valid crawl delay?
|
// Is valid crawl delay?
|
||||||
$seconds = (int) $argv[1];
|
$seconds = (int) $argv[1];
|
||||||
|
|
||||||
if ($seconds > 1) {
|
if ($seconds >= 1) {
|
||||||
$delay = $seconds;
|
$delay = $seconds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,277 +158,268 @@ if (isset($argv[1]))
|
|||||||
// Begin crawler
|
// Begin crawler
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// Get total blocks to crawl
|
if ($blocks = $kevacoin->getBlockCount())
|
||||||
if (!$blocks = $kevacoin->getBlockCount())
|
|
||||||
{
|
{
|
||||||
// Dump errors
|
// Begin block index
|
||||||
if ($error = $kevacoin->getError()) {
|
for ($block = $state + 1; $block <= $blocks; $block++)
|
||||||
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))
|
|
||||||
{
|
{
|
||||||
var_dump(
|
// Debug progress
|
||||||
$kevacoin->getError()
|
printf(
|
||||||
|
"%d/%d\r",
|
||||||
|
$block,
|
||||||
|
$blocks
|
||||||
);
|
);
|
||||||
|
|
||||||
exit(
|
// Get block hash
|
||||||
sprintf(
|
if (!$hash = $kevacoin->getBlockHash($block))
|
||||||
_('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))
|
|
||||||
{
|
{
|
||||||
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(
|
exit(
|
||||||
sprintf(
|
sprintf(
|
||||||
_('Could not receive txid of transaction "%s" in block "%d"!'),
|
_('Could not receive tx data in block "%d"!'),
|
||||||
$transaction,
|
$transaction,
|
||||||
$block
|
$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(
|
// Validate each transaction
|
||||||
sprintf(
|
if (!$raw = $kevacoin->getRawTransaction($transaction))
|
||||||
_('Could not receive vout of transaction "%s" in block "%d"!'),
|
{
|
||||||
$transaction,
|
continue;
|
||||||
$block
|
}
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($raw['time']))
|
if (empty($raw['txid']))
|
||||||
{
|
|
||||||
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']))
|
|
||||||
{
|
{
|
||||||
exit(
|
exit(
|
||||||
sprintf(
|
sprintf(
|
||||||
_('Invalid vout transaction "%s" in block "%d"!'),
|
_('Could not receive txid of transaction "%s" in block "%d"!'),
|
||||||
$transaction,
|
$transaction,
|
||||||
$block
|
$block
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse space-separated fragments to array
|
if (!isset($raw['vout']))
|
||||||
$asm = explode(
|
|
||||||
' ',
|
|
||||||
$vout['scriptPubKey']['asm']
|
|
||||||
);
|
|
||||||
|
|
||||||
// Operation ID required to continue
|
|
||||||
if (empty($asm[0]))
|
|
||||||
{
|
{
|
||||||
continue;
|
exit(
|
||||||
|
sprintf(
|
||||||
|
_('Could not receive vout of transaction "%s" in block "%d"!'),
|
||||||
|
$transaction,
|
||||||
|
$block
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect key / value
|
if (empty($raw['time']))
|
||||||
switch ($asm[0]) {
|
{
|
||||||
|
exit(
|
||||||
|
sprintf(
|
||||||
|
_('Could not receive time of transaction "%s" in block "%d"!'),
|
||||||
|
$transaction,
|
||||||
|
$block
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
case 'OP_KEVA_PUT':
|
if (empty($raw['size']))
|
||||||
case 'OP_KEVA_NAMESPACE':
|
{
|
||||||
|
exit(
|
||||||
|
sprintf(
|
||||||
|
_('Could not receive size of transaction "%s" in block "%d"!'),
|
||||||
|
$transaction,
|
||||||
|
$block
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Namespace info required to continue
|
// Parse transaction data
|
||||||
if (empty($asm[1]))
|
foreach((array) $raw['vout'] as $vout) {
|
||||||
{
|
|
||||||
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:
|
|
||||||
|
|
||||||
|
if (!$vout || empty($vout['scriptPubKey']) || empty($vout['scriptPubKey']['asm']))
|
||||||
|
{
|
||||||
exit(
|
exit(
|
||||||
sprintf(
|
sprintf(
|
||||||
_('Unknown operation "%s" of transaction "%s" in block "%d"!'),
|
_('Invalid vout transaction "%s" in block "%d"!'),
|
||||||
$asm[0],
|
|
||||||
$transaction,
|
$transaction,
|
||||||
$block
|
$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);
|
sleep($delay);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user