Fixed typedef declaration order compilation error

This commit is contained in:
Your Name
2025-08-17 12:10:09 -04:00
parent fb45970bbb
commit 57201af725
5 changed files with 534853 additions and 16 deletions

View File

@@ -19,7 +19,6 @@
#include <signal.h>
#include <sys/types.h>
#include "nostr_core_lib/nostr_core/nostr_common.h" // Common definitions and init/cleanup
#include "nostr_core_lib/nostr_core/nip001.h" // Basic protocol functions
#include "nostr_core_lib/nostr_core/nip013.h" // Proof-of-work functions
#include "nostr_core_lib/nostr_core/nip019.h" // Bech32 decoding for nsec
#include "nostr_core_lib/cjson/cJSON.h"
@@ -43,17 +42,22 @@ static void* g_worker_contexts = NULL; // Will be cast to mining_context_t*
// Forward declarations for callbacks
typedef struct mining_context mining_context_t;
typedef struct main_context main_context_t;
// Callback function types
typedef void (*solution_callback_t)(cJSON* solution, void* user_data);
// Main context for control decisions
typedef struct {
struct main_context {
volatile int solution_found;
volatile int timeout_reached;
cJSON* result_event;
int solution_thread_id; // Track which thread found the solution
pthread_mutex_t result_mutex;
} main_context_t;
};
// Global reference to main context for signal handler (unused but kept for future use)
static main_context_t* g_main_context = NULL;
// Mining context for workers (keeping legacy fields for now during transition)
struct mining_context {
@@ -351,12 +355,14 @@ static void install_signal_handlers(void) {
// Callback implementations
static void solution_found_callback(cJSON* solution, void* user_data) {
main_context_t* main_ctx = (main_context_t*)user_data;
mining_context_t* mining_ctx = (mining_context_t*)user_data;
main_context_t* main_ctx = (main_context_t*)mining_ctx->user_data;
pthread_mutex_lock(&main_ctx->result_mutex);
if (!main_ctx->solution_found) {
main_ctx->solution_found = 1;
main_ctx->result_event = cJSON_Duplicate(solution, 1);
main_ctx->solution_thread_id = mining_ctx->thread_id; // Capture thread ID
}
pthread_mutex_unlock(&main_ctx->result_mutex);
}
@@ -439,8 +445,9 @@ static void* miner_thread(void* arg) {
if (result == NOSTR_SUCCESS) {
// Found solution - report to main thread via callback
// Pass the mining context (ctx) so callback can access thread_id
if (ctx->solution_callback) {
ctx->solution_callback(local_event, ctx->user_data);
ctx->solution_callback(local_event, ctx);
}
exit_status = (void*)(intptr_t)THREAD_EXIT_SUCCESS;
log_thread_exit(ctx->thread_id, exit_status, "Solution found");
@@ -500,6 +507,8 @@ static int mine_event(mining_context_t* ctx) {
for (int i = 0; i < ctx->thread_count; i++) {
memcpy(&worker_contexts[i], ctx, sizeof(mining_context_t));
worker_contexts[i].thread_id = i;
// Each worker context gets a pointer to the main context as user_data
worker_contexts[i].user_data = &main_ctx;
}
// Create worker threads
@@ -517,7 +526,9 @@ static int mine_event(mining_context_t* ctx) {
g_worker_contexts = worker_contexts;
time_t start_time = time(NULL);
printf("[DEBUG] Starting %d mining threads...\n", ctx->thread_count);
if (ctx->verbose_enabled) {
printf("Starting %d mining threads...\n", ctx->thread_count);
}
// Start threads
for (int i = 0; i < ctx->thread_count; i++) {
@@ -538,7 +549,9 @@ static int mine_event(mining_context_t* ctx) {
pthread_mutex_destroy(&main_ctx.result_mutex);
return -1;
}
printf("[DEBUG] Thread %d started successfully\n", i);
if (ctx->verbose_enabled) {
printf("Thread %d started successfully\n", i);
}
}
// Main thread control loop - centralized monitoring
@@ -556,7 +569,9 @@ static int mine_event(mining_context_t* ctx) {
// Check for signals
if (g_signal_received) {
printf("[DEBUG] Signal received, shutting down...\n");
if (ctx->verbose_enabled) {
printf("Signal received, shutting down...\n");
}
break;
}
@@ -565,13 +580,17 @@ static int mine_event(mining_context_t* ctx) {
}
// Signal all workers to stop
printf("[DEBUG] Signaling threads to stop...\n");
if (ctx->verbose_enabled) {
printf("Signaling threads to stop...\n");
}
for (int i = 0; i < ctx->thread_count; i++) {
worker_contexts[i].should_stop = 1;
}
// Wait for all threads to finish and capture exit statuses
printf("[DEBUG] Waiting for threads to finish...\n");
if (ctx->verbose_enabled) {
printf("Waiting for threads to finish...\n");
}
for (int i = 0; i < ctx->thread_count; i++) {
void* exit_status;
pthread_join(threads[i], &exit_status);
@@ -587,16 +606,24 @@ static int mine_event(mining_context_t* ctx) {
if (main_ctx.solution_found && main_ctx.result_event) {
ctx->result_event = main_ctx.result_event; // Transfer ownership
result = 1; // Success
printf("[DEBUG] Solution found successfully\n");
if (ctx->verbose_enabled) {
printf("Solution found successfully by Thread %d\n", main_ctx.solution_thread_id);
}
} else if (main_ctx.timeout_reached) {
result = -1; // Timeout
printf("[DEBUG] Mining timed out\n");
if (ctx->verbose_enabled) {
printf("Mining timed out\n");
}
} else if (g_shutdown_requested || g_signal_received) {
result = -3; // Signal/emergency shutdown
printf("[DEBUG] Emergency shutdown completed\n");
if (ctx->verbose_enabled) {
printf("Emergency shutdown completed\n");
}
} else {
result = -2; // Error
printf("[DEBUG] Mining failed with error\n");
if (ctx->verbose_enabled) {
printf("Mining failed with error\n");
}
}
// Clear global debugging variables