Переглянути джерело

synchronization debugging done for now

tristangurtler 3 роки тому
батько
коміт
7251c066e3
2 змінених файлів з 34 додано та 17 видалено
  1. 34 1
      prsona/src/client.cpp
  2. 0 16
      prsona/src/networkClient.cpp

+ 34 - 1
prsona/src/client.cpp

@@ -1,4 +1,6 @@
 #include <iostream>
+#include <mutex>
+#include <thread>
 
 #include "client.hpp"
 
@@ -212,6 +214,15 @@ Scalar PrsonaClient::get_score() const
  * SCORE DECRYPTION
  */
 
+void alarm_bell(void *mutex, int *value)
+{
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+
+    std::mutex *realMtx = (std::mutex *) mutex;
+    std::unique_lock<std::mutex> lck(*realMtx);
+    *value = 1;
+}
+
 // Basic memoized score decryption
 Scalar PrsonaClient::decrypt_score(
     const EGCiphertext& score)
@@ -227,18 +238,40 @@ Scalar PrsonaClient::decrypt_score(
     if (lookup != decryption_memoizer.end())
         return lookup->second;
 
+    int alarm = 0;
+    bool flag = false;
+    std::mutex alarmMtx;
+    std::unique_lock<std::mutex> lck(alarmMtx, std::defer_lock);
+
+    std::thread alarmThread(alarm_bell, &alarmMtx, &alarm);
+
     // If not, iterate until we find it (adding everything to the memoization)
     max_checked++;
     Twistpoint decryptionCandidate = elGamalBlindGenerator * max_checked;
-    while (decryptionCandidate != hashedDecrypted)
+
+    lck.lock();
+    while (!alarm && decryptionCandidate != hashedDecrypted)
     {
+        lck.unlock();
+
         decryption_memoizer[decryptionCandidate] = max_checked;
 
         decryptionCandidate = decryptionCandidate + elGamalBlindGenerator;
         max_checked++;
+
+        lck.lock();
     }
+    if (alarm)
+        flag = true;
+    lck.unlock();
+
     decryption_memoizer[decryptionCandidate] = max_checked;
 
+    alarmThread.join();
+
+    if (flag)
+        return Scalar(0);
+
     // Return the value we found
     return max_checked;
 }

+ 0 - 16
prsona/src/networkClient.cpp

@@ -139,8 +139,6 @@ bool make_reputation_proof(
     std::chrono::high_resolution_clock::time_point wallTimeBefore = std::chrono::high_resolution_clock::now();
     clock_t cpuTimeBefore = clock();
 
-    std::cout << "Getting generator." << std::endl;
-
     // Get current fresh generator (it's not guaranteed we've done this in the current epoch)
     std::vector<Proof> generatorProof;
     Twistpoint freshGenerator = get_generator(rng, serverIPs, serverPorts, true, generatorProof, bandwidthData);
@@ -151,44 +149,30 @@ bool make_reputation_proof(
     // Make current short term public key
     Twistpoint shortTermPublicKey = prsonaClient->get_short_term_public_key();
 
-    std::cout << "Getting encrypted score." << std::endl;
-
     // Get this client's current encrypted score
     std::vector<Proof> encryptedScoreProof;
     EGCiphertext encryptedScore = get_server_committed_val<EGCiphertext>(rng, serverIPs, serverPorts, REQUEST_CLIENT_TALLY_URI, REQUEST_CLIENT_TALLY_COMMITMENT_URI, encryptedScoreProof, shortTermPublicKey, bandwidthData);
 
-    std::cout << "Loading encrypted score." << std::endl;
-
     // Load this current encrypted score into client object
     prsonaClient->receive_vote_tally(encryptedScoreProof, encryptedScore);
 
-    std::cout << "Converting encrypted score to an int." << std::endl;
-
     // Choose a random (valid) threshold to make a proof for
     mpz_class maxScore = prsonaClient->get_score().toInt();
-
-    std::cout << "Converting encrypted score to a usable int." << std::endl;
     int maxScoreInt = 0;
     while (maxScoreInt < maxScore)
         maxScoreInt++;
     std::uniform_int_distribution<int> scoreThresholdDistribution(0, maxScoreInt);
     Scalar threshold(scoreThresholdDistribution(rng));
 
-    std::cout << "Making proof." << std::endl;
-
     // Use client object to generate a correct reputation proof with the chosen parameters
     std::vector<Proof> repProof = prsonaClient->generate_reputation_proof(threshold, numClients);
 
     // Serialize that proof
     std::string data = make_rep_proof_string(repProof, shortTermPublicKey, threshold);
 
-    std::cout << "Sending proof." << std::endl;
-
     // Send that proof to a chosen client (and set up a file to receive whether or not the client accepted the proof)
     char *responseFile = send_item(rng, target, targetPort, VERIFY_REPUTATION_PROOF_URI, data, true, bandwidthData);
 
-    std::cout << "Work done." << std::endl;
-
     clock_t cpuTimeAfter = clock();
     std::chrono::high_resolution_clock::time_point wallTimeAfter = std::chrono::high_resolution_clock::now();
     std::vector<size_t> bandwidthDataAfter = get_server_log_data(civetServer.getContext());