Skip to content

Commit 29be4ea

Browse files
square-tombstudio
andcommitted
mobile: catch up read state only when the live sub turns ready
A reconnect can send the read-state REQ twice, and the second ready looked like a late one, so each reconnect read the snapshot twice. Catch up only when the subscription becomes ready after it was not, and let the next publish retry a failed catch-up. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Signed-off-by: Tom Brow <[email protected]>
1 parent f0cc23b commit 29be4ea

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎mobile/lib/shared/read_state/read_state_manager.dart‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ class ReadStateManager {
464464
try {
465465
final generation = ++_liveGeneration;
466466
var returned = false;
467+
var live = false;
467468
final unsub = await _relaySession!.subscribeWithStatus(
468469
NostrFilter(
469470
kinds: const [EventKind.readState],
@@ -480,9 +481,16 @@ class ReadStateManager {
480481
// went live, unless it goes live later: after the 500 ms fallback
481482
// (slow AUTH on a cold start) or after a transient CLOSED's retry.
482483
// With limit 0 nothing replays the gap, so read the snapshot then.
484+
// A repeated ready while already live (a reconnect can send the
485+
// same REQ twice) leaves no gap, so it reads nothing.
483486
onStatusChanged: (status) {
484-
if (status == RelaySubscriptionStatus.ready && returned) {
485-
unawaited(_catchUpAfterLateReady(generation));
487+
if (status == RelaySubscriptionStatus.ready) {
488+
if (!live && returned) {
489+
unawaited(_catchUpAfterLateReady(generation));
490+
}
491+
live = true;
492+
} else {
493+
live = false;
486494
}
487495
},
488496
);
@@ -508,7 +516,9 @@ class ReadStateManager {
508516
if (pass != null) await pass;
509517
if (_disposed || !_servesThisCommunity) return;
510518
if (generation != _liveGeneration) return;
511-
await _fetchAndMerge();
519+
// A failed read is retried by the next publish, which reads the
520+
// snapshot in full after a failure.
521+
if (!await _fetchAndMerge()) _schedulePublish();
512522
}
513523

514524
void _handleIncomingEvent(NostrEvent event) {

‎mobile/test/features/channels/read_state/read_state_manager_test.dart‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,16 @@ void main() {
944944
expect(relay.subscribeCount, 2);
945945
expect(relay.liveSubscriptions, 1);
946946

947+
// Like the relay, a subscription reports ready before subscribe
948+
// returns, and a reconnect can send the same REQ again. Its second
949+
// ready finds the subscription already live and reads nothing more.
950+
relay.readyOnSubscribe = true;
951+
await manager.reinitializeRemote();
952+
expect(relay.fetchCount, 3);
953+
relay.statusListeners.last(RelaySubscriptionStatus.ready);
954+
await pumpEventQueue();
955+
expect(relay.fetchCount, 3);
956+
947957
manager.dispose();
948958
expect(relay.liveSubscriptions, 0);
949959
},
@@ -1175,6 +1185,54 @@ void main() {
11751185
},
11761186
);
11771187

1188+
test('a failed late-ready read is retried by the next publish', () async {
1189+
SharedPreferences.setMockInitialValues({});
1190+
final prefs = await SharedPreferences.getInstance();
1191+
final keychain = nostr.Keys.generate();
1192+
final crypto = ReadStateCrypto.tryCreate(
1193+
nsec: keychain.nsec,
1194+
pubkey: keychain.public,
1195+
)!;
1196+
fakeAsync((async) {
1197+
final relay = _FakeRelaySession();
1198+
final manager = ReadStateManager(
1199+
pubkey: keychain.public,
1200+
relayUrl: _relayUrl,
1201+
prefs: prefs,
1202+
crypto: crypto,
1203+
relaySession: relay,
1204+
signedEventRelay: _FakeSignedEventRelay(),
1205+
remoteEnabled: true,
1206+
onChanged: () {},
1207+
);
1208+
manager.initialize();
1209+
async.flushMicrotasks();
1210+
expect(relay.fetchCount, 1);
1211+
1212+
// The REQ goes live late and its catch-up read fails.
1213+
relay.historyEvents = [
1214+
_readStateEvent(
1215+
pubkey: keychain.public,
1216+
crypto: crypto,
1217+
clientId: 'desktop',
1218+
slotId: 'desktop-slot',
1219+
contexts: {'channel-1': 100},
1220+
createdAt: 100,
1221+
),
1222+
];
1223+
relay.failNextFetch = true;
1224+
relay.statusListeners.last(RelaySubscriptionStatus.ready);
1225+
async.flushMicrotasks();
1226+
expect(relay.fetchCount, 2);
1227+
expect(manager.getEffectiveTimestamp('channel-1'), isNot(100));
1228+
1229+
async.elapse(const Duration(seconds: 5));
1230+
expect(relay.fetchCount, 3);
1231+
expect(manager.getEffectiveTimestamp('channel-1'), 100);
1232+
manager.dispose(flushPending: false);
1233+
});
1234+
});
1235+
11781236
test('remote read-state rollback is ignored', () async {
11791237
SharedPreferences.setMockInitialValues({});
11801238
final prefs = await SharedPreferences.getInstance();
@@ -1401,9 +1459,14 @@ class _FakeRelaySession extends RelaySessionNotifier {
14011459
required void Function(RelaySubscriptionStatus status) onStatusChanged,
14021460
}) {
14031461
statusListeners.add(onStatusChanged);
1404-
return subscribe(filter, onEvent, onClosed: onClosed);
1462+
final unsubscribe = subscribe(filter, onEvent, onClosed: onClosed);
1463+
if (readyOnSubscribe) onStatusChanged(RelaySubscriptionStatus.ready);
1464+
return unsubscribe;
14051465
}
14061466

1467+
/// Reports ready before subscribe returns, as the relay's EOSE does.
1468+
bool readyOnSubscribe = false;
1469+
14071470
@override
14081471
Future<void Function()> subscribe(
14091472
NostrFilter filter,

0 commit comments

Comments
 (0)