fairness: limity podle reálného provozu, 00/ff odesílatelé nejsou rezervovaní

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-06 22:33:05 +02:00
1 parent f79ea79a13
commit 4cc830e70b
3 files changed
+33 -36

No files matched your search

-20
View File
@@ -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) {
+20 -9
View File
@@ -19,8 +19,10 @@
#include <Packet.h>
#include <stdint.h>
// 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?
@@ -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) {