From b5d0fc3ef54083ea46a9d8ee050d021be9ac897b Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 3 Jun 2024 22:46:52 +0300 Subject: [PATCH] simplify _CLITOR_IS_ meta record --- cli/fix.php | 296 ++++++++++++++++++++++++++-------------------------- cli/get.php | 127 ++++++++++++---------- cli/put.php | 151 +++++++++++++++------------ 3 files changed, 300 insertions(+), 274 deletions(-) diff --git a/cli/fix.php b/cli/fix.php index dd1f8e2..1e24e28 100644 --- a/cli/fix.php +++ b/cli/fix.php @@ -1,5 +1,11 @@ value)) +{ + print( + '_CLITOR_IS_ not found for this namespace' . PHP_EOL + ); + + exit; +} + +// Validate protocol +if (empty($clitoris->value)) { - echo sprintf( - 'Search for _CLITOR_IS_ match file MD5 %s ...' . PHP_EOL, - $md5file + print( + '_CLITOR_IS_ not found' . PHP_EOL ); - $meta = _exec( + 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( $argv[1], sprintf( - "%s '%s' '%s'", - 'keva_get', - $namespace->namespaceId, - '_CLITOR_IS_' + "keva_get %s %d", + $argv[2], + $key ) ); - if ($value = @json_decode($meta->value)) + // Piece value not found + if (empty($piece->value)) { - if ( - isset($value->pieces) && - isset($value->pieces->total) && - isset($value->pieces->size) && - - isset($value->file) && - isset($value->file->md5) && - $value->file->md5 === $md5file - ) - { - // Meta found - echo sprintf( - '_CLITOR_IS_ found for this file with namespace %s' . PHP_EOL, - $namespace->namespaceId - ); - - // Split content to smaller parts using _CLITOR_IS_ size defined before - $pieces = str_split( - base64_encode( - file_get_contents( - $argv[2] - ) - ), - $value->pieces->size - ); - - // Count total pieces - $total = count( - $pieces - ); - - // Validate pieces count - if ($value->pieces->total !== $total) - { - echo '_CLITOR_IS_ have another pieces quantity' . PHP_EOL; - - exit; - } - - // Begin pieces saving - foreach ($pieces as $key => $value) - { - // Check piece stored is valid - $piece = _exec( - $argv[1], - sprintf( - "%s '%s' '%s'", - 'keva_get', - $namespace->namespaceId, - $key - ) - ); - - // Piece value not found - if (empty($piece->value)) - { - echo sprintf( - 'Piece %s/%s value not found, creating...' . PHP_EOL, - $key + 1, - $total - ); - } - - // Piece value invalid, begin blockchain record - else if ($piece->value !== $value) - { - echo sprintf( - 'Piece %s/%s value invalid (%s <> %s), rewriting...' . PHP_EOL, - $key + 1, - $total, - md5( - $piece->value - ), - md5( - $value - ), - ); - } - - // Piece valid - else - { - echo sprintf( - 'Piece %s/%s - OK' . PHP_EOL, - $key + 1, - $total - ); - - continue; - } - - print_r( - _exec( - $argv[1], - sprintf( - "%s '%s' '%s' '%s'", - 'keva_put', - $namespace->namespaceId, - $key, - $value - ) - ) - ); - - // Apply delays to prevent too-long-mempool-chain reject - $delay = isset($argv[3]) && $argv[3] > 0 ? (int) $argv[3] : 60; - - echo sprintf( - 'Piece %s/%s sent, waiting %s seconds...' . PHP_EOL, - $key + 1, - $total, - $delay - ); - - sleep($delay); - } - - // Print result - echo sprintf( - 'done! run to extract: php %s/get.php %s %s' . PHP_EOL, - __DIR__, - $argv[1], - $namespace->namespaceId - ); - - break; - } + 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, + ); } + + // 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 + ); } -echo 'Could not recover this file!' . PHP_EOL; \ No newline at end of file +// Done +printf( + 'data successfully synced with namespace "%s"' . PHP_EOL, + $argv[2] +); \ No newline at end of file diff --git a/cli/get.php b/cli/get.php index f479ab3..ac21a7c 100644 --- a/cli/get.php +++ b/cli/get.php @@ -1,5 +1,11 @@ value)) +if (!file_exists($argv[1])) { - exit( - sprintf( - '%s does not contain _CLITOR_IS_' . PHP_EOL, - $argv[2] - ) + print( + 'processor does not exist' . PHP_EOL + ); + + exit; +} + +if (empty($argv[2])) +{ + print( + 'namespace required' . PHP_EOL ); + + exit; } -if (!$clitoris = @json_decode( - $clitoris->value -)) +if (empty($argv[3])) { - exit( - sprintf( - 'could not decode _CLITOR_IS_ of %s' . PHP_EOL, - $argv[2] - ) - ); + print( + 'destination file required' . PHP_EOL + ); + + exit; } -if ($clitoris->version !== '1.0.0') +if (file_exists($argv[3])) { - exit( - sprintf( - '_CLITOR_IS_ of %s not compatible!' . PHP_EOL, - $argv[2] - ) + print( + 'destination file already exists' . PHP_EOL ); + + exit; } -if (empty($clitoris->file->name)) +// Get clitoris +$clitoris = _exec( + $argv[1], + sprintf( + 'keva_get %s "_CLITOR_IS_"', + $argv[2] + ) +); + +// Validate protocol +if (empty($clitoris->value)) { - exit( - sprintf( - '_CLITOR_IS_ format issue for %s!' . PHP_EOL, - $argv[2] - ) + print( + '_CLITOR_IS_ not found' . PHP_EOL ); + + exit; } // Merge content data $chain = []; + foreach ( (array) _exec( $argv[1], sprintf( - '%s %s "\d+"', - 'keva_filter', + 'keva_filter %s "\d+"', $argv[2] ) ) as $piece) @@ -99,9 +114,11 @@ foreach ( !isset($piece->height) ) { - exit( - 'please wait for all pieces sending complete!' + print( + 'please wait for all pieces sending complete!' . PHP_EOL ); + + exit; } // Keep all key versions in memory @@ -121,12 +138,16 @@ $pieces = []; foreach ($chain as $key => $height) { - ksort( $height ); - $pieces[$key] = $height[array_key_last($height)]; + $pieces[$key] = $height + [ + array_key_last( + $height + ) + ]; } ksort( @@ -134,21 +155,15 @@ ksort( ); // Save file to destination -$filename = isset($argv[3]) ? $argv[3] : sprintf( - '%s/../data/import/[kevacoin][%s]%s', - __DIR__, - $argv[2], - $clitoris->file->name -); - file_put_contents( - $filename, + $argv[3], base64_decode( implode('', $pieces) ) ); -echo sprintf( - 'saved to %s' . PHP_EOL, - $filename +printf( + 'namespace "%s" saved to "%s"' . PHP_EOL, + $argv[2], + $argv[3] ); \ No newline at end of file diff --git a/cli/put.php b/cli/put.php index 2add995..eefb9c0 100644 --- a/cli/put.php +++ b/cli/put.php @@ -1,5 +1,11 @@ 255) +if (mb_strlen($basename) > 255) { - $name = $md5file; -} + // Get file hash sum + $basename = 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; + // Dump event + print( + 'Filename to long, used md5_file as key...' . PHP_EOL + ); +} +// Split content to smaller parts (according to the protocol limits) $pieces = str_split( base64_encode( file_get_contents( $argv[2] ) ), - $size + 3072 ); // Count total pieces @@ -63,67 +102,35 @@ $total = count( $pieces ); -// Get software protocol details -$software = _exec( - $argv[1], - '-getinfo' -); - -print_r($software); - // Create namespace to collect there data pieces $ns = _exec( $argv[1], sprintf( - "%s '%s'", - 'keva_namespace', - $name + "keva_namespace '%s'", + str_replace( + "'", + "\'", + $basename + ) ) ); -print_r($ns); +// Validate namespace created +if (empty($ns->namespaceId)) +{ + print( + 'could not create namespace record' . PHP_EOL + ); + + exit; +} -// Create meta description for the future generations +// Create meta record print_r( _exec( $argv[1], sprintf( - "%s %s '%s' '%s'", - 'keva_put', - $ns->namespaceId, - '_CLITOR_IS_', - json_encode( - [ - 'version' => '1.0.0', - 'model' => - [ - 'name' => 'kevacoin', - 'software' => - [ - 'version' => $software->version, - 'protocol' => $software->protocolversion - ] - ], - 'pieces' => - [ - 'total' => $total, - 'size' => $size, - ], - 'file' => - [ - 'name' => basename( - $argv[2] - ), - 'mime' => mime_content_type( - $argv[2] - ), - 'size' => filesize( - $argv[2] - ), - 'md5' => $md5file - ] - ] - ) + 'keva_put %s %s %s', $ns->namespaceId, '_CLITOR_IS_', _CLITOR_IS_ ) ) ); @@ -147,20 +154,28 @@ foreach ($pieces as $key => $value) // Apply delays to prevent too-long-mempool-chain reject $delay = isset($argv[4]) && $argv[4] > 0 ? (int) $argv[4] : 60; - echo sprintf( + printf( '%s/%s sent, waiting %s seconds...' . PHP_EOL, $key + 1, $total, $delay ); - sleep($delay); + sleep( + $delay + ); } -// Print result -echo sprintf( - 'done! run to extract: php %s/get.php %s %s' . PHP_EOL, +// Done +printf( + 'data successfully sent to namespace "%s"' . PHP_EOL, + $ns->namespaceId +); + +printf( + "run to extract: /usr/bin/php '%s/get.php' '%s' '%s' '/path/to/%s'" . PHP_EOL, __DIR__, $argv[1], - $ns->namespaceId + $ns->namespaceId, + $basename ); \ No newline at end of file