From 34ab79df2462e6e5fc25a6af6892ffeda835f9d9 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Mon, 19 Oct 2026 02:56:21 -0400 Subject: [PATCH] fix(#754): scope discovered contacts per radio Discovered contacts (nodes heard but not added) were stored under one global 'discovered_contacts' blob shared by every radio, so one radio's discoveries showed up while another was connected (#754, Feature #853). Scope the store by the connected device-key prefix like contact_store and the others; the connect block sets the key and the existing startup load clears + reloads the list for that radio. An unset key loads nothing and never writes, so it can't read or clobber the legacy shared blob. No migration: the legacy blob is orphaned and each radio's list self-heals as it re-hears adverts. Part of radio-profile isolation Feature #853. Co-Authored-By: Claude Opus 4.8 --- lib/connector/meshcore_connector.dart | 4 + lib/storage/contact_discovery_store.dart | 19 ++++- .../contact_discovery_per_radio_test.dart | 84 +++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 test/storage/contact_discovery_per_radio_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index fe215e2e..841eba39 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -6780,6 +6780,10 @@ class MeshCoreConnector extends ChangeNotifier { _contactStore.setPublicKeyHex = selfPublicKeyHex; _channelStore.setPublicKeyHex = selfPublicKeyHex; _unreadStore.setPublicKeyHex = selfPublicKeyHex; + // Discovered contacts are what THIS radio heard; scope them per radio so + // they don't bleed across radios on one host (#754). The startup load that + // follows clears and reloads the in-memory list from this radio's key. + _discoveryContactStore.setPublicKeyHex = selfPublicKeyHex; // Now that we have self info, we can load all the persisted data for this // node. Each step is timed: this sequence has stalled the UI isolate for diff --git a/lib/storage/contact_discovery_store.dart b/lib/storage/contact_discovery_store.dart index 7622fdb9..b2827572 100644 --- a/lib/storage/contact_discovery_store.dart +++ b/lib/storage/contact_discovery_store.dart @@ -8,10 +8,23 @@ import 'drift/blob_store.dart'; class ContactDiscoveryStore { static const String _keyPrefix = 'discovered_contacts'; + /// First 10 hex of the connected radio's key, like every other per-radio + /// store. Discovered contacts are what THIS radio heard, so they are scoped + /// per radio and never shared across radios on the same host (#754). The + /// legacy unscoped `discovered_contacts` blob is left orphaned; the list + /// self-heals as each radio re-hears adverts. + String publicKeyHex = ''; + set setPublicKeyHex(String value) => + publicKeyHex = value.length > 10 ? value.substring(0, 10) : ''; + + String get _keyFor => '$_keyPrefix$publicKeyHex'; + Future> loadContacts() async { + // No connected radio -> no list (never the shared/legacy blob). + if (publicKeyHex.isEmpty) return []; // Bulk data lives in drift (#335), not SharedPreferences. The fallback // covers a key the migration has not moved yet and logs loudly if it fires. - final jsonStr = await BlobStore.instance.readWithPrefsFallback(_keyPrefix); + final jsonStr = await BlobStore.instance.readWithPrefsFallback(_keyFor); if (jsonStr == null) return []; try { @@ -30,8 +43,10 @@ class ContactDiscoveryStore { } Future saveContacts(List contacts) async { + // No connected radio -> don't write an unscoped/shared blob. + if (publicKeyHex.isEmpty) return; final jsonList = contacts.map(_toJson).toList(); - await BlobStore.instance.write(_keyPrefix, jsonEncode(jsonList)); + await BlobStore.instance.write(_keyFor, jsonEncode(jsonList)); } Map _toJson(Contact contact) { diff --git a/test/storage/contact_discovery_per_radio_test.dart b/test/storage/contact_discovery_per_radio_test.dart new file mode 100644 index 00000000..425ebdff --- /dev/null +++ b/test/storage/contact_discovery_per_radio_test.dart @@ -0,0 +1,84 @@ +import 'dart:typed_data'; + +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/storage/contact_discovery_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/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +// #754: discovered contacts are what THIS radio heard, so they must be scoped +// per radio like every other store, not shared across radios on one host. +void main() { + // Full-length keys; the store scopes on the first 10 hex, like production. + final keyA = 'aa' * 32; + final keyB = 'bb' * 32; + + late OffbandDatabase db; + + Contact node(int keyByte, String name) => Contact( + publicKey: Uint8List.fromList(List.filled(32, keyByte)), + name: name, + type: 1, + pathLength: -1, + path: Uint8List(0), + lastSeen: DateTime.utc(2026, 1, 1), + ); + + ContactDiscoveryStore store(String deviceKey) { + final s = ContactDiscoveryStore(); + s.setPublicKeyHex = deviceKey; + return s; + } + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + BlobStore.overrideForTest(BlobStore(db)); + }); + + tearDown(() async { + BlobStore.clearTestOverride(); + await db.close(); + }); + + test( + 'radio A discoveries are not visible while radio B is connected', + () async { + await store( + keyA, + ).saveContacts([node(0x11, 'Alpha'), node(0x22, 'Bravo')]); + + expect((await store(keyA).loadContacts()).map((c) => c.name).toSet(), { + 'Alpha', + 'Bravo', + }); + expect(await store(keyB).loadContacts(), isEmpty); + }, + ); + + test( + 'an unset device key loads nothing, never the legacy shared blob', + () async { + await store(keyA).saveContacts([node(0x11, 'Alpha')]); + + final unscoped = ContactDiscoveryStore(); // no radio connected + expect(await unscoped.loadContacts(), isEmpty); + // A save with no radio connected must not write a shared blob either. + await unscoped.saveContacts([node(0x99, 'Ghost')]); + expect(await store(keyB).loadContacts(), isEmpty); + }, + ); + + test('each radio keeps its own discovery list across a switch', () async { + await store(keyA).saveContacts([node(0x11, 'Alpha')]); + await store(keyB).saveContacts([node(0x22, 'Bravo')]); + + expect((await store(keyA).loadContacts()).single.name, 'Alpha'); + expect((await store(keyB).loadContacts()).single.name, 'Bravo'); + }); +}