gnunet-svn
[Top][All Lists]
Advanced

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

[taler-anastasis] 03/06: add long polling support for payment on challen


From: gnunet
Subject: [taler-anastasis] 03/06: add long polling support for payment on challenge solving
Date: Sat, 13 Mar 2021 19:23:25 +0100

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

grothoff pushed a commit to branch master
in repository anastasis.

commit bb6ccd8902cd953b71d1a59f48ea1f4b7272b457
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sat Mar 13 16:52:22 2021 +0100

    add long polling support for payment on challenge solving
---
 src/backend/anastasis-httpd_truth.c            | 42 +++++++++++++++++++-
 src/include/anastasis.h                        |  6 +++
 src/include/anastasis_service.h                |  3 ++
 src/lib/anastasis_recovery.c                   |  6 +++
 src/reducer/anastasis_api_recovery_redux.c     | 25 ++++++++++++
 src/restclient/anastasis_api_keyshare_lookup.c | 55 +++++++++++++++++++-------
 src/restclient/anastasis_api_truth_store.c     |  1 -
 src/testing/testing_api_cmd_keyshare_lookup.c  |  1 +
 src/testing/testing_cmd_challenge_answer.c     |  2 +
 9 files changed, 124 insertions(+), 17 deletions(-)

diff --git a/src/backend/anastasis-httpd_truth.c 
b/src/backend/anastasis-httpd_truth.c
index 3d43a94..b02ac0c 100644
--- a/src/backend/anastasis-httpd_truth.c
+++ b/src/backend/anastasis-httpd_truth.c
@@ -117,6 +117,11 @@ struct GetContext
    */
   struct MHD_Response *resp;
 
+  /**
+   * How long do we wait at most for payment?
+   */
+  struct GNUNET_TIME_Absolute timeout;
+
   /**
    * Random authorization code we are using.
    */
@@ -492,12 +497,15 @@ begin_payment (struct GetContext *gc)
   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
   {
     /* We already created the order, check if it was paid */
+    struct GNUNET_TIME_Relative timeout;
+
+    timeout = GNUNET_TIME_absolute_get_remaining (gc->timeout);
     gc->cpo = TALER_MERCHANT_merchant_order_get (AH_ctx,
                                                  AH_backend_url,
                                                  order_id,
                                                  NULL /* NOT session-bound */,
                                                  false,
-                                                 GNUNET_TIME_UNIT_ZERO,
+                                                 timeout,
                                                  &check_payment_cb,
                                                  gc);
   }
@@ -693,6 +701,38 @@ AH_handler_truth_get (
       gc->have_response = (NULL != challenge_response_s);
     }
 
+    {
+      const char *long_poll_timeout_ms;
+
+      long_poll_timeout_ms = MHD_lookup_connection_value (connection,
+                                                          
MHD_GET_ARGUMENT_KIND,
+                                                          "timeout_ms");
+      if (NULL != long_poll_timeout_ms)
+      {
+        unsigned int timeout;
+
+        if (1 != sscanf (long_poll_timeout_ms,
+                         "%u",
+                         &timeout))
+        {
+          GNUNET_break_op (0);
+          return TALER_MHD_reply_with_error (connection,
+                                             MHD_HTTP_BAD_REQUEST,
+                                             
TALER_EC_GENERIC_PARAMETER_MALFORMED,
+                                             "timeout_ms (must be non-negative 
number)");
+        }
+        gc->timeout
+          = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (
+                                                GNUNET_TIME_UNIT_MILLISECONDS,
+                                                timeout));
+      }
+      else
+      {
+        gc->timeout = GNUNET_TIME_relative_to_absolute (
+          GNUNET_TIME_UNIT_SECONDS);
+      }
+    }
+
   } /* end of first-time initialization (if NULL == gc) */
   else
   {
diff --git a/src/include/anastasis.h b/src/include/anastasis.h
index c7b356b..81e2547 100644
--- a/src/include/anastasis.h
+++ b/src/include/anastasis.h
@@ -244,6 +244,7 @@ typedef void
  *
  * @param c reference to the escrow challenge which is started
  * @param psp payment secret, NULL if no payment was yet made
+ * @param timeout how long to wait for payment
  * @param hashed_answer answer to the challenge, NULL if we have none yet
  * @param af reference to the answerfeedback which is passed back to the user
  * @param af_cls closure for @a af
@@ -252,6 +253,7 @@ typedef void
 int
 ANASTASIS_challenge_start (struct ANASTASIS_Challenge *c,
                            const struct ANASTASIS_PaymentSecretP *psp,
+                           struct GNUNET_TIME_Relative timeout,
                            const struct GNUNET_HashCode *hashed_answer,
                            ANASTASIS_AnswerFeedback af,
                            void *af_cls);
@@ -264,6 +266,7 @@ ANASTASIS_challenge_start (struct ANASTASIS_Challenge *c,
  *
  * @param c reference to the challenge which is answered
  * @param psp information about payment made for the recovery
+ * @param timeout how long to wait for payment
  * @param answer user input instruction defines which input is needed
  * @param af reference to the answerfeedback which is passed back to the user
  * @param af_cls closure for @a af
@@ -272,6 +275,7 @@ ANASTASIS_challenge_start (struct ANASTASIS_Challenge *c,
 int
 ANASTASIS_challenge_answer (struct ANASTASIS_Challenge *c,
                             const struct ANASTASIS_PaymentSecretP *psp,
+                            struct GNUNET_TIME_Relative timeout,
                             const char *answer,
                             ANASTASIS_AnswerFeedback af,
                             void *af_cls);
@@ -285,6 +289,7 @@ ANASTASIS_challenge_answer (struct ANASTASIS_Challenge *c,
  *
  * @param c reference to the challenge which is answered
  * @param psp information about payment made for the recovery
+ * @param timeout how long to wait for payment
  * @param answer user input instruction defines which input is needed
  * @param af reference to the answerfeedback which is passed back to the user
  * @param af_cls closure for @a af
@@ -293,6 +298,7 @@ ANASTASIS_challenge_answer (struct ANASTASIS_Challenge *c,
 int
 ANASTASIS_challenge_answer2 (struct ANASTASIS_Challenge *c,
                              const struct ANASTASIS_PaymentSecretP *psp,
+                             struct GNUNET_TIME_Relative timeout,
                              uint64_t answer,
                              ANASTASIS_AnswerFeedback af,
                              void *af_cls);
diff --git a/src/include/anastasis_service.h b/src/include/anastasis_service.h
index 2d9bd90..15f5d19 100644
--- a/src/include/anastasis_service.h
+++ b/src/include/anastasis_service.h
@@ -578,6 +578,8 @@ typedef void
  * @param truth_public_key identification of the Truth
  * @param truth_key Key used to Decrypt the Truth on the Server
  * @param payment_secret secret from the previously done payment NULL to 
trigger payment
+ * @param payment_timeout how long to wait for the payment, use
+ *           #GNUNET_TIME_UNIT_ZERO to let the server pick
  * @param hashed_answer hashed answer to the challenge
  * @param cb callback which will work the response gotten from the backend
  * @param cb_cls closure to pass to the callback
@@ -590,6 +592,7 @@ ANASTASIS_keyshare_lookup (
   const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
   const struct ANASTASIS_CRYPTO_TruthKeyP *truth_key,
   const struct ANASTASIS_PaymentSecretP *payment_secret,
+  struct GNUNET_TIME_Relative timeout,
   const struct GNUNET_HashCode *hashed_answer,
   ANASTASIS_KeyShareLookupCallback cb,
   void *cb_cls);
diff --git a/src/lib/anastasis_recovery.c b/src/lib/anastasis_recovery.c
index f0bf097..a504a97 100644
--- a/src/lib/anastasis_recovery.c
+++ b/src/lib/anastasis_recovery.c
@@ -418,6 +418,7 @@ ANASTASIS_challenge_get_details (struct ANASTASIS_Challenge 
*challenge)
 int
 ANASTASIS_challenge_start (struct ANASTASIS_Challenge *c,
                            const struct ANASTASIS_PaymentSecretP *psp,
+                           struct GNUNET_TIME_Relative timeout,
                            const struct GNUNET_HashCode *hashed_answer,
                            ANASTASIS_AnswerFeedback af,
                            void *af_cls)
@@ -439,6 +440,7 @@ ANASTASIS_challenge_start (struct ANASTASIS_Challenge *c,
                                        &c->ci.uuid,
                                        &c->truth_key,
                                        psp,
+                                       timeout,
                                        hashed_answer,
                                        &keyshare_lookup_cb,
                                        c);
@@ -455,6 +457,7 @@ int
 ANASTASIS_challenge_answer (
   struct ANASTASIS_Challenge *c,
   const struct ANASTASIS_PaymentSecretP *psp,
+  struct GNUNET_TIME_Relative timeout,
   const char *answer_str,
   ANASTASIS_AnswerFeedback af,
   void *af_cls)
@@ -477,6 +480,7 @@ ANASTASIS_challenge_answer (
                    0));
   return ANASTASIS_challenge_start (c,
                                     psp,
+                                    timeout,
                                     &hashed_answer,
                                     af,
                                     af_cls);
@@ -486,6 +490,7 @@ ANASTASIS_challenge_answer (
 int
 ANASTASIS_challenge_answer2 (struct ANASTASIS_Challenge *c,
                              const struct ANASTASIS_PaymentSecretP *psp,
+                             struct GNUNET_TIME_Relative timeout,
                              uint64_t answer,
                              ANASTASIS_AnswerFeedback af,
                              void *af_cls)
@@ -498,6 +503,7 @@ ANASTASIS_challenge_answer2 (struct ANASTASIS_Challenge *c,
                    (unsigned long long) answer);
   return ANASTASIS_challenge_answer (c,
                                      psp,
+                                     timeout,
                                      answer_s,
                                      af,
                                      af_cls);
diff --git a/src/reducer/anastasis_api_recovery_redux.c 
b/src/reducer/anastasis_api_recovery_redux.c
index 4d23e24..934afa6 100644
--- a/src/reducer/anastasis_api_recovery_redux.c
+++ b/src/reducer/anastasis_api_recovery_redux.c
@@ -109,6 +109,11 @@ struct SelectChallengeContext
    */
   struct ANASTASIS_CRYPTO_TruthUUIDP uuid;
 
+  /**
+   * Which timeout was set for the operation?
+   */
+  struct GNUNET_TIME_Relative timeout;
+
   /**
    * Overall recovery action.
    */
@@ -520,7 +525,11 @@ run_challenge_cb (void *cls,
   struct SelectChallengeContext *sctx = cls;
   const struct ANASTASIS_PaymentSecretP *psp = NULL;
   struct ANASTASIS_PaymentSecretP ps;
+  struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_ZERO;
   struct GNUNET_JSON_Specification spec[] = {
+    GNUNET_JSON_spec_mark_optional (
+      GNUNET_JSON_spec_relative_time ("timeout",
+                                      &timeout)),
     GNUNET_JSON_spec_fixed_auto ("payment_secret",
                                  &ps),
     GNUNET_JSON_spec_end ()
@@ -615,6 +624,7 @@ run_challenge_cb (void *cls,
                                       janswer));
       ret = ANASTASIS_challenge_answer (ci,
                                         psp,
+                                        timeout,
                                         answer,
                                         &answer_feedback_cb,
                                         sctx);
@@ -632,6 +642,7 @@ run_challenge_cb (void *cls,
 
         ret = ANASTASIS_challenge_answer2 (ci,
                                            psp,
+                                           timeout,
                                            ianswer,
                                            &answer_feedback_cb,
                                            sctx);
@@ -659,6 +670,7 @@ run_challenge_cb (void *cls,
         }
         ret = ANASTASIS_challenge_start (ci,
                                          psp,
+                                         timeout,
                                          &hashed_answer,
                                          &answer_feedback_cb,
                                          sctx);
@@ -668,6 +680,7 @@ run_challenge_cb (void *cls,
         /* no answer provided */
         ret = ANASTASIS_challenge_start (ci,
                                          psp,
+                                         timeout,
                                          NULL,   /* no answer */
                                          &answer_feedback_cb,
                                          sctx);
@@ -780,6 +793,7 @@ pay_challenge_cb (void *cls,
       }
       ret = ANASTASIS_challenge_answer (ci,
                                         &sctx->ps,
+                                        sctx->timeout,
                                         answer,
                                         &answer_feedback_cb,
                                         sctx);
@@ -788,6 +802,7 @@ pay_challenge_cb (void *cls,
     {
       ret = ANASTASIS_challenge_start (ci,
                                        &sctx->ps,
+                                       sctx->timeout,
                                        NULL,   /* no answer yet */
                                        &answer_feedback_cb,
                                        sctx);
@@ -913,12 +928,16 @@ pay_challenge (json_t *state,
   struct SelectChallengeContext *sctx
     = GNUNET_new (struct SelectChallengeContext);
   json_t *rd;
+  struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_ZERO;
   struct GNUNET_JSON_Specification spec[] = {
     GNUNET_JSON_spec_fixed_auto ("selected_challenge_uuid",
                                  &sctx->uuid),
     GNUNET_JSON_spec_end ()
   };
   struct GNUNET_JSON_Specification aspec[] = {
+    GNUNET_JSON_spec_mark_optional (
+      GNUNET_JSON_spec_relative_time ("timeout",
+                                      &timeout)),
     GNUNET_JSON_spec_fixed_auto ("payment_secret",
                                  &sctx->ps),
     GNUNET_JSON_spec_end ()
@@ -965,6 +984,7 @@ pay_challenge (json_t *state,
                            "pay_challenge");
     return NULL;
   }
+  sctx->timeout = timeout;
   sctx->cb = cb;
   sctx->cb_cls = cb_cls;
   sctx->state = json_incref (state);
@@ -1011,7 +1031,11 @@ select_challenge_cb (void *cls,
   struct SelectChallengeContext *sctx = cls;
   const struct ANASTASIS_PaymentSecretP *psp = NULL;
   struct ANASTASIS_PaymentSecretP ps;
+  struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_ZERO;
   struct GNUNET_JSON_Specification spec[] = {
+    GNUNET_JSON_spec_mark_optional (
+      GNUNET_JSON_spec_relative_time ("timeout",
+                                      &timeout)),
     GNUNET_JSON_spec_fixed_auto ("payment_secret",
                                  &ps),
     GNUNET_JSON_spec_end ()
@@ -1104,6 +1128,7 @@ select_challenge_cb (void *cls,
     /* trigger challenge */
     ret = ANASTASIS_challenge_start (ci,
                                      psp,
+                                     timeout,
                                      NULL,   /* no answer */
                                      &answer_feedback_cb,
                                      sctx);
diff --git a/src/restclient/anastasis_api_keyshare_lookup.c 
b/src/restclient/anastasis_api_keyshare_lookup.c
index 3868642..3d41009 100644
--- a/src/restclient/anastasis_api_keyshare_lookup.c
+++ b/src/restclient/anastasis_api_keyshare_lookup.c
@@ -344,6 +344,7 @@ ANASTASIS_keyshare_lookup (
   const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
   const struct ANASTASIS_CRYPTO_TruthKeyP *truth_key,
   const struct ANASTASIS_PaymentSecretP *payment_secret,
+  struct GNUNET_TIME_Relative timeout,
   const struct GNUNET_HashCode *hashed_answer,
   ANASTASIS_KeyShareLookupCallback cb,
   void *cb_cls)
@@ -353,7 +354,10 @@ ANASTASIS_keyshare_lookup (
   struct curl_slist *job_headers;
   char *path;
   char *answer_s;
+  unsigned long long tms;
 
+  tms = (unsigned long long) (timeout.rel_value_us
+                              / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
   job_headers = NULL;
   {
     struct curl_slist *ext;
@@ -417,28 +421,49 @@ ANASTASIS_keyshare_lookup (
                      uuid_str);
     GNUNET_free (uuid_str);
   }
-  if (NULL != hashed_answer)
   {
-    answer_s = GNUNET_STRINGS_data_to_string_alloc (hashed_answer,
-                                                    sizeof (*hashed_answer));
-    kslo->url = TALER_url_join (backend_url,
-                                path,
-                                "response",
-                                answer_s,
-                                NULL);
-    GNUNET_free (answer_s);
-  }
-  else
-  {
-    kslo->url = TALER_url_join (backend_url,
-                                path,
-                                NULL);
+    char timeout_ms[32];
+
+    GNUNET_snprintf (timeout_ms,
+                     sizeof (timeout_ms),
+                     "%llu",
+                     tms);
+    if (NULL != hashed_answer)
+    {
+      answer_s = GNUNET_STRINGS_data_to_string_alloc (hashed_answer,
+                                                      sizeof (*hashed_answer));
+      kslo->url = TALER_url_join (backend_url,
+                                  path,
+                                  "response",
+                                  answer_s,
+                                  "timeout_ms",
+                                  (0 != timeout.rel_value_us)
+                                ? timeout_ms
+                                : NULL,
+                                  NULL);
+      GNUNET_free (answer_s);
+    }
+    else
+    {
+      kslo->url = TALER_url_join (backend_url,
+                                  path,
+                                  "timeout_ms",
+                                  (0 != timeout.rel_value_us)
+                                ? timeout_ms
+                                : NULL,
+                                  NULL);
+    }
   }
   kslo->display_url = TALER_url_join (backend_url,
                                       path,
                                       NULL);
   GNUNET_free (path);
   eh = ANASTASIS_curl_easy_get_ (kslo->url);
+  if (0 != tms)
+    GNUNET_assert (CURLE_OK ==
+                   curl_easy_setopt (eh,
+                                     CURLOPT_TIMEOUT_MS,
+                                     (long) tms));
   GNUNET_assert (CURLE_OK ==
                  curl_easy_setopt (eh,
                                    CURLOPT_HEADERFUNCTION,
diff --git a/src/restclient/anastasis_api_truth_store.c 
b/src/restclient/anastasis_api_truth_store.c
index e465678..bc4d2ce 100644
--- a/src/restclient/anastasis_api_truth_store.c
+++ b/src/restclient/anastasis_api_truth_store.c
@@ -259,7 +259,6 @@ ANASTASIS_truth_store (
   char *json_str;
   unsigned long long tms;
 
-  /* Finished setting up headers */
   tms = (unsigned long long) (payment_timeout.rel_value_us
                               / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
   tso = GNUNET_new (struct ANASTASIS_TruthStoreOperation);
diff --git a/src/testing/testing_api_cmd_keyshare_lookup.c 
b/src/testing/testing_api_cmd_keyshare_lookup.c
index ad8527f..f86feac 100644
--- a/src/testing/testing_api_cmd_keyshare_lookup.c
+++ b/src/testing/testing_api_cmd_keyshare_lookup.c
@@ -351,6 +351,7 @@ keyshare_lookup_run (void *cls,
                                             truth_uuid,
                                             truth_key,
                                             payment_secret,
+                                            GNUNET_TIME_UNIT_ZERO,
                                             (NULL != answer)
                                             ? &h_answer
                                             : NULL,
diff --git a/src/testing/testing_cmd_challenge_answer.c 
b/src/testing/testing_cmd_challenge_answer.c
index 6b6e9f1..22af2dc 100644
--- a/src/testing/testing_cmd_challenge_answer.c
+++ b/src/testing/testing_cmd_challenge_answer.c
@@ -261,6 +261,7 @@ challenge_answer_run (void *cls,
   if (GNUNET_OK !=
       ANASTASIS_challenge_answer (cs->c,
                                   ps,
+                                  GNUNET_TIME_UNIT_ZERO,
                                   answer,
                                   &challenge_answer_cb,
                                   cs))
@@ -338,6 +339,7 @@ challenge_start_run (void *cls,
   if (GNUNET_OK !=
       ANASTASIS_challenge_start ((struct ANASTASIS_Challenge *) c,
                                  ps,
+                                 GNUNET_TIME_UNIT_ZERO,
                                  NULL,
                                  &challenge_answer_cb,
                                  cs))

-- 
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]