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.
- 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
npm install verifiable-logical-clock
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]);
import { OrdinaryClock } from 'verifiable-logical-clock';
// Create clocks
const clock1 = new OrdinaryClock();
clock1.values.set(1, 10);
clock1.values.set(2, 0);
const clock2 = new OrdinaryClock();
clock2.values.set(1, 0);
clock2.values.set(2, 20);
// Merge clocks
const merged = clock1.merge(clock2);
console.log(merged.values); // Map { 1 => 10, 2 => 20 }
// Update clock
const updated = clock1.update([clock2], 1);
console.log(updated.values); // Incremented value for ID 1
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
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)
git clone https://github.com/yourusername/verifiable-logical-clock.git
cd verifiable-logical-clock
npm install
npm test
npm run build
MIT
- 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