diff --git a/VERSION b/VERSION index 7e72641..001d752 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.22 +0.1.23 diff --git a/event_miner b/event_miner index 6812ccf..fbf5cbb 100755 Binary files a/event_miner and b/event_miner differ diff --git a/event_miner.c b/event_miner.c index c6801ad..3498b4f 100644 --- a/event_miner.c +++ b/event_miner.c @@ -56,6 +56,11 @@ struct mining_context { // Control flag (only main thread modifies) volatile int should_stop; + // Verbose mode and progress tracking + int verbose_enabled; + int best_leading_zeros; + time_t thread_start_time; + // Legacy fields for compatibility during transition volatile int found; cJSON* result_event; @@ -72,6 +77,7 @@ typedef struct { int threads; char* event_file; int timeout_sec; + int verbose; int help; } args_t; @@ -88,6 +94,7 @@ static void cleanup_context(mining_context_t* ctx); static void solution_found_callback(cJSON* solution, void* user_data); static void progress_report_callback(int thread_id, uint64_t attempts, void* user_data); static void error_report_callback(int thread_id, int error_code, void* user_data); +static void verbose_pow_callback(int current_difficulty, uint64_t nonce, void* user_data); // Usage information static void usage(const char* prog_name) { @@ -99,6 +106,7 @@ static void usage(const char* prog_name) { fprintf(stderr, "Optional arguments:\n"); fprintf(stderr, " -e Read event from file (default: stdin)\n"); fprintf(stderr, " --timeout_sec Timeout in seconds (default: no timeout)\n"); + fprintf(stderr, " -v Verbose mode - show mining progress\n"); fprintf(stderr, " -h, --help Show this help message\n\n"); fprintf(stderr, "Examples:\n"); fprintf(stderr, " echo '{\"kind\":1,...}' | %s -pow 4 -nsec nsec1... -threads 8\n", prog_name); @@ -150,6 +158,8 @@ static int parse_arguments(int argc, char* argv[], args_t* args) { return -1; } i++; // Skip the next argument + } else if (strcmp(argv[i], "-v") == 0) { + args->verbose = 1; } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { args->help = 1; return 0; @@ -265,10 +275,50 @@ static void error_report_callback(int thread_id, int error_code, void* user_data (void)user_data; // Suppress unused parameter warning } +// Verbose PoW callback - receives progress from nostr_add_proof_of_work +static void verbose_pow_callback(int current_difficulty, uint64_t nonce, void* user_data) { + mining_context_t* ctx = (mining_context_t*)user_data; + + // Only report if verbose mode is enabled + if (!ctx->verbose_enabled) { + return; + } + + // Update best difficulty achieved by this thread + if (current_difficulty > ctx->best_leading_zeros) { + ctx->best_leading_zeros = current_difficulty; + } + + // Calculate mining rate (attempts per second) + time_t current_time = time(NULL); + time_t elapsed = current_time - ctx->thread_start_time; + double rate = elapsed > 0 ? (double)nonce / elapsed : 0.0; + + // Format rate for display + char rate_str[32]; + if (rate > 1000000) { + snprintf(rate_str, sizeof(rate_str), "%.1fM/sec", rate / 1000000.0); + } else if (rate > 1000) { + snprintf(rate_str, sizeof(rate_str), "%.1fk/sec", rate / 1000.0); + } else { + snprintf(rate_str, sizeof(rate_str), "%.0f/sec", rate); + } + + // Print progress report + printf("[Thread %d] nonce: %llu, best: %d zeros, rate: %s, target: %d\n", + ctx->thread_id, (unsigned long long)nonce, ctx->best_leading_zeros, + rate_str, ctx->target_difficulty); + fflush(stdout); +} + // Mining thread function - New callback-based approach static void* miner_thread(void* arg) { mining_context_t* ctx = (mining_context_t*)arg; + // Initialize thread-specific timing for verbose mode + ctx->thread_start_time = time(NULL); + ctx->best_leading_zeros = 0; + // Create a copy of the event for this thread char* event_str = cJSON_Print(ctx->event); if (!event_str) { @@ -292,9 +342,11 @@ static void* miner_thread(void* arg) { // Mine until solution found or signaled to stop by main thread while (!ctx->should_stop) { - // Attempt mining + // Attempt mining with verbose callback if enabled + void (*progress_cb)(int, uint64_t, void*) = ctx->verbose_enabled ? verbose_pow_callback : NULL; + int result = nostr_add_proof_of_work(local_event, ctx->private_key, - ctx->target_difficulty, NULL, NULL); + ctx->target_difficulty, progress_cb, ctx); attempts++; @@ -501,6 +553,7 @@ int main(int argc, char* argv[]) { ctx.target_difficulty = args.pow; ctx.thread_count = args.threads; ctx.timeout_seconds = args.timeout_sec > 0 ? args.timeout_sec : 0; + ctx.verbose_enabled = args.verbose; // Start mining int mining_result = mine_event(&ctx);