Taken care of possible usage of uninitialized variable $hashrate.
[core.git] / contrib / chash / chash.php
index 7101f3dcdeefffdb4af5f9b353193c123b8476e7..fe490dbba2018f3dc85aade908aaf42c27e136bf 100644 (file)
@@ -241,8 +241,9 @@ $totalBlocks = 0;
 $nonce = 1 / mt_rand();
 
 while (TRUE) {
 $nonce = 1 / mt_rand();
 
 while (TRUE) {
-       // Init hash-per-block counter
+       // Init hash-per-block counter and hashrate
        $hashesPerBlock = 0;
        $hashesPerBlock = 0;
+       $hashrate = 0;
 
        // Wait for BLOCK_SIZE iterations (= found hashes). This is one block
        $timeBlock = microtime(TRUE);
 
        // Wait for BLOCK_SIZE iterations (= found hashes). This is one block
        $timeBlock = microtime(TRUE);
@@ -321,6 +322,12 @@ while (TRUE) {
        //print ('calculateSumFromHash(modulaHash)=' . calculateSumFromHash($modulaHash) . PHP_EOL);
        //print ('calculateSumFromHash(nonceHash)=' . calculateSumFromHash($nonceHash) . PHP_EOL);
 
        //print ('calculateSumFromHash(modulaHash)=' . calculateSumFromHash($modulaHash) . PHP_EOL);
        //print ('calculateSumFromHash(nonceHash)=' . calculateSumFromHash($nonceHash) . PHP_EOL);
 
+       // Is the hash rate unset?
+       if ($hashrate == 0) {
+               // Then calculate it again
+               $hashrate = 1 / $timeBlock * $iter * 2 + 2;
+       } // END - if
+
        // Calculate reward
        $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000;
        print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
        // Calculate reward
        $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000;
        print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
@@ -330,8 +337,8 @@ while (TRUE) {
        $totalBlocks++;
        $hashesPerBlock = 0;
 
        $totalBlocks++;
        $hashesPerBlock = 0;
 
-       // Calculate next hash from both hashes
-       $modulaHash = doubleHashString(modulaHash($nonceHash, $modulaHash));
+       // Use nonceHash as next modula hash
+       $modulaHash = $nonceHash;
 
        // Calculate new nonce
        $nonce = calculateNonce($nonce);
 
        // Calculate new nonce
        $nonce = calculateNonce($nonce);