Continued:
[core.git] / contrib / chash / chash.php
index fe08cd3466e55fbde567c8e92ad87602cc81dc63..4911ba57036e2c22603c4abe0ccb738e22bd968d 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 {
@@ -61,8 +61,8 @@ if (is_readable(CHECK_POINT)) {
 $gensisHashes = array(
        // A famous quote from Deus Ex 2 - Invisible War
        multiplehashString('"Informations must be free." - AI Helios from Deus Ex'),
-       // My name + URL of my first StatusNet instance
-       multipleHashString('Roland Haeder, https://status.mxchange.org'),
+       // My name + URL of my GNUSocial instance
+       multipleHashString('Roland Haeder, https://social.mxchange.org'),
        // A famous quote from Linus Torwalds
        multipleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'),
        // Well ...
@@ -88,10 +88,10 @@ $genesisBlock = array(
 
 // Calulcate final "genesis" hash
 $genesisHash = hashString(
+       $genesisBlock[3] .
        $genesisBlock[0] .
        $genesisBlock[2] .
-       $genesisBlock[1] .
-       $genesisBlock[3]
+       $genesisBlock[1]
 );
 
 // Get all elements to get the last part out
@@ -122,7 +122,7 @@ print ('difficulty=' . $GLOBALS['difficulty'] . PHP_EOL);
 while (true) {
        // Init hash-per-block counter and hashrate
        $GLOBALS['hashes_block'] = 0;
-       $hashrate = 0;
+       $hashRate = 1;
 
        // Wait for block_size iterations (= found hashes). This is one block
        $timeBlock   = microtime(true);
@@ -159,17 +159,17 @@ while (true) {
                        $testTime = abs(microtime(true) - $timeDisplay);
 
                        // Calculate hashrate/sec
-                       $hashrate = 1 / $testTime * $GLOBALS['iteration_second'] * $GLOBALS['hash_cycles'];
+                       $hashRate = 1 / $testTime * $GLOBALS['iteration_second'] * $GLOBALS['hash_cycles'];
 
                        // Only every second
                        if ($testTime >= 1) {
                                // Display hash rate
-                               print ('hashrate=' . round($hashrate) . ' hashes/sec,iterSecond=' . $GLOBALS['iteration_second'] . ' iterations/sec,difficulty=' . $GLOBALS['difficulty'] . PHP_EOL);
+                               print ('hashrate=' . round($hashRate) . ' hashes/sec,iterSecond=' . $GLOBALS['iteration_second'] . ' iterations/sec,difficulty=' . $GLOBALS['difficulty'] . PHP_EOL);
 
                                // Reset timer
                                $timeDisplay = microtime(true);
                                $GLOBALS['iteration_second']  = 0;
-                       } // END - if
+                       }
 
                        // Time spend from last flush
                        $testTime = abs(microtime(true) - $GLOBALS['time_flush']);
@@ -178,7 +178,7 @@ while (true) {
                        if ($testTime >= $GLOBALS['flush_file_time']) {
                                // Flush check-point file
                                flushCheckPointFile($GLOBALS['current_hash']);
-                       } // END - if
+                       }
 
                        // Time spend from last found block
                        $testTime = abs(microtime(true) - $GLOBALS['found_time']);
@@ -193,14 +193,14 @@ 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) {
                                                if (!isset($hash['root_hash'])) {
                                                        // Bad file
                                                        die('INCONSISTENCY: hash=' . print_r($hash, true));
-                                               } // END - if
+                                               }
 
                                                if (isset($rootHashes[$hash['root_hash']])) {
                                                        // Count up
@@ -209,23 +209,21 @@ while (true) {
                                                        // First entry found
                                                        $rootHashes[$hash['root_hash']] = 1;
                                                }
-                                       } // END - foreach
-                               } // END - foreach
+                                       }
+                               }
 
                                // Find best root hash
                                $bestRootHash = '';
                                $bestRootCount = 0;
                                foreach ($rootHashes as $hash => $count) {
-                                       // Debug message
-                                       //* NOISY-DEBUG: */ print ('hash=' . $hash . ',count=' . $count . ',bestRootHash=' . $bestRootHash . ',bestRootCount=' . $bestRootCount . PHP_EOL);
-
                                        // Is a better one found?
+                                       //* NOISY-DEBUG: */ print ('hash=' . $hash . ',count=' . $count . ',bestRootHash=' . $bestRootHash . ',bestRootCount=' . $bestRootCount . PHP_EOL);
                                        if ($count > $bestRootCount) {
                                                // Remember it
                                                $bestRootHash  = $hash;
                                                $bestRootCount = $count;
-                                       } // END - if
-                               } // END - foreach
+                                       }
+                               }
 
                                // Output message
                                print ('bestRootHash=' . $bestRootHash . ',bestRootCount=' . $bestRootCount . PHP_EOL);
@@ -249,49 +247,50 @@ while (true) {
 
                                                        // Abort search
                                                        break;
-                                               } // END - if
-                                       } // END - for
-                               } // END - foreach
-                       } // END - if
+                                               }
+                                       }
+                               }
+                       }
 
                        // Next round
                        $GLOBALS['iteration']++;
                        $GLOBALS['iteration_second']++;
-                       //print ('nonce=' . $GLOBALS['nonce'] . ',iteration=' . $GLOBALS['iteration'] . PHP_EOL);
-                       //print ('nonceHash=' . $nonceHash . PHP_EOL);
-                       //print ('sumNonce=' . $sumNonce . PHP_EOL);
-                       //print ('sumGenesis=' . $GLOBALS['sum_genesis'] . PHP_EOL);
-               } // END - while
+                       //* NOISY-DEBUG: */ print('nonce=' . $GLOBALS['nonce'] . ',iteration=' . $GLOBALS['iteration'] . PHP_EOL);
+                       //* NOISY-DEBUG: */ print('nonceHash=' . $nonceHash . PHP_EOL);
+                       //* NOISY-DEBUG: */ print('sumNonce=' . $sumNonce . PHP_EOL);
+                       //* NOISY-DEBUG: */ print('sumGenesis=' . $GLOBALS['sum_genesis'] . PHP_EOL);
+               }
 
                // If the iteration is zero, then no hash is found
                if ($GLOBALS['iteration'] == 0) {
                        // Bad hash found
                        $timeBadHashes += abs(microtime(true) - $timeHash);
 
-                       // And next round
-                       print('BAD:nonce=' . $GLOBALS['nonce'] . PHP_EOL);
-
                        // Nothing found, so calculate new nonce
+                       //* NOISY-DEBUG: */ print('BAD:nonce=' . $GLOBALS['nonce'] . PHP_EOL);
                        calculateNonce();
                        continue;
-               } // END - if
+               }
 
                // Add amount of hashes per block (multiple-hash)
                $GLOBALS['hashes_block'] += $GLOBALS['iteration'] * $GLOBALS['hash_cycles'] + $GLOBALS['hash_cycles'];
 
                // Push found hash
                addFoundHash($nonceHash);
-       } // END - while
+       }
+
+       // Flush check-point file
+       flushCheckPointFile($GLOBALS['current_hash']);
 
        // Time taken for one
        $timeBlock = abs(microtime(true) - $timeBlock);
 
        // Calculate reward
-       $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $GLOBALS['hashes_block'] / $GLOBALS['block_size'] * 1000;
-       print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $GLOBALS['hashes_block'] .',reward=' . $reward . PHP_EOL);
+       $reward = abs($timeBlock - $timeBadHashes) / max(1, $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
-       $GLOBALS['difficulty'] = $GLOBALS['difficulty'] * 2;
+       // Increase difficulty
+       $GLOBALS['difficulty'] = $GLOBALS['difficulty']++;
 
        // Block completed
        $GLOBALS['total_hashes'] += $GLOBALS['hashes_block'];
@@ -299,7 +298,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();
@@ -314,4 +313,4 @@ while (true) {
        $rewardPerHour = $GLOBALS['total_reward'] / abs(microtime(true) - START_TIME) * 3600;
 
        print ('totalReward=' . $GLOBALS['total_reward'] . ',blockValue=' . $blockValue . ',rewardPerHour=' . $rewardPerHour . PHP_EOL);
-} // END - while
+}