gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] 02/06: reduce code reuse


From: gnunet
Subject: [taler-anastasis] 02/06: reduce code reuse
Date: Thu, 22 Oct 2020 18:52:07 +0200

This is an automated email from the git hooks/post-receive script.

dennis-neufeld pushed a commit to branch master
in repository anastasis.

commit e5f246044f8616a5a879332bca2e8bad24b2b29d
Author: Dennis Neufeld <dennis.neufeld@students.bfh.ch>
AuthorDate: Thu Oct 22 10:52:26 2020 +0200

    reduce code reuse
---
 src/stasis/plugin_anastasis_postgres.c | 153 ++++++++++++++++-----------------
 src/stasis/test_anastasis_db.c         |   2 +-
 2 files changed, 73 insertions(+), 82 deletions(-)

diff --git a/src/stasis/plugin_anastasis_postgres.c 
b/src/stasis/plugin_anastasis_postgres.c
index 8a68c96..aca4ba5 100644
--- a/src/stasis/plugin_anastasis_postgres.c
+++ b/src/stasis/plugin_anastasis_postgres.c
@@ -267,6 +267,61 @@ postgres_gc (void *cls,
 }
 
 
+/**
+ * Check if there is already a valid code.
+ *
+ * @param pg postgres closure
+ * @param truth_public_key identifier for a challenge
+ * @param code code which will be safed to check later
+ * @param creation_date timestamp
+ * @return transaction status, NULL if codes are different
+ */
+static enum ANASTASIS_DB_QueryStatus
+check_valid_code (struct PostgresClosure *pg,
+                  const struct
+                  ANASTASIS_CRYPTO_TruthPublicKeyP *
+                  truth_public_key,
+                  uint64_t code,
+                  struct GNUNET_TIME_Absolute creation_date)
+{
+  enum ANASTASIS_DB_QueryStatus qs;
+  uint64_t server_code;
+  struct GNUNET_PQ_QueryParam params[] = {
+    GNUNET_PQ_query_param_auto_from_type (truth_public_key),
+    TALER_PQ_query_param_absolute_time (&creation_date),
+    GNUNET_PQ_query_param_end
+  };
+  struct GNUNET_PQ_ResultSpec rs[] = {
+    GNUNET_PQ_result_spec_uint64 ("code",
+                                  &server_code),
+    GNUNET_PQ_result_spec_end
+  };
+  qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+                                                 "challengecode_select",
+                                                 params,
+                                                 rs);
+  switch (qs)
+  {
+  case GNUNET_DB_STATUS_HARD_ERROR:
+    return ANASTASIS_DB_STATUS_HARD_ERROR;
+  case GNUNET_DB_STATUS_SOFT_ERROR:
+    GNUNET_break (0);
+    return ANASTASIS_DB_STATUS_SOFT_ERROR;
+  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
+    return ANASTASIS_DB_STATUS_NO_RESULTS;
+  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
+    if (server_code == code)
+    {
+      return ANASTASIS_DB_STATUS_VALID_CODE_STORED;
+    }
+    return ANASTASIS_DB_STATUS_CHALLENGE_CODE_MISSMATCH;
+  default:
+    GNUNET_break (0);
+    return ANASTASIS_DB_STATUS_HARD_ERROR;
+  }
+}
+
+
 /**
  * Closure for #payment_by_account_cb.
  */
@@ -1584,55 +1639,22 @@ postgres_verify_challenge_code (void *cls,
   struct PostgresClosure *pg = cls;
   enum ANASTASIS_DB_QueryStatus qs;
   struct GNUNET_TIME_Absolute time_now = GNUNET_TIME_absolute_get ();
+
+  check_connection (pg);
   GNUNET_TIME_round_abs (&time_now);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "At %s:%d Public key used to query db is: %s\n",
               __FILE__,
               __LINE__,
               TALER_B2S (truth_pub));
+  /*Check if there is already a valid code */
+  qs = check_valid_code (pg,
+                         truth_pub,
+                         code,
+                         time_now);
+  if (qs != ANASTASIS_DB_STATUS_CHALLENGE_CODE_MISSMATCH)
+    return qs;
 
-  {
-    uint64_t server_code;
-    struct GNUNET_PQ_QueryParam params[] = {
-      GNUNET_PQ_query_param_auto_from_type (truth_pub),
-      TALER_PQ_query_param_absolute_time (&time_now),
-      GNUNET_PQ_query_param_end
-    };
-
-    struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_uint64 ("code",
-                                    &server_code),
-      GNUNET_PQ_result_spec_end
-    };
-
-    check_connection (pg);
-
-    qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
-                                                   "challengecode_select",
-                                                   params,
-                                                   rs);
-
-    switch (qs)
-    {
-    case GNUNET_DB_STATUS_HARD_ERROR:
-      return ANASTASIS_DB_STATUS_HARD_ERROR;
-    case GNUNET_DB_STATUS_SOFT_ERROR:
-      GNUNET_break (0);
-      return ANASTASIS_DB_STATUS_SOFT_ERROR;
-    case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
-      return ANASTASIS_DB_STATUS_NO_RESULTS;
-    case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
-
-      if (server_code == code)
-      {
-        return ANASTASIS_DB_STATUS_SUCCESS_ONE_RESULT;
-      }
-      break;
-    default:
-      GNUNET_break (0);
-      return ANASTASIS_DB_STATUS_HARD_ERROR;
-    }
-  }
   if (GNUNET_OK != begin_transaction (pg,
                                       "update_challenge_retry"))
   {
@@ -1822,55 +1844,24 @@ postgres_store_challenge_code (void *cls,
   enum ANASTASIS_DB_QueryStatus qs;
   struct GNUNET_TIME_Absolute creation_date = GNUNET_TIME_absolute_get ();
   struct GNUNET_TIME_Absolute expiration_date;
+
+  check_connection (pg);
   GNUNET_TIME_round_abs (&creation_date);
   expiration_date = GNUNET_TIME_absolute_add (creation_date,
                                               expiration_time);
-
   /*Check if there is already a valid code */
-  /*FIXME maybe put this in a function code reusage*/
-  uint64_t server_code;
-  {
-    struct GNUNET_PQ_QueryParam params[] = {
-      GNUNET_PQ_query_param_auto_from_type (truth_public_key),
-      TALER_PQ_query_param_absolute_time (&creation_date),
-      GNUNET_PQ_query_param_end
-    };
-    struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_uint64 ("code",
-                                    &server_code),
-      GNUNET_PQ_result_spec_end
-    };
-
-    check_connection (pg);
-    qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
-                                                   "challengecode_select",
-                                                   params,
-                                                   rs);
-    switch (qs)
-    {
-    case GNUNET_DB_STATUS_HARD_ERROR:
-      return ANASTASIS_DB_STATUS_HARD_ERROR;
-    case GNUNET_DB_STATUS_SOFT_ERROR:
-      GNUNET_break (0);
-      return ANASTASIS_DB_STATUS_SOFT_ERROR;
-    case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
-      break;
-    case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
-      return ANASTASIS_DB_STATUS_VALID_CODE_STORED;
-    default:
-      GNUNET_break (0);
-      return ANASTASIS_DB_STATUS_HARD_ERROR;
-    }
-  }
-
-  check_connection (pg);
+  qs = check_valid_code (pg,
+                         truth_public_key,
+                         code,
+                         creation_date);
+  if (qs != ANASTASIS_DB_STATUS_NO_RESULTS)
+    return qs;
   if (GNUNET_OK != begin_transaction (pg,
                                       "store_challenge_code"))
   {
     GNUNET_break (0);
     return ANASTASIS_DB_STATUS_HARD_ERROR;
   }
-
   {
     struct GNUNET_PQ_QueryParam params[] = {
       GNUNET_PQ_query_param_auto_from_type (truth_public_key),
diff --git a/src/stasis/test_anastasis_db.c b/src/stasis/test_anastasis_db.c
index 0a6df85..83afdcc 100644
--- a/src/stasis/test_anastasis_db.c
+++ b/src/stasis/test_anastasis_db.c
@@ -275,7 +275,7 @@ run (void *cls)
 
 
 
-  FAILIF (ANASTASIS_DB_STATUS_SUCCESS_ONE_RESULT !=
+  FAILIF (ANASTASIS_DB_STATUS_VALID_CODE_STORED !=
           plugin->verify_challenge_code (plugin->cls,
                                          &truth_public_key,
                                          challenge_code));

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]