CLI tools for KevaCoin blockchain
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.

192 lines
3.1 KiB

1 year ago
<?php
// Init version
define(
'_CLITOR_IS_',
'1.3.0'
);
11 months ago
// Init helper
function _exec(
string $processor,
string $command
): mixed
{
if (false !== exec(sprintf('%s %s', $processor, $command), $output))
{
$rows = [];
foreach($output as $row)
{
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows)))
{
return $result;
}
}
return false;
}
// Check processor exits
if (empty($argv[1]))
11 months ago
{
print(
'processor required' . PHP_EOL
);
exit;
11 months ago
}
if (!file_exists($argv[1]))
{
print(
'processor does not exist' . PHP_EOL
);
exit;
}
11 months ago
// Check namespace provided
if (empty($argv[2]))
{
print(
'namespace required' . PHP_EOL
);
exit;
}
11 months ago
// Check target file does not exist yet
if (!file_exists($argv[3]))
{
print(
'filename does not exist' . PHP_EOL
);
exit;
}
// Get namespace meta
$clitoris = _exec(
11 months ago
$argv[1],
sprintf(
"keva_get %s _CLITOR_IS_",
$argv[2]
)
11 months ago
);
if (empty($clitoris->value))
{
print(
'_CLITOR_IS_ not found for this namespace' . PHP_EOL
);
exit;
}
// Validate protocol
if (empty($clitoris->value))
11 months ago
{
print(
'_CLITOR_IS_ not found' . PHP_EOL
11 months ago
);
exit;
}
// Split content to smaller parts (according to the protocol limits)
$pieces = str_split(
base64_encode(
file_get_contents(
$argv[3]
)
),
3072
);
// Count total pieces
$total = count(
$pieces
);
// Begin pieces saving
foreach ($pieces as $key => $value)
{
// Check piece stored in blockchain is valid
$piece = _exec(
11 months ago
$argv[1],
sprintf(
"keva_get %s %d",
$argv[2],
$key
11 months ago
)
);
// Piece value not found
if (empty($piece->value))
11 months ago
{
printf(
'Piece %s/%s value not found, create new record...' . PHP_EOL,
$key + 1,
$total
);
}
// Piece value invalid, begin blockchain record
else if ($piece->value != $value)
{
printf(
'Piece %s/%s value invalid, overwrite record...' . PHP_EOL,
$key + 1,
$total,
);
11 months ago
}
// Piece valid
else
{
printf(
'Piece %s/%s - OK' . PHP_EOL,
$key + 1,
$total
);
continue; // skip next operations for this piece
}
// Record new piece
print_r(
_exec(
$argv[1],
sprintf(
"keva_put %s %d %s",
$argv[2],
$key,
$value
)
)
);
// Apply delays to prevent too-long-mempool-chain reject
$delay = isset($argv[3]) && $argv[3] > 0 ? (int) $argv[3] : 60;
printf(
'Piece %s/%s sent, waiting %s seconds...' . PHP_EOL,
$key + 1,
$total,
$delay
);
sleep(
$delay
);
11 months ago
}
// Done
printf(
'data successfully synced with namespace "%s"' . PHP_EOL,
$argv[2]
);