[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-merchant] 120/277: implement insert_reserve
From: |
gnunet |
Subject: |
[taler-merchant] 120/277: implement insert_reserve |
Date: |
Sun, 05 Jul 2020 20:50:33 +0200 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository merchant.
commit 211a3cb0db1ac6c97835100b4d128d338b19ba0a
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Mon May 18 19:55:34 2020 +0200
implement insert_reserve
---
src/backenddb/merchant-0001.sql | 14 ++--
src/backenddb/plugin_merchantdb_postgres.c | 129 ++++++++++++++++++++++++++++-
2 files changed, 135 insertions(+), 8 deletions(-)
diff --git a/src/backenddb/merchant-0001.sql b/src/backenddb/merchant-0001.sql
index 5d50280..cebf63e 100644
--- a/src/backenddb/merchant-0001.sql
+++ b/src/backenddb/merchant-0001.sql
@@ -430,12 +430,12 @@ CREATE TABLE IF NOT EXISTS merchant_tip_reserves
,expiration INT8 NOT NULL
,merchant_initial_balance_val INT8 NOT NULL
,merchant_initial_balance_frac INT4 NOT NULL
- ,exchange_initial_balance_val INT8 NOT NULL
- ,exchange_initial_balance_frac INT4 NOT NULL
- ,tips_committed_val INT8 NOT NULL
- ,tips_committed_frac INT4 NOT NULL
- ,tips_picked_up_val INT8 NOT NULL
- ,tips_picked_up_frac INT4 NOT NULL
+ ,exchange_initial_balance_val INT8 NOT NULL DEFAULT 0
+ ,exchange_initial_balance_frac INT4 NOT NULL DEFAULT 0
+ ,tips_committed_val INT8 NOT NULL DEFAULT 0
+ ,tips_committed_frac INT4 NOT NULL DEFAULT 0
+ ,tips_picked_up_val INT8 NOT NULL DEFAULT 0
+ ,tips_picked_up_frac INT4 NOT NULL DEFAULT 0
);
COMMENT ON TABLE merchant_tip_reserves
IS 'balances of the reserves available for tips';
@@ -450,7 +450,7 @@ COMMENT ON COLUMN merchant_tip_reserves.tips_committed_val
COMMENT ON COLUMN merchant_tip_reserves.tips_picked_up_val
IS 'Total amount tips that have been picked up from this reserve';
-CREATE TABLE IF NOT EXISTS merchant_tip_reserve_kreys
+CREATE TABLE IF NOT EXISTS merchant_tip_reserve_keys
(reserve_serial BIGINT NOT NULL UNIQUE
REFERENCES merchant_tip_reserves (reserve_serial) ON DELETE CASCADE
,reserve_priv BYTEA NOT NULL UNIQUE CHECK (LENGTH(reserve_priv)=32)
diff --git a/src/backenddb/plugin_merchantdb_postgres.c
b/src/backenddb/plugin_merchantdb_postgres.c
index c69507d..5e247d2 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -3719,6 +3719,100 @@ postgres_store_wire_fee_by_exchange (
}
+/**
+ * Add @a credit to a reserve to be used for tipping. Note that
+ * this function does not actually perform any wire transfers to
+ * credit the reserve, it merely tells the merchant backend that
+ * a reserve now exists. This has to happen before tips can be
+ * authorized.
+ *
+ * @param cls closure, typically a connection to the db
+ * @param instance_id which instance is the reserve tied to
+ * @param reserve_priv which reserve is topped up or created
+ * @param reserve_pub which reserve is topped up or created
+ * @param exchange_url what URL is the exchange reachable at where the reserve
is located
+ * @param initial_balance how much money will be added to the reserve
+ * @param expiration when does the reserve expire?
+ * @return transaction status, usually
+ * #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT for success
+ */
+static enum GNUNET_DB_QueryStatus
+postgres_insert_reserve (void *cls,
+ const char *instance_id,
+ const struct TALER_ReservePrivateKeyP *reserve_priv,
+ const struct TALER_ReservePublicKeyP *reserve_pub,
+ const char *exchange_url,
+ const struct TALER_Amount *initial_balance,
+ struct GNUNET_TIME_Absolute expiration)
+{
+ struct PostgresClosure *pg = cls;
+ unsigned int retries;
+ enum GNUNET_DB_QueryStatus qs;
+
+ retries = 0;
+ check_connection (pg);
+RETRY:
+ if (MAX_RETRIES < ++retries)
+ return TALER_EC_TIP_PICKUP_DB_ERROR_SOFT;
+ if (GNUNET_OK !=
+ postgres_start (pg,
+ "insert reserve"))
+ {
+ GNUNET_break (0);
+ return TALER_EC_TIP_PICKUP_DB_ERROR_HARD;
+ }
+
+ /* Setup reserve */
+ {
+ 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 (&expiration),
+ TALER_PQ_query_param_amount (initial_balance),
+ GNUNET_PQ_query_param_end
+ };
+
+ qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_reserve",
+ params);
+ if (0 > qs)
+ {
+ postgres_rollback (pg);
+ if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
+ goto RETRY;
+ return qs;
+ }
+ }
+ /* Store private key */
+ {
+ 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_auto_from_type (reserve_priv),
+ GNUNET_PQ_query_param_string (exchange_url),
+ GNUNET_PQ_query_param_end
+ };
+
+ qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
+ "insert_reserve_key",
+ params);
+ if (0 > qs)
+ {
+ postgres_rollback (pg);
+ if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
+ goto RETRY;
+ return qs;
+ }
+ }
+ qs = postgres_commit (pg);
+ if (0 <= qs)
+ return TALER_EC_NONE; /* success */
+ if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
+ goto RETRY;
+ return qs;
+}
+
+
/* ********************* OLD API ************************** */
/**
@@ -4504,7 +4598,11 @@ RETRY:
postgres_rollback (pg);
return TALER_EC_TIP_PICKUP_AMOUNT_CHANGED;
}
- postgres_commit (pg);
+ qs = postgres_commit (pg);
+ if (qs < 0)
+ return (GNUNET_DB_STATUS_SOFT_ERROR == qs)
+ ? TALER_EC_TIP_PICKUP_DB_ERROR_SOFT
+ : TALER_EC_TIP_PICKUP_DB_ERROR_HARD;
return TALER_EC_NONE; /* we are done! */
}
}
@@ -6315,6 +6413,34 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
" VALUES "
"($1, $2, $3, $4, $5, $6, $7, $8, $9)",
9),
+ /* For postgres_insert_reserve() */
+ GNUNET_PQ_make_prepare ("insert_reserve",
+ "INSERT INTO merchant_tip_reserves"
+ "(reserve_pub"
+ ",merchant_serial"
+ ",expiration"
+ ",merchant_initial_balance_val"
+ ",merchant_initial_balance_frac"
+ ")"
+ "SELECT $2, merchant_serial, $3, $4, $5"
+ " FROM merchant_instances"
+ " WHERE merchant_id=$1",
+ 5),
+ /* For postgres_insert_reserve() */
+ GNUNET_PQ_make_prepare ("insert_reserve_key",
+ "INSERT INTO merchant_tip_reserve_keys"
+ "(reserve_serial"
+ ",reserve_priv"
+ ",exchange_url"
+ ")"
+ "SELECT reserve_serial, $3, $4"
+ " FROM merchant_tip_reserves"
+ " WHERE reserve_pub=$2"
+ " AND merchant_serial="
+ " (SELECT merchant_serial"
+ " FROM merchant_instances"
+ " WHERE merchant_id=$1)",
+ 4),
/* OLD API: */
#if 0
@@ -6628,6 +6754,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
plugin->lookup_transfer_summary = &postgres_lookup_transfer_summary;
plugin->lookup_transfer_details = &postgres_lookup_transfer_details;
plugin->lookup_transfers = &postgres_lookup_transfers;
+ plugin->insert_reserve = &postgres_insert_reserve;
/* OLD API: */
plugin->store_coin_to_transfer = &postgres_store_coin_to_transfer;
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-merchant] 90/277: starting with get-orders-ID logic, (continued)
- [taler-merchant] 90/277: starting with get-orders-ID logic, gnunet, 2020/07/05
- [taler-merchant] 92/277: fix backenddb, gnunet, 2020/07/05
- [taler-merchant] 97/277: integrate POST /tranfers handler, gnunet, 2020/07/05
- [taler-merchant] 105/277: API design for /transfers, gnunet, 2020/07/05
- [taler-merchant] 113/277: use unregister, gnunet, 2020/07/05
- [taler-merchant] 112/277: rename _v to _mv for merchant, gnunet, 2020/07/05
- [taler-merchant] 118/277: implement POST /reserves, gnunet, 2020/07/05
- [taler-merchant] 122/277: fixed bool casts in plugin initialization, gnunet, 2020/07/05
- [taler-merchant] 129/277: fixed coding style in db tests and double callbacks in lookup_instances, gnunet, 2020/07/05
- [taler-merchant] 131/277: towards POST tips pickup impl, gnunet, 2020/07/05
- [taler-merchant] 120/277: implement insert_reserve,
gnunet <=
- [taler-merchant] 125/277: more work on GET /reserves logic, gnunet, 2020/07/05
- [taler-merchant] 99/277: more work on post /transfers and the like, gnunet, 2020/07/05
- [taler-merchant] 93/277: updates, gnunet, 2020/07/05
- [taler-merchant] 100/277: more work on POST /transfer, gnunet, 2020/07/05
- [taler-merchant] 121/277: Merge branch 'master' into protocolV1, gnunet, 2020/07/05
- [taler-merchant] 126/277: more instance and product related tests, gnunet, 2020/07/05
- [taler-merchant] 124/277: expanding DB API, gnunet, 2020/07/05
- [taler-merchant] 128/277: more work on tipping implementation, gnunet, 2020/07/05
- [taler-merchant] 132/277: Merge branch 'protocolV1' of git+ssh://git.taler.net/merchant into protocolV1, gnunet, 2020/07/05
- [taler-merchant] 136/277: add GET /tips/ID handler, gnunet, 2020/07/05