summaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_insert_or_update_policy_details.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb/exchange_do_insert_or_update_policy_details.sql')
-rw-r--r--src/exchangedb/exchange_do_insert_or_update_policy_details.sql100
1 files changed, 43 insertions, 57 deletions
diff --git a/src/exchangedb/exchange_do_insert_or_update_policy_details.sql b/src/exchangedb/exchange_do_insert_or_update_policy_details.sql
index c7fe64d14..85e52d3d3 100644
--- a/src/exchangedb/exchange_do_insert_or_update_policy_details.sql
+++ b/src/exchangedb/exchange_do_insert_or_update_policy_details.sql
@@ -16,54 +16,42 @@
CREATE OR REPLACE FUNCTION exchange_do_insert_or_update_policy_details(
IN in_policy_hash_code BYTEA,
- IN in_policy_json VARCHAR,
+ IN in_policy_json TEXT,
IN in_deadline INT8,
- IN in_commitment_val INT8,
- IN in_commitment_frac INT4,
- IN in_accumulated_total_val INT8,
- IN in_accumulated_total_frac INT4,
- IN in_fee_val INT8,
- IN in_fee_frac INT4,
- IN in_transferable_val INT8,
- IN in_transferable_frac INT4,
+ IN in_commitment taler_amount,
+ IN in_accumulated_total taler_amount,
+ IN in_fee taler_amount,
+ IN in_transferable taler_amount,
IN in_fulfillment_state SMALLINT,
OUT out_policy_details_serial_id INT8,
- OUT out_accumulated_total_val INT8,
- OUT out_accumulated_total_frac INT4,
+ OUT out_accumulated_total taler_amount,
OUT out_fulfillment_state SMALLINT)
LANGUAGE plpgsql
AS $$
DECLARE
- cur_commitment_val INT8;
- cur_commitment_frac INT4;
- cur_accumulated_total_val INT8;
- cur_accumulated_total_frac INT4;
+ cur_commitment taler_amount;
+DECLARE
+ cur_accumulated_total taler_amount;
+DECLARE
+ rval RECORD;
BEGIN
-- First, try to create a new entry.
INSERT INTO policy_details
(policy_hash_code,
policy_json,
deadline,
- commitment_val,
- commitment_frac,
- accumulated_total_val,
- accumulated_total_frac,
- fee_val,
- fee_frac,
- transferable_val,
- transferable_frac,
+ commitment,
+ accumulated_total,
+ fee,
+ transferable,
fulfillment_state)
VALUES (in_policy_hash_code,
in_policy_json,
in_deadline,
- in_commitment_val,
- in_commitment_frac,
- in_accumulated_total_val,
- in_accumulated_total_frac,
- in_fee_val,
- in_fee_frac,
- in_transferable_val,
- in_transferable_frac,
+ in_commitment,
+ in_accumulated_total,
+ in_fee,
+ in_transferable,
in_fulfillment_state)
ON CONFLICT (policy_hash_code) DO NOTHING
RETURNING policy_details_serial_id INTO out_policy_details_serial_id;
@@ -71,34 +59,33 @@ BEGIN
-- If the insert was successful, return
-- We assume that the fullfilment_state was correct in first place.
IF FOUND THEN
- out_accumulated_total_val = in_accumulated_total_val;
- out_accumulated_total_frac = in_accumulated_total_frac;
- out_fulfillment_state = in_fulfillment_state;
+ out_accumulated_total = in_accumulated_total;
+ out_fulfillment_state = in_fulfillment_state;
RETURN;
END IF;
-- We had a conflict, grab the parts we need to update.
- SELECT policy_details_serial_id,
- commitment_val,
- commitment_frac,
- accumulated_total_val,
- accumulated_total_frac
- INTO out_policy_details_serial_id,
- cur_commitment_val,
- cur_commitment_frac,
- cur_accumulated_total_val,
- cur_accumulated_total_frac
+ SELECT policy_details_serial_id
+ ,commitment
+ ,accumulated_total
+ INTO rval
FROM policy_details
WHERE policy_hash_code = in_policy_hash_code;
+ -- We use rval as workaround as we cannot select
+ -- directly into the amount due to Postgres limitations.
+ out_policy_details_serial_id := rval.policy_details_serial_id;
+ cur_commitment := rval.commitment;
+ cur_accumulated_total := rval.accumulated_total;
+
-- calculate the new values (overflows throws exception)
- out_accumulated_total_val = cur_accumulated_total_val + in_accumulated_total_val;
- out_accumulated_total_frac = cur_accumulated_total_frac + in_accumulated_total_frac;
+ out_accumulated_total.val = cur_accumulated_total.val + in_accumulated_total.val;
+ out_accumulated_total.frac = cur_accumulated_total.frac + in_accumulated_total.frac;
-- normalize
- out_accumulated_total_val = out_accumulated_total_val + out_accumulated_total_frac / 100000000;
- out_accumulated_total_frac = out_accumulated_total_frac % 100000000;
+ out_accumulated_total.val = out_accumulated_total.val + out_accumulated_total.frac / 100000000;
+ out_accumulated_total.frac = out_accumulated_total.frac % 100000000;
- IF (out_accumulated_total_val > (1 << 52))
+ IF (out_accumulated_total.val > (1 << 52))
THEN
RAISE EXCEPTION 'accumulation overflow';
END IF;
@@ -106,22 +93,21 @@ BEGIN
-- Set the fulfillment_state according to the values.
-- For now, we only update the state when it was INSUFFICIENT.
- -- FIXME: What to do in case of Failure or other state?
- IF (out_fullfillment_state = 1) -- INSUFFICIENT
+ -- FIXME[oec] #7999: What to do in case of Failure or other state?
+ IF (out_fullfillment_state = 2) -- INSUFFICIENT
THEN
- IF (out_accumulated_total_val >= cur_commitment_val OR
- (out_accumulated_total_val = cur_commitment_val AND
- out_accumulated_total_frac >= cur_commitment_frac))
+ IF (out_accumulated_total.val >= cur_commitment.val OR
+ (out_accumulated_total.val = cur_commitment.val AND
+ out_accumulated_total.frac >= cur_commitment.frac))
THEN
- out_fulfillment_state = 2; -- READY
+ out_fulfillment_state = 3; -- READY
END IF;
END IF;
-- Now, update the record
UPDATE exchange.policy_details
SET
- accumulated_val = out_accumulated_total_val,
- accumulated_frac = out_accumulated_total_frac,
+ accumulated = out_accumulated_total,
fulfillment_state = out_fulfillment_state
WHERE
policy_details_serial_id = out_policy_details_serial_id;