From: Omar Tarabai Date: Wed, 20 Aug 2014 10:18:03 +0000 (+0000) Subject: sensor: fixes for proof-of-work, test passes now X-Git-Tag: initial-import-from-subversion-38251~3285 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=ab18dc025d49c720bf59eb88748a8c1d58d50af2;p=oweals%2Fgnunet.git sensor: fixes for proof-of-work, test passes now --- diff --git a/src/sensor/sensor_util_lib_crypto.c b/src/sensor/sensor_util_lib_crypto.c index 7cf505186..e097ed3ae 100644 --- a/src/sensor/sensor_util_lib_crypto.c +++ b/src/sensor/sensor_util_lib_crypto.c @@ -167,8 +167,8 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) htonl (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT); result_block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + - sizeof (struct GNUNET_TIME_Absolute) + - sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size); + sizeof (struct GNUNET_TIME_Absolute) + + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + cx->msg_size); memcpy (&result_block[1], &cx[1], cx->msg_size); sign_result = GNUNET_CRYPTO_eddsa_sign (&cx->private_key, &result_block->purpose, @@ -178,6 +178,7 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_SENSOR_crypto_pow_sign_cancel (cx); if (NULL != callback) callback (callback_cls, (GNUNET_OK == sign_result) ? result_block : NULL); + return; } cx->pow++; cx->calculate_pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, cx); @@ -193,7 +194,13 @@ void GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context *cx) { + if (GNUNET_SCHEDULER_NO_TASK != cx->calculate_pow_task) + { + GNUNET_SCHEDULER_cancel (cx->calculate_pow_task); + cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK; + } GNUNET_free (cx); + cx = NULL; } @@ -262,23 +269,27 @@ GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block * void **payload) { /* Check public key */ - if (0 != memcmp (public_key, &block->public_key, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) + if (0 != + memcmp (public_key, &block->public_key, + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))) { LOG (GNUNET_ERROR_TYPE_WARNING, "Public key mismatch.\n"); return 0; } /* Check signature */ if (GNUNET_OK != - GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose, - &block->signature, public_key)) + GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose, &block->signature, + public_key)) { LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n"); return 0; } /* Check pow */ - if (GNUNET_NO == check_pow (&block->timestamp, - sizeof (struct GNUNET_TIME_Absolute) + - sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size, block->pow, matching_bits)) + if (GNUNET_NO == + check_pow (&block->timestamp, + sizeof (struct GNUNET_TIME_Absolute) + + sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + block->msg_size, + block->pow, matching_bits)) { LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid proof-of-work.\n"); return 0; diff --git a/src/sensor/test_gnunet-service-sensor_reporting.c b/src/sensor/test_gnunet-service-sensor_reporting.c index afe45afea..acb0e7165 100644 --- a/src/sensor/test_gnunet-service-sensor_reporting.c +++ b/src/sensor/test_gnunet-service-sensor_reporting.c @@ -356,7 +356,11 @@ sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op, { struct TestPeer *peer = cls; - GNUNET_assert (NULL == emsg); + if (NULL != emsg) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg); + GNUNET_assert (0); + } peer->sensor_op = GNUNET_TESTBED_service_connect (NULL, peer->testbed_peer, "sensor", &sensor_connect_cb, peer, @@ -441,7 +445,11 @@ static void dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg) { - GNUNET_assert (NULL == emsg); + if (NULL != emsg) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg); + GNUNET_assert (0); + } GNUNET_TESTBED_operation_done (op); /* Connect to peerstore service on first peer */ peerstore_op = diff --git a/src/sensor/test_pow_sign.c b/src/sensor/test_pow_sign.c index 84e990cff..e1e78a721 100644 --- a/src/sensor/test_pow_sign.c +++ b/src/sensor/test_pow_sign.c @@ -107,18 +107,29 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } -static void pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block) +static void +pow_cb (void *cls, struct GNUNET_SENSOR_crypto_pow_block *block) { void *response; - printf ("Received block:\n" - "pow: %" PRIu64 ".\n", block->pow); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received block:\n" "pow: %" PRIu64 ".\n", block->pow); + /* Test that the block is valid */ GNUNET_assert (MSG_SIZE == - GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS, - public_key, GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &response)); - GNUNET_assert (0 == memcmp(msg, response, MSG_SIZE)); + GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS, + public_key, + GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, + &response)); + GNUNET_assert (0 == memcmp (msg, response, MSG_SIZE)); + /* Modify the payload and test that verification returns invalid */ + block->pow++; + GNUNET_assert (0 == + GNUNET_SENSOR_crypto_verify_pow_sign (block, MATCHING_BITS, + public_key, + GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, + &response)); ok = 0; - GNUNET_SCHEDULER_cancel(shutdown_task); + GNUNET_SCHEDULER_cancel (shutdown_task); GNUNET_SCHEDULER_add_now (do_shutdown, NULL); } @@ -140,17 +151,19 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, struct GNUNET_TIME_Absolute timestamp; /* generate random data block */ - GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE); + GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, msg, MSG_SIZE); /* get private and public keys */ private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (pinfo->result.cfg); GNUNET_assert (NULL != private_key); public_key = GNUNET_new (struct GNUNET_CRYPTO_EddsaPublicKey); + GNUNET_CRYPTO_eddsa_key_get_public (private_key, public_key); /* create pow and sign */ - timestamp = GNUNET_TIME_absolute_get(); - GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, ×tamp, - public_key, private_key, MATCHING_BITS, &pow_cb, NULL); + timestamp = GNUNET_TIME_absolute_get (); + GNUNET_SENSOR_crypto_pow_sign (msg, MSG_SIZE, ×tamp, public_key, + private_key, MATCHING_BITS, &pow_cb, NULL); + GNUNET_TESTBED_operation_done (op); }