/** * Find our proof of work. * * @param cls closure (unused) */ static void find_proof (void *cls) { #define ROUND_SIZE 10 uint64_t counter; char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + sizeof (uint64_t)] GNUNET_ALIGN; struct GNUNET_HashCode result; unsigned int i; proof_task = NULL; GNUNET_memcpy (&buf[sizeof (uint64_t)], &my_identity, sizeof (struct GNUNET_PeerIdentity)); i = 0; counter = my_proof; while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) { GNUNET_memcpy (buf, &counter, sizeof (uint64_t)); pow_hash (buf, sizeof (buf), &result); if (nse_work_required <= count_leading_zeroes (&result)) { my_proof = counter; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Proof of work found: %llu!\n", (unsigned long long) GNUNET_ntohll (counter)); write_proof (); setup_flood_message (estimate_index, current_timestamp); return; } counter++; i++; } if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n", (unsigned long long) counter); /* remember progress every 100 rounds */ my_proof = counter; write_proof (); } else { my_proof = counter; } proof_task = GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, GNUNET_SCHEDULER_PRIORITY_IDLE, &find_proof, NULL); }
/** * Task run during shutdown. * * @param cls unused */ static void shutdown_task (void *cls) { if (NULL != flood_task) { GNUNET_SCHEDULER_cancel (flood_task); flood_task = NULL; } if (NULL != proof_task) { GNUNET_SCHEDULER_cancel (proof_task); proof_task = NULL; write_proof (); /* remember progress */ } if (NULL != nc) { GNUNET_notification_context_destroy (nc); nc = NULL; } if (NULL != core_api) { GNUNET_CORE_disconnect (core_api); core_api = NULL; } if (NULL != stats) { GNUNET_STATISTICS_destroy (stats, GNUNET_NO); stats = NULL; } if (NULL != peers) { GNUNET_CONTAINER_multipeermap_destroy (peers); peers = NULL; } if (NULL != my_private_key) { GNUNET_free (my_private_key); my_private_key = NULL; } #if ENABLE_NSE_HISTOGRAM if (NULL != logger_test) { GNUNET_CLIENT_service_test_cancel (logger_test); logger_test = NULL; } if (NULL != lh) { GNUNET_TESTBED_LOGGER_flush (lh, &flush_comp_cb, NULL); } if (NULL != histogram) { GNUNET_BIO_write_close (histogram); histogram = NULL; } #endif }
/** * Main function that will be run by the scheduler. * * @param cls closure * @param args remaining command-line arguments * @param cfgfile name of the configuration file used (for saving, can be NULL!) * @param cfg configuration */ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { struct GNUNET_CRYPTO_EddsaPrivateKey *pk; char *pids; cfg = config; /* load proof of work */ if (NULL == pwfn) { if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE", "PROOFFILE", &pwfn)) { GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE"); GNUNET_SCHEDULER_shutdown (); return; } } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn); if ( (GNUNET_YES != GNUNET_DISK_file_test (pwfn)) || (sizeof (proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof)))) proof = 0; /* load private key */ if (NULL == pkfn) { if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &pkfn)) { GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PEER", "PRIVATE_KEY"); return; } } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn); if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn))) { FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), pkfn); GNUNET_free (pkfn); return; } GNUNET_free (pkfn); GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub); GNUNET_free (pk); pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids); GNUNET_free (pids); /* get target bit amount */ if (0 == nse_work_required) { if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS", &nse_work_required)) { GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS"); GNUNET_SCHEDULER_shutdown (); return; } if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8) { GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS", _("Value is too large.\n")); GNUNET_SCHEDULER_shutdown (); return; } else if (0 == nse_work_required) { write_proof (); GNUNET_SCHEDULER_shutdown (); return; } } GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Delay between tries: %s\n", GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1)); GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &find_proof, NULL); }
/** * Find our proof of work. * * @param cls closure (unused) * @param tc task context */ static void find_proof (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { #define ROUND_SIZE 10 uint64_t counter; char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) + sizeof (uint64_t)] GNUNET_ALIGN; struct GNUNET_HashCode result; unsigned int i; struct GNUNET_TIME_Absolute timestamp; struct GNUNET_TIME_Relative elapsed; if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) { write_proof (); return; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got Proof of Work %llu\n", proof); proof_task = NULL; memcpy (&buf[sizeof (uint64_t)], &pub, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)); i = 0; counter = proof; timestamp = GNUNET_TIME_absolute_get (); while ((counter != UINT64_MAX) && (i < ROUND_SIZE)) { memcpy (buf, &counter, sizeof (uint64_t)); pow_hash (buf, sizeof (buf), &result); if (nse_work_required <= count_leading_zeroes (&result)) { proof = counter; FPRINTF (stdout, "Proof of work found: %llu!\n", (unsigned long long) proof); write_proof (); return; } counter++; i++; } elapsed = GNUNET_TIME_absolute_get_duration (timestamp); elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Current: %llu [%s/proof]\n", (unsigned long long) counter, GNUNET_STRINGS_relative_time_to_string (elapsed, 0)); if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE)) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n", (unsigned long long) counter); /* remember progress every 100 rounds */ proof = counter; write_proof (); } else { proof = counter; } proof_task = GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay, GNUNET_SCHEDULER_PRIORITY_IDLE, &find_proof, NULL); }