Ignore impossible hops in path map resolution

Reject repeater matches and peer inferences whose distance from the previous plausible hop anchor exceeds the per-hop LoRa range budget (250 km scaled by consecutive unresolved hops).

Without a known self location, select the anchor chain that explains the most local hops so a single distant prefix collision does not poison resolution for the remaining hops. Apply the same hop-order resolution and anchor-bounded peer inference in PathTraceMapScreen.
This commit is contained in:
zjs81 committed 2026-11-21 13:14:32 -07:00
1 parent 3a93481bdf
commit 79f624c743
5 files changed
+1292 -189

No files matched your search

+256 -1
View File
@@ -8,13 +8,16 @@ import 'package:meshcore_open/models/contact.dart';
Contact _contact({
required int prefix,
int? secondByte,
required String name,
required double latitude,
required double longitude,
DateTime? lastSeen,
}) {
final key = Uint8List(32)..first = prefix;
if (secondByte != null) key[1] = secondByte;
return Contact(
publicKey: Uint8List(32)..first = prefix,
publicKey: key,
name: name,
type: advTypeRepeater,
pathLength: 0,
@@ -91,4 +94,256 @@ void main() {
expect(resolved.single?.name, 'Newer');
});
test(
'rejects single candidate impossibly far and preserves previousPosition',
() {
// Arizona self/local repeaters vs East Coast collision (~3700 km away).
const selfArizona = LatLng(33.4484, -112.0740);
final azHop1 = _contact(
prefix: 0x48,
name: 'AZ Repeater 1',
latitude: 33.60097,
longitude: -112.02457,
);
final eastCoastBogus = _contact(
prefix: 0x9C,
name: 'East Coast 9C',
latitude: 42.3601,
longitude: -71.0589,
);
final azHop3 = _contact(
prefix: 0x53,
name: 'AZ Repeater 2',
latitude: 33.46300,
longitude: -111.65000,
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0x53, 0x9C, 0x48],
contacts: [azHop1, eastCoastBogus, azHop3],
endpoint: selfArizona,
resolveFromEnd: true,
);
expect(resolved.map((c) => c?.name), [
'AZ Repeater 2',
null,
'AZ Repeater 1',
]);
},
);
test('rejects multiple candidates when all are impossibly far', () {
const selfArizona = LatLng(33.4484, -112.0740);
final eastCoast = _contact(
prefix: 0x9C,
name: 'East Coast 9C',
latitude: 42.3601,
longitude: -71.0589,
lastSeen: DateTime.utc(2026, 2),
);
final europe = _contact(
prefix: 0x9C,
name: 'Europe 9C',
latitude: 48.8566,
longitude: 2.3522,
lastSeen: DateTime.utc(2026, 1),
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0x9C],
contacts: [eastCoast, europe],
endpoint: selfArizona,
);
expect(resolved, [null]);
});
test(
'picks plausible candidate when another candidate is impossibly far',
() {
const selfArizona = LatLng(33.4484, -112.0740);
final eastCoastNewer = _contact(
prefix: 0x9C,
name: 'East Coast 9C',
latitude: 42.3601,
longitude: -71.0589,
lastSeen: DateTime.utc(2026, 5),
);
final localOlder = _contact(
prefix: 0x9C,
name: 'Local AZ 9C',
latitude: 33.50,
longitude: -111.90,
lastSeen: DateTime.utc(2026, 1),
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0x9C],
contacts: [eastCoastNewer, localOlder],
endpoint: selfArizona,
);
expect(resolved.single?.name, 'Local AZ 9C');
},
);
test('consecutive unresolved hops widen distance budget proportionally', () {
// At equator, 1 degree longitude is ~111.32 km.
// With 2 unresolved intermediate hops (hopGap = 3), budget is 3 * 250 km = 750 km.
// 6.0 degrees (~667.9 km) is beyond 1-hop (250 km) and 2-hop (500 km),
// but within 3-hop (750 km).
const endpoint = LatLng(0, 0);
final distantValid = _contact(
prefix: 0xCC,
name: 'Valid 3-hop target',
latitude: 0,
longitude: 6.0,
);
final resolvedWithTwoUnknowns = PathHopResolver.resolve(
pathBytes: const [0xAA, 0xBB, 0xCC],
contacts: [distantValid],
endpoint: endpoint,
);
expect(resolvedWithTwoUnknowns, [null, null, distantValid]);
// With only 1 hop (hopGap = 1, budget = 250 km), 6.0 degrees is rejected.
final resolvedDirect = PathHopResolver.resolve(
pathBytes: const [0xCC],
contacts: [distantValid],
endpoint: endpoint,
);
expect(resolvedDirect, [null]);
});
test(
'endpoint == null rejects a leading distant outlier and keeps local cluster',
() {
final bogusFirst = _contact(
prefix: 0x9C,
name: 'East Coast Outlier',
latitude: 42.3601,
longitude: -71.0589,
lastSeen: DateTime.utc(2026, 6),
);
final local1 = _contact(
prefix: 0x48,
name: 'Local 1',
latitude: 33.60097,
longitude: -112.02457,
);
final local2 = _contact(
prefix: 0x53,
name: 'Local 2',
latitude: 33.46300,
longitude: -111.65000,
);
final local3 = _contact(
prefix: 0x6C,
name: 'Local 3',
latitude: 33.42,
longitude: -111.80,
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0x9C, 0x48, 0x53, 0x6C],
contacts: [bogusFirst, local1, local2, local3],
endpoint: null,
);
expect(resolved.map((c) => c?.name), [
null,
'Local 1',
'Local 2',
'Local 3',
]);
},
);
test(
'endpoint != null rejects first-hop outlier and resolves subsequent local hops',
() {
const selfArizona = LatLng(33.4484, -112.0740);
final bogusFirst = _contact(
prefix: 0xEA,
name: 'East Coast First Hop',
latitude: 42.3601,
longitude: -71.0589,
);
final local2 = _contact(
prefix: 0x48,
name: 'Local 2',
latitude: 33.60097,
longitude: -112.02457,
);
final local3 = _contact(
prefix: 0x53,
name: 'Local 3',
latitude: 33.46300,
longitude: -111.65000,
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0xEA, 0x48, 0x53],
contacts: [bogusFirst, local2, local3],
endpoint: selfArizona,
);
expect(resolved.map((c) => c?.name), [null, 'Local 2', 'Local 3']);
},
);
test('legitimate long-range line-of-sight LoRa hops still resolve', () {
// ~200 km mountain-to-mountain hop (within 250 km single-hop budget).
const self = LatLng(33.4484, -112.0740);
final mountainPeak = _contact(
prefix: 0xAA,
name: 'Mountain Peak',
latitude: 35.1983,
longitude: -111.6513, // Flagstaff (~198 km from Phoenix)
);
final secondPeak = _contact(
prefix: 0xBB,
name: 'North Rim',
latitude: 36.2100,
longitude: -112.0600, // ~118 km from Flagstaff
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0xAA, 0xBB],
contacts: [mountainPeak, secondPeak],
endpoint: self,
);
expect(resolved.map((c) => c?.name), ['Mountain Peak', 'North Rim']);
});
test('multi-byte path hash width rejects impossible hop', () {
const self = LatLng(33.4484, -112.0740);
final local = _contact(
prefix: 0x48,
secondByte: 0x01,
name: 'Local 2-byte',
latitude: 33.60,
longitude: -112.02,
);
final bogus = _contact(
prefix: 0x9C,
secondByte: 0x02,
name: 'Bogus 2-byte',
latitude: 40.7128,
longitude: -74.0060,
);
final resolved = PathHopResolver.resolve(
pathBytes: const [0x48, 0x01, 0x9C, 0x02],
contacts: [local, bogus],
endpoint: self,
pathHashByteWidth: 2,
);
expect(resolved.map((c) => c?.name), ['Local 2-byte', null]);
});
}
@@ -0,0 +1,292 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/channel_message.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/screens/channel_message_path_screen.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/map_tile_cache_service.dart';
import 'package:provider/provider.dart';
class _FakeConnector extends MeshCoreConnector {
_FakeConnector(this._contacts, {this.lat = 33.4484, this.lon = -112.0740});
final List<Contact> _contacts;
final double? lat;
final double? lon;
@override
List<Contact> get allContacts => _contacts;
@override
double? get selfLatitude => lat;
@override
double? get selfLongitude => lon;
}
Contact _repeater({
required int prefix,
required String name,
required double latitude,
required double longitude,
}) {
return Contact(
publicKey: Uint8List(32)..first = prefix,
name: name,
type: advTypeRepeater,
pathLength: 0,
path: Uint8List(0),
latitude: latitude,
longitude: longitude,
lastSeen: DateTime.utc(2026),
);
}
Widget _wrapWithProviders(MeshCoreConnector connector, Widget child) {
final settings = AppSettingsService();
return MultiProvider(
providers: [
ChangeNotifierProvider<MeshCoreConnector>.value(value: connector),
ChangeNotifierProvider<AppSettingsService>.value(value: settings),
ChangeNotifierProvider<MapTileCacheService>(
create: (_) => MapTileCacheService(appSettingsService: settings),
),
],
child: MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: child,
),
);
}
void main() {
testWidgets(
'ChannelMessagePathMapScreen ignores impossible distant hop in polylines, markers, bounds, distance, and hop list',
(tester) async {
final localHop3 = _repeater(
prefix: 0x53,
name: "Rod's Repeater",
latitude: 33.46300,
longitude: -111.65000,
);
final localHop4 = _repeater(
prefix: 0x48,
name: 'Twain - Clearview',
latitude: 33.60097,
longitude: -112.02457,
);
final bogusEastCoastHop5 = _repeater(
prefix: 0x9C,
name: 'Beacon Hill 1W Repeater',
latitude: 42.3601,
longitude: -71.0589,
);
final connector = _FakeConnector([
localHop3,
localHop4,
bogusEastCoastHop5,
]);
final message = ChannelMessage(
senderName: 'Alice',
text: 'test',
timestamp: DateTime.utc(2026),
isOutgoing: false,
pathBytes: Uint8List.fromList([
0x6C,
0x84,
0x53,
0x48,
0x9C,
0x21,
0xC9,
0xEA,
]),
channelIndex: 0,
);
await tester.pumpWidget(
_wrapWithProviders(
connector,
ChannelMessagePathMapScreen(message: message, channelMessage: true),
),
);
await tester.pumpAndSettle();
// East Coast repeater must not appear as a resolved repeater or in markers.
expect(find.textContaining('Beacon Hill 1W Repeater'), findsNothing);
expect(find.textContaining('42.36010'), findsNothing);
// Expand the hop list panel so all 8 hops in the ListView are built.
await tester.tap(find.byIcon(Icons.open_in_full));
await tester.pumpAndSettle();
expect(find.text('(9C) Unknown Repeater'), findsOneWidget);
// Local repeaters should still be listed and plotted.
expect(find.text("(53) Rod's Repeater"), findsOneWidget);
expect(find.text('(48) Twain - Clearview'), findsOneWidget);
// Collapse back so map assertions (PolylineLayer, MarkerLayer, header distance) are visible.
await tester.tap(find.byIcon(Icons.close_fullscreen));
await tester.pumpAndSettle();
// Polyline points and markers must stay in Arizona (no East Coast jump).
final polylineLayer = tester.widget<PolylineLayer>(
find.byType(PolylineLayer),
);
final allPolylinePoints = polylineLayer.polylines
.expand((p) => p.points)
.toList();
expect(allPolylinePoints, isNotEmpty);
for (final point in allPolylinePoints) {
expect(point.latitude, inInclusiveRange(33.0, 34.0));
expect(point.longitude, inInclusiveRange(-113.0, -111.0));
}
// Segment across unresolved/ignored hops (4 -> You) must be dashed while
// adjacent local hops (3 -> 4) remain solid.
expect(
polylineLayer.polylines.any((p) => p.pattern != StrokePattern.solid()),
isTrue,
);
final markerLayer = tester.widget<MarkerLayer>(
find.byType(MarkerLayer).first,
);
for (final marker in markerLayer.markers) {
expect(marker.point.latitude, inInclusiveRange(33.0, 34.0));
expect(marker.point.longitude, inInclusiveRange(-113.0, -111.0));
}
// Camera bounds must stay local, not continental.
final flutterMap = tester.widget<FlutterMap>(find.byType(FlutterMap));
final fit = flutterMap.options.initialCameraFit as FitBounds;
expect(fit.bounds.north, lessThan(34.0));
expect(fit.bounds.east, lessThan(-111.0));
// Reported path distance must be local (~50-65 km), not thousands of km.
final distanceFinder = find.textContaining(' km)');
expect(distanceFinder, findsOneWidget);
final distanceText = tester.widget<Text>(distanceFinder).data!;
final kmMatch = RegExp(r'\((\d+\.\d+) km\)').firstMatch(distanceText);
expect(kmMatch, isNotNull);
final kmValue = double.parse(kmMatch!.group(1)!);
expect(kmValue, lessThan(200.0));
expect(kmValue, greaterThan(10.0));
},
);
testWidgets(
'ChannelMessagePathMapScreen without self GPS rejects leading outlier and keeps local cluster',
(tester) async {
final bogusEastCoast = _repeater(
prefix: 0x9C,
name: 'Beacon Hill 1W Repeater',
latitude: 42.3601,
longitude: -71.0589,
);
final localHop1 = _repeater(
prefix: 0x48,
name: 'Twain - Clearview',
latitude: 33.60097,
longitude: -112.02457,
);
final localHop2 = _repeater(
prefix: 0x53,
name: "Rod's Repeater",
latitude: 33.46300,
longitude: -111.65000,
);
final connector = _FakeConnector(
[bogusEastCoast, localHop1, localHop2],
lat: null,
lon: null,
);
final message = ChannelMessage(
senderName: 'Alice',
text: 'test',
timestamp: DateTime.utc(2026),
isOutgoing: false,
pathBytes: Uint8List.fromList([0x53, 0x48, 0x9C]),
channelIndex: 0,
);
await tester.pumpWidget(
_wrapWithProviders(
connector,
ChannelMessagePathMapScreen(message: message, channelMessage: true),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byIcon(Icons.open_in_full));
await tester.pumpAndSettle();
expect(find.textContaining('Beacon Hill 1W Repeater'), findsNothing);
expect(find.text('(9C) Unknown Repeater'), findsOneWidget);
expect(find.text('(48) Twain - Clearview'), findsOneWidget);
expect(find.text("(53) Rod's Repeater"), findsOneWidget);
final polylineLayer = tester.widget<PolylineLayer>(
find.byType(PolylineLayer),
);
final allPoints = polylineLayer.polylines
.expand((p) => p.points)
.toList();
expect(allPoints.length, 2);
for (final point in allPoints) {
expect(point.latitude, inInclusiveRange(33.0, 34.0));
expect(point.longitude, inInclusiveRange(-113.0, -111.0));
}
},
);
testWidgets(
'ChannelMessagePathScreen timeline treats impossible distant hop as unknown',
(tester) async {
final localHop = _repeater(
prefix: 0x48,
name: 'Twain - Clearview',
latitude: 33.60097,
longitude: -112.02457,
);
final bogusHop = _repeater(
prefix: 0x9C,
name: 'Beacon Hill 1W Repeater',
latitude: 42.3601,
longitude: -71.0589,
);
final connector = _FakeConnector([localHop, bogusHop]);
final message = ChannelMessage(
senderName: 'Alice',
text: 'test',
timestamp: DateTime.utc(2026),
isOutgoing: false,
pathBytes: Uint8List.fromList([0x48, 0x9C]),
channelIndex: 0,
);
await tester.pumpWidget(
_wrapWithProviders(
connector,
ChannelMessagePathScreen(message: message, channelMessage: true),
),
);
await tester.pumpAndSettle();
expect(find.text('(48) Twain - Clearview'), findsOneWidget);
expect(find.text('(9C) Unknown Repeater'), findsOneWidget);
expect(find.textContaining('Beacon Hill 1W Repeater'), findsNothing);
},
);
}
+186 -4
View File
@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'dart:typed_data';
@@ -40,16 +41,31 @@ class _FakeMeshCoreConnector extends MeshCoreConnector {
Uint8List.fromList(List.generate(32, (i) => i));
@override
double? get selfLatitude => 48.8566;
double? get selfLatitude => _selfLat;
@override
double? get selfLongitude => 2.3522;
double? get selfLongitude => _selfLon;
@override
List<Contact> get allContactsUnfiltered => [];
List<Contact> get allContactsUnfiltered => _allContacts;
@override
List<Contact> get contacts => [];
List<Contact> get contacts => _peerContacts;
final double? _selfLat;
final double? _selfLon;
final List<Contact> _allContacts;
final List<Contact> _peerContacts;
_FakeMeshCoreConnector({
double? selfLat = 48.8566,
double? selfLon = 2.3522,
List<Contact> allContacts = const [],
List<Contact> peerContacts = const [],
}) : _selfLat = selfLat,
_selfLon = selfLon,
_allContacts = allContacts,
_peerContacts = peerContacts;
void emitFrame(Uint8List frame) {
_receivedFramesController.add(frame);
@@ -400,4 +416,170 @@ void main() {
// Verify it parsed correctly (should not show the unavailable message)
expect(find.text('Path trace not available.'), findsNothing);
});
testWidgets(
'PathTraceMapScreen ignores impossible distant repeater and peer inference',
(tester) async {
final localHop1 = Contact(
publicKey: Uint8List(32)..first = 0x48,
name: 'Local Hop',
type: advTypeRepeater,
path: Uint8List(0),
pathLength: 0,
latitude: 33.60097,
longitude: -112.02457,
lastSeen: DateTime.utc(2026),
);
final eastCoastBogus = Contact(
publicKey: Uint8List(32)..first = 0x9C,
name: 'East Coast 9C',
type: advTypeRepeater,
path: Uint8List(0),
pathLength: 0,
latitude: 42.3601,
longitude: -71.0589,
lastSeen: DateTime.utc(2026),
);
final distantPeerSharing9C = Contact(
publicKey: Uint8List(32)..first = 0xAA,
name: 'Distant Chat Peer',
type: advTypeChat,
path: Uint8List.fromList([0x9C]),
pathLength: 1,
latitude: 42.35,
longitude: -71.06,
lastSeen: DateTime.utc(2026),
);
final connector = _FakeMeshCoreConnector(
selfLat: 33.4484,
selfLon: -112.0740,
allContacts: [localHop1, eastCoastBogus],
peerContacts: [distantPeerSharing9C],
);
final screen = PathTraceMapScreen(
title: 'Trace Impossible Hop',
path: Uint8List.fromList([0x48, 0x9C]),
pathHashByteWidth: 1,
);
await tester.pumpWidget(
_buildTestApp(connector: connector, child: screen),
);
await tester.pump();
final sentFrame = connector.sentFrames.first;
final tag = sentFrame.sublist(1, 5);
final pushTraceFrame = Uint8List(17);
pushTraceFrame[0] = pushCodeTraceData;
pushTraceFrame[1] = 0;
pushTraceFrame[2] = 2;
pushTraceFrame[3] = 0;
pushTraceFrame.setRange(4, 8, tag);
pushTraceFrame.setRange(12, 14, [0x48, 0x9C]);
pushTraceFrame.setRange(14, 17, [12, 16, 20]);
connector.emitFrame(pushTraceFrame);
for (int i = 0; i < 5; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
await tester.tap(find.byIcon(Icons.open_in_full));
await tester.pumpAndSettle();
expect(find.textContaining('East Coast 9C'), findsNothing);
expect(find.textContaining('9C: Unknown Repeater'), findsWidgets);
final polylineLayer = tester.widget<PolylineLayer>(
find.byType(PolylineLayer),
);
final allPoints = polylineLayer.polylines
.expand((p) => p.points)
.toList();
expect(allPoints, isNotEmpty);
for (final point in allPoints) {
expect(point.latitude, inInclusiveRange(33.0, 34.0));
expect(point.longitude, inInclusiveRange(-113.0, -111.0));
}
},
);
testWidgets(
'PathTraceMapScreen ignores impossible distant contact passed via pathContacts',
(tester) async {
final localHop1 = Contact(
publicKey: Uint8List(32)..first = 0x48,
name: 'Local Hop',
type: advTypeRepeater,
path: Uint8List(0),
pathLength: 0,
latitude: 33.60097,
longitude: -112.02457,
lastSeen: DateTime.utc(2026),
);
final eastCoastBogus = Contact(
publicKey: Uint8List(32)..first = 0x9C,
name: 'East Coast 9C',
type: advTypeRepeater,
path: Uint8List(0),
pathLength: 0,
latitude: 42.3601,
longitude: -71.0589,
lastSeen: DateTime.utc(2026),
);
final connector = _FakeMeshCoreConnector(
selfLat: 33.4484,
selfLon: -112.0740,
allContacts: [localHop1, eastCoastBogus],
);
final screen = PathTraceMapScreen(
title: 'Manual Trace Impossible Hop',
path: Uint8List.fromList([0x48, 0x9C]),
pathHashByteWidth: 1,
pathContacts: [localHop1, eastCoastBogus],
);
await tester.pumpWidget(
_buildTestApp(connector: connector, child: screen),
);
await tester.pump();
final sentFrame = connector.sentFrames.first;
final tag = sentFrame.sublist(1, 5);
final pushTraceFrame = Uint8List(17);
pushTraceFrame[0] = pushCodeTraceData;
pushTraceFrame[1] = 0;
pushTraceFrame[2] = 2;
pushTraceFrame[3] = 0;
pushTraceFrame.setRange(4, 8, tag);
pushTraceFrame.setRange(12, 14, [0x48, 0x9C]);
pushTraceFrame.setRange(14, 17, [12, 16, 20]);
connector.emitFrame(pushTraceFrame);
for (int i = 0; i < 5; i++) {
await tester.pump(const Duration(milliseconds: 100));
}
await tester.tap(find.byIcon(Icons.open_in_full));
await tester.pumpAndSettle();
expect(find.textContaining('East Coast 9C'), findsNothing);
expect(find.textContaining('9C: Unknown Repeater'), findsWidgets);
final polylineLayer = tester.widget<PolylineLayer>(
find.byType(PolylineLayer),
);
final allPoints = polylineLayer.polylines
.expand((p) => p.points)
.toList();
expect(allPoints.length, 2);
for (final point in allPoints) {
expect(point.latitude, inInclusiveRange(33.0, 34.0));
expect(point.longitude, inInclusiveRange(-113.0, -111.0));
}
},
);
}