From fa3f0dd7c95897b06a9185317910b2abb474fc28 Mon Sep 17 00:00:00 2001 From: Daniel Wigton Date: Sat, 6 Jul 2024 15:53:10 -0500 Subject: empty --- include/nostr.hpp | 5 +++-- src/nostr_service.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/nostr.hpp b/include/nostr.hpp index b2cdfc5..e699f21 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -177,7 +177,8 @@ public: * @brief Queries all open relay connections for events matching the given set of filters, and * returns all stored matching events returned by the relays. * @param filters The filters to use for the query. - * @returns A vector of all events matching the filters from all open relay connections. + * @returns A std::future that will eventually hold a vector of all events matching the filters + * from all open relay connections. * @remark This method runs until the relays send an EOSE message, indicating they have no more * stored events matching the given filters. When the EOSE message is received, the method * will close the subscription for each relay and return the received events. @@ -185,7 +186,7 @@ public: * set on the filters in the range 1-64, inclusive. If no valid limit is given, it will be * defaulted to 16. */ - std::vector> queryRelays(std::shared_ptr filters); + std::future>> queryRelays(std::shared_ptr filters); /** * @brief Queries all open relay connections for events matching the given set of filters. diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 10d9fc6..83555eb 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -184,7 +184,7 @@ tuple, vector> NostrService::publishEvent(shared_ptr> NostrService::queryRelays(shared_ptr filters) +std::future> NostrService::queryRelays(shared_ptr filters) { if (filters->limit > 64 || filters->limit < 1) { -- cgit From 9cbe27d3e5e15a3790cc9d5d3e9cc5852ce08df4 Mon Sep 17 00:00:00 2001 From: Daniel Wigton Date: Wed, 10 Jul 2024 16:19:32 -0500 Subject: return future from queryRelays --- src/nostr_service.cpp | 170 +++++++++++++++++++++++--------------------- test/nostr_service_test.cpp | 4 +- 2 files changed, 90 insertions(+), 84 deletions(-) diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 83555eb..2c1f9a8 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -182,104 +182,108 @@ tuple, vector> NostrService::publishEvent(shared_ptr> NostrService::queryRelays(shared_ptr filters) +std::future>> NostrService::queryRelays(shared_ptr filters) { - if (filters->limit > 64 || filters->limit < 1) - { - PLOG_WARNING << "Filters limit must be between 1 and 64, inclusive. Setting limit to 16."; - filters->limit = 16; - } - - vector> events; - - string subscriptionId = this->generateSubscriptionId(); - string request; - - try - { - request = filters->serialize(subscriptionId); - } - catch (const invalid_argument& e) - { - PLOG_ERROR << "Failed to serialize filters - invalid object: " << e.what(); - throw e; - } - catch (const json::exception& je) + return std::async(std::launch::async, [this, filters]() -> vector> { - PLOG_ERROR << "Failed to serialize filters - JSON exception: " << je.what(); - throw je; - } + if (filters->limit > 64 || filters->limit < 1) + { + PLOG_WARNING << "Filters limit must be between 1 and 64, inclusive. Setting limit to 16."; + filters->limit = 16; + } - vector>> requestFutures; + vector> events; - unordered_set uniqueEventIds; + string subscriptionId = this->generateSubscriptionId(); + string request; - // Send the same query to each relay. As events trickle in from each relay, they will be added - // to the events vector. Duplicate copies of the same event will be ignored, as events are - // stored on multiple relays. The function will block until all of the relays send an EOSE or - // CLOSE message. - for (const string relay : this->_activeRelays) - { - promise> eosePromise; - requestFutures.push_back(move(eosePromise.get_future())); - - auto [uri, success] = this->_client->send( - request, - relay, - [this, &relay, &events, &eosePromise, &uniqueEventIds](string payload) - { - this->onSubscriptionMessage( - payload, - [&events, &uniqueEventIds](const string&, shared_ptr event) - { - // Check if the event is unique before adding. - if (uniqueEventIds.insert(event->id).second) - { - events.push_back(event); - } - }, - [relay, &eosePromise](const string&) - { - eosePromise.set_value(make_tuple(relay, true)); - }, - [relay, &eosePromise](const string&, const string&) - { - eosePromise.set_value(make_tuple(relay, false)); - }); - }); - - if (success) + try { - PLOG_INFO << "Sent query to relay " << relay; - lock_guard lock(this->_propertyMutex); - this->_subscriptions[subscriptionId].push_back(relay); + request = filters->serialize(subscriptionId); } - else + catch (const invalid_argument& e) { - PLOG_WARNING << "Failed to send query to relay " << relay; - eosePromise.set_value(make_tuple(uri, false)); + PLOG_ERROR << "Failed to serialize filters - invalid object: " << e.what(); + throw e; } - } - - // Close open subscriptions and disconnect from relays after events are received. - for (auto& publishFuture : requestFutures) - { - auto [relay, isEose] = publishFuture.get(); - if (isEose) + catch (const json::exception& je) { - PLOG_INFO << "Received EOSE message from relay " << relay; + PLOG_ERROR << "Failed to serialize filters - JSON exception: " << je.what(); + throw je; } - else + + vector>> requestFutures; + + unordered_set uniqueEventIds; + + // Send the same query to each relay. As events trickle in from each relay, they will be added + // to the events vector. Duplicate copies of the same event will be ignored, as events are + // stored on multiple relays. The function will block until all of the relays send an EOSE or + // CLOSE message. + for (const string relay : this->_activeRelays) { - PLOG_WARNING << "Received CLOSE message from relay " << relay; - this->closeRelayConnections({ relay }); + promise> eosePromise; + requestFutures.push_back(move(eosePromise.get_future())); + + auto [uri, success] = this->_client->send( + request, + relay, + [this, &relay, &events, &eosePromise, &uniqueEventIds](string payload) + { + this->onSubscriptionMessage( + payload, + [&events, &uniqueEventIds](const string&, shared_ptr event) + { + // Check if the event is unique before adding. + if (uniqueEventIds.insert(event->id).second) + { + events.push_back(event); + } + }, + [relay, &eosePromise](const string&) + { + eosePromise.set_value(make_tuple(relay, true)); + }, + [relay, &eosePromise](const string&, const string&) + { + eosePromise.set_value(make_tuple(relay, false)); + }); + }); + + if (success) + { + PLOG_INFO << "Sent query to relay " << relay; + lock_guard lock(this->_propertyMutex); + this->_subscriptions[subscriptionId].push_back(relay); + } + else + { + PLOG_WARNING << "Failed to send query to relay " << relay; + eosePromise.set_value(make_tuple(uri, false)); + } } - } - this->closeSubscription(subscriptionId); - return events; + + // Close open subscriptions and disconnect from relays after events are received. + + for (auto& publishFuture : requestFutures) + { + auto [relay, isEose] = publishFuture.get(); + if (isEose) + { + PLOG_INFO << "Received EOSE message from relay " << relay; + } + else + { + PLOG_WARNING << "Received CLOSE message from relay " << relay; + this->closeRelayConnections({ relay }); + } + } + this->closeSubscription(subscriptionId); + + return events; + }); }; string NostrService::queryRelays( diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 4e29960..6000423 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -771,7 +771,9 @@ TEST_F(NostrServiceTest, QueryRelays_ReturnsEvents_UpToEOSE) })); auto filters = make_shared(getKind0And1TestFilters()); - auto results = nostrService->queryRelays(filters); + + // await the future and get the results + auto results = nostrService->queryRelays(filters).get(); // Check results size to ensure there are no duplicates. ASSERT_EQ(results.size(), testEvents.size()); -- cgit From 1e6bf40d457def0faa12e3f6cf58be0e9c69e6f5 Mon Sep 17 00:00:00 2001 From: dwigton Date: Mon, 15 Jul 2024 12:05:09 -0500 Subject: Apply suggestions from code review Co-authored-by: Michael J <37635304+buttercat1791@users.noreply.github.com> --- src/nostr_service.cpp | 32 ++++++++++++++++---------------- test/nostr_service_test.cpp | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 2c1f9a8..73cd602 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -183,9 +183,9 @@ tuple, vector> NostrService::publishEvent(shared_ptr>> NostrService::queryRelays(shared_ptr filters) +future>> NostrService::queryRelays(shared_ptr filters) { - return std::async(std::launch::async, [this, filters]() -> vector> + return async(launch::async, [this, filters]() -> vector> { if (filters->limit > 64 || filters->limit < 1) { @@ -267,20 +267,20 @@ std::future>> NostrService::queryRelays(shared_ptrcloseRelayConnections({ relay }); - } - } - this->closeSubscription(subscriptionId); + for (auto& publishFuture : requestFutures) + { + auto [relay, isEose] = publishFuture.get(); + if (isEose) + { + PLOG_INFO << "Received EOSE message from relay " << relay; + } + else + { + PLOG_WARNING << "Received CLOSE message from relay " << relay; + this->closeRelayConnections({ relay }); + } + } + this->closeSubscription(subscriptionId); return events; }); diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 6000423..d29fc87 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -772,7 +772,7 @@ TEST_F(NostrServiceTest, QueryRelays_ReturnsEvents_UpToEOSE) auto filters = make_shared(getKind0And1TestFilters()); - // await the future and get the results + // await the future and get the results auto results = nostrService->queryRelays(filters).get(); // Check results size to ensure there are no duplicates. -- cgit