kevacoin-cli/cli/put.php

156 lines
3.2 KiB
PHP
Raw Normal View History

2023-11-16 11:26:01 +00:00
<?php
// 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;
}
2023-11-16 20:17:37 +00:00
// Check file exits
if (!file_exists($argv[2]))
{
exit('filename does not exist!' . PHP_EOL);
}
// Get file hash sum
$md5file = md5_file(
$argv[2]
);
// Split content to smaller parts, according to the protocol limits
$size = isset($argv[3]) && $argv[3] <= 3072 ? (int) $argv[3] : 3072;
$pieces = str_split(
base64_encode(
file_get_contents(
$argv[2]
)
),
$size
);
// Count total pieces
$total = count(
$pieces
);
// Get software protocol details
$software = _exec(
$argv[1],
'-getinfo'
);
print_r($software);
// Create namespace to collect there data pieces
$ns = _exec(
2023-11-16 11:26:01 +00:00
$argv[1],
sprintf(
2023-11-16 20:17:37 +00:00
"%s '%s'",
2023-11-16 11:26:01 +00:00
'keva_namespace',
2023-11-16 20:17:37 +00:00
$md5file
2023-11-16 11:26:01 +00:00
)
);
2023-11-16 20:17:37 +00:00
print_r($ns);
2023-11-16 11:26:01 +00:00
2023-11-16 20:17:37 +00:00
// Create meta description for the future generations
print_r(
_exec(
$argv[1],
sprintf(
"%s %s '%s' '%s'",
'keva_put',
$ns->namespaceId,
'_CLITOR_IS_',
json_encode(
[
2023-11-17 15:43:23 +00:00
'version' => '1.0.0',
2023-11-16 20:17:37 +00:00
'model' =>
[
'name' => 'kevacoin',
'software' =>
[
'version' => $software->version,
'protocol' => $software->protocolversion
2023-11-17 13:58:50 +00:00
],
'namespace' => $ns->namespaceId
2023-11-16 20:17:37 +00:00
],
'pieces' =>
[
'total' => $total,
'size' => $size,
],
'file' =>
[
'name' => basename(
$argv[2]
),
'mime' => mime_content_type(
$argv[2]
),
'size' => filesize(
$argv[2]
),
'md5' => $md5file
]
]
)
)
)
);
2023-11-16 11:26:01 +00:00
2023-11-16 20:17:37 +00:00
// Begin pieces saving
foreach ($pieces as $key => $value)
{
print_r(
_exec(
2023-11-16 11:26:01 +00:00
$argv[1],
sprintf(
2023-11-16 20:17:37 +00:00
"%s '%s' '%s' '%s'",
2023-11-16 11:26:01 +00:00
'keva_put',
2023-11-16 20:17:37 +00:00
$ns->namespaceId,
2023-11-16 11:26:01 +00:00
$key,
$value
)
2023-11-16 20:17:37 +00:00
)
);
2023-11-16 12:17:19 +00:00
2023-11-16 20:17:37 +00:00
// Apply delays to prevent too-long-mempool-chain reject
$delay = isset($argv[4]) && $argv[4] > 0 ? (int) $argv[4] : 60;
echo sprintf(
2023-11-16 20:17:37 +00:00
'%s/%s sent, waiting %s seconds...' . PHP_EOL,
$key + 1,
$total,
$delay
);
2023-11-16 20:17:37 +00:00
sleep($delay);
}
// Print result
echo sprintf(
2023-11-22 13:33:00 +00:00
'done! run to extract: php %s/get.php %s %s' . PHP_EOL,
__DIR__,
2023-11-16 20:17:37 +00:00
$argv[1],
$ns->namespaceId
);