From 111b9914c601730a3697a3b7ff8a60fd2c15a38a Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Sat, 23 Mar 2024 11:38:29 -0500 Subject: Get smarter with pointers so tests pass --- src/event.cpp | 8 +++---- src/nostr_service.cpp | 64 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/event.cpp b/src/event.cpp index a24a594..e77e33d 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -11,6 +11,7 @@ using nlohmann::json; using std::hex; using std::invalid_argument; +using std::make_shared; using std::setw; using std::setfill; using std::shared_ptr; @@ -20,7 +21,7 @@ using std::time; namespace nostr { -string Event::serialize(shared_ptr signer) +string Event::serialize() { try { @@ -41,7 +42,6 @@ string Event::serialize(shared_ptr signer) }; j["id"] = this->generateId(j.dump()); - j["sig"] = signer->generateSignature(shared_ptr(this)); json jarr = json::array({ "EVENT", j }); @@ -80,8 +80,8 @@ void Event::validate() throw std::invalid_argument("Event::validate: A valid event kind is required."); } - bool hasSig = this->sig.length() > 0; - if (!hasSig) + bool hasSignature = this->sig.length() > 0; + if (!hasSignature) { throw std::invalid_argument("Event::validate: The event must be signed."); } diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index ac63f23..971516f 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,6 +16,7 @@ using boost::uuids::to_string; using boost::uuids::uuid; using nlohmann::json; using std::async; +using std::find_if; using std::function; using std::future; using std::lock_guard; @@ -33,14 +35,16 @@ namespace nostr { NostrService::NostrService( shared_ptr appender, - shared_ptr client) -: NostrService(appender, client, {}) { }; + shared_ptr client, + shared_ptr signer) +: NostrService(appender, client, signer, {}) { }; NostrService::NostrService( shared_ptr appender, shared_ptr client, + shared_ptr signer, RelayList relays) -: _defaultRelays(relays), _client(client) +: _defaultRelays(relays), _client(client), _signer(signer) { plog::init(plog::debug, appender.get()); client->start(); @@ -118,20 +122,33 @@ void NostrService::closeRelayConnections(RelayList relays) } }; -tuple NostrService::publishEvent(Event event) +tuple NostrService::publishEvent(shared_ptr event) { RelayList successfulRelays; RelayList failedRelays; PLOG_INFO << "Attempting to publish event to Nostr relays."; + string serializedEvent; + try + { + this->_signer->sign(event); + serializedEvent = event->serialize(); + } + catch (const std::invalid_argument& error) + { + PLOG_ERROR << "Failed to sign event: " << error.what(); + throw error; + } + + lock_guard lock(this->_propertyMutex); vector>> publishFutures; for (const string& relay : this->_activeRelays) { - future> publishFuture = async([this, &relay, &event]() { - return this->_client->send(event.serialize(this->_signer), relay); + PLOG_INFO << "Entering lambda."; + future> publishFuture = async([this, relay, serializedEvent]() { + return this->_client->send(serializedEvent, relay); }); - publishFutures.push_back(move(publishFuture)); } @@ -159,7 +176,7 @@ string NostrService::queryRelays(Filters filters) { return this->queryRelays(filters, [this](string subscriptionId, Event event) { lock_guard lock(this->_propertyMutex); - this->_eventIterators[subscriptionId] = this->_events[subscriptionId].begin(); + this->_lastRead[subscriptionId] = event.id; this->onEvent(subscriptionId, event); }); }; @@ -228,16 +245,21 @@ vector NostrService::getNewEvents(string subscriptionId) throw out_of_range("No events found for subscription: " + subscriptionId); } - if (this->_eventIterators.find(subscriptionId) == this->_eventIterators.end()) + if (this->_lastRead.find(subscriptionId) == this->_lastRead.end()) { - PLOG_ERROR << "No event iterator found for subscription: " << subscriptionId; - throw out_of_range("No event iterator found for subscription: " + subscriptionId); + PLOG_ERROR << "No last read event ID found for subscription: " << subscriptionId; + throw out_of_range("No last read event ID found for subscription: " + subscriptionId); } lock_guard lock(this->_propertyMutex); vector newEvents; vector receivedEvents = this->_events[subscriptionId]; - vector::iterator eventIt = this->_eventIterators[subscriptionId]; + vector::iterator eventIt = find_if( + receivedEvents.begin(), + receivedEvents.end(), + [this,subscriptionId](Event event) { + return event.id == this->_lastRead[subscriptionId]; + }) + 1; while (eventIt != receivedEvents.end()) { @@ -480,20 +502,26 @@ void NostrService::onMessage(string message, function lock(this->_propertyMutex); - _events[subscriptionId].push_back(event); + this->_events[subscriptionId].push_back(event); PLOG_INFO << "Received event for subscription: " << subscriptionId; // To protect memory, only keep a limited number of events per subscription. - while (_events[subscriptionId].size() > NostrService::MAX_EVENTS_PER_SUBSCRIPTION) + while (this->_events[subscriptionId].size() > NostrService::MAX_EVENTS_PER_SUBSCRIPTION) { - auto startIt = _events[subscriptionId].begin(); - auto eventIt = _eventIterators[subscriptionId]; - - if (eventIt == startIt) + auto events = this->_events[subscriptionId]; + auto eventIt = find_if( + events.begin(), + events.end(), + [this, subscriptionId](Event event) { + return event.id == this->_lastRead[subscriptionId]; + }); + + if (eventIt == events.begin()) { eventIt++; } + this->_lastRead[subscriptionId] = eventIt->id; _events[subscriptionId].erase(_events[subscriptionId].begin()); } }; -- cgit