Flood when the direct-attempt budget is already spent
A flood-only send was keeping the contact's direct path whenever route history had not loaded yet. Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
2 files changed
+113
-13
No files matched your search
@@ -2868,6 +2868,18 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
required int directRetries,
|
required int directRetries,
|
||||||
List<PathSelection> recentSelections = const [],
|
List<PathSelection> 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 =
|
final hasKnownPaths =
|
||||||
_pathHistoryService?.getRecentPaths(contactPubKeyHex).isNotEmpty ??
|
_pathHistoryService?.getRecentPaths(contactPubKeyHex).isNotEmpty ??
|
||||||
false;
|
false;
|
||||||
@@ -2884,19 +2896,34 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
recentSelections: recentSelections,
|
recentSelections: recentSelections,
|
||||||
context: routeContext,
|
context: routeContext,
|
||||||
);
|
);
|
||||||
if (selection != null) {
|
if (selection == null) return null;
|
||||||
_pathHistoryService?.recordPathAttempt(contactPubKeyHex, selection);
|
return _recordAutoPath(
|
||||||
// Logged so route choices and their outcomes can be compared from the
|
contactPubKeyHex,
|
||||||
// app debug log on a live mesh.
|
selection,
|
||||||
_appDebugLogService?.info(
|
attemptIndex: attemptIndex,
|
||||||
'Route pick for ${contact?.name ?? contactPubKeyHex.substring(0, 8)}: '
|
routeContext: routeContext,
|
||||||
'attempt ${attemptIndex + 1} '
|
);
|
||||||
'→ ${_describeRoute(selection)} '
|
}
|
||||||
'(heard nearby: ${routeContext.hasNeighbourInfo}, '
|
|
||||||
'contact heard direct: ${routeContext.contactHeardDirectly})',
|
PathSelection _recordAutoPath(
|
||||||
tag: 'Routing',
|
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;
|
return selection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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<int>.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]);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user