Move send controls into Routing and split retry limits

Direct and flood attempts are separate settings, available with auto
route rotation off. Saved single retry limits migrate to that split,
and reset restores the whole Routing section.

Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
Cursor Agentandzjs81 committed 2026-11-21 00:50:53 +00:00
1 parent 34ea0cb7ca
commit 1b804bd1eb
69 files changed
+2199 -784

No files matched your search

+22 -10
View File
@@ -404,7 +404,9 @@ void main() {
expect(settings.initialRouteWeight, equals(3.0));
expect(settings.routeWeightSuccessIncrement, equals(0.5));
expect(settings.routeWeightFailureDecrement, equals(0.2));
expect(settings.maxMessageRetries, equals(5));
expect(settings.directRetries, equals(4));
expect(settings.floodRetries, equals(1));
expect(settings.loginAttempts, equals(5));
expect(settings.evictDiscoveredContactsEnabled, isTrue);
});
@@ -415,13 +417,17 @@ void main() {
expect(json.containsKey('initial_route_weight'), isTrue);
expect(json.containsKey('route_weight_success_increment'), isTrue);
expect(json.containsKey('route_weight_failure_decrement'), isTrue);
expect(json.containsKey('max_message_retries'), isTrue);
expect(json.containsKey('max_direct_retries'), isTrue);
expect(json.containsKey('max_flood_retries'), isTrue);
expect(json.containsKey('login_attempts'), isTrue);
expect(json.containsKey('evict_discovered_contacts_enabled'), isTrue);
expect(json['max_route_weight'], equals(5.0));
expect(json['initial_route_weight'], equals(3.0));
expect(json['route_weight_success_increment'], equals(0.5));
expect(json['route_weight_failure_decrement'], equals(0.2));
expect(json['max_message_retries'], equals(5));
expect(json['max_direct_retries'], equals(4));
expect(json['max_flood_retries'], equals(1));
expect(json['login_attempts'], equals(5));
expect(json['evict_discovered_contacts_enabled'], isTrue);
});
@@ -431,7 +437,9 @@ void main() {
'initial_route_weight': 2.0,
'route_weight_success_increment': 1.0,
'route_weight_failure_decrement': 1.5,
'max_message_retries': 8,
'max_direct_retries': 8,
'max_flood_retries': 3,
'login_attempts': 7,
'evict_discovered_contacts_enabled': false,
};
final settings = AppSettings.fromJson(json);
@@ -439,7 +447,9 @@ void main() {
expect(settings.initialRouteWeight, equals(2.0));
expect(settings.routeWeightSuccessIncrement, equals(1.0));
expect(settings.routeWeightFailureDecrement, equals(1.5));
expect(settings.maxMessageRetries, equals(8));
expect(settings.directRetries, equals(8));
expect(settings.floodRetries, equals(3));
expect(settings.loginAttempts, equals(7));
expect(settings.evictDiscoveredContactsEnabled, isFalse);
});
@@ -503,7 +513,9 @@ void main() {
expect(settings.initialRouteWeight, equals(3.0));
expect(settings.routeWeightSuccessIncrement, equals(0.5));
expect(settings.routeWeightFailureDecrement, equals(0.2));
expect(settings.maxMessageRetries, equals(5));
expect(settings.directRetries, equals(4));
expect(settings.floodRetries, equals(1));
expect(settings.loginAttempts, equals(5));
},
);
@@ -513,7 +525,7 @@ void main() {
expect(updated.maxRouteWeight, equals(8.0));
// Other fields should be unchanged
expect(updated.initialRouteWeight, equals(settings.initialRouteWeight));
expect(updated.maxMessageRetries, equals(settings.maxMessageRetries));
expect(updated.directRetries, equals(settings.directRetries));
});
test('copyWith works for initialRouteWeight', () {
@@ -543,10 +555,10 @@ void main() {
);
});
test('copyWith works for maxMessageRetries', () {
test('copyWith works for directRetries', () {
final settings = AppSettings();
final updated = settings.copyWith(maxMessageRetries: 10);
expect(updated.maxMessageRetries, equals(10));
final updated = settings.copyWith(directRetries: 10);
expect(updated.directRetries, equals(10));
expect(updated.maxRouteWeight, equals(settings.maxRouteWeight));
});
});
+151
View File
@@ -0,0 +1,151 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meshcore_open/models/app_settings.dart';
import 'package:meshcore_open/models/send_attempt_plan.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
void main() {
group('SendAttemptPlan', () {
const rotating = SendAttemptPlan(
directRetries: 4,
floodRetries: 1,
rotateRoutes: true,
);
const fixed = SendAttemptPlan(
directRetries: 4,
floodRetries: 2,
rotateRoutes: false,
);
test('rotation spends direct attempts then flood attempts', () {
expect(rotating.attemptLimit(floodPath: false), 5);
expect(rotating.attemptLimit(floodPath: true), 5);
expect(rotating.floodFromAttempt, 4);
});
test('a fixed path uses only that path kind', () {
expect(fixed.attemptLimit(floodPath: false), 4);
expect(fixed.attemptLimit(floodPath: true), 2);
expect(fixed.floodFromAttempt, isNull);
});
test('both limits at zero still send once and do not switch to flood', () {
const plan = SendAttemptPlan(
directRetries: 0,
floodRetries: 0,
rotateRoutes: true,
);
expect(plan.attemptLimit(floodPath: false), 1);
expect(plan.floodFromAttempt, isNull);
});
test('direct zero with rotation floods from the first attempt', () {
const plan = SendAttemptPlan(
directRetries: 0,
floodRetries: 3,
rotateRoutes: true,
);
expect(plan.attemptLimit(floodPath: false), 3);
expect(plan.floodFromAttempt, 0);
});
test('flood zero with rotation never switches to flood', () {
const plan = SendAttemptPlan(
directRetries: 6,
floodRetries: 0,
rotateRoutes: true,
);
expect(plan.attemptLimit(floodPath: true), 6);
expect(plan.floodFromAttempt, isNull);
});
});
group('AppSettings retry migration', () {
test('a saved single retry limit becomes direct plus one flood', () {
final settings = AppSettings.fromJson({'max_message_retries': 5});
expect(settings.directRetries, 4);
expect(settings.floodRetries, 1);
expect(settings.toJson().containsKey('max_message_retries'), isFalse);
});
test('the old maximum of 10 keeps nine direct attempts and one flood', () {
final settings = AppSettings.fromJson({'max_message_retries': 10});
expect(settings.directRetries, 9);
expect(settings.floodRetries, 1);
});
test('new keys win over a leftover single limit and are clamped', () {
final settings = AppSettings.fromJson({
'max_message_retries': 5,
'max_direct_retries': 40,
'max_flood_retries': -2,
'login_attempts': 0,
});
expect(settings.directRetries, SendAttemptPlan.maxDirectRetries);
expect(settings.floodRetries, SendAttemptPlan.minFloodRetries);
expect(settings.loginAttempts, SendAttemptPlan.minLoginAttempts);
});
test('round trip keeps the split limits', () {
final saved = AppSettings(
directRetries: 12,
floodRetries: 7,
loginAttempts: 3,
clearPathOnMaxRetry: true,
autoRouteRotationEnabled: false,
channelMinHopsEnabled: true,
).toJson();
final restored = AppSettings.fromJson(saved);
expect(restored.directRetries, 12);
expect(restored.floodRetries, 7);
expect(restored.loginAttempts, 3);
expect(restored.clearPathOnMaxRetry, isTrue);
expect(restored.autoRouteRotationEnabled, isFalse);
expect(restored.channelMinHopsEnabled, isTrue);
});
});
test('reset routing settings restores the moved controls', () async {
SharedPreferences.setMockInitialValues({});
await PrefsManager.initialize();
final service = AppSettingsService();
await service.loadSettings();
await service.updateSettings(
service.settings.copyWith(
clearPathOnMaxRetry: true,
autoRouteRotationEnabled: false,
directRetries: 15,
floodRetries: 10,
loginAttempts: 9,
channelMinHopsEnabled: true,
channelMinHops: 6,
channelMinHopsRetries: 4,
channelMinHopsCountRepeaters: true,
maxRouteWeight: 9,
),
);
await service.resetRoutingSettingsToDefault();
final settings = service.settings;
expect(settings.clearPathOnMaxRetry, isFalse);
expect(settings.autoRouteRotationEnabled, isTrue);
expect(settings.directRetries, SendAttemptPlan.defaultDirectRetries);
expect(settings.floodRetries, SendAttemptPlan.defaultFloodRetries);
expect(settings.loginAttempts, SendAttemptPlan.defaultLoginAttempts);
expect(settings.channelMinHopsEnabled, isFalse);
expect(settings.channelMinHops, 2);
expect(settings.channelMinHopsRetries, 1);
expect(settings.channelMinHopsCountRepeaters, isFalse);
expect(settings.maxRouteWeight, AppSettings.defaultMaxRouteWeight);
final reloaded = AppSettingsService();
await reloaded.loadSettings();
expect(
reloaded.settings.directRetries,
SendAttemptPlan.defaultDirectRetries,
);
expect(reloaded.settings.floodRetries, SendAttemptPlan.defaultFloodRetries);
});
}
@@ -0,0 +1,107 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/send_attempt_plan.dart';
import 'package:meshcore_open/screens/app_settings_screen.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/network_map/network_map_upload_service.dart';
import 'package:meshcore_open/services/translation_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:meshcore_open/widgets/settings_reveal.dart';
void main() {
testWidgets(
'routing settings show both retry limits and reveal channel options',
(tester) async {
SharedPreferences.setMockInitialValues({});
await PrefsManager.initialize();
final settings = AppSettingsService();
await settings.loadSettings();
await settings.updateSettings(
settings.settings.copyWith(autoRouteRotationEnabled: false),
);
tester.view.physicalSize = const Size(800, 8000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final connector = MeshCoreConnector();
final translation = TranslationService(settings);
final uploads = NetworkMapUploadService(
connector: connector,
settings: settings,
);
addTearDown(connector.dispose);
addTearDown(translation.dispose);
addTearDown(uploads.dispose);
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: settings),
ChangeNotifierProvider.value(value: connector),
ChangeNotifierProvider.value(value: translation),
ChangeNotifierProvider.value(value: uploads),
],
child: const MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: AppSettingsScreen(),
),
),
);
await tester.pumpAndSettle();
final context = tester.element(find.byType(AppSettingsScreen));
final l10n = AppLocalizations.of(context);
expect(find.text(l10n.appSettings_clearPathOnMaxRetry), findsOneWidget);
expect(
find.text(
l10n.appSettings_directRetries(SendAttemptPlan.defaultDirectRetries),
),
findsOneWidget,
);
expect(
find.text(
l10n.appSettings_floodRetries(SendAttemptPlan.defaultFloodRetries),
),
findsOneWidget,
);
expect(
find.text(
l10n.appSettings_loginAttempts(SendAttemptPlan.defaultLoginAttempts),
),
findsOneWidget,
);
// Retry limits stay visible while route rotation is off.
expect(find.text(l10n.appSettings_autoRouteRotation), findsOneWidget);
expect(find.text(l10n.appSettings_channelMinHopsCount(2)), findsNothing);
expect(find.text(l10n.appSettings_resetRoutingToDefault), findsOneWidget);
final channelSwitch = find.text(l10n.appSettings_channelMinHops);
await tester.tap(channelSwitch);
await tester.pump();
await tester.pump(RevealOnEnable.duration);
await tester.pumpAndSettle();
expect(
find.text(l10n.appSettings_channelMinHopsCount(2)),
findsOneWidget,
);
expect(
find.text(l10n.appSettings_channelMinHopsRetries(1)),
findsOneWidget,
);
await tester.tap(channelSwitch);
await tester.pumpAndSettle();
expect(find.text(l10n.appSettings_channelMinHopsCount(2)), findsNothing);
},
);
}
@@ -0,0 +1,168 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/models/message.dart';
import 'package:meshcore_open/models/path_selection.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/message_retry_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
Contact _contact({int pathLength = 1, List<int> path = const [0x11]}) =>
Contact(
publicKey: Uint8List.fromList(List<int>.generate(32, (i) => i + 1)),
name: 'Test',
type: 1,
pathLength: pathLength,
path: Uint8List.fromList(path),
lastSeen: DateTime.now(),
);
void main() {
setUp(() async {
SharedPreferences.setMockInitialValues({});
await PrefsManager.initialize();
});
test('rotation sends direct attempts, then flood, then stops', () async {
final settings = AppSettingsService();
await settings.loadSettings();
await settings.updateSettings(
settings.settings.copyWith(
autoRouteRotationEnabled: true,
directRetries: 1,
floodRetries: 1,
),
);
final service = MessageRetryService(
sentWatchdogGrace: const Duration(milliseconds: 5),
);
addTearDown(service.dispose);
final sends = <int>[];
final thresholds = <int>[];
service.initialize(
RetryServiceConfig(
sendMessage: (_, _, attempt, _) async {
sends.add(attempt);
},
addMessage: (_, _) {},
updateMessage: (_) {},
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
15,
appSettingsService: settings,
selectRetryPath: (_, attemptIndex, directRetries, _) {
thresholds.add(directRetries);
if (attemptIndex >= directRetries) {
return const PathSelection(
pathBytes: [],
hopCount: -1,
useFlood: true,
);
}
return const PathSelection(
pathBytes: [0x22],
hopCount: 1,
useFlood: false,
);
},
),
);
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
await pumpEventQueue();
await Future<void>.delayed(const Duration(milliseconds: 1400));
expect(sends, [0, 1]);
expect(thresholds, [1, 1]);
});
test('rotation off keeps retrying a direct path without flooding', () async {
final settings = AppSettingsService();
await settings.loadSettings();
await settings.updateSettings(
settings.settings.copyWith(
autoRouteRotationEnabled: false,
directRetries: 2,
floodRetries: 5,
),
);
final service = MessageRetryService(
sentWatchdogGrace: const Duration(milliseconds: 5),
);
addTearDown(service.dispose);
final sends = <int>[];
var picked = false;
service.initialize(
RetryServiceConfig(
sendMessage: (_, _, attempt, _) async {
sends.add(attempt);
},
addMessage: (_, _) {},
updateMessage: (_) {},
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
15,
appSettingsService: settings,
selectRetryPath: (_, _, _, _) {
picked = true;
return const PathSelection(
pathBytes: [],
hopCount: -1,
useFlood: true,
);
},
),
);
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
await pumpEventQueue();
await Future<void>.delayed(const Duration(milliseconds: 1400));
expect(sends, [0, 1]);
expect(picked, isFalse);
});
test('a flood path without rotation stops after the flood budget', () async {
final settings = AppSettingsService();
await settings.loadSettings();
await settings.updateSettings(
settings.settings.copyWith(
autoRouteRotationEnabled: false,
directRetries: 4,
floodRetries: 1,
),
);
final service = MessageRetryService(
sentWatchdogGrace: const Duration(milliseconds: 5),
);
addTearDown(service.dispose);
final latest = <Message>[];
final sends = <int>[];
service.initialize(
RetryServiceConfig(
sendMessage: (_, _, attempt, _) async {
sends.add(attempt);
},
addMessage: (_, message) => latest.add(message),
updateMessage: (message) => latest.add(message),
calculateTimeout: (_, _, {String? contactKey, int? deviceTimeoutMs}) =>
15,
appSettingsService: settings,
),
);
await service.sendMessageWithRetry(
contact: _contact(pathLength: -1, path: const []),
text: 'hello',
);
await pumpEventQueue();
await Future<void>.delayed(const Duration(milliseconds: 200));
expect(sends, [0]);
expect(latest.last.status, MessageStatus.failed);
});
}
+10 -2
View File
@@ -56,7 +56,12 @@ void main() {
);
test('missing RESP_CODE_SENT times out and retries', () async {
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
// A direct path uses the direct budget (default 4). A flood path would
// stop after the default single flood attempt.
await service.sendMessageWithRetry(
contact: _contact(pathLength: 1, path: const [0x11]),
text: 'hello',
);
await pumpEventQueue();
expect(sends.map((s) => s.attempt), [0]);
@@ -80,7 +85,10 @@ void main() {
});
test('late ACK for an attempt whose SENT was lost still delivers', () async {
await service.sendMessageWithRetry(contact: _contact(), text: 'hello');
await service.sendMessageWithRetry(
contact: _contact(pathLength: 1, path: const [0x11]),
text: 'hello',
);
await pumpEventQueue();
// Let the watchdog fire; the retry is now waiting on its backoff.
+19 -12
View File
@@ -121,7 +121,7 @@ void main() {
final pick = svc.selectPathForAttempt(
pubKey,
attemptIndex: attemptIndex,
maxRetries: 5,
directRetries: 4,
recentSelections: recent,
);
final key = pick.useFlood
@@ -139,7 +139,7 @@ void main() {
final selection = svc.selectPathForAttempt(
pubKey,
attemptIndex: 0,
maxRetries: 5,
directRetries: 4,
);
expect(selection.useFlood, isTrue);
});
@@ -150,7 +150,7 @@ void main() {
final selection = svc.selectPathForAttempt(
pubKey,
attemptIndex: 0,
maxRetries: 0,
directRetries: 0,
);
expect(selection.useFlood, isTrue);
});
@@ -163,7 +163,7 @@ void main() {
final selection = svc.selectPathForAttempt(
pubKey,
attemptIndex: i,
maxRetries: 5,
directRetries: 4,
);
expect(
selection.useFlood,
@@ -274,7 +274,7 @@ void main() {
final pick = svc.selectPathForAttempt(
pubKey,
attemptIndex: 0,
maxRetries: 5,
directRetries: 4,
);
expect(pick.useFlood, isFalse);
expect(pick.hopCount, 0);
@@ -299,21 +299,28 @@ void main() {
expect(resolved.pathBytes, isEmpty);
});
test('final attempt is always flood regardless of known paths', () async {
test('attempts at and after directRetries flood', () async {
final pubKey = _hex('ef01');
await _seed(svc, pubKey, pathBytes: [0x01], hopCount: 1);
for (final retries in [1, 2, 5, 10]) {
final lastAttempt = svc.selectPathForAttempt(
for (final direct in [0, 1, 4, 9]) {
final flooded = svc.selectPathForAttempt(
pubKey,
attemptIndex: retries - 1,
maxRetries: retries,
attemptIndex: direct,
directRetries: direct,
);
expect(
lastAttempt.useFlood,
flooded.useFlood,
isTrue,
reason: 'maxRetries=$retries: last attempt must be flood',
reason: 'directRetries=$direct: that attempt floods',
);
if (direct == 0) continue;
final routed = svc.selectPathForAttempt(
pubKey,
attemptIndex: direct - 1,
directRetries: direct,
);
expect(routed.useFlood, isFalse);
}
});
});
+1 -1
View File
@@ -205,7 +205,7 @@ void main() {
final pick = svc.selectPathForAttempt(
contact,
attemptIndex: attempt,
maxRetries: maxRetries,
directRetries: maxRetries - 1,
recentSelections: used,
context: context,
);