Continued with chash:
authorRoland Häder <roland@mxchange.org>
Sat, 2 Mar 2019 16:09:10 +0000 (17:09 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 2 Mar 2019 16:09:10 +0000 (17:09 +0100)
- fixed division-by-zero
- fixed restart code, need to decode JSON as associative array
- renamed constant
- new array style

Signed-off-by: Roland Häder <roland@mxchange.org>
contrib/chash/chash.php
contrib/chash/lib/functions.php

index 90dff03..ed583d9 100644 (file)
@@ -6,7 +6,7 @@ require 'lib/functions.php';
 require 'lib/scrypt.php';
 
 define('START_TIME'            , microtime(true));
-define('CHECK_POINT'           , 'chash.pos');
+define('CHECKPOINT_FILE'           , 'chash.pos');
 
 // Hashes needed to complete a "block"
 $GLOBALS['block_size']          = 100;
@@ -34,7 +34,7 @@ $GLOBALS['salt'] = Scrypt::generateSalt();
 $GLOBALS['difficulty'] = 2;
 
 // Found hashes
-$GLOBALS['found_hashes'] = array(0 => array());
+$GLOBALS['found_hashes'] = array([]);
 
 /**
  * Continued-hashing
@@ -45,7 +45,7 @@ $GLOBALS['found_hashes'] = array(0 => array());
  */
 
 // Is the check point there?
-if (is_readable(CHECK_POINT)) {
+if (is_readable(CHECKPOINT_FILE)) {
        // Load it
        loadCheckpointFile();
 } else {
@@ -193,7 +193,7 @@ while (true) {
                                print('total_restarts=' . $GLOBALS['total_restarts'] . ' - Restarting ...');
 
                                // Count all root (genesis) hashes
-                               $rootHashes = array();
+                               $rootHashes = [];
                                foreach ($GLOBALS['found_hashes'] as $block) {
                                        // "Walk" through all blocks
                                        foreach ($block as $hash) {
@@ -287,7 +287,7 @@ while (true) {
        $timeBlock = abs(microtime(true) - $timeBlock);
 
        // Calculate reward
-       $reward = abs($timeBlock - $timeBadHashes) / $hashRate * $GLOBALS['hashes_block'] / $GLOBALS['block_size'] * 1000;
+       $reward = abs($timeBlock - $timeBadHashes) / $hashRate * $GLOBALS['hashes_block'] / max(1, $GLOBALS['block_size']) * 1000;
        print('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $GLOBALS['hashes_block'] .',reward=' . $reward . PHP_EOL);
 
        // Double difficulty
@@ -299,7 +299,7 @@ while (true) {
        $GLOBALS['hashes_block'] = 0;
 
        // Init next block
-       $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = array();
+       $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = [];
 
        // Calculate new nonce
        calculateNonce();
index 8401a10..c56cd6f 100644 (file)
@@ -80,7 +80,7 @@ function flushCheckPointFile ($hash) {
 
        // Flush data
        file_put_contents(
-               CHECK_POINT,
+               CHECKPOINT_FILE,
                $GLOBALS['total_blocks'] . '|' .
                $GLOBALS['total_reward'] . '|' .
                $GLOBALS['total_hashes'] . '|' .
@@ -110,7 +110,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 +119,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);
@@ -165,9 +165,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 +187,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
 }