+ $GLOBALS['iteration_second'] = 0;
+ } // END - if
+
+ // Time spend from last flush
+ $testTime = abs(microtime(TRUE) - $GLOBALS['time_flush']);
+
+ // Only once per 10 seconds
+ if ($testTime >= $GLOBALS['flush_file_time']) {
+ // Flush check-point file
+ flushCheckPointFile($GLOBALS['modula_hash']);
+ } // END - if
+
+ // Time spend from last found block
+ $testTime = abs(microtime(TRUE) - $GLOBALS['found_time']);
+
+ // Is the last found time to far away?
+ if ($testTime >= $GLOBALS['restart_search_time']) {
+ // Count up restart
+ $GLOBALS['total_restarts']++;
+
+ // Output message
+ print('total_restarts=' . $GLOBALS['total_restarts'] . ' - Restarting ...');
+
+ // Count all root (genesis) hashes
+ $rootHashes = array();
+ foreach ($GLOBALS['found_hashes'] as $block) {
+ // "Walk" through all blocks
+ foreach ($block as $hash) {
+ if (!isset($hash['root_hash'])) {
+ // Bad file
+ die('INCONSISTENCY: hash=' . print_r($hash, TRUE));
+ } // END - if
+
+ if (isset($rootHashes[$hash['root_hash']])) {
+ // Count up
+ $rootHashes[$hash['root_hash']]++;
+ } else {
+ // First entry found
+ $rootHashes[$hash['root_hash']] = 1;
+ }
+ } // END - foreach
+ } // END - foreach
+
+ // Find best root hash
+ $bestRootHash = '';
+ $bestRootCount = 0;
+ foreach ($rootHashes as $hash => $count) {
+ // Debug message
+ //* NOISY-DEBUG: */ print ('hash=' . $hash . ',count=' . $count . ',bestRootHash=' . $bestRootHash . ',bestRootCount=' . $bestRootCount . PHP_EOL);
+
+ // Is a better one found?
+ if ($count > $bestRootCount) {
+ // Remember it
+ $bestRootHash = $hash;
+ $bestRootCount = $count;
+ } // END - if
+ } // END - foreach
+
+ // Output message
+ print ('bestRootHash=' . $bestRootHash . ',bestRootCount=' . $bestRootCount . PHP_EOL);
+
+ // Search for latest best root hash
+ foreach ($GLOBALS['found_hashes'] as $block) {
+ // "Walk" through whole block and search for first appearance of best root hash
+ foreach ($block as $idx => $hash) {
+ // Is the root hash there?
+ if ($hash['root_hash'] == $bestRootHash) {
+ // Set found modula hash as new root and current modula hash
+ $GLOBALS['root_hash'] = $hash['nonce_hash'];
+ setModulaHash($hash['nonce_hash']);
+ print ('idx=' . $idx . ',modulaHash=' . $GLOBALS['root_hash'] . ' - Is now new root hash!' . PHP_EOL);
+
+ // Reset "found time" (when a hash was found)
+ $GLOBALS['found_time'] = microtime(TRUE);
+
+ // Re-initialize nonce
+ initNonce();
+
+ // Abort search
+ break;
+ } // END - if
+ } // END - for
+ } // END - foreach