From 11b7a967505cee2d3977f653bc6dafac400c0fdf Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 19 May 2020 23:26:06 +0200 Subject: more work on GET /reserves logic --- src/backenddb/merchant-0001.sql | 1 + src/backenddb/plugin_merchantdb_postgres.c | 147 ++++++++++++++++++++++------- 2 files changed, 115 insertions(+), 33 deletions(-) (limited to 'src/backenddb') diff --git a/src/backenddb/merchant-0001.sql b/src/backenddb/merchant-0001.sql index cebf63ee..bd05b6bb 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 6f5158d9..670f1d00 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 = <c->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" -- cgit v1.2.3