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.

Why PTP on Blockchain?

Traditional blockchain timestamps operate at second-level precision, limiting their utility for high-precision applications. By implementing PTP on peaq, we achieve:

  • Nanosecond-level precision timing
  • Distributed time synchronization
  • Blockchain-verified timestamps
  • Trustless time coordination for machine networks
  • Superior Network Time Accuracy: Unlike NTP, which allows for variable offset ranges, PTP provides deterministic and precise time synchronization

Architecture

Implementation Guide

peaq’s PTP implementation provides nanosecond-precise time synchronization with just a few lines of code.

Usage

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.

subscribeToPtp(masterUrl, interval)

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.network. 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.

import { Sdk } from '@peaq-network/sdk';

const MASTER_URL = 'https://ptp.peaq.network';
// ...contd.
// Subscribe to PTP updates
const unsubscribe = Sdk.subscribeToPtp(
 { masterUrl: MASTER_URL },
 ({ offset, synchronizedTime }) => {
   console.log(`Clock Offset: ${offset} nanoseconds`);
   console.log(`Synchronized Time: ${synchronizedTime} nanoseconds`);
 }
);
// To stop receiving updates
unsubscribe();

This is a simple subscription that, when subscribed, will return the results with a 1-second interval, until unsubscribed.

Sample Output

Clock Offset: 1849833 nanoseconds
Synchronized Time: 1740669653296849833 nanoseconds

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.

Synchronization Result Details

The synchronization result contains two key properties:

  • Offset:

    • Description: This represents the calculated time difference between the local clock and the master clock.
    • Purpose: The offset is used to adjust the local clock to align it with the master clock. A positive offset indicates that the local clock is behind the master clock, while a negative offset indicates it is ahead.
  • Synchronized Time:

    • Description: This is the local time adjusted by the calculated offset, effectively representing the current time as per the master clock.
    • Purpose: The synchronized time provides a corrected timestamp that can be used for time-sensitive operations, ensuring consistency with the master clock

Synchronization Process

The synchronization process involves the following steps:

  1. Sync Message:

    • A request is sent to the master clock server.
    • The server returns a timestamp, and the local timestamp is recorded immediately after receiving the response.
  2. Delay Request:

    • A local timestamp is recorded, and a request is sent to the master clock server.
    • The server returns another timestamp.
  3. 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

Error Handling

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.

Conclusion

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.