diff --git a/lib/storage/channel_message_store.dart b/lib/storage/channel_message_store.dart index c962a5b1..6c16e4ff 100644 --- a/lib/storage/channel_message_store.dart +++ b/lib/storage/channel_message_store.dart @@ -47,6 +47,35 @@ String mergeChannelHistoryJson((String?, String) input) { return jsonEncode(merged.map(ChannelMessageStore._messageToJson).toList()); } +/// Drops the record whose merge key is `target` from a channel history blob. +/// +/// `input` is `(storedJson, targetMergeKey)`. Top-level and static-only so +/// `compute` can send it (#919). Returns null, so nothing is written, when +/// there is no history or the target is not in it: re-encoding 5 MB to change +/// nothing is exactly the cost this Epic exists to stop paying. +/// +/// Throws on undecodable stored JSON, which `mergeOffThread` turns into a +/// refused write. That matches the inline code it replaces, which logged and +/// left the blob alone. +String? dropFromChannelHistoryJson((String?, String) input) { + final (stored, target) = input; + if (stored == null || stored.isEmpty) return null; + + final kept = []; + var removed = false; + for (final e in jsonDecode(stored) as List) { + final m = ChannelMessageStore._messageFromJson(e as Map); + if (ChannelMessageStore._mergeKey(m) == target) { + removed = true; + } else { + kept.add(m); + } + } + if (!removed) return null; + + return jsonEncode(kept.map(ChannelMessageStore._messageToJson).toList()); +} + class ChannelMessageStore { static const String _keyPrefix = 'channel_messages_'; @@ -144,33 +173,21 @@ class ChannelMessageStore { /// Removes a single message from the persisted history. The explicit delete /// path (#343): save merges and never removes, so deletion cannot go through /// save. + /// + /// Decode and re-encode run off the UI isolate for the same reason saving + /// does (#919): a delete rewrites the whole blob, so on a large channel it + /// blocked the UI for as long as a save did, right when the user had just + /// tapped something. Future removeChannelMessage( int channelIndex, ChannelMessage message, ) async { if (publicKeyHex.isEmpty) return; - final key = _storageKey(channelIndex); - final blobs = BlobStore.instance; - await blobs.synchronized(key, () async { - final existing = await blobs.readWithPrefsFallback(key); - if (existing == null || existing.isEmpty) return; - final List raw; - try { - raw = jsonDecode(existing) as List; - } catch (e) { - appLogger.error( - 'Failed to decode channel $channelIndex history for delete: $e', - tag: 'Storage', - ); - return; - } - final target = _mergeKey(message); - final kept = raw - .map((e) => _messageFromJson(e as Map)) - .where((m) => _mergeKey(m) != target) - .toList(); - await blobs.write(key, jsonEncode(kept.map(_messageToJson).toList())); - }); + await BlobStore.instance.mergeOffThread( + _storageKey(channelIndex), + _mergeKey(message), + dropFromChannelHistoryJson, + ); } /// Load messages for a specific channel diff --git a/lib/storage/drift/blob_store.dart b/lib/storage/drift/blob_store.dart index 8015f380..ec31ba2e 100644 --- a/lib/storage/drift/blob_store.dart +++ b/lib/storage/drift/blob_store.dart @@ -157,8 +157,13 @@ class BlobStore { /// /// [merge] **must be a top-level or static function**: `compute` sends it to /// another isolate, and a closure capturing `this` is not sendable. It - /// receives `(storedJson, incomingJson)` and returns the JSON to store, or - /// null to leave the key untouched. + /// receives `(storedJson, payload)` and returns the JSON to store, or null to + /// leave the key untouched. + /// + /// [payload] is whatever single string the callback needs alongside the + /// stored blob: the incoming batch as JSON when saving, the merge key of the + /// record to drop when deleting. The callback defines its meaning; this + /// method only carries it across the isolate boundary. /// /// Fails closed. If [merge] throws, including on malformed stored JSON, the /// error is logged and **the key is not written**, so a corrupt or @@ -174,19 +179,19 @@ class BlobStore { /// Returns true when the key was written. Future mergeOffThread( String key, - String incomingJson, + String payload, BlobMergeCallback merge, ) { return synchronized(key, () async { final stored = await readWithPrefsFallback(key); final String? merged; try { - merged = await compute(merge, (stored, incomingJson)); + merged = await compute(merge, (stored, payload)); } catch (e, stack) { appLogger.error( 'Off-thread merge FAILED for $key (stored ' - '${stored?.length ?? "null"} chars, incoming ' - '${incomingJson.length} chars): $e. The key was NOT written, so ' + '${stored?.length ?? "null"} chars, payload ' + '${payload.length} chars): $e. The key was NOT written, so ' 'nothing was truncated.\n$stack', tag: 'Storage', ); @@ -409,10 +414,10 @@ class BlobStore { /// Merge step for [BlobStore.mergeOffThread], run on another isolate. /// -/// Takes `(storedJson, incomingJson)` and returns the JSON to store, or null to +/// Takes `(storedJson, payload)` and returns the JSON to store, or null to /// leave the key untouched. Must be a top-level or static function so `compute` -/// can send it; a record of strings is the payload so sendability is not in -/// question. +/// can send it; a record of two strings crosses the boundary, so sendability is +/// not in question. typedef BlobMergeCallback = String? Function((String?, String) input); /// Mutable tally of a migration run; surfaced in the log and used by tests. diff --git a/lib/storage/message_store.dart b/lib/storage/message_store.dart index 584adcad..8e417d56 100644 --- a/lib/storage/message_store.dart +++ b/lib/storage/message_store.dart @@ -38,6 +38,30 @@ String mergeDmHistoryJson((String?, String) input) { return jsonEncode(merged.map(MessageStore._messageToJson).toList()); } +/// Drops the record whose merge key is `target` from a DM history blob. +/// +/// `input` is `(storedJson, targetMergeKey)`. Returns null, so nothing is +/// written, when there is no history or the target is not in it. Throws on +/// undecodable stored JSON, which `mergeOffThread` turns into a refused write. +String? dropFromDmHistoryJson((String?, String) input) { + final (stored, target) = input; + if (stored == null || stored.isEmpty) return null; + + final kept = []; + var removed = false; + for (final e in jsonDecode(stored) as List) { + final m = MessageStore._messageFromJson(e as Map); + if (MessageStore._mergeKey(m) == target) { + removed = true; + } else { + kept.add(m); + } + } + if (!removed) return null; + + return jsonEncode(kept.map(MessageStore._messageToJson).toList()); +} + class MessageStore { static const String _keyPrefix = 'messages_'; @@ -76,29 +100,17 @@ class MessageStore { } /// Explicit delete path (#343): save merges and never removes. + /// + /// Decode and re-encode run off the UI isolate (#919), as in the channel + /// store. This also gains the per-key serialisation it did not have before: + /// it was a read-modify-write outside [BlobStore.synchronized], so a save + /// landing after a delete could resurrect the deleted message. Future removeMessage(String contactKeyHex, Message message) async { if (publicKeyHex.isEmpty) return; - final key = '$keyFor$contactKeyHex'; - final existing = await BlobStore.instance.readWithPrefsFallback(key); - if (existing == null || existing.isEmpty) return; - final List raw; - try { - raw = jsonDecode(existing) as List; - } catch (e) { - appLogger.error( - 'Failed to decode DM history for delete: $e', - tag: 'Storage', - ); - return; - } - final target = _mergeKey(message); - final kept = raw - .map((e) => _messageFromJson(e as Map)) - .where((m) => _mergeKey(m) != target) - .toList(); - await BlobStore.instance.write( - key, - jsonEncode(kept.map(_messageToJson).toList()), + await BlobStore.instance.mergeOffThread( + '$keyFor$contactKeyHex', + _mergeKey(message), + dropFromDmHistoryJson, ); } diff --git a/test/storage/blob_delete_off_thread_test.dart b/test/storage/blob_delete_off_thread_test.dart new file mode 100644 index 00000000..58ca7935 --- /dev/null +++ b/test/storage/blob_delete_off_thread_test.dart @@ -0,0 +1,188 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/models/channel_message.dart'; +import 'package:meshcore_open/models/message.dart'; +import 'package:meshcore_open/storage/channel_message_store.dart'; +import 'package:meshcore_open/storage/drift/blob_store.dart'; +import 'package:meshcore_open/storage/drift/offband_database.dart'; +import 'package:meshcore_open/storage/message_store.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// #919: the explicit delete path rewrites the whole blob too, so it moved off +/// the UI isolate with the save path. These tests pin that it still removes +/// exactly one record, never writes when there is nothing to change, and never +/// replaces a blob it could not read. +void main() { + late OffbandDatabase db; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + BlobStore.overrideForTest(BlobStore(db)); + }); + + tearDown(() async { + await db.close(); + BlobStore.clearTestOverride(); + }); + + ChannelMessage msg(int i) => ChannelMessage( + senderName: 's', + text: 'm$i', + timestamp: DateTime.fromMillisecondsSinceEpoch(1000 + i), + isOutgoing: false, + channelIndex: 0, + messageId: 'id$i', + ); + + Message dm(int i) => Message( + senderKey: Uint8List.fromList(const [1, 2, 3, 4]), + text: 'm$i', + timestamp: DateTime.fromMillisecondsSinceEpoch(1000 + i), + isOutgoing: false, + messageId: 'id$i', + ); + + // Fixtures go through the store's own encoder rather than a hand-written map, + // which silently rots the moment a non-null field is added to the schema. + Future encodeVia(List ms) async { + final fixtureDb = OffbandDatabase(NativeDatabase.memory()); + final previous = BlobStore.instance; + BlobStore.overrideForTest(BlobStore(fixtureDb)); + try { + final store = ChannelMessageStore()..setPublicKeyHex = 'f' * 20; + await store.saveChannelMessages(0, ms); + final keys = await BlobStore.instance.keysWithPrefix('channel_messages_'); + return (await BlobStore.instance.read(keys.single))!; + } finally { + BlobStore.overrideForTest(previous); + await fixtureDb.close(); + } + } + + group('dropFromChannelHistoryJson', () { + test('drops only the target and keeps the rest', () async { + final blobs = BlobStore.instance; + await blobs.write('k', await encodeVia([msg(1), msg(2), msg(3)])); + + final wrote = await blobs.mergeOffThread( + 'k', + 'id:id2', + dropFromChannelHistoryJson, + ); + + expect(wrote, isTrue); + final out = jsonDecode((await blobs.read('k'))!) as List; + expect(out.map((e) => (e as Map)['messageId']), [ + 'id1', + 'id3', + ]); + }); + + test('a target that is not present writes nothing at all', () async { + final blobs = BlobStore.instance; + final original = await encodeVia([msg(1)]); + await blobs.write('k', original); + + final wrote = await blobs.mergeOffThread( + 'k', + 'id:nope', + dropFromChannelHistoryJson, + ); + + expect( + wrote, + isFalse, + reason: + 'rewriting megabytes to change nothing is the cost #919 exists ' + 'to remove', + ); + expect(await blobs.read('k'), original); + }); + + test('a corrupt blob survives a delete untouched', () async { + final blobs = BlobStore.instance; + const corrupt = '{not json at all'; + await blobs.write('k', corrupt); + + final wrote = await blobs.mergeOffThread( + 'k', + 'id:id1', + dropFromChannelHistoryJson, + ); + + expect(wrote, isFalse); + expect(await blobs.read('k'), corrupt); + }); + + test('an absent blob is a no-op, not an empty write', () async { + final blobs = BlobStore.instance; + + final wrote = await blobs.mergeOffThread( + 'missing', + 'id:id1', + dropFromChannelHistoryJson, + ); + + expect(wrote, isFalse); + expect(await blobs.read('missing'), isNull); + }); + + test('removeChannelMessage deletes through the store API', () async { + final store = ChannelMessageStore()..setPublicKeyHex = 'c' * 20; + await store.saveChannelMessages(0, [msg(1), msg(2), msg(3)]); + + await store.removeChannelMessage(0, msg(2)); + + final loaded = await store.loadChannelMessages(0); + expect(loaded.map((m) => m.messageId), ['id1', 'id3']); + }); + }); + + group('dropFromDmHistoryJson', () { + test('removeMessage deletes through the store API', () async { + final store = MessageStore()..setPublicKeyHex = 'c' * 20; + await store.saveMessages('cafe', [dm(1), dm(2), dm(3)]); + + await store.removeMessage('cafe', dm(2)); + + final loaded = await store.loadMessages('cafe'); + expect(loaded.map((m) => m.messageId), ['id1', 'id3']); + }); + + test( + 'a delete racing a save cannot resurrect the deleted message', + () async { + final store = MessageStore()..setPublicKeyHex = 'd' * 20; + await store.saveMessages('f00d', [dm(1), dm(2)]); + + // Both are read-modify-writes on one key. Before #919 the delete ran + // outside the per-key chain, so a save landing after it could bring + // the deleted message back. + await Future.wait([ + store.removeMessage('f00d', dm(2)), + store.saveMessages('f00d', [dm(3)]), + ]); + + final loaded = await store.loadMessages('f00d'); + expect(loaded.map((m) => m.messageId), ['id1', 'id3']); + }, + ); + + test('a corrupt DM blob survives a delete untouched', () async { + final store = MessageStore()..setPublicKeyHex = 'e' * 20; + const corrupt = '{not json at all'; + await BlobStore.instance.write('messages_eeeeeeeeeedead', corrupt); + + await store.removeMessage('dead', dm(1)); + + expect(await BlobStore.instance.read('messages_eeeeeeeeeedead'), corrupt); + }); + }); +} diff --git a/test/storage/blob_merge_off_thread_test.dart b/test/storage/blob_merge_off_thread_test.dart index 09b7ef52..3ba927d6 100644 --- a/test/storage/blob_merge_off_thread_test.dart +++ b/test/storage/blob_merge_off_thread_test.dart @@ -326,7 +326,6 @@ void main() { ); }); }); - } /// Top-level so `compute` can send it.