From 5230f5efb2d1cdaa91f72965cd653ff5aa251794 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 1 Sep 2026 19:34:32 +0200 Subject: [PATCH] =?UTF-8?q?fix(mesh):=20markSeen=20je=20zase=20idempotentn?= =?UTF-8?q?=C3=AD,=20jinak=20si=20send=20path=20uj=C3=ADd=C3=A1=20dedup=20?= =?UTF-8?q?okno?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 (1M context) --- src/helpers/SimpleMeshTables.h | 17 ++++++++++ .../test_simple_mesh_tables.cpp | 32 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/helpers/SimpleMeshTables.h b/src/helpers/SimpleMeshTables.h index 956f36fa..fa54cd1f 100644 --- a/src/helpers/SimpleMeshTables.h +++ b/src/helpers/SimpleMeshTables.h @@ -52,6 +52,23 @@ public: void markSeen(const mesh::Packet* packet) override { uint8_t hash[MAX_HASH_SIZE]; packet->calculatePacketHash(hash); + + // CUSTOM (TeTeHacko): idempotent, the way the single hasSeen() this replaced + // was -- that one inserted only when the hash was absent. 1.17 split it into + // wasSeen() + markSeen(), and while every receive-side caller still gates on + // !wasSeen(), the SEND paths (Mesh::sendFlood, sendDirect, sendZeroHop) call + // this one unguarded. Every re-send of the same packet then occupies another + // slot of a fixed 160-entry ring, which shortens the dedup window and lets a + // late echo of our own flood read as new traffic -- and on a repeater that + // means rebroadcasting it. + // + // Cost is one extra 160x8 byte scan per send, which is nothing against LoRa + // airtime. Fix taken from ALLFATHER-BV/meshcomod e62be9b (MIT). + const uint8_t* sp = _hashes; + for (int i = 0; i < MAX_PACKET_HASHES; i++, sp += MAX_HASH_SIZE) { + if (memcmp(hash, sp, MAX_HASH_SIZE) == 0) return; // already recorded + } + memcpy(&_hashes[_next_idx * MAX_HASH_SIZE], hash, MAX_HASH_SIZE); _next_idx = (_next_idx + 1) % MAX_PACKET_HASHES; } diff --git a/test/test_mesh_tables/test_simple_mesh_tables.cpp b/test/test_mesh_tables/test_simple_mesh_tables.cpp index 46b477d9..f9a7c75d 100644 --- a/test/test_mesh_tables/test_simple_mesh_tables.cpp +++ b/test/test_mesh_tables/test_simple_mesh_tables.cpp @@ -97,6 +97,38 @@ TEST(SimpleMeshTables, Clear_RemovesSeenPacket) { EXPECT_FALSE(t.wasSeen(&p)); } +// ── markSeen is idempotent ─────────────────────────────────────────── +// +// The send paths (Mesh::sendFlood, sendDirect, sendZeroHop) call markSeen() +// without a wasSeen() gate, so a packet sent more than once must not occupy more +// than one slot of the fixed ring -- that would shorten the dedup window for +// every other packet and let a late echo of our own flood read as new traffic. + +TEST(SimpleMeshTables, MarkSeen_Repeated_DoesNotEvictOlderEntries) { + SimpleMeshTables t; + Packet older = makeFloodPacket(0x01); + Packet resent = makeFloodPacket(0x02); + + t.markSeen(&older); + for (int i = 0; i < MAX_PACKET_HASHES * 2; i++) { + t.markSeen(&resent); // one packet, sent over and over + } + + EXPECT_TRUE(t.wasSeen(&older)); // must not have been pushed out of the ring + EXPECT_TRUE(t.wasSeen(&resent)); +} + +TEST(SimpleMeshTables, MarkSeen_Repeated_StillClearsInOneCall) { + SimpleMeshTables t; + Packet p = makeFloodPacket(0x01); + + t.markSeen(&p); + t.markSeen(&p); + t.clear(&p); // clear() removes the first match only + + EXPECT_FALSE(t.wasSeen(&p)); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();