summaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_reserve_open.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/exchangedb/exchange_do_reserve_open.sql')
-rw-r--r--src/exchangedb/exchange_do_reserve_open.sql150
1 files changed, 67 insertions, 83 deletions
diff --git a/src/exchangedb/exchange_do_reserve_open.sql b/src/exchangedb/exchange_do_reserve_open.sql
index 5e80f713f..dd7a578ee 100644
--- a/src/exchangedb/exchange_do_reserve_open.sql
+++ b/src/exchangedb/exchange_do_reserve_open.sql
@@ -16,80 +16,66 @@
CREATE OR REPLACE FUNCTION exchange_do_reserve_open(
IN in_reserve_pub BYTEA,
- IN in_total_paid_val INT8,
- IN in_total_paid_frac INT4,
- IN in_reserve_payment_val INT8,
- IN in_reserve_payment_frac INT4,
+ IN in_total_paid taler_amount,
+ IN in_reserve_payment taler_amount,
IN in_min_purse_limit INT4,
IN in_default_purse_limit INT4,
IN in_reserve_sig BYTEA,
IN in_desired_expiration INT8,
IN in_reserve_gc_delay INT8,
IN in_now INT8,
- IN in_open_fee_val INT8,
- IN in_open_fee_frac INT4,
- OUT out_open_cost_val INT8,
- OUT out_open_cost_frac INT4,
+ IN in_open_fee taler_amount,
+ OUT out_open_cost taler_amount,
OUT out_final_expiration INT8,
- OUT out_no_funds BOOLEAN)
+ OUT out_no_reserve BOOLEAN,
+ OUT out_no_funds BOOLEAN,
+ OUT out_reserve_balance taler_amount)
LANGUAGE plpgsql
AS $$
DECLARE
- my_balance_val INT8;
-DECLARE
- my_balance_frac INT4;
-DECLARE
- my_cost_val INT8;
-DECLARE
+ my_balance taler_amount;
+ my_cost taler_amount;
my_cost_tmp INT8;
-DECLARE
- my_cost_frac INT4;
-DECLARE
my_years_tmp INT4;
-DECLARE
my_years INT4;
-DECLARE
my_needs_update BOOL;
-DECLARE
- my_purses_allowed INT8;
-DECLARE
my_expiration_date INT8;
-DECLARE
- my_reserve_expiration INT8;
+ reserve RECORD;
BEGIN
--- FIXME: use SELECT FOR UPDATE?
-SELECT
- purses_allowed
- ,expiration_date
- ,current_balance_val
- ,current_balance_frac
-INTO
- my_purses_allowed
- ,my_reserve_expiration
- ,my_balance_val
- ,my_balance_frac
-FROM reserves
-WHERE
- reserve_pub=in_reserve_pub;
+SELECT current_balance
+ ,expiration_date
+ ,purses_allowed
+ INTO reserve
+ FROM reserves
+ WHERE reserve_pub=in_reserve_pub;
IF NOT FOUND
THEN
- -- FIXME: do we need to set a 'not found'?
RAISE NOTICE 'reserve not found';
+ out_no_reserve = TRUE;
+ out_no_funds = TRUE;
+ out_reserve_balance.val = 0;
+ out_reserve_balance.frac = 0;
+ out_open_cost.val = 0;
+ out_open_cost.frac = 0;
+ out_final_expiration = 0;
RETURN;
END IF;
+out_no_reserve = FALSE;
+out_reserve_balance = reserve.current_balance;
+
-- Do not allow expiration time to start in the past already
-IF (my_reserve_expiration < in_now)
+IF (reserve.expiration_date < in_now)
THEN
my_expiration_date = in_now;
ELSE
- my_expiration_date = my_reserve_expiration;
+ my_expiration_date = reserve.expiration_date;
END IF;
-my_cost_val = 0;
-my_cost_frac = 0;
+my_cost.val = 0;
+my_cost.frac = 0;
my_needs_update = FALSE;
my_years = 0;
@@ -97,62 +83,62 @@ my_years = 0;
IF (my_expiration_date < in_desired_expiration)
THEN
my_years = (31535999999999 + in_desired_expiration - my_expiration_date) / 31536000000000;
- my_purses_allowed = in_default_purse_limit;
+ reserve.purses_allowed = in_default_purse_limit;
my_expiration_date = my_expiration_date + 31536000000000 * my_years;
END IF;
-- Increase years based on purses requested
-IF (my_purses_allowed < in_min_purse_limit)
+IF (reserve.purses_allowed < in_min_purse_limit)
THEN
my_years = (31535999999999 + in_desired_expiration - in_now) / 31536000000000;
my_expiration_date = in_now + 31536000000000 * my_years;
- my_years_tmp = (in_min_purse_limit + in_default_purse_limit - my_purses_allowed - 1) / in_default_purse_limit;
+ my_years_tmp = (in_min_purse_limit + in_default_purse_limit - reserve.purses_allowed - 1) / in_default_purse_limit;
my_years = my_years + my_years_tmp;
- my_purses_allowed = my_purses_allowed + (in_default_purse_limit * my_years_tmp);
+ reserve.purses_allowed = reserve.purses_allowed + (in_default_purse_limit * my_years_tmp);
END IF;
-- Compute cost based on annual fees
IF (my_years > 0)
THEN
- my_cost_val = my_years * in_open_fee_val;
- my_cost_tmp = my_years * in_open_fee_frac / 100000000;
- IF (CAST (my_cost_val + my_cost_tmp AS INT8) < my_cost_val)
+ my_cost.val = my_years * in_open_fee.val;
+ my_cost_tmp = my_years * in_open_fee.frac / 100000000;
+ IF (CAST (my_cost.val + my_cost_tmp AS INT8) < my_cost.val)
THEN
- out_open_cost_val=9223372036854775807;
- out_open_cost_frac=2147483647;
+ out_open_cost.val=9223372036854775807;
+ out_open_cost.frac=2147483647;
out_final_expiration=my_expiration_date;
out_no_funds=FALSE;
RAISE NOTICE 'arithmetic issue computing amount';
RETURN;
END IF;
- my_cost_val = CAST (my_cost_val + my_cost_tmp AS INT8);
- my_cost_frac = my_years * in_open_fee_frac % 100000000;
+ my_cost.val = CAST (my_cost.val + my_cost_tmp AS INT8);
+ my_cost.frac = my_years * in_open_fee.frac % 100000000;
my_needs_update = TRUE;
END IF;
-- check if we actually have something to do
IF NOT my_needs_update
THEN
- out_final_expiration = my_reserve_expiration;
- out_open_cost_val = 0;
- out_open_cost_frac = 0;
+ out_final_expiration = reserve.expiration_date;
+ out_open_cost.val = 0;
+ out_open_cost.frac = 0;
out_no_funds=FALSE;
RAISE NOTICE 'no change required';
RETURN;
END IF;
-- Check payment (coins and reserve) would be sufficient.
-IF ( (in_total_paid_val < my_cost_val) OR
- ( (in_total_paid_val = my_cost_val) AND
- (in_total_paid_frac < my_cost_frac) ) )
+IF ( (in_total_paid.val < my_cost.val) OR
+ ( (in_total_paid.val = my_cost.val) AND
+ (in_total_paid.frac < my_cost.frac) ) )
THEN
- out_open_cost_val = my_cost_val;
- out_open_cost_frac = my_cost_frac;
+ out_open_cost.val = my_cost.val;
+ out_open_cost.frac = my_cost.frac;
out_no_funds=FALSE;
-- We must return a failure, which is indicated by
-- the expiration being below the desired expiration.
- IF (my_reserve_expiration >= in_desired_expiration)
+ IF (reserve.expiration_date >= in_desired_expiration)
THEN
-- This case is relevant especially if the purse
-- count was to be increased and the payment was
@@ -160,32 +146,32 @@ THEN
RAISE NOTICE 'forcing low expiration time';
out_final_expiration = 0;
ELSE
- out_final_expiration = my_reserve_expiration;
+ out_final_expiration = reserve.expiration_date;
END IF;
RAISE NOTICE 'amount paid too low';
RETURN;
END IF;
-- Check reserve balance is sufficient.
-IF (my_balance_val > in_reserve_payment_val)
+IF (out_reserve_balance.val > in_reserve_payment.val)
THEN
- IF (my_balance_frac >= in_reserve_payment_frac)
+ IF (out_reserve_balance.frac >= in_reserve_payment.frac)
THEN
- my_balance_val=my_balance_val - in_reserve_payment_val;
- my_balance_frac=my_balance_frac - in_reserve_payment_frac;
+ my_balance.val=out_reserve_balance.val - in_reserve_payment.val;
+ my_balance.frac=out_reserve_balance.frac - in_reserve_payment.frac;
ELSE
- my_balance_val=my_balance_val - in_reserve_payment_val - 1;
- my_balance_frac=my_balance_frac + 100000000 - in_reserve_payment_frac;
+ my_balance.val=out_reserve_balance.val - in_reserve_payment.val - 1;
+ my_balance.frac=out_reserve_balance.frac + 100000000 - in_reserve_payment.frac;
END IF;
ELSE
- IF (my_balance_val = in_reserve_payment_val) AND (my_balance_frac >= in_reserve_payment_frac)
+ IF (out_reserve_balance.val = in_reserve_payment.val) AND (out_reserve_balance.frac >= in_reserve_payment.frac)
THEN
- my_balance_val=0;
- my_balance_frac=my_balance_frac - in_reserve_payment_frac;
+ my_balance.val=0;
+ my_balance.frac=out_reserve_balance.frac - in_reserve_payment.frac;
ELSE
- out_final_expiration = my_reserve_expiration;
- out_open_cost_val = my_cost_val;
- out_open_cost_frac = my_cost_frac;
+ out_final_expiration = reserve.expiration_date;
+ out_open_cost.val = my_cost.val;
+ out_open_cost.frac = my_cost.frac;
out_no_funds=TRUE;
RAISE NOTICE 'reserve balance too low';
RETURN;
@@ -193,17 +179,15 @@ ELSE
END IF;
UPDATE reserves SET
- current_balance_val=my_balance_val
- ,current_balance_frac=my_balance_frac
- ,gc_date=my_reserve_expiration + in_reserve_gc_delay
+ current_balance=my_balance
+ ,gc_date=reserve.expiration_date + in_reserve_gc_delay
,expiration_date=my_expiration_date
- ,purses_allowed=my_purses_allowed
+ ,purses_allowed=reserve.purses_allowed
WHERE
reserve_pub=in_reserve_pub;
out_final_expiration=my_expiration_date;
-out_open_cost_val = my_cost_val;
-out_open_cost_frac = my_cost_frac;
+out_open_cost = my_cost;
out_no_funds=FALSE;
RETURN;