Continued:
[core.git] / contrib / chash / lib / functions.php
index 8401a1084772b08252b2376aa54e03cbf44bf66d..fe05fd3820f9677c56bdcf9a66c0f16fb00724e5 100644 (file)
@@ -29,7 +29,7 @@ function multipleHashString ($str) {
        for ($idx = 0; $idx < ($GLOBALS['hash_cycles'] - 1); $idx++) {
                // Over-hash the given hash
                $hash = hashString($hash);
-       } // END - for
+       }
 
        // Return it
        return $hash;
@@ -43,15 +43,22 @@ function multipleHashString ($str) {
  */
 function calculateSumFromHash ($hash) {
        // Everything starts with zero ...
+       //* NOISY-DEBUG: */ printf('[%s:%d]: hash(%d)=%s - CALLED!'. PHP_EOL, __FUNCTION__, __LINE__, strlen($hash), $hash);
        $sum = 0;
 
+       // Part of the hash is not decodeable
+       $decodeA = explode('$', $hash);
+       $decode = $decodeA[4];
+
        // Loop through hash
-       for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) {
+       //* NOISY-DEBUG: */ printf('[%s:%d]: decode=%s' . PHP_EOL, __FUNCTION__, __LINE__, $decode);
+       for ($idx = 0; $idx < (strlen($decode) / 2); $idx++) {
                // And add it
-               $sum = $sum + hexdec(substr($hash, $idx, 2));
-       } // END - for
+               $sum = $sum + hexdec(substr($decode, $idx, 2));
+       }
 
        // And return it
+       //* NOISY-DEBUG: */ printf('[%s:%d]: sum=%d - EXIT!' . PHP_EOL, __FUNCTION__, __LINE__, $sum);
        return $sum;
 }
 
@@ -80,19 +87,34 @@ function flushCheckPointFile ($hash) {
 
        // Flush data
        file_put_contents(
-               CHECK_POINT,
-               $GLOBALS['total_blocks'] . '|' .
-               $GLOBALS['total_reward'] . '|' .
-               $GLOBALS['total_hashes'] . '|' .
-               $GLOBALS['total_found'] . '|' .
-               $GLOBALS['total_restarts'] . '|' .
-               $GLOBALS['hash_cycles'] . '|' .
-               $GLOBALS['salt'] . '|' .
-               $GLOBALS['difficulty'] . '|' .
-               base64_encode((float) $GLOBALS['nonce']) . '|' .
-               $hash . '|' .
-               $GLOBALS['root_hash'] . '|' .
-               base64_encode(gzcompress(json_encode($GLOBALS['found_hashes'])))
+               CHECKPOINT_FILE,
+               //        0  1  2  3  4  5  6  7  8  9 10 11
+               sprintf('%d|%s|%d|%d|%d|%d|%s|%d|%s|%s|%s|%s',
+                       // 0
+                       $GLOBALS['total_blocks'],
+                       // 1
+                       $GLOBALS['total_reward'],
+                       // 2
+                       $GLOBALS['total_hashes'],
+                       // 3
+                       $GLOBALS['total_found'],
+                       // 4
+                       $GLOBALS['total_restarts'],
+                       // 5
+                       $GLOBALS['hash_cycles'],
+                       // 6
+                       $GLOBALS['salt'],
+                       // 7
+                       $GLOBALS['difficulty'],
+                       // 8
+                       base64_encode((float) $GLOBALS['nonce']),
+                       // 9
+                       $hash,
+                       // 10
+                       $GLOBALS['root_hash'],
+                       // 11
+                       base64_encode(gzcompress(json_encode($GLOBALS['found_hashes'])))
+               )
        );
 
        // Set time
@@ -110,7 +132,7 @@ function addFoundHash ($hash) {
        $GLOBALS['total_found']++;
 
        // Add hash to array
-       array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], array(
+       array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], [
                'current_hash' => $GLOBALS['current_hash'],
                'root_hash'    => $GLOBALS['root_hash'],
                'nonce'        => (float) $GLOBALS['nonce'],
@@ -119,16 +141,13 @@ function addFoundHash ($hash) {
                'hash_cycles'  => $GLOBALS['hash_cycles'],
                'difficulty'   => $GLOBALS['difficulty'],
                'nonce_hash'   => $hash,
-       ));
+       ]);
 
        // Found hash:
-       print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',total_found=' . $GLOBALS['total_found'] . PHP_EOL);
+       //* NOISY-DEBUG: */ print ('FOUND: hash=' . $hash . ',nonce=' . $GLOBALS['nonce'] . ',total_found=' . $GLOBALS['total_found'] . PHP_EOL);
 
        // Set time as a new hash was found
        $GLOBALS['found_time'] = microtime(true);
-
-       // Flush check-point file after new hash is found
-       flushCheckPointFile($hash);
 }
 
 /**
@@ -165,9 +184,9 @@ function sumHash ($hash) {
  */
 function loadCheckpointFile () {
        // Is the check point there?
-       if (is_readable(CHECK_POINT)) {
+       if (is_readable(CHECKPOINT_FILE)) {
                // Then load it
-               $checkPoint = file_get_contents(CHECK_POINT);
+               $checkPoint = file_get_contents(CHECKPOINT_FILE);
 
                // Explode it
                $data = explode('|', $checkPoint);
@@ -186,7 +205,7 @@ function loadCheckpointFile () {
                $GLOBALS['difficulty']     = $data[7];
                $GLOBALS['nonce']          = (float) base64_decode($data[8]);
                $GLOBALS['current_hash']   = $data[9];
-               $GLOBALS['root_hash']      = $data[9];
-               $GLOBALS['found_hashes']   = json_decode(gzuncompress(base64_decode($data[11])));
-       } // END - if
+               $GLOBALS['root_hash']      = $data[10];
+               $GLOBALS['found_hashes']   = json_decode(gzuncompress(base64_decode($data[11])), TRUE);
+       }
 }