diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-04-20 07:48:19 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2020-04-20 07:48:19 +0200 |
commit | 196a465c2254c224055b71a5bdb3697e7a468801 (patch) | |
tree | e6ab30ab3458ceb77bca7a54f995a604de540f7c /src | |
parent | 2408dd18af486517a484544ebf468e5790e7ce05 (diff) | |
download | gnunet-196a465c2254c224055b71a5bdb3697e7a468801.tar.gz gnunet-196a465c2254c224055b71a5bdb3697e7a468801.zip |
more comments, allow to pick up pow later
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_revocation_service.h | 44 | ||||
-rw-r--r-- | src/revocation/gnunet-revocation.c | 57 | ||||
-rw-r--r-- | src/revocation/revocation_api.c | 51 |
3 files changed, 129 insertions, 23 deletions
diff --git a/src/include/gnunet_revocation_service.h b/src/include/gnunet_revocation_service.h index 775da01ac..a5a014708 100644 --- a/src/include/gnunet_revocation_service.h +++ b/src/include/gnunet_revocation_service.h | |||
@@ -56,6 +56,9 @@ extern "C" | |||
56 | */ | 56 | */ |
57 | #define POW_COUNT 32 | 57 | #define POW_COUNT 32 |
58 | 58 | ||
59 | |||
60 | GNUNET_NETWORK_STRUCT_BEGIN | ||
61 | |||
59 | struct GNUNET_REVOCATION_Pow | 62 | struct GNUNET_REVOCATION_Pow |
60 | { | 63 | { |
61 | /** | 64 | /** |
@@ -66,12 +69,12 @@ struct GNUNET_REVOCATION_Pow | |||
66 | /** | 69 | /** |
67 | * The TTL of this revocation (purely informational) | 70 | * The TTL of this revocation (purely informational) |
68 | */ | 71 | */ |
69 | uint64_t ttl; | 72 | uint64_t ttl GNUNET_PACKED; |
70 | 73 | ||
71 | /** | 74 | /** |
72 | * The PoWs | 75 | * The PoWs |
73 | */ | 76 | */ |
74 | uint64_t pow[POW_COUNT]; | 77 | uint64_t pow[POW_COUNT] GNUNET_PACKED; |
75 | 78 | ||
76 | /** | 79 | /** |
77 | * The signature | 80 | * The signature |
@@ -89,6 +92,9 @@ struct GNUNET_REVOCATION_Pow | |||
89 | struct GNUNET_CRYPTO_EcdsaPublicKey key; | 92 | struct GNUNET_CRYPTO_EcdsaPublicKey key; |
90 | }; | 93 | }; |
91 | 94 | ||
95 | GNUNET_NETWORK_STRUCT_END | ||
96 | |||
97 | |||
92 | struct GNUNET_REVOCATION_PowCalculationHandle; | 98 | struct GNUNET_REVOCATION_PowCalculationHandle; |
93 | 99 | ||
94 | /** | 100 | /** |
@@ -185,6 +191,15 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_Pow *pow, | |||
185 | unsigned int matching_bits); | 191 | unsigned int matching_bits); |
186 | 192 | ||
187 | 193 | ||
194 | |||
195 | /** | ||
196 | * Initializes a fresh PoW computation | ||
197 | * | ||
198 | * @param key the key to calculate the PoW for. | ||
199 | * @param epochs the number of epochs for which the PoW must be valid. | ||
200 | * @param difficulty the base difficulty of the PoW | ||
201 | * @return a handle for use in PoW rounds | ||
202 | */ | ||
188 | struct GNUNET_REVOCATION_PowCalculationHandle* | 203 | struct GNUNET_REVOCATION_PowCalculationHandle* |
189 | GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, | 204 | GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, |
190 | int epochs, | 205 | int epochs, |
@@ -192,6 +207,20 @@ GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, | |||
192 | 207 | ||
193 | 208 | ||
194 | /** | 209 | /** |
210 | * Initializes PoW computation based on an existing PoW. | ||
211 | * | ||
212 | * @param pow the PoW to continue the calculations from. | ||
213 | * @param epochs the number of epochs for which the PoW must be valid. | ||
214 | * @param difficulty the base difficulty of the PoW | ||
215 | * @return a handle for use in PoW rounds | ||
216 | */ | ||
217 | struct GNUNET_REVOCATION_PowCalculationHandle* | ||
218 | GNUNET_REVOCATION_pow_init2 (const struct GNUNET_REVOCATION_Pow *pow, | ||
219 | int epochs, | ||
220 | unsigned int difficulty); | ||
221 | |||
222 | |||
223 | /** | ||
195 | * Calculate a key revocation valid for broadcasting for a number | 224 | * Calculate a key revocation valid for broadcasting for a number |
196 | * of epochs. | 225 | * of epochs. |
197 | * | 226 | * |
@@ -205,11 +234,22 @@ int | |||
205 | GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc); | 234 | GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc); |
206 | 235 | ||
207 | 236 | ||
237 | /** | ||
238 | * Return the curren PoW state from the calculation | ||
239 | * | ||
240 | * @param pc the calculation to get it from | ||
241 | * @return a pointer to the PoW | ||
242 | */ | ||
208 | const struct GNUNET_REVOCATION_Pow* | 243 | const struct GNUNET_REVOCATION_Pow* |
209 | GNUNET_REVOCATION_pow_get (const struct | 244 | GNUNET_REVOCATION_pow_get (const struct |
210 | GNUNET_REVOCATION_PowCalculationHandle *pc); | 245 | GNUNET_REVOCATION_PowCalculationHandle *pc); |
211 | 246 | ||
212 | 247 | ||
248 | /** | ||
249 | * Cleanup a PoW calculation | ||
250 | * | ||
251 | * @param pc the calculation to clean up | ||
252 | */ | ||
213 | void | 253 | void |
214 | GNUNET_REVOCATION_pow_cleanup (struct | 254 | GNUNET_REVOCATION_pow_cleanup (struct |
215 | GNUNET_REVOCATION_PowCalculationHandle *pc); | 255 | GNUNET_REVOCATION_PowCalculationHandle *pc); |
diff --git a/src/revocation/gnunet-revocation.c b/src/revocation/gnunet-revocation.c index 16f62de9d..5566162f4 100644 --- a/src/revocation/gnunet-revocation.c +++ b/src/revocation/gnunet-revocation.c | |||
@@ -28,6 +28,10 @@ | |||
28 | #include "gnunet_revocation_service.h" | 28 | #include "gnunet_revocation_service.h" |
29 | #include "gnunet_identity_service.h" | 29 | #include "gnunet_identity_service.h" |
30 | 30 | ||
31 | /** | ||
32 | * Pow passes | ||
33 | */ | ||
34 | static unsigned int pow_passes = 1; | ||
31 | 35 | ||
32 | /** | 36 | /** |
33 | * Final status code. | 37 | * Final status code. |
@@ -93,6 +97,7 @@ static struct GNUNET_SCHEDULER_Task *pow_task; | |||
93 | static void | 97 | static void |
94 | do_shutdown (void *cls) | 98 | do_shutdown (void *cls) |
95 | { | 99 | { |
100 | fprintf (stderr, "%s", _ ("Shutting down...\n")); | ||
96 | if (NULL != el) | 101 | if (NULL != el) |
97 | { | 102 | { |
98 | GNUNET_IDENTITY_ego_lookup_cancel (el); | 103 | GNUNET_IDENTITY_ego_lookup_cancel (el); |
@@ -220,8 +225,6 @@ struct RevocationData | |||
220 | static void | 225 | static void |
221 | perform_revocation (const struct GNUNET_REVOCATION_Pow *pow) | 226 | perform_revocation (const struct GNUNET_REVOCATION_Pow *pow) |
222 | { | 227 | { |
223 | struct GNUNET_TIME_Absolute ts; | ||
224 | |||
225 | h = GNUNET_REVOCATION_revoke (cfg, | 228 | h = GNUNET_REVOCATION_revoke (cfg, |
226 | pow, | 229 | pow, |
227 | &print_revocation_result, | 230 | &print_revocation_result, |
@@ -239,10 +242,10 @@ static void | |||
239 | sync_pow (const struct GNUNET_REVOCATION_Pow *pow) | 242 | sync_pow (const struct GNUNET_REVOCATION_Pow *pow) |
240 | { | 243 | { |
241 | if ((NULL != filename) && | 244 | if ((NULL != filename) && |
242 | (sizeof(struct GNUNET_REVOCATION_Pow) == | 245 | (sizeof(struct GNUNET_REVOCATION_Pow) != |
243 | GNUNET_DISK_fn_write (filename, | 246 | GNUNET_DISK_fn_write (filename, |
244 | &pow, | 247 | pow, |
245 | sizeof(pow), | 248 | sizeof(struct GNUNET_REVOCATION_Pow), |
246 | GNUNET_DISK_PERM_USER_READ | 249 | GNUNET_DISK_PERM_USER_READ |
247 | | GNUNET_DISK_PERM_USER_WRITE))) | 250 | | GNUNET_DISK_PERM_USER_WRITE))) |
248 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", filename); | 251 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", filename); |
@@ -258,7 +261,8 @@ static void | |||
258 | calculate_pow_shutdown (void *cls) | 261 | calculate_pow_shutdown (void *cls) |
259 | { | 262 | { |
260 | struct GNUNET_REVOCATION_PowCalculationHandle *ph = cls; | 263 | struct GNUNET_REVOCATION_PowCalculationHandle *ph = cls; |
261 | 264 | fprintf (stderr, "%s", _ ("Cancelling calculation.\n")); | |
265 | sync_pow (GNUNET_REVOCATION_pow_get (ph)); | ||
262 | if (NULL != pow_task) | 266 | if (NULL != pow_task) |
263 | { | 267 | { |
264 | GNUNET_SCHEDULER_cancel (pow_task); | 268 | GNUNET_SCHEDULER_cancel (pow_task); |
@@ -280,8 +284,8 @@ calculate_pow (void *cls) | |||
280 | 284 | ||
281 | /* store temporary results */ | 285 | /* store temporary results */ |
282 | pow_task = NULL; | 286 | pow_task = NULL; |
283 | // if (0 == (rd->pow % 128)) | 287 | if (0 == (pow_passes % 128)) |
284 | // sync_rd (rd); | 288 | sync_pow (GNUNET_REVOCATION_pow_get(ph)); |
285 | /* actually do POW calculation */ | 289 | /* actually do POW calculation */ |
286 | if (GNUNET_OK == GNUNET_REVOCATION_pow_round (ph)) | 290 | if (GNUNET_OK == GNUNET_REVOCATION_pow_round (ph)) |
287 | { | 291 | { |
@@ -309,7 +313,19 @@ calculate_pow (void *cls) | |||
309 | } | 313 | } |
310 | return; | 314 | return; |
311 | } | 315 | } |
312 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, ph); | 316 | pow_passes++; |
317 | /** | ||
318 | * Otherwise CTRL-C does not work | ||
319 | */ | ||
320 | if (0 == pow_passes % 128) | ||
321 | pow_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, | ||
322 | &calculate_pow, | ||
323 | ph); | ||
324 | else | ||
325 | pow_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, | ||
326 | &calculate_pow, | ||
327 | ph); | ||
328 | |||
313 | } | 329 | } |
314 | 330 | ||
315 | 331 | ||
@@ -324,6 +340,7 @@ ego_callback (void *cls, const struct GNUNET_IDENTITY_Ego *ego) | |||
324 | { | 340 | { |
325 | struct GNUNET_REVOCATION_Pow *pow; | 341 | struct GNUNET_REVOCATION_Pow *pow; |
326 | struct GNUNET_CRYPTO_EcdsaPublicKey key; | 342 | struct GNUNET_CRYPTO_EcdsaPublicKey key; |
343 | struct GNUNET_REVOCATION_PowCalculationHandle *ph = NULL; | ||
327 | 344 | ||
328 | el = NULL; | 345 | el = NULL; |
329 | if (NULL == ego) | 346 | if (NULL == ego) |
@@ -360,20 +377,24 @@ ego_callback (void *cls, const struct GNUNET_IDENTITY_Ego *ego) | |||
360 | GNUNET_free (pow); | 377 | GNUNET_free (pow); |
361 | return; | 378 | return; |
362 | } | 379 | } |
380 | /** | ||
381 | * Certificate not yet ready | ||
382 | */ | ||
363 | fprintf (stderr, | 383 | fprintf (stderr, |
364 | _ ("Error: revocation certificate in `%s' invalid\n"), | 384 | "%s", |
365 | filename); | 385 | _("Continuing calculation where left off...\n")); |
386 | ph = GNUNET_REVOCATION_pow_init2 (pow, | ||
387 | 1, /* Epochs */ | ||
388 | matching_bits); | ||
366 | GNUNET_free (pow); | 389 | GNUNET_free (pow); |
367 | return; | ||
368 | } | 390 | } |
369 | fprintf (stderr, | 391 | fprintf (stderr, |
370 | "%s", | 392 | "%s", |
371 | _ ("Revocation certificate not ready, calculating proof of work\n")); | 393 | _ ("Revocation certificate not ready, calculating proof of work\n")); |
372 | GNUNET_free (pow); | 394 | if (NULL == ph) |
373 | struct GNUNET_REVOCATION_PowCalculationHandle *ph; | 395 | ph = GNUNET_REVOCATION_pow_init (&key, |
374 | ph = GNUNET_REVOCATION_pow_init (&key, | 396 | 1, /* Epochs */ |
375 | 1, /* Epochs */ | 397 | matching_bits); |
376 | matching_bits); | ||
377 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, ph); | 398 | pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, ph); |
378 | GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, ph); | 399 | GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, ph); |
379 | } | 400 | } |
@@ -456,7 +477,7 @@ run (void *cls, | |||
456 | (unsigned int) matching_bits)) | 477 | (unsigned int) matching_bits)) |
457 | { | 478 | { |
458 | struct GNUNET_REVOCATION_PowCalculationHandle *ph; | 479 | struct GNUNET_REVOCATION_PowCalculationHandle *ph; |
459 | ph = GNUNET_REVOCATION_pow_init (&pk, | 480 | ph = GNUNET_REVOCATION_pow_init2 (&pow, |
460 | 1, /* Epochs */ | 481 | 1, /* Epochs */ |
461 | matching_bits); | 482 | matching_bits); |
462 | 483 | ||
diff --git a/src/revocation/revocation_api.c b/src/revocation/revocation_api.c index 667ed4ec1..565ce9d0d 100644 --- a/src/revocation/revocation_api.c +++ b/src/revocation/revocation_api.c | |||
@@ -467,12 +467,20 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_Pow *pow, | |||
467 | } | 467 | } |
468 | 468 | ||
469 | 469 | ||
470 | /** | ||
471 | * Initializes a fresh PoW computation | ||
472 | * | ||
473 | * @param key the key to calculate the PoW for. | ||
474 | * @param epochs the number of epochs for which the PoW must be valid. | ||
475 | * @param difficulty the base difficulty of the PoW | ||
476 | * @return a handle for use in PoW rounds | ||
477 | */ | ||
470 | struct GNUNET_REVOCATION_PowCalculationHandle* | 478 | struct GNUNET_REVOCATION_PowCalculationHandle* |
471 | GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, | 479 | GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, |
472 | int epochs, | 480 | int epochs, |
473 | unsigned int difficulty) | 481 | unsigned int difficulty) |
474 | { | 482 | { |
475 | struct GNUNET_REVOCATION_PowCalculationHandle*pc; | 483 | struct GNUNET_REVOCATION_PowCalculationHandle *pc; |
476 | struct GNUNET_TIME_Absolute ts = GNUNET_TIME_absolute_get (); | 484 | struct GNUNET_TIME_Absolute ts = GNUNET_TIME_absolute_get (); |
477 | 485 | ||
478 | pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); | 486 | pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); |
@@ -487,6 +495,33 @@ GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, | |||
487 | 495 | ||
488 | 496 | ||
489 | /** | 497 | /** |
498 | * Initializes PoW computation based on an existing PoW. | ||
499 | * | ||
500 | * @param pow the PoW to continue the calculations from. | ||
501 | * @param epochs the number of epochs for which the PoW must be valid. | ||
502 | * @param difficulty the base difficulty of the PoW | ||
503 | * @return a handle for use in PoW rounds | ||
504 | */ | ||
505 | struct GNUNET_REVOCATION_PowCalculationHandle* | ||
506 | GNUNET_REVOCATION_pow_init2 (const struct GNUNET_REVOCATION_Pow *pow, | ||
507 | int epochs, | ||
508 | unsigned int difficulty) | ||
509 | { | ||
510 | struct GNUNET_REVOCATION_PowCalculationHandle *pc; | ||
511 | |||
512 | pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); | ||
513 | pc->pow.key = pow->key; | ||
514 | pc->pow.timestamp = pow->timestamp; | ||
515 | pc->current_pow = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
516 | UINT64_MAX); | ||
517 | pc->difficulty = difficulty; | ||
518 | pc->epochs = epochs; | ||
519 | return pc; | ||
520 | } | ||
521 | |||
522 | |||
523 | |||
524 | /** | ||
490 | * Calculate a key revocation valid for broadcasting for a number | 525 | * Calculate a key revocation valid for broadcasting for a number |
491 | * of epochs. | 526 | * of epochs. |
492 | * | 527 | * |
@@ -544,15 +579,25 @@ GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc) | |||
544 | } | 579 | } |
545 | 580 | ||
546 | 581 | ||
582 | /** | ||
583 | * Return the curren PoW state from the calculation | ||
584 | * | ||
585 | * @param pc the calculation to get it from | ||
586 | * @return a pointer to the PoW | ||
587 | */ | ||
547 | const struct GNUNET_REVOCATION_Pow* | 588 | const struct GNUNET_REVOCATION_Pow* |
548 | GNUNET_REVOCATION_pow_get (const struct | 589 | GNUNET_REVOCATION_pow_get (const struct |
549 | GNUNET_REVOCATION_PowCalculationHandle *pc) | 590 | GNUNET_REVOCATION_PowCalculationHandle *pc) |
550 | { | 591 | { |
551 | return calculate_score (pc) >= pc->difficulty + pc->epochs ? &pc->pow : | 592 | return &pc->pow; |
552 | NULL; | ||
553 | } | 593 | } |
554 | 594 | ||
555 | 595 | ||
596 | /** | ||
597 | * Cleanup a PoW calculation | ||
598 | * | ||
599 | * @param pc the calculation to clean up | ||
600 | */ | ||
556 | void | 601 | void |
557 | GNUNET_REVOCATION_pow_cleanup (struct | 602 | GNUNET_REVOCATION_pow_cleanup (struct |
558 | GNUNET_REVOCATION_PowCalculationHandle *pc) | 603 | GNUNET_REVOCATION_PowCalculationHandle *pc) |