Js-Verifiable Logical Clock
Verifiable Logical Clock
A JavaScript implementation of Verifiable Logical Clock (VLC) for distributed systems. This library provides a verifiable logical clock construct that can be used in peer-to-peer networks to order events and ensure consistency.
Features
Standard logical clock implementation with vector clock properties
Optimized ordinary clock implementation for better performance
Support for clock comparison, merging, and difference calculation
SHA256 hash calculation for verification
Support for both numeric and string identifiers
Comprehensive test coverage
Installation
npm install verifiable-logical-clockUsage
Basic Clock Usage
import { Clock } from 'verifiable-logical-clock';
// Create a new clock
const clock = new Clock();
// Increment clock for specific IDs (can be numbers or strings)
clock.inc('alice');
clock.inc('bob');
clock.inc('alice'); // increment again
// Get clock value for an ID
console.log(clock.get('alice')); // 2
console.log(clock.get('bob')); // 1
// Check if clock is in genesis state
console.log(clock.isGenesis()); // false
// Calculate verification hash
console.log(clock.calculateSHA256());
// Merge with another clock
const clock2 = new Clock();
clock2.inc('charlie');
clock.merge([clock2]);Ordinary Clock Usage
API Reference
Clock Class
constructor(): Create a new clock instanceinc(id): Increment counter for specified IDget(id): Get counter value for specified IDclear(): Reset all countersmerge(others): Merge with other clocksdiff(other): Calculate difference with another clockindexKey(): Generate index key for the clockbaseCommon(other): Calculate common base with another clockisGenesis(): Check if clock is in genesis statecompareTo(other): Compare with another clock (-1, 0, 1, or null)calculateSHA256(): Calculate verification hashclone(): Create a deep copy of the clock
OrdinaryClock Class
constructor(): Create a new ordinary clock instanceisGenesis(): Check if clock is in genesis statemerge(other): Merge with another clockupdate(others, id): Update clock with others and increment IDstatic base(others): Calculate base clock from multiple clockscalculateSHA256(): Calculate verification hashclone(): Create a deep copy of the clockcompareTo(other): Compare with another clock (-1, 0, 1, or null)
Development
Setup
Testing
Building
Contributing
Fork the repository
Create your feature branch (
git checkout -b feature/amazing-feature)Commit your changes (
git commit -m 'Add some amazing feature')Push to the branch (
git push origin feature/amazing-feature)Open a Pull Request
References
Last updated