gnunet-svn
[Top][All Lists]
Advanced

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

[taler-merchant] 125/277: more work on GET /reserves logic


From: gnunet
Subject: [taler-merchant] 125/277: more work on GET /reserves logic
Date: Sun, 05 Jul 2020 20:50:38 +0200

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

grothoff pushed a commit to branch master
in repository merchant.

commit 11b7a967505cee2d3977f653bc6dafac400c0fdf
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Tue May 19 23:26:06 2020 +0200

    more work on GET /reserves logic
---
 .../taler-merchant-httpd_private-get-reserves.c    | 188 +++++++++++++++++++++
 .../taler-merchant-httpd_private-get-reserves.h    |  41 +++++
 src/backenddb/merchant-0001.sql                    |   1 +
 src/backenddb/plugin_merchantdb_postgres.c         | 147 ++++++++++++----
 src/include/taler_merchantdb_plugin.h              |   4 +-
 5 files changed, 346 insertions(+), 35 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_private-get-reserves.c 
b/src/backend/taler-merchant-httpd_private-get-reserves.c
new file mode 100644
index 0000000..780f88c
--- /dev/null
+++ b/src/backend/taler-merchant-httpd_private-get-reserves.c
@@ -0,0 +1,188 @@
+/*
+  This file is part of TALER
+  (C) 2019, 2020 Taler Systems SA
+
+  TALER is free software; you can redistribute it and/or modify it under the
+  terms of the GNU Affero General Public License as published by the Free 
Software
+  Foundation; either version 3, or (at your option) any later version.
+
+  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along with
+  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file backend/taler-merchant-httpd_private-get-reserves.c
+ * @brief implement GET /reserves
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <taler/taler_json_lib.h>
+#include "taler-merchant-httpd_private-get-reserves.h"
+
+
+/**
+ * Add reserve details to our JSON array.
+ *
+ * @param cls a `json_t *` JSON array to build
+ * @param reserve_pub public key of the reserve
+ * @param creation_time time when the reserve was setup
+ * @param expiration_time time when the reserve will be closed by the exchange
+ * @param merchant_initial_amount initial amount that the merchant claims to 
have filled the
+ *           reserve with
+ * @param exchange_initial_amount initial amount that the exchange claims to 
have received
+ * @param pickup_amount total of tips that were picked up from this reserve
+ * @param committed_amount total of tips that the merchant committed to, but 
that were not
+ *           picked up yet
+ * @param active  true if the reserve is still active (we have the private key)
+ */
+static void
+add_reserve (void *cls,
+             const struct TALER_ReservePublicKeyP *reserve_pub,
+             struct GNUNET_TIME_Absolute creation_time,
+             struct GNUNET_TIME_Absolute expiration_time,
+             const struct TALER_Amount *merchant_initial_amount,
+             const struct TALER_Amount *exchange_initial_amount,
+             const struct TALER_Amount *pickup_amount,
+             const struct TALER_Amount *committed_amount,
+             bool active)
+{
+  json_t *pa = cls;
+
+  GNUNET_assert (0 ==
+                 json_array_append_new (
+                   pa,
+                   json_pack (
+                     "{s:o,s:o,s:o,"
+                     " s:o,s:o,s:o,s:o,"
+                     " s:b}",
+                     "reserve_pub",
+                     GNUNET_JSON_from_data_auto (reserve_pub),
+                     "creation_time",
+                     GNUNET_JSON_from_time_abs (creation_time),
+                     "expiration_time",
+                     GNUNET_JSON_from_time_abs (expiration_time),
+                     "merchant_initial_amount",
+                     TALER_JSON_from_amount (merchant_initial_amount),
+                     "exchange_initial_amount",
+                     TALER_JSON_from_amount (exchange_initial_amount),
+                     "pickup_amount",
+                     TALER_JSON_from_amount (pickup_amount),
+                     "commited_amount",
+                     TALER_JSON_from_amount (committed_amount),
+                     "active",
+                     active)));
+}
+
+
+/**
+ * Handle a GET "/reserves" request.
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] hc context with further information about the request
+ * @return MHD result code
+ */
+MHD_RESULT
+TMH_private_get_reserves (const struct TMH_RequestHandler *rh,
+                          struct MHD_Connection *connection,
+                          struct TMH_HandlerContext *hc)
+{
+  json_t *ra;
+  enum GNUNET_DB_QueryStatus qs;
+  struct GNUNET_TIME_Absolute created_after = { 0 };
+  enum TALER_MERCHANTDB_YesNoAll active;
+  enum TALER_MERCHANTDB_YesNoAll failures;
+
+  {
+    const char *active_s;
+
+    active_s = MHD_lookup_connection_value (connection,
+                                            MHD_GET_ARGUMENT_KIND,
+                                            "active");
+    if (NULL == active_s)
+    {
+      active = TALER_MERCHANTDB_YNA_ALL;
+    }
+    else if (0 == strcasecmp (active_s,
+                              "yes"))
+    {
+      active = TALER_MERCHANTDB_YNA_YES;
+    }
+    else if (0 == strcasecmp (active_s,
+                              "no"))
+    {
+      active = TALER_MERCHANTDB_YNA_NO;
+    }
+    else if (0 == strcasecmp (active_s,
+                              "all"))
+    {
+      active = TALER_MERCHANTDB_YNA_ALL;
+    }
+    else
+      return TALER_MHD_reply_with_error (connection,
+                                         MHD_HTTP_BAD_REQUEST,
+                                         TALER_EC_PARAMETER_MALFORMED,
+                                         "active");
+  }
+
+  {
+    const char *failures_s;
+
+    failures_s = MHD_lookup_connection_value (connection,
+                                              MHD_GET_ARGUMENT_KIND,
+                                              "failures");
+    if (NULL == failures_s)
+    {
+      failures = TALER_MERCHANTDB_YNA_ALL;
+    }
+    else if (0 == strcasecmp (failures_s,
+                              "yes"))
+    {
+      failures = TALER_MERCHANTDB_YNA_YES;
+    }
+    else if (0 == strcasecmp (failures_s,
+                              "no"))
+    {
+      failures = TALER_MERCHANTDB_YNA_NO;
+    }
+    else if (0 == strcasecmp (failures_s,
+                              "all"))
+    {
+      failures = TALER_MERCHANTDB_YNA_ALL;
+    }
+    else
+      return TALER_MHD_reply_with_error (connection,
+                                         MHD_HTTP_BAD_REQUEST,
+                                         TALER_EC_PARAMETER_MALFORMED,
+                                         "failures");
+  }
+
+  ra = json_array ();
+  GNUNET_assert (NULL != ra);
+  qs = TMH_db->lookup_reserves (TMH_db->cls,
+                                hc->instance->settings.id,
+                                created_after,
+                                active,
+                                failures,
+                                &add_reserve,
+                                ra);
+  if (0 > qs)
+  {
+    GNUNET_break (0);
+    json_decref (ra);
+    return TALER_MHD_reply_with_error (connection,
+                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
+                                       TALER_EC_GET_RESERVES_DB_LOOKUP_ERROR,
+                                       "failed to lookup reserves in 
database");
+  }
+  return TALER_MHD_reply_json_pack (connection,
+                                    MHD_HTTP_OK,
+                                    "{s:o}",
+                                    "reserves", ra);
+}
+
+
+/* end of taler-merchant-httpd_private-get-reserves.c */
diff --git a/src/backend/taler-merchant-httpd_private-get-reserves.h 
b/src/backend/taler-merchant-httpd_private-get-reserves.h
new file mode 100644
index 0000000..46d21ec
--- /dev/null
+++ b/src/backend/taler-merchant-httpd_private-get-reserves.h
@@ -0,0 +1,41 @@
+/*
+  This file is part of TALER
+  (C) 2019, 2020 Taler Systems SA
+
+  TALER is free software; you can redistribute it and/or modify it under the
+  terms of the GNU Affero General Public License as published by the Free 
Software
+  Foundation; either version 3, or (at your option) any later version.
+
+  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along with
+  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file backend/taler-merchant-httpd_private-get-reserves.h
+ * @brief implement GET /reserves
+ * @author Christian Grothoff
+ */
+#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_RESERVES_H
+#define TALER_MERCHANT_HTTPD_PRIVATE_GET_RESERVES_H
+
+#include "taler-merchant-httpd.h"
+
+
+/**
+ * Handle a GET "/reserves" request.
+ *
+ * @param rh context of the handler
+ * @param connection the MHD connection to handle
+ * @param[in,out] hc context with further information about the request
+ * @return MHD result code
+ */
+MHD_RESULT
+TMH_private_get_reserves (const struct TMH_RequestHandler *rh,
+                          struct MHD_Connection *connection,
+                          struct TMH_HandlerContext *hc);
+
+/* end of taler-merchant-httpd_private-get-reserves.h */
+#endif
diff --git a/src/backenddb/merchant-0001.sql b/src/backenddb/merchant-0001.sql
index cebf63e..bd05b6b 100644
--- a/src/backenddb/merchant-0001.sql
+++ b/src/backenddb/merchant-0001.sql
@@ -427,6 +427,7 @@ CREATE TABLE IF NOT EXISTS merchant_tip_reserves
   ,reserve_pub BYTEA NOT NULL UNIQUE CHECK (LENGTH(reserve_pub)=32)
   ,merchant_serial BIGINT NOT NULL
     REFERENCES merchant_instances (merchant_serial) ON DELETE CASCADE
+  ,creation_time INT8 NOT NULL
   ,expiration INT8 NOT NULL
   ,merchant_initial_balance_val INT8 NOT NULL
   ,merchant_initial_balance_frac INT4 NOT NULL
diff --git a/src/backenddb/plugin_merchantdb_postgres.c 
b/src/backenddb/plugin_merchantdb_postgres.c
index 6f5158d..670f1d0 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -3764,14 +3764,18 @@ RETRY:
 
   /* Setup reserve */
   {
+    struct GNUNET_TIME_Absolute now;
     struct GNUNET_PQ_QueryParam params[] = {
       GNUNET_PQ_query_param_string (instance_id),
       GNUNET_PQ_query_param_auto_from_type (reserve_pub),
+      GNUNET_PQ_query_param_absolute_time (&now),
       GNUNET_PQ_query_param_absolute_time (&expiration),
       TALER_PQ_query_param_amount (initial_balance),
       GNUNET_PQ_query_param_end
     };
 
+    now = GNUNET_TIME_absolute_get ();
+    (void) GNUNET_TIME_round_abs (&now);
     qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                              "insert_reserve",
                                              params);
@@ -3872,8 +3876,8 @@ lookup_reserves_cb (void *cls,
     struct TALER_ReservePublicKeyP reserve_pub;
     struct GNUNET_TIME_Absolute creation_time;
     struct GNUNET_TIME_Absolute expiration_time;
-    struct TALER_Amount merchant_initial_amount;
-    struct TALER_Amount exchange_initial_amount;
+    struct TALER_Amount merchant_initial_balance;
+    struct TALER_Amount exchange_initial_balance;
     struct TALER_Amount pickup_amount;
     struct TALER_Amount committed_amount;
     uint8_t active;
@@ -3882,16 +3886,16 @@ lookup_reserves_cb (void *cls,
                                             &reserve_pub),
       GNUNET_PQ_result_spec_absolute_time ("creation_time",
                                            &creation_time),
-      GNUNET_PQ_result_spec_absolute_time ("expiration_time",
+      GNUNET_PQ_result_spec_absolute_time ("expiration",
                                            &expiration_time),
-      TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_amount",
-                                   &merchant_initial_amount),
-      TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_initial_amount",
-                                   &exchange_initial_amount),
-      TALER_PQ_RESULT_SPEC_AMOUNT ("pickup_amount",
-                                   &pickup_amount),
-      TALER_PQ_RESULT_SPEC_AMOUNT ("committed_amount",
+      TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_balance",
+                                   &merchant_initial_balance),
+      TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_initial_balance",
+                                   &exchange_initial_balance),
+      TALER_PQ_RESULT_SPEC_AMOUNT ("tips_committed",
                                    &committed_amount),
+      TALER_PQ_RESULT_SPEC_AMOUNT ("tips_picked_up",
+                                   &pickup_amount),
       GNUNET_PQ_result_spec_auto_from_type ("active",
                                             &active),
       GNUNET_PQ_result_spec_end
@@ -3910,8 +3914,8 @@ lookup_reserves_cb (void *cls,
              &reserve_pub,
              creation_time,
              expiration_time,
-             &merchant_initial_amount,
-             &exchange_initial_amount,
+             &merchant_initial_balance,
+             &exchange_initial_balance,
              &pickup_amount,
              &committed_amount,
              (0 != active));
@@ -4017,11 +4021,11 @@ lookup_reserve_tips_cb (void *cls,
   {
     struct TALER_MERCHANTDB_TipDetails *td = &ltc->tips[i];
     struct GNUNET_PQ_ResultSpec rs[] = {
-      GNUNET_PQ_result_spec_string ("reason",
+      GNUNET_PQ_result_spec_string ("justification",
                                     &td->reason),
       GNUNET_PQ_result_spec_auto_from_type ("tip_id",
                                             &td->tip_id),
-      TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount",
+      TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
                                    &td->total_amount),
       GNUNET_PQ_result_spec_end
     };
@@ -4069,23 +4073,23 @@ postgres_lookup_reserve (void *cls,
   };
   struct GNUNET_TIME_Absolute creation_time;
   struct GNUNET_TIME_Absolute expiration_time;
-  struct TALER_Amount merchant_initial_amount;
-  struct TALER_Amount exchange_initial_amount;
+  struct TALER_Amount merchant_initial_balance;
+  struct TALER_Amount exchange_initial_balance;
   struct TALER_Amount pickup_amount;
   struct TALER_Amount committed_amount;
   uint8_t active;
   struct GNUNET_PQ_ResultSpec rs[] = {
     GNUNET_PQ_result_spec_absolute_time ("creation_time",
                                          &creation_time),
-    GNUNET_PQ_result_spec_absolute_time ("expiration_time",
+    GNUNET_PQ_result_spec_absolute_time ("expiration",
                                          &expiration_time),
-    TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_amount",
-                                 &merchant_initial_amount),
-    TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_initial_amount",
-                                 &exchange_initial_amount),
-    TALER_PQ_RESULT_SPEC_AMOUNT ("pickup_amount",
+    TALER_PQ_RESULT_SPEC_AMOUNT ("merchant_initial_balance",
+                                 &merchant_initial_balance),
+    TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_initial_balance",
+                                 &exchange_initial_balance),
+    TALER_PQ_RESULT_SPEC_AMOUNT ("tips_picked_up",
                                  &pickup_amount),
-    TALER_PQ_RESULT_SPEC_AMOUNT ("committed_amount",
+    TALER_PQ_RESULT_SPEC_AMOUNT ("tips_committed",
                                  &committed_amount),
     GNUNET_PQ_result_spec_auto_from_type ("active",
                                           &active),
@@ -4105,8 +4109,8 @@ postgres_lookup_reserve (void *cls,
     cb (cb_cls,
         creation_time,
         expiration_time,
-        &merchant_initial_amount,
-        &exchange_initial_amount,
+        &merchant_initial_balance,
+        &exchange_initial_balance,
         &pickup_amount,
         &committed_amount,
         0,
@@ -4126,8 +4130,8 @@ postgres_lookup_reserve (void *cls,
     cb (cb_cls,
         creation_time,
         expiration_time,
-        &merchant_initial_amount,
-        &exchange_initial_amount,
+        &merchant_initial_balance,
+        &exchange_initial_balance,
         &pickup_amount,
         &committed_amount,
         ltc.tips_length,
@@ -6803,14 +6807,15 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
                             "INSERT INTO merchant_tip_reserves"
                             "(reserve_pub"
                             ",merchant_serial"
+                            ",creation_time"
                             ",expiration"
                             ",merchant_initial_balance_val"
                             ",merchant_initial_balance_frac"
                             ")"
-                            "SELECT $2, merchant_serial, $3, $4, $5"
+                            "SELECT $2, merchant_serial, $3, $4, $5, $6"
                             " FROM merchant_instances"
                             " WHERE merchant_id=$1",
-                            5),
+                            6),
     /* For postgres_insert_reserve() */
     GNUNET_PQ_make_prepare ("insert_reserve_key",
                             "INSERT INTO merchant_tip_reserve_keys"
@@ -6826,11 +6831,87 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
                             "        FROM merchant_instances"
                             "       WHERE merchant_id=$1)",
                             4),
+    /* For postgres_lookup_reserves() */
+    GNUNET_PQ_make_prepare ("lookup_reserves",
+                            "SELECT"
+                            " reserve_pub"
+                            ",expiration"
+                            ",merchant_initial_balance_val"
+                            ",merchant_initial_balance_frac"
+                            ",exchange_initial_balance_val"
+                            ",exchange_initial_balance_frac"
+                            ",tips_committed_val"
+                            ",tips_committed_frac"
+                            ",tips_picked_up_val"
+                            ",tips_picked_up_frac"
+                            " FROM merchant_tip_reserves"
+                            " WHERE creation_time > $2"
+                            "   AND merchant_serial ="
+                            "     (SELECT merchant_serial"
+                            "        FROM merchant_instances"
+                            "       WHERE merchant_id=$1)",
+                            2),
+    /* For postgres_lookup_reserve() */
+    GNUNET_PQ_make_prepare ("lookup_reserve",
+                            "SELECT"
+                            " creation_time"
+                            ",expiration"
+                            ",merchant_initial_balance_val"
+                            ",merchant_initial_balance_frac"
+                            ",exchange_initial_balance_val"
+                            ",exchange_initial_balance_frac"
+                            ",tips_committed_val"
+                            ",tips_committed_frac"
+                            ",tips_picked_up_val"
+                            ",tips_picked_up_frac"
+                            " FROM merchant_tip_reserves"
+                            " WHERE reserve_pub = $2"
+                            "   AND merchant_serial ="
+                            "     (SELECT merchant_serial"
+                            "        FROM merchant_instances"
+                            "       WHERE merchant_id=$1)",
+                            2),
+    /* For postgres_lookup_reserve() */
+    GNUNET_PQ_make_prepare ("lookup_reserve_tips",
+                            "SELECT"
+                            " justification"
+                            ",tip_id"
+                            ",amount_val"
+                            ",amount_frac"
+                            " FROM merchant_tips"
+                            " WHERE reserve_serial ="
+                            "  (SELECT reserve_serial"
+                            "     FROM merchant_tip_reserves"
+                            "      WHERE reserve_pub=$2"
+                            "        AND merchant_serial ="
+                            "       (SELECT merchant_serial"
+                            "          FROM merchant_instances"
+                            "         WHERE merchant_id=$1))",
+                            2),
+    /* for postgres_delete_reserve() */
+    GNUNET_PQ_make_prepare ("delete_reserve",
+                            "DELETE"
+                            " FROM merchant_tip_reserve_keys"
+                            " WHERE reserve_serial="
+                            "   (SELECT reserve_serial"
+                            "      FROM merchant_tip_reserves"
+                            "       WHERE reserve_pub=$2"
+                            "         AND merchant_serial="
+                            "     (SELECT merchant_serial"
+                            "        FROM merchant_instances"
+                            "       WHERE merchant_id=$1))",
+                            2),
+    /* for postgres_purge_reserve() */
+    GNUNET_PQ_make_prepare ("purge_reserve",
+                            "DELETE"
+                            "   FROM merchant_tip_reserves"
+                            "  WHERE reserve_pub=$2"
+                            "    AND merchant_serial="
+                            "    (SELECT merchant_serial"
+                            "       FROM merchant_instances"
+                            "      WHERE merchant_id=$1)",
+                            2),
     /* OLD API: */
-    // DO: lookup_reserves
-    // DO: lookup_reserve
-    // DO: lookup_reserve_tips
-    // DO: delete_reserve / purge_reserve
 #if 0
     GNUNET_PQ_make_prepare ("insert_contract_terms",
                             "INSERT INTO merchant_contract_terms"
diff --git a/src/include/taler_merchantdb_plugin.h 
b/src/include/taler_merchantdb_plugin.h
index f04f873..b51fce8 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -513,7 +513,7 @@ struct TALER_MERCHANTDB_TipDetails
  * @param merchant_initial_amount initial amount that the merchant claims to 
have filled the
  *           reserve with
  * @param exchange_initial_amount initial amount that the exchange claims to 
have received
- * @param pickup_amount total of tips that were picked up from this reserve
+ * @param picked_up_amount total of tips that were picked up from this reserve
  * @param committed_amount total of tips that the merchant committed to, but 
that were not
  *           picked up yet
  * @param tips_length length of the @a tips array
@@ -526,7 +526,7 @@ typedef void
   struct GNUNET_TIME_Absolute expiration_time,
   const struct TALER_Amount *merchant_initial_amount,
   const struct TALER_Amount *exchange_initial_amount,
-  const struct TALER_Amount *pickup_amount,
+  const struct TALER_Amount *picked_up_amount,
   const struct TALER_Amount *committed_amount,
   unsigned int tips_length,
   const struct TALER_MERCHANTDB_TipDetails *tips);

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