From: Roland Häder Date: Sun, 15 Nov 2020 01:51:35 +0000 (+0100) Subject: Continued with chash: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=05df7f70f0502385f1d80a027afa4792bfb372a9 Continued with chash: - fixed hexdec() errors, scrypt returns non-hexadecimal characters, too - fixed division by zero Signed-off-by: Roland Häder --- diff --git a/contrib/chash/chash.php b/contrib/chash/chash.php index ed583d99..e04f03a4 100644 --- a/contrib/chash/chash.php +++ b/contrib/chash/chash.php @@ -257,10 +257,10 @@ while (true) { // 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); + //* 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); } // END - while // If the iteration is zero, then no hash is found @@ -269,7 +269,7 @@ while (true) { $timeBadHashes += abs(microtime(true) - $timeHash); // And next round - print('BAD:nonce=' . $GLOBALS['nonce'] . PHP_EOL); + //* NOISY-DEBUG: */ print('BAD:nonce=' . $GLOBALS['nonce'] . PHP_EOL); // Nothing found, so calculate new nonce calculateNonce(); @@ -287,7 +287,7 @@ while (true) { $timeBlock = abs(microtime(true) - $timeBlock); // Calculate reward - $reward = abs($timeBlock - $timeBadHashes) / $hashRate * $GLOBALS['hashes_block'] / max(1, $GLOBALS['block_size']) * 1000; + $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 diff --git a/contrib/chash/lib/functions.php b/contrib/chash/lib/functions.php index c56cd6f0..c4a9433b 100644 --- a/contrib/chash/lib/functions.php +++ b/contrib/chash/lib/functions.php @@ -43,15 +43,22 @@ function multipleHashString ($str) { */ function calculateSumFromHash ($hash) { // Everything starts with zero ... + //* NOISY-DEBUG: */ printf('[%s:%d]: hash(%d)=%s - CALLED!'. PHP_EOL, __FUNCTION__, __LINE__, strlen($hash), $hash); $sum = 0; + // Part of the hash is not decodeable + $decodeA = explode('$', $hash); + $decode = $decodeA[4]; + // Loop through hash - for ($idx = 0; $idx < (strlen($hash) / 2); $idx++) { + //* NOISY-DEBUG: */ printf('[%s:%d]: decode=%s' . PHP_EOL, __FUNCTION__, __LINE__, $decode); + for ($idx = 0; $idx < (strlen($decode) / 2); $idx++) { // And add it - $sum = $sum + hexdec(substr($hash, $idx, 2)); + $sum = $sum + hexdec(substr($decode, $idx, 2)); } // END - for // And return it + //* NOISY-DEBUG: */ printf('[%s:%d]: sum=%d - EXIT!' . PHP_EOL, __FUNCTION__, __LINE__, $sum); return $sum; }