From b9a448f75dcfac0a4102fff4e03d3fd3c8243380 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:08:57 -0500 Subject: [PATCH] fix(#1183): a channel repeat needs the sender's exact send second Sending the same channel text twice quickly showed one message, with the second send counted as a repeat. The two sends are two packets: the client gives each its own second, and the firmware puts that second at the start of the encrypted payload, so their packet hashes differ. The client merged them anyway. Its own message is filed before sending with no packet hash, so the exact-hash pass cannot see it, and the fallback treated same text + same sender + within 30 seconds as one message. Repeats arriving later fell to the same rule, because the RX log and the sync queue hash differently, and landed on the newest same-text message. The fallback now requires the same timestamp second. A real repeat is the same packet, so it carries the sender's exact second, and both receive paths read it from the packet. The 30-second window dates from before #524 stamped the outgoing message with the exact second sent. Tests drive real frames (0x88 RX log and V3 queue) through the connector. 7 of 9 fail on the old rule; the 2 controls pass on both. Co-Authored-By: Claude Opus 5.5 --- lib/connector/meshcore_connector.dart | 12 +- test/connector/channel_repeat_test.dart | 295 ++++++++++++++++++++++++ 2 files changed, 302 insertions(+), 5 deletions(-) create mode 100644 test/connector/channel_repeat_test.dart diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 41c34352..d4abb7a5 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -12014,14 +12014,16 @@ class MeshCoreConnector extends ChangeNotifier { return -1; } + /// A repeat is the same packet heard again, so it carries the sender's exact + /// send second; that second is inside the encrypted payload and both receive + /// paths read it. Two sends get different seconds, so they never match (#1183). bool _isChannelRepeat(ChannelMessage existing, ChannelMessage incoming) { if (existing.text != incoming.text) return false; - final diffMs = - (existing.timestamp.millisecondsSinceEpoch - - incoming.timestamp.millisecondsSinceEpoch) - .abs(); - if (diffMs > 30000) return false; + if (existing.timestamp.millisecondsSinceEpoch ~/ 1000 != + incoming.timestamp.millisecondsSinceEpoch ~/ 1000) { + return false; + } if (existing.senderName == incoming.senderName) return true; diff --git a/test/connector/channel_repeat_test.dart b/test/connector/channel_repeat_test.dart new file mode 100644 index 00000000..ef5a42f1 --- /dev/null +++ b/test/connector/channel_repeat_test.dart @@ -0,0 +1,295 @@ +// #1183: a channel repeat is the same packet heard again, so it carries the +// sender's exact send second. Two sends of the same text are two packets (the +// client gives each send its own second), and must stay two messages, each +// counting only the repeaters that heard it. + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:crypto/crypto.dart' as crypto; +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/models/channel_message.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:pointycastle/export.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +const int _slot = 0; +const String _self = 'Base'; + +final Uint8List _psk = Uint8List.fromList(List.generate(16, (i) => i + 1)); + +Uint8List _u32(int v) => + Uint8List(4)..buffer.asByteData().setUint32(0, v, Endian.little); + +/// SELF_INFO, so the connector knows its own name (the self-echo drop needs it). +Uint8List _selfInfo() => + (BytesBuilder() + ..addByte(respCodeSelfInfo) + ..addByte(advTypeChat) + ..addByte(20) // tx power + ..addByte(22) // max tx power + ..add(List.filled(pubKeySize, 0x5A)) + ..add(_u32(0)) // lat + ..add(_u32(0)) // lon + ..addByte(0) // multi acks + ..addByte(0) // advert location policy + ..addByte(0) // telemetry modes + ..addByte(0) // manual add + ..add(_u32(910525000)) + ..add(_u32(62500)) + ..addByte(7) + ..addByte(5) + ..add(utf8.encode(_self)) + ..addByte(0)) + .toBytes(); + +List _channelInfo() => [ + respCodeChannelInfo, + _slot, + ...'Public'.codeUnits, + ...List.filled(32 - 'Public'.length, 0), + ..._psk, +]; + +/// A GRP_TXT packet as the radio overhears it, wrapped in 0x88: the same +/// AES-128-ECB + 2-byte HMAC the firmware uses. +Uint8List _rxLogFrame(int ts, String sender, String text, {List? path}) { + final plain = BytesBuilder() + ..add(_u32(ts)) + ..addByte(txtTypePlain) + ..add(utf8.encode('$sender: $text')) + ..addByte(0); + final padded = Uint8List(((plain.length + 15) ~/ 16) * 16) + ..setRange(0, plain.length, plain.toBytes()); + final aes = ECBBlockCipher(AESEngine())..init(true, KeyParameter(_psk)); + final cipher = Uint8List(padded.length); + for (var i = 0; i < padded.length; i += 16) { + aes.processBlock(padded, i, cipher, i); + } + final key32 = Uint8List(32)..setRange(0, 16, _psk); + final mac = crypto.Hmac(crypto.sha256, key32).convert(cipher).bytes; + final hops = path ?? const []; + return (BytesBuilder() + ..addByte(pushCodeLogRxData) + ..addByte(0) // snr + ..addByte(0) // rssi + ..addByte((0x05 << 2) | 0x01) // GRP_TXT, flood + ..addByte(hops.length) // 1-byte hashes, one per hop + ..add(hops) + ..addByte(crypto.sha256.convert(_psk).bytes[0]) + ..add(mac.sublist(0, 2)) + ..add(cipher)) + .toBytes(); +} + +/// The same packet delivered through the radio's message queue (V3). +/// [deviceComposed] sets reserved1 bit 0: written on the radio's own screen. +Uint8List _queueFrame( + int ts, + String sender, + String text, { + bool deviceComposed = false, +}) => + (BytesBuilder() + ..addByte(respCodeChannelMsgRecvV3) + ..addByte(0) // snr + ..addByte(deviceComposed ? 0x01 : 0x00) // reserved1 + ..addByte(0) // reserved2 + ..addByte(_slot) + ..addByte(1) // one hop + ..addByte(txtTypePlain) + ..add(_u32(ts)) + ..add(utf8.encode('$sender: $text')) + ..addByte(0)) + .toBytes(); + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + late OffbandDatabase db; + late MeshCoreConnector connector; + late List sent; + + List messages() => + connector.channelMessagesForTest[_slot] ?? const []; + + /// The seconds each channel send put on the wire, in send order. + List sentSeconds() => sent + .where((f) => f.isNotEmpty && f[0] == cmdSendChannelTxtMsg) + .map((f) => ByteData.sublistView(f, 3, 7).getUint32(0, Endian.little)) + .toList(); + + Future settle() => Future.delayed(Duration.zero); + + Future sendTwice(String text) async { + final channel = connector.channels.firstWhere((c) => c.index == _slot); + await Future.wait([ + connector.sendChannelMessage(channel, text), + connector.sendChannelMessage(channel, text), + ]); + expect(sentSeconds(), hasLength(2)); + } + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + BlobStore.overrideForTest(BlobStore(db)); + connector = MeshCoreConnector(); + connector.setConnectedForTest(); + sent = []; + connector.sendFrameOverrideForTest = sent.add; + connector.handleFrameForTest(_selfInfo()); + await settle(); + expect(connector.selfName, _self); + + await connector.getChannels(maxChannels: 1, force: true); + connector.handleFrameForTest(_channelInfo()); + await settle(); + expect(connector.canSendOnChannel(_slot), isTrue); + }); + + tearDown(() async { + await db.close(); + BlobStore.clearTestOverride(); + }); + + test( + 'two sends of the same text stay two messages, neither repeated', + () async { + await sendTwice('hello'); + + expect(messages(), hasLength(2)); + for (final m in messages()) { + expect(m.isOutgoing, isTrue); + expect(m.repeatCount, 0); + expect(m.status, ChannelMessageStatus.pending); + } + }, + ); + + test('a repeat of the second send counts on the second send only', () async { + await sendTwice('hello'); + final ts = sentSeconds(); + + connector.handleFrameForTest( + _rxLogFrame(ts[1], _self, 'hello', path: [0xAB]), + ); + await settle(); + + expect(messages(), hasLength(2)); + expect(messages()[0].repeatCount, 0); + expect(messages()[1].repeatCount, 1); + }); + + test('two repeaters of the first send count on the first only', () async { + await sendTwice('hello'); + final ts = sentSeconds(); + + connector.handleFrameForTest( + _rxLogFrame(ts[0], _self, 'hello', path: [0xAB]), + ); + await settle(); + expect(messages()[0].repeatCount, 1); + + connector.handleFrameForTest( + _rxLogFrame(ts[0], _self, 'hello', path: [0xCD]), + ); + await settle(); + + expect(messages(), hasLength(2)); + expect(messages()[0].repeatCount, 2); + expect(messages()[1].repeatCount, 0); + }); + + test( + 'another sender\'s same text one second apart is two messages', + () async { + connector.handleFrameForTest(_queueFrame(1790000000, 'Bob', 'hi')); + connector.handleFrameForTest(_queueFrame(1790000001, 'Bob', 'hi')); + await settle(); + + expect(messages(), hasLength(2)); + expect(messages().map((m) => m.repeatCount), everyElement(0)); + }, + ); + + test( + 'a real repeat of another sender, by both paths, is one message', + () async { + connector.handleFrameForTest(_queueFrame(1790000000, 'Bob', 'hi')); + await settle(); + connector.handleFrameForTest( + _rxLogFrame(1790000000, 'Bob', 'hi', path: [0xCD]), + ); + await settle(); + + expect(messages(), hasLength(1)); + expect(messages().single.repeatCount, 1); + }, + ); + + test('a direct (no-path) copy of my own message is still dropped', () async { + await sendTwice('hello'); + final ts = sentSeconds(); + + connector.handleFrameForTest(_rxLogFrame(ts[0], _self, 'hello')); + await settle(); + + expect(messages(), hasLength(2)); + expect(messages().map((m) => m.repeatCount), everyElement(0)); + }); + + test( + 'a copy whose second matches no message is new, never a repeat', + () async { + await sendTwice('hello'); + final ts = sentSeconds(); + + connector.handleFrameForTest( + _rxLogFrame(ts[1] + 5, _self, 'hello', path: [0xAB]), + ); + await settle(); + + expect(messages(), hasLength(3)); + expect(messages().map((m) => m.repeatCount), everyElement(0)); + }, + ); + + // Messages written on the radio's own screen arrive as V3 frames with the + // outgoing flag set, not through sendChannelMessage. + group('composed on the radio itself', () { + test('the same message delivered twice is one message', () async { + final frame = _queueFrame( + 1790000000, + _self, + 'from the badge', + deviceComposed: true, + ); + connector.handleFrameForTest(frame); + connector.handleFrameForTest(frame); + await settle(); + + expect(messages(), hasLength(1)); + expect(messages().single.isOutgoing, isTrue); + }); + + test('two sends of the same text a second apart are two', () async { + connector.handleFrameForTest( + _queueFrame(1790000000, _self, 'again', deviceComposed: true), + ); + connector.handleFrameForTest( + _queueFrame(1790000001, _self, 'again', deviceComposed: true), + ); + await settle(); + + expect(messages(), hasLength(2)); + expect(messages().map((m) => m.repeatCount), everyElement(0)); + }); + }); +}