Stamp legacy images inside the migration; hold early channel frames

- Image entries are stamped with their channel ID before the migration is
  marked done, so an interrupted run repeats it instead of leaving them
  unstamped (and invisible) forever.
- Channel message/data frames that arrive before this session's slot map
  is confirmed are held and replayed once it is. Current firmware only
  sends them in reply to SYNC_NEXT_MESSAGE, which already waits; this
  guards against a stale saved map filing them under the wrong channel.
This commit is contained in:
zjs81 committed 2026-10-11 18:04:53 -07:00
1 parent 679116ed03
commit 6a8df764bb
4 files changed
+87 -5

No files matched your search

+31 -4
View File
@@ -379,6 +379,12 @@ class MeshCoreConnector extends ChangeNotifier {
bool _isProcessingDeferredQueuedContactMessages = false;
bool _queuedMessageSyncInFlight = false;
final List<Uint8List> _deferredQueuedContactMessageFrames = [];
/// Channel frames that arrived before this session's slot map was
/// confirmed. The firmware only sends them in reply to SYNC_NEXT_MESSAGE,
/// which waits for the map, so this is a guard: their slot index cannot be
/// resolved yet, and they are replayed once it can.
final List<Uint8List> _channelFramesAwaitingMap = [];
bool _pendingQueueSync = false;
// CMD_APP_START stops the firmware's contact iterator (END_OF_CONTACTS is
// then never sent), so APP_START requested during a contact sync is held
@@ -3431,6 +3437,7 @@ class MeshCoreConnector extends ChangeNotifier {
_isProcessingDeferredQueuedContactMessages = false;
_queuedMessageSyncInFlight = false;
_deferredQueuedContactMessageFrames.clear();
_channelFramesAwaitingMap.clear();
_pendingQueueSync = false;
_resetSyncFlowState();
_queueSyncTimeout?.cancel();
@@ -5682,6 +5689,19 @@ class MeshCoreConnector extends ChangeNotifier {
_finishChannelSync();
}
void _replayChannelFramesAwaitingMap() {
if (_channelFramesAwaitingMap.isEmpty) return;
final frames = List<Uint8List>.of(_channelFramesAwaitingMap);
_channelFramesAwaitingMap.clear();
for (final frame in frames) {
if (frame[0] != respCodeChannelDataRecv) {
_handleIncomingChannelMessage(frame);
} else if (_appSettingsService?.settings.imageMessagesEnabled ?? false) {
_handleIncomingChannelData(frame);
}
}
}
/// A channel whose key changed in place keeps its position in the custom
/// order: [after] takes [before]'s place when no slot holds [before] any
/// more. Returns true when the order changed.
@@ -5714,6 +5734,7 @@ class MeshCoreConnector extends ChangeNotifier {
}
void _finishChannelSync() {
if (_channelMapConfirmed) _replayChannelFramesAwaitingMap();
final completer = _channelSyncCompleter;
_channelSyncCompleter = null;
if (completer != null && !completer.isCompleted) completer.complete();
@@ -5745,12 +5766,11 @@ class MeshCoreConnector extends ChangeNotifier {
// First: the channel sync is already running and carries unread counts
// over from these. The migration never touches the channel cache.
await loadCachedChannels();
final migrated = await ChannelStorageMigration.migrate(
await ChannelStorageMigration.migrate(
_channelStore.publicKeyHex,
beforeDone: (slotIds) async =>
_receivedImageStore?.stampLegacyChannelIds(slotIds),
);
if (migrated.isNotEmpty) {
await _receivedImageStore?.stampLegacyChannelIds(migrated);
}
await _loadChannelOrder();
final saved = await _channelStore.loadSlotChannelIds();
_slotChannelIds
@@ -5967,6 +5987,13 @@ class MeshCoreConnector extends ChangeNotifier {
unawaited(_handleIncomingMessage(frame));
}
break;
case respCodeChannelMsgRecv ||
respCodeChannelMsgRecvV3 ||
respCodeChannelDataRecv
when !_channelMapConfirmed:
if (_isSyncingQueuedMessages) _handleQueuedMessageReceived();
_channelFramesAwaitingMap.add(frame);
break;
case respCodeChannelMsgRecv:
case respCodeChannelMsgRecvV3:
_handleIncomingChannelMessage(frame);
+9 -1
View File
@@ -25,7 +25,14 @@ class ChannelStorageMigration {
/// Migrates [scope] (first 10 hex chars of the radio's public key) and
/// returns the slot -> channel ID mapping it used. Returns an empty map when
/// the scope was already migrated or must not adopt legacy data.
static Future<Map<int, String>> migrate(String scope) async {
///
/// [beforeDone] runs with that mapping before the migration is marked done,
/// so data kept elsewhere (images) is moved on the same one-shot terms: an
/// interrupted run repeats it on the next launch.
static Future<Map<int, String>> migrate(
String scope, {
Future<void> Function(Map<int, String> slotIds)? beforeDone,
}) async {
if (scope.isEmpty || !canMigrateLegacyKeys(scope)) return {};
final prefs = PrefsManager.instance;
final doneKey = '$_doneKeyPrefix$scope';
@@ -61,6 +68,7 @@ class ChannelStorageMigration {
);
}
await _migrateOrder(scope, slotIds);
if (slotIds.isNotEmpty) await beforeDone?.call(slotIds);
await prefs.setBool(doneKey, true);
await channelStore.saveSlotChannelIds(slotIds);
+26
View File
@@ -243,6 +243,32 @@ void main() {
expect(_storedTexts(hikingId), isEmpty);
});
test('a channel frame that arrives before the slot map is held, then filed '
'by the confirmed map', () async {
final radio = _TestRadio(
afterHandle: (radio, frame) {
if (frame[0] == cmdAppStart) {
radio.emitForTest(
ReviewFrames.channelMessage(
channelIndex: 1,
sender: 'Sam',
message: 'before the map',
timestampSecs: DateTime.now().millisecondsSinceEpoch ~/ 1000,
),
);
}
},
);
await _connect(
radio,
// A stale map from last session would file it under the ghost channel.
seed: () => _seedSlotMap({0: _id(_psk(0)), 1: ghostId, 2: _id(_psk(2))}),
);
await _waitFor(() => _texts(1).contains('before the map'));
expect(_storedTexts(hikingId), contains('before the map'));
expect(_storedTexts(ghostId), isNot(contains('before the map')));
});
test('MSG_WAITING before the channel sync waits for the slot map', () async {
// DEVICE_INFO is held back so MSG_WAITING lands between SELF_INFO and the
// start of the channel sync.
@@ -254,6 +254,27 @@ void main() {
);
});
test('images are stamped before the migration is marked done', () async {
await _init(legacyValues());
await expectLater(
ChannelStorageMigration.migrate(
_scopeA,
beforeDone: (_) async => throw StateError('killed'),
),
throwsStateError,
);
// Not marked done, so the next launch stamps again.
Map<int, String>? stamped;
await ChannelStorageMigration.migrate(
_scopeA,
beforeDone: (slotIds) async => stamped = slotIds,
);
expect(stamped, {0: _idOne, 2: _idTwo});
// Data moved by the first run is still there.
final messages = ChannelMessageStore()..setPublicKeyHex = _deviceA;
expect((await messages.loadChannelMessages(_idOne)).single.text, 'hello');
});
test('skips a scope that must not adopt legacy data', () async {
await _init(legacyValues());
scopesWithoutLegacyMigration.add(_scopeA);