diff --git a/nostr_core/core_relay_pool.c b/nostr_core/core_relay_pool.c index e77aa1dc..65d85937 100644 --- a/nostr_core/core_relay_pool.c +++ b/nostr_core/core_relay_pool.c @@ -1302,87 +1302,93 @@ cJSON** nostr_relay_pool_query_sync( // Handle pong response for connection health monitoring handle_pong_response(relay); } else { - // Process as regular NOSTR message + // Parse once: query-sync consumes only its own sub-id messages; + // everything else is forwarded to normal pool dispatch. char* msg_type = NULL; cJSON* parsed = NULL; if (nostr_parse_relay_message(buffer, &msg_type, &parsed) == 0) { + int handled_for_query = 0; + if (msg_type && strcmp(msg_type, "EVENT") == 0) { - // Handle EVENT message - if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 3) { - cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1); - cJSON* event = cJSON_GetArrayItem(parsed, 2); - - if (cJSON_IsString(sub_id_json) && event && - strcmp(cJSON_GetStringValue(sub_id_json), subscription_id) == 0) { - - cJSON* event_id_json = cJSON_GetObjectItem(event, "id"); - if (event_id_json && cJSON_IsString(event_id_json)) { - const char* event_id = cJSON_GetStringValue(event_id_json); - - // Check for duplicate - if (!is_event_seen(pool, event_id)) { - mark_event_seen(pool, event_id); - relay->stats.events_received++; - - // Add to results array - if (*event_count >= events_capacity) { - events_capacity = events_capacity == 0 ? 10 : events_capacity * 2; - events = realloc(events, events_capacity * sizeof(cJSON*)); - if (!events) { - *event_count = 0; - break; + if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 3) { + cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1); + cJSON* event = cJSON_GetArrayItem(parsed, 2); + + if (cJSON_IsString(sub_id_json) && event && + strcmp(cJSON_GetStringValue(sub_id_json), subscription_id) == 0) { + handled_for_query = 1; + + cJSON* event_id_json = cJSON_GetObjectItem(event, "id"); + if (event_id_json && cJSON_IsString(event_id_json)) { + const char* event_id = cJSON_GetStringValue(event_id_json); + + if (!is_event_seen(pool, event_id)) { + mark_event_seen(pool, event_id); + relay->stats.events_received++; + + if (*event_count >= events_capacity) { + events_capacity = events_capacity == 0 ? 10 : events_capacity * 2; + events = realloc(events, events_capacity * sizeof(cJSON*)); + if (!events) { + *event_count = 0; + break; + } } + + events[(*event_count)++] = cJSON_Duplicate(event, 1); } - - events[(*event_count)++] = cJSON_Duplicate(event, 1); + } + } + } + } else if (msg_type && strcmp(msg_type, "EOSE") == 0) { + if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) { + cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1); + if (cJSON_IsString(sub_id_json) && + strcmp(cJSON_GetStringValue(sub_id_json), subscription_id) == 0) { + handled_for_query = 1; + eose_count++; + } + } + } else if (msg_type && strcmp(msg_type, "AUTH") == 0) { + if (relay->nip42_enabled && pool->has_private_key && + cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) { + cJSON* challenge_json = cJSON_GetArrayItem(parsed, 1); + if (cJSON_IsString(challenge_json)) { + handled_for_query = 1; + const char* challenge = cJSON_GetStringValue(challenge_json); + + strncpy(relay->auth_challenge, challenge, sizeof(relay->auth_challenge) - 1); + relay->auth_challenge[sizeof(relay->auth_challenge) - 1] = '\0'; + relay->auth_challenge_time = time(NULL); + relay->auth_state = NOSTR_AUTH_STATE_CHALLENGE_RECEIVED; + + cJSON* auth_event = nostr_nip42_create_auth_event( + challenge, + relay->url, + pool->private_key, + 0 + ); + if (auth_event) { + char* auth_message = nostr_nip42_create_auth_message(auth_event); + if (auth_message) { + if (nostr_ws_send_text(relay->ws_client, auth_message) >= 0) { + relay->auth_state = NOSTR_AUTH_STATE_AUTHENTICATING; + } + free(auth_message); + } + cJSON_Delete(auth_event); } } } } - } else if (msg_type && strcmp(msg_type, "EOSE") == 0) { - // Handle EOSE message - if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) { - cJSON* sub_id_json = cJSON_GetArrayItem(parsed, 1); - if (cJSON_IsString(sub_id_json) && - strcmp(cJSON_GetStringValue(sub_id_json), subscription_id) == 0) { - eose_count++; - } - } - } else if (msg_type && strcmp(msg_type, "AUTH") == 0) { - // Handle AUTH challenge message: ["AUTH", ] - if (relay->nip42_enabled && pool->has_private_key && - cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 2) { - cJSON* challenge_json = cJSON_GetArrayItem(parsed, 1); - if (cJSON_IsString(challenge_json)) { - const char* challenge = cJSON_GetStringValue(challenge_json); - strncpy(relay->auth_challenge, challenge, sizeof(relay->auth_challenge) - 1); - relay->auth_challenge[sizeof(relay->auth_challenge) - 1] = '\0'; - relay->auth_challenge_time = time(NULL); - relay->auth_state = NOSTR_AUTH_STATE_CHALLENGE_RECEIVED; + if (msg_type) free(msg_type); + if (parsed) cJSON_Delete(parsed); - cJSON* auth_event = nostr_nip42_create_auth_event( - challenge, - relay->url, - pool->private_key, - 0 - ); - if (auth_event) { - char* auth_message = nostr_nip42_create_auth_message(auth_event); - if (auth_message) { - if (nostr_ws_send_text(relay->ws_client, auth_message) >= 0) { - relay->auth_state = NOSTR_AUTH_STATE_AUTHENTICATING; - } - free(auth_message); - } - cJSON_Delete(auth_event); - } - } + if (!handled_for_query) { + process_relay_message(pool, relay, buffer); } } - if (msg_type) free(msg_type); - if (parsed) cJSON_Delete(parsed); - } } } }