diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 9d59570e..9bcf9075 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -2868,6 +2868,18 @@ class MeshCoreConnector extends ChangeNotifier { required int directRetries, List recentSelections = const [], }) { + // Flood attempts do not need a saved route. History loads asynchronously + // and is empty on the first read after startup, so checking it first + // would keep the contact's direct path and spend a flood-only budget + // without flooding. + if (directRetries <= 0 || attemptIndex >= directRetries) { + return _recordAutoPath( + contactPubKeyHex, + const PathSelection(pathBytes: [], hopCount: -1, useFlood: true), + attemptIndex: attemptIndex, + ); + } + final hasKnownPaths = _pathHistoryService?.getRecentPaths(contactPubKeyHex).isNotEmpty ?? false; @@ -2884,19 +2896,34 @@ class MeshCoreConnector extends ChangeNotifier { recentSelections: recentSelections, context: routeContext, ); - if (selection != null) { - _pathHistoryService?.recordPathAttempt(contactPubKeyHex, selection); - // Logged so route choices and their outcomes can be compared from the - // app debug log on a live mesh. - _appDebugLogService?.info( - 'Route pick for ${contact?.name ?? contactPubKeyHex.substring(0, 8)}: ' - 'attempt ${attemptIndex + 1} ' - '→ ${_describeRoute(selection)} ' - '(heard nearby: ${routeContext.hasNeighbourInfo}, ' - 'contact heard direct: ${routeContext.contactHeardDirectly})', - tag: 'Routing', - ); - } + if (selection == null) return null; + return _recordAutoPath( + contactPubKeyHex, + selection, + attemptIndex: attemptIndex, + routeContext: routeContext, + ); + } + + PathSelection _recordAutoPath( + String contactPubKeyHex, + PathSelection selection, { + required int attemptIndex, + RouteContext? routeContext, + }) { + _pathHistoryService?.recordPathAttempt(contactPubKeyHex, selection); + final contact = getContactByPubKeyHex(contactPubKeyHex); + final context = routeContext ?? routeContextFor(contact); + // Logged so route choices and their outcomes can be compared from the + // app debug log on a live mesh. + _appDebugLogService?.info( + 'Route pick for ${contact?.name ?? contactPubKeyHex.substring(0, 8)}: ' + 'attempt ${attemptIndex + 1} ' + '→ ${_describeRoute(selection)} ' + '(heard nearby: ${context.hasNeighbourInfo}, ' + 'contact heard direct: ${context.contactHeardDirectly})', + tag: 'Routing', + ); return selection; } diff --git a/test/connector/flood_only_attempt_test.dart b/test/connector/flood_only_attempt_test.dart new file mode 100644 index 00000000..f436c53b --- /dev/null +++ b/test/connector/flood_only_attempt_test.dart @@ -0,0 +1,73 @@ +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/services/app_settings_service.dart'; +import 'package:meshcore_open/services/message_retry_service.dart'; +import 'package:meshcore_open/services/path_history_service.dart'; +import 'package:meshcore_open/services/storage_service.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; + +Contact _directContact() { + return Contact( + publicKey: Uint8List.fromList(List.generate(32, (i) => i + 1)), + name: 'Hill', + type: 1, + pathLength: 1, + path: Uint8List.fromList(const [0x11]), + lastSeen: DateTime.now(), + ); +} + +void main() { + late MeshCoreConnector connector; + late PathHistoryService history; + late MessageRetryService retries; + late AppSettingsService settings; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + settings = AppSettingsService(); + await settings.loadSettings(); + connector = MeshCoreConnector(); + history = PathHistoryService(StorageService()); + retries = MessageRetryService(); + connector.initialize( + retryService: retries, + pathHistoryService: history, + appSettingsService: settings, + ); + }); + + tearDown(() { + connector.dispose(); + history.dispose(); + retries.dispose(); + }); + + test('flood attempts do not wait for route history', () async { + await settings.updateSettings( + settings.settings.copyWith( + autoRouteRotationEnabled: true, + directRetries: 0, + floodRetries: 1, + ), + ); + + final flooded = await connector.preparePathForContactSend(_directContact()); + expect(flooded.useFlood, isTrue); + + await settings.updateSettings( + settings.settings.copyWith(directRetries: 2, floodRetries: 1), + ); + final direct = await connector.preparePathForContactSend(_directContact()); + expect(direct.useFlood, isFalse); + expect(direct.hopCount, 1); + expect(direct.pathBytes, [0x11]); + }); +}