diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-05-18 19:55:34 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-05-18 19:55:34 +0200 |
commit | 211a3cb0db1ac6c97835100b4d128d338b19ba0a (patch) | |
tree | 44145efc9486f95c314c6d93ae6af8ebd612bb74 /src/backenddb | |
parent | 7d83b800d2aeb814032ae69007290f69aace42c3 (diff) | |
download | merchant-211a3cb0db1ac6c97835100b4d128d338b19ba0a.tar.gz merchant-211a3cb0db1ac6c97835100b4d128d338b19ba0a.tar.bz2 merchant-211a3cb0db1ac6c97835100b4d128d338b19ba0a.zip |
implement insert_reserve
Diffstat (limited to 'src/backenddb')
-rw-r--r-- | src/backenddb/merchant-0001.sql | 14 | ||||
-rw-r--r-- | 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 5d502802..cebf63ee 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 c69507d6..5e247d2e 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; |