Continued with chash:
[core.git] / contrib / chash / lib / functions.php
index b6bbc079d40e9b8f363ddd60a5855beb504515c0..c4a9433b22ec3de9a6f7f0aa1a07e6bb59c7757c 100644 (file)
@@ -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));
+               $sum = $sum + hexdec(substr($decode, $idx, 2));
        } // END - for
 
        // And return it
+       //* NOISY-DEBUG: */ printf('[%s:%d]: sum=%d - EXIT!' . PHP_EOL, __FUNCTION__, __LINE__, $sum);
        return $sum;
 }
 
@@ -80,7 +87,7 @@ function flushCheckPointFile ($hash) {
 
        // Flush data
        file_put_contents(
-               CHECK_POINT,
+               CHECKPOINT_FILE,
                $GLOBALS['total_blocks'] . '|' .
                $GLOBALS['total_reward'] . '|' .
                $GLOBALS['total_hashes'] . '|' .
@@ -110,7 +117,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,7 +126,7 @@ 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);
@@ -137,7 +144,7 @@ function addFoundHash ($hash) {
  * @return     void
  */
 function initNonce () {
-       $GLOBALS['nonce'] = 1 / (mt_rand() ^ pi());
+       $GLOBALS['nonce'] = 1 / (mt_rand() ^ (1 / pi()));
        print (__FUNCTION__ . ': nonce=' . $GLOBALS['nonce'] . PHP_EOL);
 }
 
@@ -165,9 +172,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);
@@ -187,6 +194,6 @@ function loadCheckpointFile () {
                $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])));
+               $GLOBALS['found_hashes']   = json_decode(gzuncompress(base64_decode($data[11])), TRUE);
        } // END - if
 }