refactor MWP: platform abstractions

This commit is contained in:
2026-02-19 11:36:27 -03:00
parent 7b120731a6
commit 3da00503a2
4 changed files with 73 additions and 42 deletions

View File

@@ -3,6 +3,9 @@
//! Handles the DHT-based meeting point where peers find each other.
const std = @import("std");
const platform = @import("platform");
const platform_time = platform.time;
const platform_random = platform.random;
const types = @import("types.zig");
const Allocator = std.mem.Allocator;
@@ -111,14 +114,14 @@ pub const MeetingPoint = struct {
.peer_id = self.our_keypair.public,
.addresses = self.our_addresses,
.nonce = undefined,
.timestamp = std.time.milliTimestamp(),
.timestamp = platform_time.milliTimestamp(),
.ttl = ttl,
.sequence = self.next_sequence,
.signature = undefined,
};
// Generate random nonce
std.crypto.random.bytes(&pub_data.nonce);
platform_random.bytes(&pub_data.nonce);
// Sign
const hash = pub_data.computeHash();
@@ -149,9 +152,9 @@ pub const MeetingPoint = struct {
timeout_ms: u64,
) !struct { publication: MeetingPublication, seen_time: i64 } {
const meeting_key = computeMeetingKey(self.our_keypair.public, their_pubkey);
const deadline = std.time.milliTimestamp() + @as(i64, @intCast(timeout_ms));
const deadline = platform_time.milliTimestamp() + @as(i64, @intCast(timeout_ms));
while (std.time.milliTimestamp() < deadline) {
while (platform_time.milliTimestamp() < deadline) {
// Query DHT
const entries = try self.dht.getAll(meeting_key, self.allocator);
defer {
@@ -186,12 +189,12 @@ pub const MeetingPoint = struct {
// Found valid publication
return .{
.publication = publication,
.seen_time = std.time.milliTimestamp(),
.seen_time = platform_time.milliTimestamp(),
};
}
// Poll interval
std.Thread.sleep(Defaults.dht_poll_interval_ms * std.time.ns_per_ms);
platform.time.sleep(Defaults.dht_poll_interval_ms * std.time.ns_per_ms);
}
return error.Timeout;
@@ -309,8 +312,8 @@ test "computeRendezvousParams produces same result for both peers" {
// Simulate two peers with their publications
var our_nonce: [32]u8 = undefined;
var their_nonce: [32]u8 = undefined;
std.crypto.random.bytes(&our_nonce);
std.crypto.random.bytes(&their_nonce);
platform_random.bytes(&our_nonce);
platform_random.bytes(&their_nonce);
const our_pub = MeetingPublication{
.peer_id = [_]u8{1} ** 32,