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 <noreply@anthropic.com>
fix/754-discovery-per-radio
diffwireauto 1 month ago
parent 8cea58ec1d
commit 34ab79df24

@ -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

@ -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<List<Contact>> 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<void> saveContacts(List<Contact> 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<String, dynamic> _toJson(Contact contact) {

@ -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<int>.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');
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.