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.

169 lines
2.3 KiB

1 year ago
<?php
// Init version
define(
'_CLITOR_IS_',
'1.3.0'
);
1 year 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;
}
// Validate arguments
if (empty($argv[1]))
{
print(
'processor required' . PHP_EOL
);
1 year ago
exit;
}
1 year ago
if (!file_exists($argv[1]))
1 year ago
{
print(
'processor does not exist' . PHP_EOL
);
exit;
}
if (empty($argv[2]))
{
print(
'namespace required' . PHP_EOL
);
exit;
1 year ago
}
if (empty($argv[3]))
{
print(
'destination file required' . PHP_EOL
);
exit;
}
1 year ago
if (file_exists($argv[3]))
{
print(
'destination file already exists' . PHP_EOL
);
exit;
}
1 year ago
// Get clitoris
$clitoris = _exec(
$argv[1],
sprintf(
'keva_get %s "_CLITOR_IS_"',
$argv[2]
)
);
// Validate protocol
if (empty($clitoris->value))
{
print(
'_CLITOR_IS_ not found' . PHP_EOL
);
exit;
}
1 year ago
// Merge content data
$chain = [];
foreach (
(array)
_exec(
$argv[1],
sprintf(
'keva_filter %s "\d+"',
$argv[2]
)
) as $piece)
1 year ago
{
if (
!isset($piece->key) ||
!isset($piece->value) ||
!isset($piece->height)
)
{
print(
'please wait for all pieces sending complete!' . PHP_EOL
);
exit;
}
// Keep all key versions in memory
$chain[$piece->key][$piece->height] = $piece->value;
print_r(
$piece
);
1 year ago
}
// Select last piece value by it max block height
//
// piece could have many of versions (with same key)
// this feature related to data reading correction after recovery #1
$pieces = [];
foreach ($chain as $key => $height)
{
ksort(
$height
);
$pieces[$key] = $height
[
array_key_last(
$height
)
];
}
ksort(
$pieces
);
1 year ago
// Save file to destination
1 year ago
file_put_contents(
$argv[3],
1 year ago
base64_decode(
implode('', $pieces)
1 year ago
)
);
printf(
'namespace "%s" saved to "%s"' . PHP_EOL,
$argv[2],
$argv[3]
1 year ago
);