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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
This file is part of TALER
(C) 2020, 2021 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero 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/>
*/
/**
* @file taler-merchant-httpd_reserves.c
* @brief logic for initially tracking a reserve's status
* @author Christian Grothoff
*/
#include "platform.h"
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd.h"
#include "taler-merchant-httpd_exchanges.h"
#include "taler-merchant-httpd_reserves.h"
/**
* How long do we keep the long-poller open?
* Not very long here, as if the money has not
* yet arrived, there is a fair chance that it'll
* take much longer, and in that case we rather
* enter into the delay created by try_later().
*/
#define LONGPOLL_DELAY GNUNET_TIME_UNIT_MINUTES
/**
* Our representation of a reserve that we are (still) checking the status of.
*/
struct Reserve
{
/**
* Kept in a DLL.
*/
struct Reserve *next;
/**
* Kept in a DLL.
*/
struct Reserve *prev;
/**
* Reserve's public key.
*/
struct TALER_ReservePublicKeyP reserve_pub;
/**
* Amount the merchant expects to see in the reserve initially.
* We log a warning if there is a mismatch.
*/
struct TALER_Amount expected_amount;
/**
* URL of the exchange hosting this reserve.
*/
char *exchange_url;
/**
* Instance this reserve belongs with.
*/
char *instance_id;
/**
* Task scheduled waiting for a timeout for this reserve.
*/
struct GNUNET_SCHEDULER_Task *tt;
/**
* Get operation with the exchange.
*/
struct TALER_EXCHANGE_ReservesGetHandle *gh;
/**
* How long do we wait before trying this reserve again?
*/
struct GNUNET_TIME_Relative delay;
};
/**
* Head of DLL of pending reserves.
*/
static struct Reserve *reserves_head;
/**
* Tail of DLL of pending reserves.
*/
static struct Reserve *reserves_tail;
/**
* Function called to probe a reserve now.
*
* @param cls a `struct Reserve` to query
*/
static void
try_now (void *cls);
/**
* Free reserve data structure.
*
* @param r reserve to free
*/
static void
free_reserve (struct Reserve *r)
{
GNUNET_CONTAINER_DLL_remove (reserves_head,
reserves_tail,
r);
if (NULL != r->gh)
{
TALER_EXCHANGE_reserves_get_cancel (r->gh);
r->gh = NULL;
}
if (NULL != r->tt)
{
GNUNET_SCHEDULER_cancel (r->tt);
r->tt = NULL;
}
GNUNET_free (r->exchange_url);
GNUNET_free (r->instance_id);
GNUNET_free (r);
}
/**
* Schedule a job to probe a reserve later again.
*
* @param r reserve to try again later
*/
static void
try_later (struct Reserve *r)
{
/* minimum delay is the #LONGPOLL_DELAY */
r->delay = GNUNET_TIME_relative_max (LONGPOLL_DELAY,
r->delay);
/* STD_BACKOFF has a maximum of 15 minutes */
r->delay = GNUNET_TIME_STD_BACKOFF (r->delay);
r->tt = GNUNET_SCHEDULER_add_delayed (r->delay,
&try_now,
r);
}
/**
* Callbacks of this type are used to serve the result of submitting a
* reserve status request to a exchange.
*
* @param cls closure with a `struct Reserve *`
* @param rs HTTP response data
*/
static void
reserve_cb (void *cls,
const struct TALER_EXCHANGE_ReserveSummary *rs)
{
struct Reserve *r = cls;
enum GNUNET_DB_QueryStatus qs;
r->gh = NULL;
if (MHD_HTTP_OK != rs->hr.http_status)
{
try_later (r);
return;
}
if (GNUNET_OK !=
TALER_amount_cmp_currency (&r->expected_amount,
&rs->details.ok.balance))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Reserve currency disagreement: exchange `%s' has %s, expected %s\n",
r->exchange_url,
rs->details.ok.balance.currency,
r->expected_amount.currency);
free_reserve (r);
return;
}
if (0 !=
TALER_amount_cmp (&r->expected_amount,
&rs->details.ok.balance))
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Reserve initial balance disagreement: exchange `%s' received `%s'\n",
r->exchange_url,
TALER_amount2s (&rs->details.ok.balance));
}
qs = TMH_db->activate_reserve (TMH_db->cls,
r->instance_id,
&r->reserve_pub,
&rs->details.ok.balance);
if (qs <= 0)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to commit reserve activation to database (%d)\n",
(int) qs);
}
else
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Reserve activated with initial balance %s\n",
TALER_amount2s (&rs->details.ok.balance));
}
free_reserve (r);
}
/**
* Function called to probe a reserve now.
*
* @param cls a `struct Reserve` to query
*/
static void
try_now (void *cls)
{
struct Reserve *r = cls;
r->tt = NULL;
r->gh = TALER_EXCHANGE_reserves_get (TMH_curl_ctx,
r->exchange_url,
&r->reserve_pub,
LONGPOLL_DELAY,
&reserve_cb,
r);
if (NULL == r->gh)
{
try_later (r);
return;
}
}
/**
* Function called with information about a reserve that we need
* to check the status from at the exchange to see if/when it has
* been filled (and with what amount).
*
* @param cls closure, NULL
* @param instance_id for which instance is this reserve
* @param exchange_url base URL of the exchange at which the reserve lives
* @param reserve_pub public key of the reserve
* @param expected_amount how much do we expect to see in the reserve
*/
static void
add_reserve (void *cls,
const char *instance_id,
const char *exchange_url,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *expected_amount)
{
struct Reserve *r;
(void) cls;
r = GNUNET_new (struct Reserve);
r->exchange_url = GNUNET_strdup (exchange_url);
r->instance_id = GNUNET_strdup (instance_id);
r->reserve_pub = *reserve_pub;
r->expected_amount = *expected_amount;
GNUNET_CONTAINER_DLL_insert (reserves_head,
reserves_tail,
r);
try_now (r);
}
void
TMH_RESERVES_init (void)
{
TMH_db->lookup_pending_reserves (TMH_db->cls,
&add_reserve,
NULL);
}
void
TMH_RESERVES_check (const char *instance_id,
const char *exchange_url,
const struct TALER_ReservePublicKeyP *reserve_pub,
const struct TALER_Amount *expected_amount)
{
add_reserve (NULL,
instance_id,
exchange_url,
reserve_pub,
expected_amount);
}
void
TMH_RESERVES_done (void)
{
while (NULL != reserves_head)
free_reserve (reserves_head);
}
/* end of taler-merchant-httpd_reserves.c */
|