From 196a465c2254c224055b71a5bdb3697e7a468801 Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Mon, 20 Apr 2020 07:48:19 +0200 Subject: [PATCH] more comments, allow to pick up pow later --- src/include/gnunet_revocation_service.h | 44 ++++++++++++++++++- src/revocation/gnunet-revocation.c | 57 +++++++++++++++++-------- 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" */ #define POW_COUNT 32 + +GNUNET_NETWORK_STRUCT_BEGIN + struct GNUNET_REVOCATION_Pow { /** @@ -66,12 +69,12 @@ struct GNUNET_REVOCATION_Pow /** * The TTL of this revocation (purely informational) */ - uint64_t ttl; + uint64_t ttl GNUNET_PACKED; /** * The PoWs */ - uint64_t pow[POW_COUNT]; + uint64_t pow[POW_COUNT] GNUNET_PACKED; /** * The signature @@ -89,6 +92,9 @@ struct GNUNET_REVOCATION_Pow struct GNUNET_CRYPTO_EcdsaPublicKey key; }; +GNUNET_NETWORK_STRUCT_END + + struct GNUNET_REVOCATION_PowCalculationHandle; /** @@ -185,12 +191,35 @@ GNUNET_REVOCATION_check_pow (const struct GNUNET_REVOCATION_Pow *pow, unsigned int matching_bits); + +/** + * Initializes a fresh PoW computation + * + * @param key the key to calculate the PoW for. + * @param epochs the number of epochs for which the PoW must be valid. + * @param difficulty the base difficulty of the PoW + * @return a handle for use in PoW rounds + */ struct GNUNET_REVOCATION_PowCalculationHandle* GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, int epochs, unsigned int difficulty); +/** + * Initializes PoW computation based on an existing PoW. + * + * @param pow the PoW to continue the calculations from. + * @param epochs the number of epochs for which the PoW must be valid. + * @param difficulty the base difficulty of the PoW + * @return a handle for use in PoW rounds + */ +struct GNUNET_REVOCATION_PowCalculationHandle* +GNUNET_REVOCATION_pow_init2 (const struct GNUNET_REVOCATION_Pow *pow, + int epochs, + unsigned int difficulty); + + /** * Calculate a key revocation valid for broadcasting for a number * of epochs. @@ -205,11 +234,22 @@ int GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc); +/** + * Return the curren PoW state from the calculation + * + * @param pc the calculation to get it from + * @return a pointer to the PoW + */ const struct GNUNET_REVOCATION_Pow* GNUNET_REVOCATION_pow_get (const struct GNUNET_REVOCATION_PowCalculationHandle *pc); +/** + * Cleanup a PoW calculation + * + * @param pc the calculation to clean up + */ void GNUNET_REVOCATION_pow_cleanup (struct 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 @@ #include "gnunet_revocation_service.h" #include "gnunet_identity_service.h" +/** + * Pow passes + */ +static unsigned int pow_passes = 1; /** * Final status code. @@ -93,6 +97,7 @@ static struct GNUNET_SCHEDULER_Task *pow_task; static void do_shutdown (void *cls) { + fprintf (stderr, "%s", _ ("Shutting down...\n")); if (NULL != el) { GNUNET_IDENTITY_ego_lookup_cancel (el); @@ -220,8 +225,6 @@ struct RevocationData static void perform_revocation (const struct GNUNET_REVOCATION_Pow *pow) { - struct GNUNET_TIME_Absolute ts; - h = GNUNET_REVOCATION_revoke (cfg, pow, &print_revocation_result, @@ -239,10 +242,10 @@ static void sync_pow (const struct GNUNET_REVOCATION_Pow *pow) { if ((NULL != filename) && - (sizeof(struct GNUNET_REVOCATION_Pow) == + (sizeof(struct GNUNET_REVOCATION_Pow) != GNUNET_DISK_fn_write (filename, - &pow, - sizeof(pow), + pow, + sizeof(struct GNUNET_REVOCATION_Pow), GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", filename); @@ -258,7 +261,8 @@ static void calculate_pow_shutdown (void *cls) { struct GNUNET_REVOCATION_PowCalculationHandle *ph = cls; - + fprintf (stderr, "%s", _ ("Cancelling calculation.\n")); + sync_pow (GNUNET_REVOCATION_pow_get (ph)); if (NULL != pow_task) { GNUNET_SCHEDULER_cancel (pow_task); @@ -280,8 +284,8 @@ calculate_pow (void *cls) /* store temporary results */ pow_task = NULL; - // if (0 == (rd->pow % 128)) - // sync_rd (rd); + if (0 == (pow_passes % 128)) + sync_pow (GNUNET_REVOCATION_pow_get(ph)); /* actually do POW calculation */ if (GNUNET_OK == GNUNET_REVOCATION_pow_round (ph)) { @@ -309,7 +313,19 @@ calculate_pow (void *cls) } return; } - pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, ph); + pow_passes++; + /** + * Otherwise CTRL-C does not work + */ + if (0 == pow_passes % 128) + pow_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, + &calculate_pow, + ph); + else + pow_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, + &calculate_pow, + ph); + } @@ -324,6 +340,7 @@ ego_callback (void *cls, const struct GNUNET_IDENTITY_Ego *ego) { struct GNUNET_REVOCATION_Pow *pow; struct GNUNET_CRYPTO_EcdsaPublicKey key; + struct GNUNET_REVOCATION_PowCalculationHandle *ph = NULL; el = NULL; if (NULL == ego) @@ -360,20 +377,24 @@ ego_callback (void *cls, const struct GNUNET_IDENTITY_Ego *ego) GNUNET_free (pow); return; } + /** + * Certificate not yet ready + */ fprintf (stderr, - _ ("Error: revocation certificate in `%s' invalid\n"), - filename); + "%s", + _("Continuing calculation where left off...\n")); + ph = GNUNET_REVOCATION_pow_init2 (pow, + 1, /* Epochs */ + matching_bits); GNUNET_free (pow); - return; } fprintf (stderr, "%s", _ ("Revocation certificate not ready, calculating proof of work\n")); - GNUNET_free (pow); - struct GNUNET_REVOCATION_PowCalculationHandle *ph; - ph = GNUNET_REVOCATION_pow_init (&key, - 1, /* Epochs */ - matching_bits); + if (NULL == ph) + ph = GNUNET_REVOCATION_pow_init (&key, + 1, /* Epochs */ + matching_bits); pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, ph); GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, ph); } @@ -456,7 +477,7 @@ run (void *cls, (unsigned int) matching_bits)) { struct GNUNET_REVOCATION_PowCalculationHandle *ph; - ph = GNUNET_REVOCATION_pow_init (&pk, + ph = GNUNET_REVOCATION_pow_init2 (&pow, 1, /* Epochs */ matching_bits); 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, } +/** + * Initializes a fresh PoW computation + * + * @param key the key to calculate the PoW for. + * @param epochs the number of epochs for which the PoW must be valid. + * @param difficulty the base difficulty of the PoW + * @return a handle for use in PoW rounds + */ struct GNUNET_REVOCATION_PowCalculationHandle* GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, int epochs, unsigned int difficulty) { - struct GNUNET_REVOCATION_PowCalculationHandle*pc; + struct GNUNET_REVOCATION_PowCalculationHandle *pc; struct GNUNET_TIME_Absolute ts = GNUNET_TIME_absolute_get (); pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); @@ -486,6 +494,33 @@ GNUNET_REVOCATION_pow_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *key, } +/** + * Initializes PoW computation based on an existing PoW. + * + * @param pow the PoW to continue the calculations from. + * @param epochs the number of epochs for which the PoW must be valid. + * @param difficulty the base difficulty of the PoW + * @return a handle for use in PoW rounds + */ +struct GNUNET_REVOCATION_PowCalculationHandle* +GNUNET_REVOCATION_pow_init2 (const struct GNUNET_REVOCATION_Pow *pow, + int epochs, + unsigned int difficulty) +{ + struct GNUNET_REVOCATION_PowCalculationHandle *pc; + + pc = GNUNET_new (struct GNUNET_REVOCATION_PowCalculationHandle); + pc->pow.key = pow->key; + pc->pow.timestamp = pow->timestamp; + pc->current_pow = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, + UINT64_MAX); + pc->difficulty = difficulty; + pc->epochs = epochs; + return pc; +} + + + /** * Calculate a key revocation valid for broadcasting for a number * of epochs. @@ -544,15 +579,25 @@ GNUNET_REVOCATION_pow_round (struct GNUNET_REVOCATION_PowCalculationHandle *pc) } +/** + * Return the curren PoW state from the calculation + * + * @param pc the calculation to get it from + * @return a pointer to the PoW + */ const struct GNUNET_REVOCATION_Pow* GNUNET_REVOCATION_pow_get (const struct GNUNET_REVOCATION_PowCalculationHandle *pc) { - return calculate_score (pc) >= pc->difficulty + pc->epochs ? &pc->pow : - NULL; + return &pc->pow; } +/** + * Cleanup a PoW calculation + * + * @param pc the calculation to clean up + */ void GNUNET_REVOCATION_pow_cleanup (struct GNUNET_REVOCATION_PowCalculationHandle *pc) -- 2.25.1