diff options
Diffstat (limited to 'src/revocation/gnunet-revocation.c')
-rw-r--r-- | src/revocation/gnunet-revocation.c | 102 |
1 files changed, 65 insertions, 37 deletions
diff --git a/src/revocation/gnunet-revocation.c b/src/revocation/gnunet-revocation.c index f68c18aa4..5e983a079 100644 --- a/src/revocation/gnunet-revocation.c +++ b/src/revocation/gnunet-revocation.c | |||
@@ -79,6 +79,11 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
79 | */ | 79 | */ |
80 | static unsigned long long matching_bits; | 80 | static unsigned long long matching_bits; |
81 | 81 | ||
82 | /** | ||
83 | * Task used for proof-of-work calculation. | ||
84 | */ | ||
85 | static struct GNUNET_SCHEDULER_Task *pow_task; | ||
86 | |||
82 | 87 | ||
83 | /** | 88 | /** |
84 | * Function run if the user aborts with CTRL-C. | 89 | * Function run if the user aborts with CTRL-C. |
@@ -226,32 +231,60 @@ perform_revocation (const struct RevocationData *rd) | |||
226 | 231 | ||
227 | 232 | ||
228 | /** | 233 | /** |
234 | * Write the current state of the revocation data | ||
235 | * to disk. | ||
236 | * | ||
237 | * @param rd data to sync | ||
238 | */ | ||
239 | static void | ||
240 | sync_rd (const struct RevocationData *rd) | ||
241 | { | ||
242 | if ( (NULL != filename) && | ||
243 | (sizeof (struct RevocationData) == | ||
244 | GNUNET_DISK_fn_write (filename, | ||
245 | &rd, | ||
246 | sizeof (rd), | ||
247 | GNUNET_DISK_PERM_USER_READ | | ||
248 | GNUNET_DISK_PERM_USER_WRITE)) ) | ||
249 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
250 | "write", | ||
251 | filename); | ||
252 | } | ||
253 | |||
254 | |||
255 | /** | ||
229 | * Perform the proof-of-work calculation. | 256 | * Perform the proof-of-work calculation. |
230 | * | 257 | * |
231 | * @param cls the `struct RevocationData` | 258 | * @param cls the `struct RevocationData` |
232 | */ | 259 | */ |
233 | static void | 260 | static void |
234 | calculate_pow (void *cls) | 261 | calculate_pow_shutdown (void *cls) |
235 | { | 262 | { |
236 | struct RevocationData *rd = cls; | 263 | struct RevocationData *rd = cls; |
237 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
238 | 264 | ||
239 | /* store temporary results */ | 265 | if (NULL != pow_task) |
240 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
241 | if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) || | ||
242 | (0 == (rd->pow % 128) ) ) | ||
243 | { | 266 | { |
244 | if ( (NULL != filename) && | 267 | GNUNET_SCHEDULER_cancel (pow_task); |
245 | (sizeof (struct RevocationData) == | 268 | pow_task = NULL; |
246 | GNUNET_DISK_fn_write (filename, | ||
247 | &rd, | ||
248 | sizeof (rd), | ||
249 | GNUNET_DISK_PERM_USER_READ | | ||
250 | GNUNET_DISK_PERM_USER_WRITE)) ) | ||
251 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
252 | "write", | ||
253 | filename); | ||
254 | } | 269 | } |
270 | sync_rd (rd); | ||
271 | GNUNET_free (rd); | ||
272 | } | ||
273 | |||
274 | |||
275 | /** | ||
276 | * Perform the proof-of-work calculation. | ||
277 | * | ||
278 | * @param cls the `struct RevocationData` | ||
279 | */ | ||
280 | static void | ||
281 | calculate_pow (void *cls) | ||
282 | { | ||
283 | struct RevocationData *rd = cls; | ||
284 | |||
285 | /* store temporary results */ | ||
286 | if (0 == (rd->pow % 128)) | ||
287 | sync_rd (rd); | ||
255 | /* display progress estimate */ | 288 | /* display progress estimate */ |
256 | if ( (0 == ((1 << matching_bits) / 100 / 50)) || | 289 | if ( (0 == ((1 << matching_bits) / 100 / 50)) || |
257 | (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) ) | 290 | (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) ) |
@@ -261,11 +294,6 @@ calculate_pow (void *cls) | |||
261 | (0 == (rd->pow % ((1 << matching_bits) / 100))) ) ) | 294 | (0 == (rd->pow % ((1 << matching_bits) / 100))) ) ) |
262 | FPRINTF (stderr, " - @ %3u%% (estimate)\n", | 295 | FPRINTF (stderr, " - @ %3u%% (estimate)\n", |
263 | (unsigned int) (rd->pow * 100) / (1 << matching_bits)); | 296 | (unsigned int) (rd->pow * 100) / (1 << matching_bits)); |
264 | if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) | ||
265 | { | ||
266 | GNUNET_free (rd); | ||
267 | return; | ||
268 | } | ||
269 | /* actually do POW calculation */ | 297 | /* actually do POW calculation */ |
270 | rd->pow++; | 298 | rd->pow++; |
271 | if (GNUNET_OK == | 299 | if (GNUNET_OK == |
@@ -297,8 +325,8 @@ calculate_pow (void *cls) | |||
297 | GNUNET_free (rd); | 325 | GNUNET_free (rd); |
298 | return; | 326 | return; |
299 | } | 327 | } |
300 | GNUNET_SCHEDULER_add_now (&calculate_pow, | 328 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, |
301 | rd); | 329 | rd); |
302 | } | 330 | } |
303 | 331 | ||
304 | 332 | ||
@@ -371,8 +399,10 @@ ego_callback (void *cls, | |||
371 | FPRINTF (stderr, | 399 | FPRINTF (stderr, |
372 | "%s", | 400 | "%s", |
373 | _("Revocation certificate not ready, calculating proof of work\n")); | 401 | _("Revocation certificate not ready, calculating proof of work\n")); |
374 | GNUNET_SCHEDULER_add_now (&calculate_pow, | 402 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, |
375 | rd); | 403 | rd); |
404 | GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, | ||
405 | rd); | ||
376 | } | 406 | } |
377 | 407 | ||
378 | 408 | ||
@@ -406,9 +436,8 @@ run (void *cls, | |||
406 | test_ego); | 436 | test_ego); |
407 | return; | 437 | return; |
408 | } | 438 | } |
409 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 439 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
410 | &do_shutdown, | 440 | NULL); |
411 | NULL); | ||
412 | q = GNUNET_REVOCATION_query (cfg, | 441 | q = GNUNET_REVOCATION_query (cfg, |
413 | &pk, | 442 | &pk, |
414 | &print_query_result, | 443 | &print_query_result, |
@@ -444,9 +473,8 @@ run (void *cls, | |||
444 | revoke_ego, | 473 | revoke_ego, |
445 | &ego_callback, | 474 | &ego_callback, |
446 | NULL); | 475 | NULL); |
447 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 476 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
448 | &do_shutdown, | 477 | NULL); |
449 | NULL); | ||
450 | return; | 478 | return; |
451 | } | 479 | } |
452 | if ( (NULL != filename) && | 480 | if ( (NULL != filename) && |
@@ -462,9 +490,8 @@ run (void *cls, | |||
462 | filename); | 490 | filename); |
463 | return; | 491 | return; |
464 | } | 492 | } |
465 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 493 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
466 | &do_shutdown, | 494 | NULL); |
467 | NULL); | ||
468 | if (GNUNET_YES != | 495 | if (GNUNET_YES != |
469 | GNUNET_REVOCATION_check_pow (&rd.key, | 496 | GNUNET_REVOCATION_check_pow (&rd.key, |
470 | rd.pow, | 497 | rd.pow, |
@@ -473,10 +500,11 @@ run (void *cls, | |||
473 | struct RevocationData *cp = GNUNET_new (struct RevocationData); | 500 | struct RevocationData *cp = GNUNET_new (struct RevocationData); |
474 | 501 | ||
475 | *cp = rd; | 502 | *cp = rd; |
476 | GNUNET_SCHEDULER_add_now (&calculate_pow, | 503 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, |
477 | cp); | 504 | cp); |
505 | GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, | ||
506 | cp); | ||
478 | return; | 507 | return; |
479 | |||
480 | } | 508 | } |
481 | perform_revocation (&rd); | 509 | perform_revocation (&rd); |
482 | return; | 510 | return; |