Universal Machine Time (UMT) is the first onchain implementation of the Precision Time Protocol (PTP), enabling robots, machines, drones, vehicles, and devices to achieve nanosecond-precise time synchronization.
Traditional blockchain timestamps operate at second-level precision, limiting their utility for high-precision applications. By implementing PTP on peaq, we achieve:
peaq’s PTP implementation provides nanosecond-precise time synchronization with just a few lines of code.
This can be run on any JavaScript-based project (server or client). You would need the peaq-sdk to run it, please refer here for more information.
Parameters:
masterUrl REQUIRED <string>
: The URL of the master clock server, which is used to send requests for synchronization. This is defined as a constant in the code, which is currently https://ptp.peaq.xyz. This server is a central time source that fetches the timestamp from the network at regular block intervals using real-time updates and is responsible for synchronizing the clocks of connected systems to ensure accurate and consistent timekeeping.
interval OPTIONAL <number>
: Periodically synchronizes the clock at a fixed rate. Recorded in milliseconds, defaults to 1000.
This is a simple subscription that, when subscribed, will return the results with a 1-second interval, until unsubscribed.
The function returns a result that includes the time offset and the synchronized unix timestamp in nanoseconds based on the current local time. Both are computed in bigint format to be accuracy consistent. If an error occurs during synchronization, it returns nothing. The timestamp is in unix, you can verify it using any online unix timestamp conversion tool.
The synchronization result contains two key properties:
Offset:
Synchronized Time:
The synchronization process involves the following steps:
Sync Message:
Delay Request:
Offset Calculation:
The offset is calculated using the formula:
offset = ((local timestamp after sync - server timestamp) - (local timestamp before delay - delay)) / 2
The synchronized time is calculated as:
synchronized time = local timestamp after sync + offset
If any error occurs during the synchronization process, such as network issues or server errors, the function logs the error to the console and returns nothing. However, the ptp server is configured to be highly resilient and capable of handling a large volume of concurrent requests while maintaining availability, through auto-scaling and load balancing.
The synchronization function is essential for achieving precise time synchronization with a master clock server. By calculating the time offset and adjusting the local time, it ensures that the local system time is accurate and consistent with the master clock. The synchronization result provides both the offset and the synchronized time, which are crucial for maintaining time accuracy in distributed systems.