Much smaller nonce means much more to calculate + logged it on success (found hash).
[core.git] / contrib / chash / chash.php
index 6dc0eff3dc0002334bd72029155aef13ab4a445e..6705b960e81887b67cc17d2974c6599fb32a26c8 100644 (file)
@@ -2,10 +2,13 @@
 error_reporting(E_ALL | E_STRICT);
 
 define('HASH_ALGO', MHASH_RIPEMD320);
-define('BLOCK_SIZE', 1000);
-define('NONCE_INCREMENT', 0.00001);
+define('BLOCK_SIZE', 100);
+define('NONCE_INCREMENT', 0.0000000000000001);
 define('START_TIME', microtime(TRUE));
 
+// Found hashes
+$foundHashes = array();
+
 /**
  * Continued-hashing
  *
@@ -252,7 +255,7 @@ while (TRUE) {
        // Time waited for a good block again (no iteration)
        $timeBadHashes = 0;
 
-       while ($hashesPerBlock <= BLOCK_SIZE) {
+       while (count($foundHashes) <= BLOCK_SIZE) {
                // Create hash from modulaHash ("genesis hash") and nonce
                $nonceHash = doubleHashString($modulaHash . $nonce);
 
@@ -262,6 +265,7 @@ while (TRUE) {
 
                // Init counter
                $iter = 0;
+               $iterSecond = 0;
 
                // Now start the "mining" ...
                $timeHash = microtime(TRUE);
@@ -274,49 +278,51 @@ while (TRUE) {
 
                        // Calculate sums
                        $sumNonce  = calculateSumFromHash($nonceHash);
-                       //print('hashesPerBlock=' . $hashesPerBlock . PHP_EOL);
 
                        // Time spend in loop
                        $testTime = abs(microtime(TRUE) - $timeDisplay);
 
                        // Calculate hashrate/sec
-                       $hashrate = 1 / $testTime * $iter * 2 + 2;
+                       $hashrate = 1 / $testTime * $iterSecond * 2;
 
                        // Only every second
                        if ($testTime >= 1) {
                                // Display hash rate
-                               print ('hashesPerBlock=' . $hashesPerBlock . ',hashrate=' . $hashrate . ' hashes/sec.' . PHP_EOL);
+                               print ('hashrate=' . $hashrate . ' hashes/sec,iterSecond=' . $iterSecond . ' iterations/sec' . PHP_EOL);
 
                                // Reset timer
                                $timeDisplay = microtime(TRUE);
+                               $iterSecond  = 0;
                        } // END - if
 
                        // Next round
                        $iter++;
+                       $iterSecond++;
                        //print ('nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
                        //print ('nonceHash=' . $nonceHash . PHP_EOL);
                        //print ('sumNonce=' . $sumNonce . PHP_EOL);
                        //print ('sumModula=' . $sumModula . PHP_EOL);
                } // END - while
 
-               // Add amount of hashes to block (double-hash)
-               $hashesPerBlock += $iter * 2 + 2;
-
                // If the iteration is zero, then no hash is found
                if ($iter == 0) {
-                       // Bad block found
+                       // Bad hash found
                        $timeBadHashes += abs(microtime(TRUE) - $timeHash);
 
                        // And next round
-                       //print('bad:nonce=' . $nonce . PHP_EOL);
+                       //print('BAD:nonce=' . $nonce . PHP_EOL);
 
                        // Nothing found, so calculate new nonce
                        $nonce = calculateNonce($nonce);
                        continue;
                } // END - if
 
+               // Add amount of hashes per block (double-hash)
+               $hashesPerBlock += $iter * 2 + 2;
+               array_push($foundHashes, $nonceHash);
+
                // Found hash:
-               //print ('nonceHash=' . $nonceHash .',iter=' . $iter . PHP_EOL);
+               print ('FOUND: nonceHash=' . $nonceHash .',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
 
                // Use nonceHash as next modula hash
                $modulaHash = $nonceHash;
@@ -324,17 +330,16 @@ while (TRUE) {
 
        // Time taken for one block
        $timeBlock = abs(microtime(TRUE) - $timeBlock);
-       //print ('calculateSumFromHash(modulaHash)=' . calculateSumFromHash($modulaHash) . PHP_EOL);
-       //print ('calculateSumFromHash(nonceHash)=' . calculateSumFromHash($nonceHash) . PHP_EOL);
 
        // Calculate reward
        $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / BLOCK_SIZE * 1000;
-       //print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
+       print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
 
        // Block completed
        $totalHashes += $hashesPerBlock;
        $totalBlocks++;
        $hashesPerBlock = 0;
+       $foundHashes = array();
 
        // Calculate new nonce
        $nonce = calculateNonce($nonce);