Merge pull request #1352 from OffbandMesh/fix/1270-nearby-width-dev
fix(#1270): cut the nearest-repeater hop at the packet's own widthpull/1359/head
commit
9c09e37a3f
@ -0,0 +1,223 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:drift/native.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/services/mesh_topology_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/drift/blob_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/offband_database.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// #1270: every received packet says its own hop width in the high 2 bits of
|
||||
// its path-length byte. Nearby Repeaters cut the last hop at the width OUR
|
||||
// radio is set to send instead, so on a 3-byte radio one repeater (F2 83 9C...)
|
||||
// heard through 1- and 2-byte packets showed up as several look-alike IDs
|
||||
// (`11f283`, `d08af2`), each carrying bytes from the hop before it.
|
||||
|
||||
/// A path-length byte: hop count in the low 6 bits, width - 1 in the high 2.
|
||||
int _pathLen(int hops, int width) => ((width - 1) << 6) | hops;
|
||||
|
||||
/// A PUSH_CODE_LOG_RX_DATA frame. routeType 0x01 keeps it off the
|
||||
/// transport-code bytes.
|
||||
Uint8List _rxFrame({
|
||||
required int payloadType,
|
||||
required int pathLenRaw,
|
||||
required List<int> path,
|
||||
required List<int> payload,
|
||||
int snrQuarterDb = -12,
|
||||
}) {
|
||||
return (BytesBuilder()
|
||||
..addByte(pushCodeLogRxData)
|
||||
..addByte(snrQuarterDb & 0xFF)
|
||||
..addByte(0) // rssi
|
||||
..addByte((payloadType << 2) | 0x01)
|
||||
..addByte(pathLenRaw)
|
||||
..add(path)
|
||||
..add(payload))
|
||||
.toBytes();
|
||||
}
|
||||
|
||||
Uint8List _u32(int v) =>
|
||||
Uint8List(4)..buffer.asByteData().setUint32(0, v, Endian.little);
|
||||
|
||||
List<int> _advertPayload(int keyByte, String name) =>
|
||||
(BytesBuilder()
|
||||
..add(List<int>.filled(32, keyByte))
|
||||
..add(_u32(1790000000))
|
||||
..add(List<int>.filled(64, 0))
|
||||
..addByte(advTypeChat | 0x80)
|
||||
..add(name.codeUnits)
|
||||
..addByte(0))
|
||||
.toBytes();
|
||||
|
||||
/// DEVICE_INFO long enough to carry the path-hash mode at byte 81.
|
||||
Uint8List _deviceInfo({required int pathHashMode}) {
|
||||
final frame = Uint8List(82);
|
||||
frame[0] = respCodeDeviceInfo;
|
||||
frame[1] = 14; // ver code
|
||||
frame[2] = 100; // MAX_CONTACTS / 2
|
||||
frame[3] = 8; // MAX_GROUP_CHANNELS
|
||||
frame[81] = pathHashMode;
|
||||
return frame;
|
||||
}
|
||||
|
||||
void main() {
|
||||
late OffbandDatabase db;
|
||||
late MeshCoreConnector connector;
|
||||
late MeshTopologyService topology;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
|
||||
topology = MeshTopologyService();
|
||||
connector = MeshCoreConnector();
|
||||
connector.initialize(
|
||||
retryService: MessageRetryService(),
|
||||
pathHistoryService: PathHistoryService(StorageService()),
|
||||
topologyService: topology,
|
||||
);
|
||||
connector.setSelfPublicKeyForTest(
|
||||
Uint8List.fromList(List.filled(32, 0x0e)),
|
||||
);
|
||||
connector.sendFrameOverrideForTest = (_) {};
|
||||
connector.setConnectedForTest();
|
||||
connector.handleFrameForTest(_deviceInfo(pathHashMode: 2));
|
||||
expect(connector.pathHashByteWidth, 3);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
BlobStore.clearTestOverride();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
Set<String> prefixes() =>
|
||||
connector.directRepeaters.map((r) => r.prefixHex).toSet();
|
||||
|
||||
test(
|
||||
'a 2-byte packet names its own last hop, not a byte of the hop before',
|
||||
() {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(2, 2),
|
||||
path: [0xa8, 0x11, 0xf2, 0x83],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(prefixes(), {'f283'});
|
||||
},
|
||||
);
|
||||
|
||||
test('a 1-byte packet names its own last hop, not the last three hops', () {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(3, 1),
|
||||
path: [0xd0, 0x8a, 0xf2],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(prefixes(), {'f2'});
|
||||
});
|
||||
|
||||
test('one repeater heard at 1, 2 and 3 bytes is one entry', () {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(2, 2),
|
||||
path: [0xa8, 0x11, 0xf2, 0x83],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(3, 1),
|
||||
path: [0xd0, 0x8a, 0xf2],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(1, 3),
|
||||
path: [0xf2, 0x83, 0x9c],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(prefixes(), {'f2839c'});
|
||||
});
|
||||
|
||||
test('an advert relayed through 2-byte hops names its own last hop', () {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeADVERT,
|
||||
pathLenRaw: _pathLen(2, 2),
|
||||
path: [0xa8, 0x11, 0xf2, 0x83],
|
||||
payload: _advertPayload(0x55, 'Echo'),
|
||||
),
|
||||
);
|
||||
expect(prefixes(), {'f283'});
|
||||
});
|
||||
|
||||
test('different repeaters stay separate entries', () {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(1, 2),
|
||||
path: [0xf2, 0x83],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(1, 2),
|
||||
path: [0xf2, 0x99],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(prefixes(), {'f283', 'f299'});
|
||||
});
|
||||
|
||||
test('the topology map never learns hops split at the wrong width', () {
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(2, 2),
|
||||
path: [0xa8, 0x11, 0xf2, 0x83],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(3, 1),
|
||||
path: [0xd0, 0x8a, 0xf2],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(topology.hasNode([0xa8, 0x11, 0xf2]), isFalse);
|
||||
expect(topology.hasNode([0xd0, 0x8a, 0xf2]), isFalse);
|
||||
|
||||
connector.handleFrameForTest(
|
||||
_rxFrame(
|
||||
payloadType: payloadTypeGRPTXT,
|
||||
pathLenRaw: _pathLen(2, 3),
|
||||
path: [0x11, 0x22, 0x33, 0xf2, 0x83, 0x9c],
|
||||
payload: [1, 2, 3, 4],
|
||||
),
|
||||
);
|
||||
expect(topology.hasNode([0x11, 0x22, 0x33]), isTrue);
|
||||
expect(topology.hasNode([0xf2, 0x83, 0x9c]), isTrue);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue