]> git.mxchange.org Git - core.git/blobdiff - contrib/chash/chash.php
Don't increment nonce after block was found, some other improvements, nonce increment...
[core.git] / contrib / chash / chash.php
index 7101f3dcdeefffdb4af5f9b353193c123b8476e7..d41a45ba213a2cbd755410665b568e7466a14eb5 100644 (file)
@@ -3,7 +3,7 @@ error_reporting(E_ALL | E_STRICT);
 
 define('HASH_ALGO', MHASH_RIPEMD320);
 define('BLOCK_SIZE', 1000);
-define('NONCE_INCREMENT', 0.1);
+define('NONCE_INCREMENT', 1.0);
 define('START_TIME', microtime(TRUE));
 
 /**
@@ -181,6 +181,17 @@ function calculateNonce ($nonce) {
        return $newNonce;
 }
 
+/**
+ * Calculates current block
+ *
+ * @return     $blockSize      Block size
+ * @todo       This needs improvement
+ */
+function getBlockSize () {
+       // For now only a constant block size
+       return BLOCK_SIZE;
+}
+
 /*
  * Calculate "genesis" hashes, please note that these "genesis strings" are now
  * known to the public as you can read them here in source code and therefore I
@@ -241,8 +252,9 @@ $totalBlocks = 0;
 $nonce = 1 / mt_rand();
 
 while (TRUE) {
-       // Init hash-per-block counter
+       // Init hash-per-block counter and hashrate
        $hashesPerBlock = 0;
+       $hashrate = 0;
 
        // Wait for BLOCK_SIZE iterations (= found hashes). This is one block
        $timeBlock = microtime(TRUE);
@@ -251,7 +263,7 @@ while (TRUE) {
        // Time waited for a good block again (no iteration)
        $timeBadHashes = 0;
 
-       while ($hashesPerBlock <= BLOCK_SIZE) {
+       while ($hashesPerBlock <= getBlockSize()) {
                // Init counter
                $iter = 0;
 
@@ -321,8 +333,14 @@ while (TRUE) {
        //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;
+       $reward = abs($timeBlock - $timeBadHashes) / $hashrate * $hashesPerBlock / getBlockSize() * 1000;
        print ('timeBlock=' . $timeBlock . ',timeBadHashes=' . $timeBadHashes . ',hashesPerBlock=' . $hashesPerBlock .',reward=' . $reward . PHP_EOL);
 
        // Block completed
@@ -330,17 +348,14 @@ while (TRUE) {
        $totalBlocks++;
        $hashesPerBlock = 0;
 
-       // Calculate next hash from both hashes
-       $modulaHash = doubleHashString(modulaHash($nonceHash, $modulaHash));
-
-       // Calculate new nonce
-       $nonce = calculateNonce($nonce);
+       // Use nonceHash as next modula hash
+       $modulaHash = $nonceHash;
 
        // Add reward to total
        $totalReward += $reward;
 
        // Calculate average block value
-       $blockValue = $totalReward / $totalBlocks * $totalHashes / (BLOCK_SIZE * $totalBlocks);
+       $blockValue = $totalReward / $totalBlocks * $totalHashes / (getBlockSize() * $totalBlocks);
 
        // Calculate reward per hour (= 3600 seconds)
        $rewardPerHour = $totalReward / abs(microtime(TRUE) - START_TIME) * 3600;