From 4cc830e70b89ca512b345a9f4c0dacfeae8fe20b Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 6 Oct 2026 22:33:05 +0200 Subject: [PATCH] =?UTF-8?q?fairness:=20limity=20podle=20re=C3=A1ln=C3=A9ho?= =?UTF-8?q?=20provozu,=2000/ff=20odes=C3=ADlatel=C3=A9=20nejsou=20rezervov?= =?UTF-8?q?an=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- src/helpers/FairnessLimiter.cpp | 20 ------------- src/helpers/FairnessLimiter.h | 29 +++++++++++++------ .../test_fairness_limiter.cpp | 20 ++++++++----- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/helpers/FairnessLimiter.cpp b/src/helpers/FairnessLimiter.cpp index 93c4e3d8..82e233e7 100644 --- a/src/helpers/FairnessLimiter.cpp +++ b/src/helpers/FairnessLimiter.cpp @@ -35,11 +35,6 @@ void FairnessLimiter::refillGroup() { } uint8_t FairnessLimiter::peekSenderNormal(const uint8_t* hash) { - // Always zero as 0x00 and 0xFF are reserved - if (hash[0] == 0x00 || hash[0] == 0xFF) { - return 0; - } - auto i = sender_normal_idx(hash); if (i >= SENDER_NORMAL_MAP_SIZE) { return 0; @@ -48,11 +43,6 @@ uint8_t FairnessLimiter::peekSenderNormal(const uint8_t* hash) { } bool FairnessLimiter::takeSenderNormal(const uint8_t* hash) { - // Always false as 0x00 and 0xFF are reserved - if (hash[0] == 0x00 || hash[0] == 0xFF) { - return false; - } - auto i = sender_normal_idx(hash); bool allow = false; if (i < SENDER_NORMAL_MAP_SIZE && sender_normal_map[i] > 0) { @@ -75,11 +65,6 @@ void FairnessLimiter::refillSenderNormal() { } uint8_t FairnessLimiter::peekSenderLow(const uint8_t* hash) { - // Always zero as 0x00 and 0xFF are reserved - if (hash[0] == 0x00 || hash[0] == 0xFF) { - return 0; - } - auto i = sender_low_idx(hash); if (i >= SENDER_LOW_MAP_SIZE) { return 0; @@ -88,11 +73,6 @@ uint8_t FairnessLimiter::peekSenderLow(const uint8_t* hash) { } bool FairnessLimiter::takeSenderLow(const uint8_t* hash) { - // Always false as 0x00 and 0xFF are reserved - if (hash[0] == 0x00 || hash[0] == 0xFF) { - return false; - } - auto i = sender_low_idx(hash); bool allow = false; if (i < SENDER_LOW_MAP_SIZE && sender_low_map[i] > 0) { diff --git a/src/helpers/FairnessLimiter.h b/src/helpers/FairnessLimiter.h index adf65d07..5dafa608 100644 --- a/src/helpers/FairnessLimiter.h +++ b/src/helpers/FairnessLimiter.h @@ -19,8 +19,10 @@ #include #include -// For senders 0x00 and 0xFF are reserved (Identity.cpp rejects them as path -// hash prefixes). Group channel hashes are pure sha256, so no reserved values. +// Sender hashes 0x00 and 0xFF are NOT special here: repeaters, room servers +// and sensors re-roll such identities, but companions do not, so real users +// send with them (~1 in 128). Denying them always (as the original branch did) +// dropped every flood DM/PATH/REQ of those users. // Note: mask, cap and refill interval are highly connected parameters. #define GROUP_MASK 0b00011111 // 5-bit address #define SENDER_NORMAL_MASK 0b00111111 // 6-bit address @@ -28,25 +30,34 @@ // Refill intervals: ONE token per bucket per interval. Steady-state rate is // 1/interval, cap bounds the burst. Overridable per env from platformio ini. +// +// Sized from real traffic (6 Sep - 6 Oct 2026: 207k floods heard on the CZ +// mesh at one-site, replayed through this limiter). The zero-loss frontier +// over those 30 days was group 60 s/cap 60, sender 10 s/cap 60, advert 600 s/ +// cap 12; the defaults below keep about 2x headroom on top. The busiest +// legitimate channel runs ~26 msg/h around the clock, so the limiter is a net +// for runaway floods (the bot that prompted it sent ~330 msg/h), not a quota. +// The earlier defaults (cap 20/30/4, refill 180 s/240 s/7200 s) would have +// shed 9 % of channel messages, 15 % of sender-keyed traffic and 15 % of adverts. #ifndef FAIRNESS_GROUP_REFILL_MS - #define FAIRNESS_GROUP_REFILL_MS (3 * 60 * 1000) + #define FAIRNESS_GROUP_REFILL_MS (30 * 1000) #endif #ifndef FAIRNESS_SENDER_NORMAL_REFILL_MS - #define FAIRNESS_SENDER_NORMAL_REFILL_MS (4 * 60 * 1000) + #define FAIRNESS_SENDER_NORMAL_REFILL_MS (10 * 1000) #endif #ifndef FAIRNESS_SENDER_LOW_REFILL_MS - #define FAIRNESS_SENDER_LOW_REFILL_MS (120 * 60 * 1000) + #define FAIRNESS_SENDER_LOW_REFILL_MS (10 * 60 * 1000) #endif -// Capacity (max burst) per bucket. +// Capacity (max burst) per bucket. uint8_t, so at most 255. #ifndef FAIRNESS_GROUP_CAP - #define FAIRNESS_GROUP_CAP 20 + #define FAIRNESS_GROUP_CAP 100 #endif #ifndef FAIRNESS_SENDER_NORMAL_CAP - #define FAIRNESS_SENDER_NORMAL_CAP 30 + #define FAIRNESS_SENDER_NORMAL_CAP 150 #endif #ifndef FAIRNESS_SENDER_LOW_CAP - #define FAIRNESS_SENDER_LOW_CAP 4 + #define FAIRNESS_SENDER_LOW_CAP 30 #endif // Simply allow all direct messages? diff --git a/test/test_fairness_limiter/test_fairness_limiter.cpp b/test/test_fairness_limiter/test_fairness_limiter.cpp index 7f3fd69d..77474e1f 100644 --- a/test/test_fairness_limiter/test_fairness_limiter.cpp +++ b/test/test_fairness_limiter/test_fairness_limiter.cpp @@ -98,13 +98,18 @@ TEST(FairnessLimiter, SenderBucketKeysOnSrcHash) { EXPECT_EQ(fl.deniedSenderNormal(), 1u); } -TEST(FairnessLimiter, ReservedSenderHashesAlwaysDeny) { +TEST(FairnessLimiter, SenderHashes00AndFFAreOrdinaryBuckets) { + // Companions don't re-roll 0x00/0xFF identities, so these are real users. FairnessLimiter fl; - for (int i = 0; i < 5; i++) fl.refillSenderNormal(); + fl.refillSenderNormal(); + fl.refillSenderLow(); Packet z = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_TXT_MSG, {0x10, 0x00, 0, 0}); Packet f = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_TXT_MSG, {0x10, 0xFF, 0, 0}); - EXPECT_FALSE(fl.allowPacket(&z)); // 0x00 reserved (Identity.cpp:56) - EXPECT_FALSE(fl.allowPacket(&f)); // 0xFF reserved + Packet a = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_ADVERT, {0xFF, 0, 0}); + EXPECT_TRUE(fl.allowPacket(&z)); + EXPECT_TRUE(fl.allowPacket(&f)); + EXPECT_TRUE(fl.allowPacket(&a)); + EXPECT_FALSE(fl.allowPacket(&z)); // and they are still limited like any other } // ── advert bucket (keyed by payload[0] = pubkey first byte) ────────────────── @@ -159,7 +164,7 @@ TEST(FairnessLimiter, RaisedSenderCapAllowsBiggerBurst) { Packet p = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_TXT_MSG, {0x10, 0x22, 0, 0}); int allowed = 0; for (int i = 0; i < 200; i++) if (fl.allowPacket(&p)) allowed++; - EXPECT_EQ(allowed, 100); // the new cap, not the default 30 + EXPECT_EQ(allowed, 100); // the new cap, not the build default } // ── grid: which buckets we shed ────────────────────────────────────────────── @@ -175,9 +180,10 @@ TEST(FairnessLimiter, GridReportsDeniedBuckets) { char grid[180]; fl.formatGrid(grid, sizeof(grid)); std::string s(grid); - EXPECT_NE(s.find("group(cap 20):"), std::string::npos); + EXPECT_NE(s.find("group(cap " + std::to_string(FAIRNESS_GROUP_CAP) + "):"), std::string::npos); EXPECT_NE(s.find("0f=2"), std::string::npos); // bucket 0x0f denied twice - EXPECT_NE(s.find("sender(cap 30): -"), std::string::npos); // nothing shed there + EXPECT_NE(s.find("sender(cap " + std::to_string(FAIRNESS_SENDER_NORMAL_CAP) + "): -"), + std::string::npos); // nothing shed there } TEST(FairnessLimiter, GridIsNonDestructive) {