1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
--
-- This file is part of TALER
-- Copyright (C) 2023 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
-- @author Özgür Kesim
CREATE OR REPLACE FUNCTION exchange_do_age_withdraw(
IN amount_with_fee taler_amount,
IN rpub BYTEA,
IN rsig BYTEA,
IN now INT8,
IN min_reserve_gc INT8,
IN h_commitment BYTEA,
IN maximum_age_committed INT2, -- in years ϵ [0,1..)
IN noreveal_index INT2,
IN blinded_evs BYTEA[],
IN denom_serials INT8[],
IN denom_sigs BYTEA[],
OUT reserve_found BOOLEAN,
OUT balance_ok BOOLEAN,
OUT age_ok BOOLEAN,
OUT required_age INT2, -- in years ϵ [0,1..)
OUT reserve_birthday INT4,
OUT conflict BOOLEAN)
LANGUAGE plpgsql
AS $$
DECLARE
reserve_gc INT8;
difference RECORD;
balance taler_amount;
new_balance taler_amount;
not_before date;
earliest_date date;
BEGIN
-- Shards: reserves by reserve_pub (SELECT)
-- reserves_out (INSERT, with CONFLICT detection) by wih
-- reserves by reserve_pub (UPDATE)
-- reserves_in by reserve_pub (SELECT)
-- wire_targets by wire_target_h_payto
SELECT
current_balance_val
,current_balance_frac
,gc_date
,birthday
INTO
balance.val
,balance.frac
,reserve_gc
,reserve_birthday
FROM exchange.reserves
WHERE reserves.reserve_pub=rpub;
IF NOT FOUND
THEN
reserve_found=FALSE;
age_ok = FALSE;
required_age=-1;
conflict=FALSE;
balance_ok=FALSE;
RETURN;
END IF;
reserve_found = TRUE;
conflict=FALSE; -- not really yet determined
-- Check age requirements
IF (reserve_birthday <> 0)
THEN
not_before=date '1970-01-01' + reserve_birthday;
earliest_date = current_date - make_interval(maximum_age_committed);
--
-- 1970-01-01 + birthday == not_before now
-- | | |
-- <.......not allowed......>[<.....allowed range......>]
-- | | |
-- ____*_____________________*_________*________________* timeline
-- |
-- earliest_date ==
-- now - maximum_age_committed*year
--
IF (earliest_date < not_before)
THEN
required_age = extract(year from age(current_date, not_before));
age_ok = FALSE;
balance_ok=TRUE; -- NOT REALLY
RETURN;
END IF;
END IF;
age_ok = TRUE;
required_age=0;
-- Check reserve balance is sufficient.
SELECT *
INTO
difference
FROM
amount_left_minus_right(
balance
,amount_with_fee);
balance_ok = difference.ok;
IF NOT balance_ok
THEN
RETURN;
END IF;
new_balance = difference.diff;
-- Calculate new expiration dates.
min_reserve_gc=GREATEST(min_reserve_gc,reserve_gc);
-- Update reserve balance.
UPDATE reserves SET
gc_date=min_reserve_gc
,current_balance_val=new_balance.val
,current_balance_frac=new_balance.frac
WHERE
reserves.reserve_pub=rpub;
-- Write the commitment into the age-withdraw table
INSERT INTO exchange.age_withdraw
(h_commitment
,max_age
,amount_with_fee
,reserve_pub
,reserve_sig
,noreveal_index
,denom_serials
,h_blind_evs
,denom_sigs)
VALUES
(h_commitment
,maximum_age_committed
,amount_with_fee
,rpub
,rsig
,noreveal_index
,denom_serials
,blinded_evs
,denom_sigs)
ON CONFLICT DO NOTHING;
IF NOT FOUND
THEN
-- Signal a conflict so that the caller
-- can fetch the actual data from the DB.
conflict=TRUE;
RETURN;
ELSE
conflict=FALSE;
END IF;
END $$;
COMMENT ON FUNCTION exchange_do_age_withdraw(taler_amount, BYTEA, BYTEA, INT8, INT8, BYTEA, INT2, INT2, BYTEA[], INT8[], BYTEA[])
IS 'Checks whether the reserve has sufficient balance for an age-withdraw operation (or the request is repeated and was previously approved) and that age requirements are met. If so updates the database with the result. Includes storing the blinded planchets and denomination signatures, or signaling conflict';
|