Added flushCheckPointFile().
[core.git] / contrib / chash / chash.php
index 0e60d0fbbf183432ed64fff3605ff395c9f7d913..1bc7f147e5b7c68fb9ac13231c47bd1b9aa47ce1 100644 (file)
@@ -10,6 +10,9 @@ define('CHECK_POINT', 'chash.pos');
 // Found hashes
 $foundHashes = array();
 
 // Found hashes
 $foundHashes = array();
 
+// Time when last flush occured
+global $timeFlush;
+
 /**
  * Continued-hashing
  *
 /**
  * Continued-hashing
  *
@@ -185,6 +188,24 @@ function calculateNonce ($nonce) {
        return $newNonce;
 }
 
        return $newNonce;
 }
 
+/**
+ * Writes/flushes check-point file
+ *
+ * @return     void
+ */
+function flushCheckPointFile ($nonce, $modulaHash, array $foundHashes) {
+       global $timeFlush;
+
+       // Display message
+       print ('FLUSHING: ' . count($foundHashes) . ' total found.' . PHP_EOL);
+
+       // Flush data
+       file_put_contents(CHECK_POINT, base64_encode($nonce) . ':' . $modulaHash . ':' . base64_encode(serialize($foundHashes)));
+
+       // Set time
+       $timeFlush = microtime(TRUE);
+}
+
 /*
  * 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
 /*
  * 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
@@ -328,13 +349,8 @@ while (TRUE) {
 
                        // Only once per 10 seconds
                        if ($testTime >= 10) {
 
                        // Only once per 10 seconds
                        if ($testTime >= 10) {
-                               // Display message
-                               print ('FLUSHING: ' . count($foundHashes) . ' total found.' . PHP_EOL);
-
-                               // Write check-point
-                               file_put_contents(CHECK_POINT, base64_encode($nonce) . ':' . $modulaHash . ':' . base64_encode(serialize($foundHashes)));
-
-                               $timeFlush = microtime(TRUE);
+                               // Flush check-point file
+                               flushCheckPointFile($nonce, $modulaHash, $foundHashes);
                        } // END - if
 
                        // Next round
                        } // END - if
 
                        // Next round
@@ -375,6 +391,8 @@ while (TRUE) {
                // Found hash:
                print ('FOUND: nonceHash=' . $nonceHash . ',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
 
                // Found hash:
                print ('FOUND: nonceHash=' . $nonceHash . ',nonce=' . $nonce . ',iter=' . $iter . PHP_EOL);
 
+               // Flush check-point file after new hash is found
+               flushCheckPointFile($nonce, $nonceHash, $foundHashes);
 
                // Use nonceHash as next modula hash
                $modulaHash = $nonceHash;
 
                // Use nonceHash as next modula hash
                $modulaHash = $nonceHash;