Skip to content

Commit 5d7b9f6

Browse files
committed
feat: add the implementation of hvlc (hybrid vector logical clock)
1 parent b5f2ec1 commit 5d7b9f6

File tree

6 files changed

+412
-1
lines changed

6 files changed

+412
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"crates/accumulator",
1010
"crates/vlc",
1111
"crates/hlc",
12+
"crates/hvlc",
1213
"crates/cops",
1314
"crates/vrf",
1415
"crates/crypto",
@@ -19,7 +20,7 @@ members = [
1920
"demos/coll_tx",
2021
"demos/vlc_dag",
2122
"demos/tee_vlc",
22-
"demos/test_vlc_net",
23+
"demos/test_vlc_net",
2324
]
2425

2526
[profile.dev]

crates/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ The crates folder of Chronos includes core functional code crates and utility li
1515
- Each timestamp consists of a wall-clock time and a logical component, allowing for easy comparison and conflict resolution.
1616
- This crate is an implementation of the [Hybrid Logical Clock](http://www.cse.buffalo.edu/tech-reports/2014-04.pdf).
1717

18+
## [hvlc](./hvlc/)
19+
20+
- This Hybrid Vector Logical Clock (HVLC) crate implements a hybrid vector clock structure that combines physical timestamps with vector clock properties.
21+
- HVLC uses a BTreeMap to store logical clock values for multiple nodes while maintaining a physical timestamp, enabling efficient tracking of causality and concurrent events in distributed systems.
22+
- Each clock instance contains:
23+
- A mapping table (inner) that records logical clock values for each node ID
24+
- A physical timestamp used to provide total ordering when logical clock comparison is insufficient
25+
- The implementation provides core functionalities like event ordering, clock merging, and base calculation, suitable for scenarios requiring distributed causality tracking.
26+
- Compared to regular vector clocks, HVLC offers better total ordering support through physical timestamps while maintaining the causal consistency properties of vector clocks.
27+
- It can be used to as the [CRDTs](https://crdt.tech/)(Conflict-free Replicated Data Type) algorithm in distributed scenarios for providing total ordering.
28+
1829
## [accumulator](./accumulator/)
1930

2031
- A simple accumulator application.

crates/hlc/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ pub struct State<F> {
8484
now: F,
8585
}
8686

87+
impl<F: FnMut() -> SystemTime> PartialEq for State<F> {
88+
fn eq(&self, other: &Self) -> bool {
89+
self.s == other.s
90+
}
91+
}
92+
93+
impl<F: FnMut() -> SystemTime> Eq for State<F> {}
94+
95+
impl<F: FnMut() -> SystemTime> PartialOrd for State<F> {
96+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
97+
self.s.partial_cmp(&other.s)
98+
}
99+
}
100+
101+
impl<F: FnMut() -> SystemTime> Ord for State<F> {
102+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
103+
self.s.cmp(&other.s)
104+
}
105+
}
106+
87107
impl State<()> {
88108
// Creates a standard hybrid logical clock, using `std::time::SystemTime` as
89109
// supplier of the physical clock's wall time.
@@ -164,6 +184,15 @@ mod tests {
164184
HLTimespec::new(s, ns, l)
165185
}
166186

187+
#[test]
188+
fn hlts_comparing() {
189+
let mut hlc = State::new();
190+
let hlc_1 = hlc.get_time();
191+
let hlc_2 = hlc.get_time();
192+
println!("hlc1 {:?}, \nhlc2 {:?}", hlc_1, hlc_2);
193+
assert!(hlc_1 < hlc_2);
194+
}
195+
167196
#[test]
168197
fn it_works() {
169198
// Start with a reference time for tests

crates/hvlc/Cargo.toml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "hvlc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sha2 = "0.10.8"
8+
sha3 = "0.10.1"
9+
rand = "0.8.5"
10+
rand_distr = "0.4.3"
11+
bincode = "1.3.3"
12+
hex = "0.4.3"
13+
tracing = "0.1.40"
14+
futures = "0.3.30"
15+
num_cpus = "1.13.1"
16+
derive_more = "0.99.17"
17+
derive-where = "1.2.7"
18+
serde = { version = "1", features = ["derive"] }
19+
anyhow = { version = "1.0.79", features = ["backtrace"] }
20+
tracing-subscriber = "0.3.18"
21+
secp256k1 = { version = "0.29.0", features = ["rand-std", "serde", "recovery"] }
22+
tokio = { version = "1.35.1", features = [
23+
"net",
24+
"time",
25+
"sync",
26+
"rt",
27+
"signal",
28+
"macros",
29+
"rt-multi-thread",
30+
"fs",
31+
"process",
32+
"io-util",
33+
] }
34+
tokio-util = "0.7.10"
35+
crypto = { path = "../crypto", version = "0.1.0" }

0 commit comments

Comments
 (0)