fix protocol+tests: replay protection rework, wire envelope nonce auth,

flaky e2e test deadlines, build imports
This commit is contained in:
2026-02-13 16:37:30 -03:00
parent c94d189d42
commit 6ec1346cfd
22 changed files with 2275 additions and 786 deletions

View File

@@ -463,7 +463,10 @@ pub const DhtCoordinator = struct {
pending_responses: std.AutoHashMap(u64, PendingResponse),
/// Network sender for propagation (set by P2P layer)
network_sender: NetworkSender = .{},
/// Next request ID (atomic for thread safety)
/// Next request ID (atomic for thread safety).
/// Starts at 1 on all nodes — this is safe because replay protection uses per-message
/// random wire_nonce (not request_id) for global deduplication. request_id is only used
/// for correlating request/response pairs within a single node.
next_request_id: std.atomic.Value(u64) = std.atomic.Value(u64).init(1),
/// Mutex for thread safety
mutex: std.Thread.Mutex = .{},
@@ -504,13 +507,7 @@ pub const DhtCoordinator = struct {
// Generate Ed25519 identity keypair for signing
const identity_keypair = Ed25519.KeyPair.generate();
// SECURITY FIX: Initialize request_id with random value to prevent nonce collisions
// When multiple nodes start simultaneously, they would all start with request_id=1
// causing the replay protection system to detect false positives (DuplicateNonce errors)
var random_start: u64 = undefined;
std.crypto.random.bytes(std.mem.asBytes(&random_start));
var dht = DhtCoordinator{
return DhtCoordinator{
.allocator = allocator,
.routing_table = routing_table,
.local_id = local_id,
@@ -522,8 +519,6 @@ pub const DhtCoordinator = struct {
.peer_rate_limits = std.AutoHashMap(NodeId, PeerRateLimit).init(allocator),
.discovered_peers = std.AutoHashMap(NodeId, DiscoveredPeer).init(allocator),
};
dht.next_request_id.store(random_start, .monotonic);
return dht;
}
/// Initialize with custom TOFU callback for peer verification (recommended for production)