-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock-server.js
More file actions
82 lines (69 loc) · 2.11 KB
/
clock-server.js
File metadata and controls
82 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// clock-server.js
import express from "express";
import { Sdk } from "@peaq-network/sdk";
const app = express();
const PORT = process.argv[2] || 3000;
// Add drift factor to simulate clock imperfections
const driftFactor = (Math.random() - 0.5) * 0.0001;
// Add random offset of 10-15 seconds (in nanoseconds)
const initialOffset = BigInt(
Math.floor(
// Random number between 10 and 15 (seconds)
(Math.random() * 5 + 10) *
// Convert to nanoseconds
1_000_000_000
)
);
// Make it positive or negative randomly
const signedOffset = Math.random() > 0.5 ? initialOffset : -initialOffset;
// Convert current Unix timestamp to nanoseconds and add random offset
let baseTime = BigInt(Date.now()) * BigInt(1_000_000) + signedOffset;
let lastSyncTime = process.hrtime.bigint();
let isDisplaying = true;
function getNanoClock() {
const elapsed = process.hrtime.bigint() - lastSyncTime;
const drift = BigInt(Math.floor(Number(elapsed) * driftFactor));
return baseTime + elapsed + drift;
}
function displayClock() {
if (isDisplaying) {
const time = getNanoClock();
process.stdout.write(`\rServer ${PORT} Time: ${time} ns`);
}
}
// PTP Sync endpoint
app.get("/sync", async (req, res) => {
isDisplaying = false;
console.log("\nSynchronizing...");
try {
await new Promise((resolve) => {
const unsubscribe = Sdk.subscribeToPtp(
{ masterUrl: "https://ptp.peaq.xyz" },
({ synchronizedTime }) => {
baseTime = synchronizedTime;
lastSyncTime = process.hrtime.bigint();
console.log(`\nSynchronized to: ${synchronizedTime} ns`);
unsubscribe();
resolve(true);
}
);
});
res.send("Synchronized");
} catch (error) {
console.error("Sync failed:", error);
res.status(500).send("Sync failed");
} finally {
isDisplaying = true;
}
});
const clockInterval = setInterval(displayClock, 1);
app.listen(PORT, () => {
console.log(
`Clock server running on port ${PORT} with initial time: ${baseTime} ns`
);
});
// Cleanup on exit
process.on("SIGINT", () => {
clearInterval(clockInterval);
process.exit();
});