Universal Machine Time (UMT) is the first-ever onchain implementation of the precision time protocol (PTP), which enables universal blockchain-verified timestamps at a nanosecond precision. This function enables Machines and DePINs to sync and coordinate their local time with a master clock that works as a high-percision onchain clock for the machine economy.
UMT enables such use cases and functionalities as:
- Precise machine coordination
- Spatial awareness for AI and robots
- Data timestamp consistency
- Ultra-fast smart contracts
- Decentralized time coordination
- And more
UMT implements a modified version of the IEEE 1588 Precision Time Protocol (PTP) with the aim to achieve nanosecond-level time synchronization. This implementation uses a master clock server that derives its time from the blockchain, providing a trustless and decentralized time source.
The implementation follows the core principles of PTP while adapting them for blockchain-based time synchronization:
-
Master-Slave Architecture: A designated master clock server maintains the reference time derived from blockchain timestamps.
-
Two-Way Message Exchange:
- Sync Message: Client requests time from the master (T1)
- Delay_Req Message: Client sends a timestamp request and the master responds with arrival time (T2')
- Client calculates offset using its local timestamps and the master's responses
-
Clock Drift Compensation: The system continuously monitors and adjusts for drift between local time and blockchain time.
-
Blockchain Time Source:
- Timestamps from the blockchain are converted from milliseconds to nanoseconds
- Block intervals are tracked using a moving average to smooth variations
- The system detects and warns about significant time drift
-
High-Precision Timekeeping:
- Uses
process.hrtime.bigint()for nanosecond precision in local measurements - Calculates elapsed time between blockchain updates with nanosecond resolution
- Implements drift detection with configurable thresholds (currently 1 second)
- Uses
-
Nanosecond Precision:
- Converting all time values to nanoseconds (1 second = 1,000,000,000 ns)
- Using bigint values to prevent floating-point precision issues
- Implementing adaptive block interval tracking
-
Resilient Time Synchronization:
- Handles missed blocks gracefully with warning mechanisms
- Uses REST API endpoints for time synchronization messages
- Maintains continuous blockchain connection with proper cleanup handlers
The UMT server maintains a persistent connection to the blockchain network using the following mechanisms:
-
API Integration:
- The server connects to the peaq blockchain using the
@polkadot/apilibrary - Establishes a WebSocket connection to the blockchain node
- Manages connection lifecycle with proper error handling and reconnection logic
- The server connects to the peaq blockchain using the
-
Real-Time Block Subscription:
- Subscribes to new blocks using
api.query.system.numberto track block sequence - Subscribes to timestamp updates with
api.query.timestamp.nowto get authoritative blockchain time - Detects missed blocks to maintain synchronization accuracy
- Subscribes to new blocks using
-
Time Propagation Process:
- New block timestamps are immediately captured and converted to nanosecond precision
- Local time is recorded simultaneously to establish the reference point
- Elapsed time since last block is calculated using high-precision local clock
- Time drift is continuously monitored and compensated
This architecture provides a reliable time synchronization mechanism with precision suitable for timestamps in distributed applications, robots, vehicles, drones, machines, devices, and blockchain transactions where highly precise time sync is critical.
UMT is available through the peaq SDK, allowing any Machine or DePIN to easily integrate and sync with the high precision clock.
The peaq SDK implements a client-side PTP module that handles:
- Time Synchronization Protocol: Implements the same PTP algorithm as the master clock server
- High-Precision Timestamps: Uses platform-specific methods to achieve nanosecond precision
- Continuous Synchronization: Maintains time accuracy through periodic adjustments
- Offset Calculation: Computes the local clock offset from the master clock
We've provided a simple example implementation in index.js. To run it:
-
Install dependencies:
npm i
-
Run the example:
npm start
The example will connect to the peaq network's PTP server, calculate the time offset, and display the synchronized time in your console.
The SDK's PTP implementation follows these steps:
-
Sync Message Exchange:
- Client requests time from the master (T1)
- Client records local receipt time (T1')
-
Delay Request Exchange:
- Client sends timestamp request (T2)
- Master timestamps arrival and responds (T2')
-
Offset Calculation:
- Computes clock offset using the formula:
offset = (T1' - T1 - (T2 - T2')) / 2 - Calculates synchronized time as:
synchronizedTime = localTime + offset
- Computes clock offset using the formula:
-
Periodic Updates:
- Automatically maintains synchronization at configurable intervals
- Default interval is 1000ms (1 second)
- Calls the provided callback with updated offset and time values
We've included a clock-server.js file that serves as a practical demonstration of the UMT system in action, showing how the PTP implementation synchronizes multiple machines across a network.
The clock-server.js simulates machine clocks with deliberately introduced drift and random initial offsets:
- Simulated Time Drift: Each server instance introduces a small random drift factor to emulate real-world clock imperfections
- Random Initial Offset: Servers start with a random time offset (10-15 seconds) to simulate unsynchronized machines
- Real-time Display: The current time in nanoseconds is continuously displayed on the console
- On-Demand Synchronization: An HTTP endpoint (/sync) triggers synchronization with the PTP master
To experience the power of UMT synchronization:
-
Start multiple clock servers on different ports:
# Terminal 1 node clock-server.js 3000 # Terminal 2 node clock-server.js 3001
-
Observe the different times displayed in each terminal - they will be significantly different due to the random offsets
-
Trigger synchronization for each server:
# In a new terminal curl http://localhost:3000/sync # Wait a moment to observe the first server synchronizing, then curl http://localhost:3001/sync
-
Observe how both servers now display identical nanosecond-level timestamps, despite running on different processes/machines
The synchronization process connects each server to peaq's PTP master clock, which derives its time from the blockchain. This creates a trustless, decentralized time reference that all machines can agree upon with nanosecond precision.