X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=contrib%2Fchash%2Fchash.php;h=ed9265f38afb3585d976d65106998c3b780650ee;hp=fe490dbba2018f3dc85aade908aaf42c27e136bf;hb=cc6118d8436018bcba7d705aa6a99ca4dcdee6dd;hpb=6abb05749348fe89e45c9a69cfec0c3e50040358 diff --git a/contrib/chash/chash.php b/contrib/chash/chash.php index fe490dbb..ed9265f3 100644 --- a/contrib/chash/chash.php +++ b/contrib/chash/chash.php @@ -1,10 +1,17 @@ array()); /** * Continued-hashing @@ -30,15 +37,24 @@ function hashString ($str) { } /** - * Double-hashes given string. This is done by hashing the given string and + * Multiple-hashes given string. This is done by hashing the given string and * then hashing the generated hash again. * * @param $str The string to be hashed 4 times * @return $hash The generated hash */ -function doubleHashString ($str) { +function multipleHashString ($str) { + // One less to go (see below) + $totalHashes = $GLOBALS['cycles'] - 1; + // Generate hash from given hash - $hash = hashString(hashString($str)); + $hash = hashString($str); + + // Now over-hash it + for ($idx = 0; $idx < $totalHashes; $idx++) { + // Over-hash the given hash + $hash = hashString($hash); + } // END - for // Return it return $hash; @@ -181,40 +197,62 @@ function calculateNonce ($nonce) { return $newNonce; } +/** + * Writes/flushes check-point file + * + * @param $nonce Nonce + * @param $modulaHash Modula hash (or hash to save) + * @return void + */ +function flushCheckPointFile ($nonce, $modulaHash) { + // Display message + print ('FLUSHING: Writing ' . count($GLOBALS['found_hashes']) . ' blocks ...' . PHP_EOL); + + // Start timer + $timer = microtime(TRUE); + + // Flush data + file_put_contents(CHECK_POINT, $GLOBALS['total_blocks'] . ':' . $GLOBALS['total_hashes'] . ':' . $GLOBALS['cycles'] . ':' . base64_encode($nonce) . ':' . $modulaHash . ':' . base64_encode(gzcompress(serialize($GLOBALS['found_hashes'])))); + + // Set time + $GLOBALS['time_flush'] = microtime(TRUE); + print ('FLUSHING: Took ' . ($GLOBALS['time_flush'] - $timer) . ' seconds.' . PHP_EOL); +} + /* * 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 * will not use them for the real genesis hashes. */ -$hashes = array( +$gensisHashes = array( // A famous quote from Deus Ex 2 - Invisible War - doublehashString('"Informations must be free." - AI Helios from Deus Ex'), + multiplehashString('"Informations must be free." - AI Helios from Deus Ex'), // My name + URL of my first StatusNet instance - doubleHashString('Roland Haeder, https://status.mxchange.org'), + multipleHashString('Roland Haeder, https://status.mxchange.org'), // A famous quote from Linus Torwalds - doubleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'), + multipleHashString('"Software is like sex. Its better when its free." - Linus Torwalds'), // Possible truth ;-) - doubleHashString('September 11 is a big lie.'), + multipleHashString('September 11 is a big lie.'), // GNU is not Uni* - doubleHashString('GNU is Not Uni*.'), + multipleHashString('GNU is Not Uni*.'), // WINE is not an emulator - doubleHashString('WINE Is Not an Emulator.'), + multipleHashString('WINE Is Not an Emulator.'), // FlightGear - Fly free! - doubleHashString('FlightGear - Fly free!'), + multipleHashString('FlightGear - Fly free!'), // Linus Torwalds Quote - doubleHashString('Your code is shit.. your argument is shit.'), + multipleHashString('Your code is shit.. your argument is shit.'), ); // Calculate "modula hash" from 1st/4th and 2nd/3rd $modulaHashes = array( // "Block" 0 - modulaHash($hashes[0], $hashes[3]), - modulaHash($hashes[1], $hashes[2]), + modulaHash($gensisHashes[0], $gensisHashes[3]), + modulaHash($gensisHashes[1], $gensisHashes[2]), // "Block" 1 - modulaHash($hashes[4], $hashes[7]), - modulaHash($hashes[5], $hashes[6]), + modulaHash($gensisHashes[4], $gensisHashes[7]), + modulaHash($gensisHashes[5], $gensisHashes[6]), ); // Calculate "sqrt hash" @@ -224,60 +262,116 @@ $sqrtHashes = array( ); // Calulcate modula hash -$modulaHash = doubleHashString(modulaHash($sqrtHashes[0], $sqrtHashes[1])); +$modulaHash = multipleHashString(modulaHash($sqrtHashes[0], $sqrtHashes[1])); + +// This is also the "genesis hash" +$genesisHash = $modulaHash; // Output results -print ('hashes=' . print_r($hashes, TRUE)); +print ('hashes=' . print_r($gensisHashes, TRUE)); print ('modulaHashes=' . print_r($modulaHashes, TRUE)); print ('sqrtHashes=' . print_r($sqrtHashes, TRUE)); print ('modulaHash=' . $modulaHash . PHP_EOL); // Total reward + hashes $totalReward = 0; -$totalHashes = 0; -$totalBlocks = 0; +$GLOBALS['total_hashes'] = 0; +$GLOBALS['total_blocks'] = 0; + +// Is the check point there? +if (is_readable(CHECK_POINT)) { + // Then load it + $checkPoint = file_get_contents(CHECK_POINT); + + // Explode it + $data = explode(':', $checkPoint); + + // Assert on count + assert(count($data) == 6); + + // 1st element is nonce, 2nd hash, 3rd found hashes + $GLOBALS['total_blocks'] = $data[0]; + $GLOBALS['total_hashes'] = $data[1]; + $GLOBALS['cycles'] = intval($data[2]); + $nonce = base64_decode($data[3]); + $modulaHash = $data[4]; + $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = unserialize(gzuncompress(base64_decode($data[5]))); +} else { + // Create nonce (small) + $nonce = 1 / mt_rand(); +} -// Create nonce (small) -$nonce = 1 / mt_rand(); +// Output again +print ('modulaHash=' . $modulaHash . PHP_EOL); +print ('nonce=' . $nonce . PHP_EOL); +print ('found=' . count($GLOBALS['found_hashes'][$GLOBALS['total_blocks']]) . PHP_EOL); +// Start "mining" while (TRUE) { // 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); + $timeBlock = microtime(TRUE); $timeDisplay = $timeBlock; + $GLOBALS['time_flush'] = $timeBlock; // Time waited for a good block again (no iteration) $timeBadHashes = 0; - while ($hashesPerBlock <= BLOCK_SIZE) { - // Init counter - $iter = 0; - + while (count($GLOBALS['found_hashes'][$GLOBALS['total_blocks']]) <= BLOCK_SIZE) { // Create hash from modulaHash ("genesis hash") and nonce - $nonceHash = doubleHashString($modulaHash . $nonce); + $nonceHash = multipleHashString($modulaHash . $nonce); // Calculate sums $sumNonce = calculateSumFromHash($nonceHash); $sumModula = calculateSumFromHash($modulaHash); + // Init counter + $iter = 0; + $iterSecond = 0; + // Now start the "mining" ... $timeHash = microtime(TRUE); - while ($sumNonce <= $sumModula) { - // Calculate sums - $sumNonce = calculateSumFromHash($nonceHash); - $sumModula = calculateSumFromHash($modulaHash); - + while ($sumNonce >= $sumModula) { // Calculate new nonce $nonce = calculateNonce($nonce); // And hash again - $nonceHash = doubleHashString($modulaHash . $nonce); + $nonceHash = multipleHashString($modulaHash . $nonce); + + // Calculate sums + $sumNonce = calculateSumFromHash($nonceHash); + + // Time spend in loop + $testTime = abs(microtime(TRUE) - $timeDisplay); + + // Calculate hashrate/sec + $hashrate = 1 / $testTime * $iterSecond * $GLOBALS['cycles']; + + // Only every second + if ($testTime >= 1) { + // Display hash rate + print ('hashrate=' . round($hashrate) . ' hashes/sec,iterSecond=' . $iterSecond . ' iterations/sec' . PHP_EOL); + + // Reset timer + $timeDisplay = microtime(TRUE); + $iterSecond = 0; + } // END - if + + // Time spend from last flush + $testTime = abs(microtime(TRUE) - $GLOBALS['time_flush']); + + // Only once per 10 seconds + if ($testTime >= 10) { + // Flush check-point file + flushCheckPointFile($nonce, $modulaHash); + } // END - if // Next round $iter++; + $iterSecond++; //print ('nonce=' . $nonce . ',iter=' . $iter . PHP_EOL); //print ('nonceHash=' . $nonceHash . PHP_EOL); //print ('sumNonce=' . $sumNonce . PHP_EOL); @@ -286,59 +380,54 @@ while (TRUE) { // 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); + // Nothing found, so calculate new nonce $nonce = calculateNonce($nonce); - - // And next round continue; } // END - if - // Add amount of hashes to block (double-hash) - $hashesPerBlock += $iter * 2 + 2; + // Add amount of hashes per block (multiple-hash) + $hashesPerBlock += $iter * $GLOBALS['cycles'] + $GLOBALS['cycles']; - // Time spend in loop - $testTime = abs(microtime(TRUE) - $timeDisplay); + // Push found hash + array_push($GLOBALS['found_hashes'][$GLOBALS['total_blocks']], array( + 'modula_hash' => $modulaHash, + 'genesis_hash' => $genesisHash, + 'nonce' => $nonce, + 'iter' => $iter, + 'hashes_block' => $hashesPerBlock, + 'nonce_hash' => $nonceHash + )); - // Only every second - if ($testTime >= 1) { - // Calculate hashrate/sec - $timeHash = abs(microtime(TRUE) - $timeHash); - $hashrate = 1 / $timeHash * $iter * 2 + 2; - print ('hashesPerBlock=' . $hashesPerBlock . ',hashrate=' . $hashrate . ' hashes/sec.' . PHP_EOL); + // Found hash: + print ('FOUND: nonceHash=' . $nonceHash . ',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL); - // Reset timer - $timeDisplay = microtime(TRUE); - } // END - if + // Flush check-point file after new hash is found + flushCheckPointFile($nonce, $nonceHash); - // Found hash: - //print ('nonceHash=' . $nonceHash .',iter=' . $iter . PHP_EOL); + // Use nonceHash as next modula hash + $modulaHash = $nonceHash; } // END - while // Time taken for one block $timeBlock = abs(microtime(TRUE) - $timeBlock); - //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); // Block completed - $totalHashes += $hashesPerBlock; - $totalBlocks++; + $GLOBALS['total_hashes'] += $hashesPerBlock; + $GLOBALS['total_blocks']++; $hashesPerBlock = 0; - // Use nonceHash as next modula hash - $modulaHash = $nonceHash; + // Init next block + $GLOBALS['found_hashes'][$GLOBALS['total_blocks']] = array(); // Calculate new nonce $nonce = calculateNonce($nonce); @@ -347,7 +436,7 @@ while (TRUE) { $totalReward += $reward; // Calculate average block value - $blockValue = $totalReward / $totalBlocks * $totalHashes / (BLOCK_SIZE * $totalBlocks); + $blockValue = $totalReward / $GLOBALS['total_blocks'] * $GLOBALS['total_hashes'] / (BLOCK_SIZE * $GLOBALS['total_blocks']); // Calculate reward per hour (= 3600 seconds) $rewardPerHour = $totalReward / abs(microtime(TRUE) - START_TIME) * 3600;