Commit Graph

4397 Commits

Author SHA1 Message Date
Daniel D’Aquino
61eb833239 Add temporary experimental delay to check hypothesis on occasional init timeline staleness
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-05 16:52:56 -07:00
Daniel D’Aquino
d9306d4153 Modify NostrNetworkManager pipeline architecture
Previously, we combined the ndb and network stream within a "session
subscription" stream, which was teared down and rebuilt every time the
app went into the background and back to the foreground (This was done to
prevent crashes related to access to Ndb memory when Ndb is closed).

However, this caused complications and instability on the network
stream, leading to timeline staleness.

To address this, the pipeline was modified to merge the ndb and network
streams further upstream, on the multi-session stage, allowing the
session subscription streams to be completely split between Ndb and the
network.

For the ndb stream, we still tear it down and bring it up along the app
foreground state, to prevent memory crashes. However, the network stream
is kept intact between sessions, since RelayPool will now automatically
handle resubscription on websocket reconnection. This prevents
complexity and potential race conditions that could lead to timeline
staleness.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-05 15:28:16 -07:00
Daniel D’Aquino
3437cf5347 Further improvements to app lifecycle handling
- Resend subscription requests to relays when websocket connection is
  re-established
- More safeguard checks on whether Ndb is opened before accessing its
  memory
- Cancel queued unsubscribe requests on app backgrounding to avoid race
  conditions with subscribe requests when app enters the foreground
- Call Ndb re-open when Damus is active (not only on active notify), as
  experimentally there have been instances where active notify code has
  not been run. The operation is idempotent, so there should be no risk
  of it being called twice.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-05 13:18:59 -07:00
Daniel D’Aquino
667a228e1a Ensure to publish object changes on the main thread
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-03 10:29:26 -07:00
Daniel D’Aquino
84c4594d30 Fix timeline staleness
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-03 10:14:32 -07:00
Daniel D’Aquino
32e8c1b6e1 Improve logging in SubscriptionManager
Use Apple's unified logging system, and specify proper privacy levels
for each piece of information.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-01 11:18:15 -07:00
Daniel D’Aquino
1b5f107ac6 Add more safeguards to prevent RUNNINGBOARD 0xdead10cc crashes
This commit adds more safeguards to prevent RUNNINGBOARD 0xdead10cc
crashes, by:
1. Using the `beginBackgroundTask(withName:expirationHandler:)` to
   request additional background execution time before completely
   suspending the app. See https://developer.apple.com/documentation/xcode/sigkill
2. Reorganizing app closing/cleanup tasks to be done in parallel when
   possible to decrease time needed to cleanup resources.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-29 16:39:14 -07:00
Daniel D’Aquino
fe62aea08a Stop ProfileManager when app is being backgrounded
This should prevent RUNNINGBOARD 0xdead10cc crashes related to
ProfileManager and app background states.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-26 13:01:05 -07:00
Daniel D’Aquino
258d08723f Check if Ndb is closed before running subscribe and query operations
This should prevent background crashes caused by race conditions between
usages of Ndb and the Ndb/app lifecycle operations.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-26 12:04:51 -07:00
Daniel D’Aquino
9153a912b0 Cancel timeout task on stream cancellation
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 17:55:35 -07:00
Daniel D’Aquino
fe491bf694 Increase MAX_CONCURRENT_SUBSCRIPTION_LIMIT
Through some local experimentation, it seems that network relays can support higher subscription limits.

Increase internal limits to avoid hitting issues with subscriptions
waiting on subscription pool to clear and appearing stale.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 17:52:38 -07:00
Daniel D’Aquino
e55675a336 Optimize HomeModel subscription usage
This reduces the overall subscription usage throughout the app, thus
reducing issues associated with too many subscriptions being used at
once, and the resulting staleness.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 16:15:52 -07:00
Daniel D’Aquino
eda4212aa7 Disable refreshable on Universe view
Updates are streamed from the network, removing the need for a refresh
action

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:11 -07:00
Daniel D’Aquino
798f9ec7b4 Improve loading speeds for home timeline and universe view
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:11 -07:00
Daniel D’Aquino
a09e22df24 Improve streaming interfaces and profile loading logic
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:11 -07:00
Daniel D’Aquino
a3ef36120e Fix OS 26 build errors
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
de528f3f70 Improve loading speed on home timeline
This commit improves the loading speed for the home timeline (and likely
other areas of the app) by employing various techniques and changes:
- Network EOSE timeout reduced from 10 seconds down to 5 seconds
- Network EOSE does not wait on relays with broken connections
- Offload HomeModel handler event processing to separate tasks to
  avoid a large backlog
- Give SubscriptionManager streamers more fine-grained EOSE signals for
  local optimization
- Only wait for Ndb EOSE on the home timeline for faster loading
- Add logging with time elapsed measurements for easier identification of
  loading problems

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
8164eee479 Return network EOSE in normal mode if device is offline
This is done to prevent hang ups when the device is offline.

Changelog-Added: Added the ability to load saved notes if device is offline
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
0582892cae Improve Follow pack timeline loading logic in the Universe view
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
2185984ed7 Stream from both NDB and network relays
This commit takes a step back from the full local relay model by
treating NostrDB as one of the many relays streamed from, instead of the
one exclusive relay that other classes rely on.

This was done to reduce regression risk from the local relay model
migration, without discarding the migration work already done.

The full "local relay model" behavior (exclusive NDB streaming) was
hidden behind a feature flag for easy migration later on.

Closes: https://github.com/damus-io/damus/issues/3225
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
1caad24364 Add note provenance filter support to SubscriptionManager
Closes: https://github.com/damus-io/damus/issues/3222
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
ecbfb3714b Fix incompatibilities with new nostrdb version
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:03 -07:00
William Casarin
d565eb20f7 nostrdb: query: enforce author matches in author_kind queries
before we weren't checking this, meaning we were getting
results from other keys. oops.

Reported-by: Jeff Gardner
Fixes: #84
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a040a0244b nostrdb: search: fix memleak in profile search
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
387af198d6 nostrdb: win: fix heap corruption with flatbuf 2025-09-24 14:06:03 -07:00
William Casarin
66e10db6b2 nostrdb: mem: re-enable profile freeing
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
42a0f2c08d nostrdb: Revert "mem: search cursor close"
this is causing heap corruption on the windows
build

This reverts commit a8d6925a5b33ddbdd4306423527b5d8314f7dd36.
2025-09-24 14:06:03 -07:00
William Casarin
aa8ce31941 nostrdb: mem: close cursors in print helpers 2025-09-24 14:06:03 -07:00
William Casarin
8014d772ba nostrdb: mem: builder clear before free 2025-09-24 14:06:03 -07:00
William Casarin
4d8313c788 nostrdb: mem: relay iter cleanup 2025-09-24 14:06:03 -07:00
William Casarin
342067640f nostrdb: mem: reaction stats cleanup 2025-09-24 14:06:03 -07:00
William Casarin
84839d1c43 nostrdb: mem: search cursor close 2025-09-24 14:06:03 -07:00
William Casarin
b5079c42d5 nostrdb: memory: fix a bunch of memory leaks
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
0847c53a39 nostrdb: add ndb_builder_push_tag_id
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
fa2d240ddf nostrdb: nostrdb: calculate id in ndb_note_verify
Rogue relays could in theory attack nostrdb by replaying ids and
signatures from other notes. This fixes this weakness by calculating the
id again in ndb_note_verify.

There is no known relays exploiting this, but lets get ahead of it
before we switch to the outbox model in damus iOS/notedeck

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
3a37a6c18e nostrdb: change <=10 author search queries to ==1
These queries are broken anyways. Rely on scans until we fix this

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
5c75e87ed5 nostrdb: eq: fix fallthrough bug
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
64c16e7cc8 nostrdb: filter: add initial custom filtering logic
This adds some helpers for adding custom filtering logic
to nostr filters. These are just a callback and a closure.
There can only be one custom callback filter per filter.

Fixes: https://github.com/damus-io/nostrdb/issues/33
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
0b8090cb28 nostrdb: query: implement profile search query plans
The basic idea of this is to allow you to use the standard
nip50 query interface to search for profiles using our profile
index.

query: {"search":"jb55", "kinds":[0]}

will result in a profile_search query plan that searches kind0 profiles
for the corresponding `name` or `display_name`.

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
9cff8608f6 nostrdb: fix build on macos 2025-09-24 14:06:03 -07:00
William Casarin
c728210be8 nostrdb: query: implement author_kind query plan
This should help author kind query performance
2025-09-24 14:06:03 -07:00
William Casarin
0f66e87faf nostrdb: Relay queries
Add support for relay-based filtering in nostr queries.

Filters can now include a "relays" field. Optimal performance when
you include a kind as well:

{"relays":["wss://pyramid.fiatjaf.com/"], "kinds":[1]}

This corresponds to a `ndb` query like so:

$ ndb query -r wss://pyramid.fiatjaf.com/ -k 1 -l 1
using filter '{"relays":["wss://pyramid.fiatjaf.com/"],"kinds":[1],"limit":1}'
1 results in 0.094929 ms
{"id":"277dd4ed26d0b44576..}

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
af2298dcb7 nostrdb: relay: fix potential relay index corruption
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a0b85129d4 nostrdb: relay: fix race condition bug
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
e42b14cc6f nostrdb: debug: add a print for debugging rust integration
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
f0521ba406 nostrdb: relay-index: fix a few bugs
There were a few race conditions and lmdb bugs in the
relay index implementation. Fix those!

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
c29027ff5b nostrdb: note: always write relay index
This fixes a race condition where if multiple of the same note
is processed at the same time, we still manage to write the
note relays

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
c6674199de nostrdb: win: fix build on windows 2025-09-24 14:06:03 -07:00
William Casarin
5961bf7958 nostrdb: ndb: add print-relay-kind-index-keys
for debugging

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a877a19c25 nostrdb: relay: add note relay iteration
This is a simple cursor that walks the NDB_DB_NOTE_RELAYS db

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00