@optimystic/db-core 1.21.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/README.md +8 -4
  2. package/dist/src/btree/btree.d.ts +36 -4
  3. package/dist/src/btree/btree.d.ts.map +1 -1
  4. package/dist/src/btree/btree.js +86 -18
  5. package/dist/src/btree/btree.js.map +1 -1
  6. package/dist/src/collection/action.d.ts +7 -4
  7. package/dist/src/collection/action.d.ts.map +1 -1
  8. package/dist/src/collection/action.js +13 -5
  9. package/dist/src/collection/action.js.map +1 -1
  10. package/dist/src/collection/collection.d.ts +177 -51
  11. package/dist/src/collection/collection.d.ts.map +1 -1
  12. package/dist/src/collection/collection.js +265 -81
  13. package/dist/src/collection/collection.js.map +1 -1
  14. package/dist/src/collection/struct.d.ts +44 -8
  15. package/dist/src/collection/struct.d.ts.map +1 -1
  16. package/dist/src/collection/struct.js.map +1 -1
  17. package/dist/src/collection/write-attempts.d.ts +138 -34
  18. package/dist/src/collection/write-attempts.d.ts.map +1 -1
  19. package/dist/src/collection/write-attempts.js +168 -101
  20. package/dist/src/collection/write-attempts.js.map +1 -1
  21. package/dist/src/collections/diary/diary.d.ts.map +1 -1
  22. package/dist/src/collections/diary/diary.js +5 -1
  23. package/dist/src/collections/diary/diary.js.map +1 -1
  24. package/dist/src/collections/tree/tree.d.ts +18 -10
  25. package/dist/src/collections/tree/tree.d.ts.map +1 -1
  26. package/dist/src/collections/tree/tree.js +17 -9
  27. package/dist/src/collections/tree/tree.js.map +1 -1
  28. package/dist/src/hlc/clock-store.d.ts +63 -0
  29. package/dist/src/hlc/clock-store.d.ts.map +1 -0
  30. package/dist/src/hlc/clock-store.js +49 -0
  31. package/dist/src/hlc/clock-store.js.map +1 -0
  32. package/dist/src/hlc/clock.d.ts +64 -0
  33. package/dist/src/hlc/clock.d.ts.map +1 -0
  34. package/dist/src/hlc/clock.js +171 -0
  35. package/dist/src/hlc/clock.js.map +1 -0
  36. package/dist/src/hlc/hlc.d.ts +38 -0
  37. package/dist/src/hlc/hlc.d.ts.map +1 -0
  38. package/dist/src/hlc/hlc.js +41 -0
  39. package/dist/src/hlc/hlc.js.map +1 -0
  40. package/dist/src/hlc/index.d.ts +5 -0
  41. package/dist/src/hlc/index.d.ts.map +1 -0
  42. package/dist/src/hlc/index.js +5 -0
  43. package/dist/src/hlc/index.js.map +1 -0
  44. package/dist/src/hlc/skew.d.ts +35 -0
  45. package/dist/src/hlc/skew.d.ts.map +1 -0
  46. package/dist/src/hlc/skew.js +35 -0
  47. package/dist/src/hlc/skew.js.map +1 -0
  48. package/dist/src/index.d.ts +2 -0
  49. package/dist/src/index.d.ts.map +1 -1
  50. package/dist/src/index.js +2 -0
  51. package/dist/src/index.js.map +1 -1
  52. package/dist/src/lane/index.d.ts +2 -0
  53. package/dist/src/lane/index.d.ts.map +1 -0
  54. package/dist/src/lane/index.js +2 -0
  55. package/dist/src/lane/index.js.map +1 -0
  56. package/dist/src/lane/lane.d.ts +46 -0
  57. package/dist/src/lane/lane.d.ts.map +1 -0
  58. package/dist/src/lane/lane.js +26 -0
  59. package/dist/src/lane/lane.js.map +1 -0
  60. package/dist/src/log/log.d.ts +50 -22
  61. package/dist/src/log/log.d.ts.map +1 -1
  62. package/dist/src/log/log.js +21 -13
  63. package/dist/src/log/log.js.map +1 -1
  64. package/dist/src/log/struct.d.ts +61 -1
  65. package/dist/src/log/struct.d.ts.map +1 -1
  66. package/dist/src/log/struct.js +24 -0
  67. package/dist/src/log/struct.js.map +1 -1
  68. package/dist/src/network/i-repo.d.ts +10 -0
  69. package/dist/src/network/i-repo.d.ts.map +1 -1
  70. package/dist/src/network/stale-failure.d.ts +20 -7
  71. package/dist/src/network/stale-failure.d.ts.map +1 -1
  72. package/dist/src/network/stale-failure.js +29 -7
  73. package/dist/src/network/stale-failure.js.map +1 -1
  74. package/dist/src/network/struct.d.ts +50 -0
  75. package/dist/src/network/struct.d.ts.map +1 -1
  76. package/dist/src/network/struct.js.map +1 -1
  77. package/dist/src/offline/entry.d.ts +14 -0
  78. package/dist/src/offline/entry.d.ts.map +1 -1
  79. package/dist/src/offline/entry.js +2 -0
  80. package/dist/src/offline/entry.js.map +1 -1
  81. package/dist/src/offline/journal.d.ts +10 -0
  82. package/dist/src/offline/journal.d.ts.map +1 -1
  83. package/dist/src/offline/queue.d.ts +10 -0
  84. package/dist/src/offline/queue.d.ts.map +1 -1
  85. package/dist/src/offline/queue.js +29 -3
  86. package/dist/src/offline/queue.js.map +1 -1
  87. package/dist/src/offline/status.d.ts +7 -0
  88. package/dist/src/offline/status.d.ts.map +1 -1
  89. package/dist/src/offline/status.js.map +1 -1
  90. package/dist/src/testing/test-transactor.d.ts +9 -1
  91. package/dist/src/testing/test-transactor.d.ts.map +1 -1
  92. package/dist/src/testing/test-transactor.js +12 -3
  93. package/dist/src/testing/test-transactor.js.map +1 -1
  94. package/dist/src/transaction/actions-engine.d.ts +3 -0
  95. package/dist/src/transaction/actions-engine.d.ts.map +1 -1
  96. package/dist/src/transaction/actions-engine.js +3 -0
  97. package/dist/src/transaction/actions-engine.js.map +1 -1
  98. package/dist/src/transaction/coordinator.d.ts +16 -5
  99. package/dist/src/transaction/coordinator.d.ts.map +1 -1
  100. package/dist/src/transaction/coordinator.js +203 -48
  101. package/dist/src/transaction/coordinator.js.map +1 -1
  102. package/dist/src/transaction/entry-commit.d.ts +45 -10
  103. package/dist/src/transaction/entry-commit.d.ts.map +1 -1
  104. package/dist/src/transaction/entry-commit.js +20 -7
  105. package/dist/src/transaction/entry-commit.js.map +1 -1
  106. package/dist/src/transaction/errors.d.ts +69 -1
  107. package/dist/src/transaction/errors.d.ts.map +1 -1
  108. package/dist/src/transaction/errors.js +70 -1
  109. package/dist/src/transaction/errors.js.map +1 -1
  110. package/dist/src/transaction/index.d.ts +1 -1
  111. package/dist/src/transaction/index.d.ts.map +1 -1
  112. package/dist/src/transaction/index.js +1 -1
  113. package/dist/src/transaction/index.js.map +1 -1
  114. package/dist/src/transaction/session.d.ts.map +1 -1
  115. package/dist/src/transaction/session.js +18 -5
  116. package/dist/src/transaction/session.js.map +1 -1
  117. package/dist/src/transaction/transaction.d.ts +74 -15
  118. package/dist/src/transaction/transaction.d.ts.map +1 -1
  119. package/dist/src/transaction/transaction.js +35 -15
  120. package/dist/src/transaction/transaction.js.map +1 -1
  121. package/dist/src/transaction/validator.d.ts +25 -4
  122. package/dist/src/transaction/validator.d.ts.map +1 -1
  123. package/dist/src/transaction/validator.js +45 -5
  124. package/dist/src/transaction/validator.js.map +1 -1
  125. package/dist/src/transactor/block-floors.d.ts +12 -1
  126. package/dist/src/transactor/block-floors.d.ts.map +1 -1
  127. package/dist/src/transactor/block-floors.js +14 -1
  128. package/dist/src/transactor/block-floors.js.map +1 -1
  129. package/dist/src/transactor/network-transactor.d.ts +8 -0
  130. package/dist/src/transactor/network-transactor.d.ts.map +1 -1
  131. package/dist/src/transactor/network-transactor.js +9 -4
  132. package/dist/src/transactor/network-transactor.js.map +1 -1
  133. package/dist/src/transactor/transactor-source.d.ts +11 -0
  134. package/dist/src/transactor/transactor-source.d.ts.map +1 -1
  135. package/dist/src/transactor/transactor-source.js +5 -1
  136. package/dist/src/transactor/transactor-source.js.map +1 -1
  137. package/dist/src/transactor/transactor.d.ts +8 -0
  138. package/dist/src/transactor/transactor.d.ts.map +1 -1
  139. package/dist/src/transform/cache-source.d.ts +7 -0
  140. package/dist/src/transform/cache-source.d.ts.map +1 -1
  141. package/dist/src/transform/cache-source.js +14 -18
  142. package/dist/src/transform/cache-source.js.map +1 -1
  143. package/package.json +1 -1
  144. package/src/btree/btree.ts +92 -18
  145. package/src/collection/action.ts +13 -5
  146. package/src/collection/collection.ts +3272 -3039
  147. package/src/collection/struct.ts +493 -456
  148. package/src/collection/write-attempts.ts +213 -109
  149. package/src/collections/diary/diary.ts +5 -1
  150. package/src/collections/tree/tree.ts +33 -18
  151. package/src/hlc/clock-store.ts +68 -0
  152. package/src/hlc/clock.ts +196 -0
  153. package/src/hlc/hlc.ts +56 -0
  154. package/src/hlc/index.ts +4 -0
  155. package/src/hlc/skew.ts +55 -0
  156. package/src/index.ts +2 -0
  157. package/src/lane/index.ts +1 -0
  158. package/src/lane/lane.ts +54 -0
  159. package/src/log/log.ts +341 -310
  160. package/src/log/struct.ts +62 -1
  161. package/src/network/i-repo.ts +11 -1
  162. package/src/network/stale-failure.ts +31 -7
  163. package/src/network/struct.ts +50 -0
  164. package/src/offline/entry.ts +16 -0
  165. package/src/offline/journal.ts +10 -0
  166. package/src/offline/queue.ts +38 -4
  167. package/src/offline/status.ts +7 -0
  168. package/src/testing/test-transactor.ts +1065 -1050
  169. package/src/transaction/actions-engine.ts +3 -0
  170. package/src/transaction/coordinator.ts +235 -54
  171. package/src/transaction/entry-commit.ts +56 -14
  172. package/src/transaction/errors.ts +60 -0
  173. package/src/transaction/index.ts +1 -1
  174. package/src/transaction/session.ts +18 -5
  175. package/src/transaction/transaction.ts +90 -17
  176. package/src/transaction/validator.ts +46 -5
  177. package/src/transactor/block-floors.ts +15 -1
  178. package/src/transactor/network-transactor.ts +1908 -1896
  179. package/src/transactor/transactor-source.ts +15 -1
  180. package/src/transactor/transactor.ts +9 -0
  181. package/src/transform/cache-source.ts +15 -17
@@ -0,0 +1,171 @@
1
+ import { createLogger } from '../logger.js';
2
+ import { compareHlc, isHlc } from './hlc.js';
3
+ import { DEFAULT_HLC_PERSIST_INTERVAL_MS, DEFAULT_HLC_RESTART_SLACK_MS, restartFloor } from './clock-store.js';
4
+ const log = createLogger('hlc');
5
+ /**
6
+ * A node's hybrid logical clock: the only thing in the repository that mints an {@link Hlc}.
7
+ *
8
+ * It holds the last HLC it issued or observed. {@link now} issues above it, {@link observe} raises
9
+ * it. Built with `new`, the clock is purely in memory; {@link HlcClock.start} builds one whose mark
10
+ * survives a restart through an {@link HlcClockStore}, so a store-backed clock is always one that
11
+ * has loaded its mark before it can issue.
12
+ */
13
+ export class HlcClock {
14
+ nodeId;
15
+ localClock;
16
+ last;
17
+ persistence;
18
+ constructor(options) {
19
+ if (typeof options.nodeId !== 'string' || options.nodeId === '') {
20
+ throw new Error('HlcClock needs a non-empty nodeId: the empty node id marks a legacy HLC');
21
+ }
22
+ this.nodeId = options.nodeId;
23
+ this.localClock = options.localClock ?? Date.now;
24
+ }
25
+ /**
26
+ * A clock that resumes from the mark `store` holds: it treats {@link restartFloor} of that mark as
27
+ * already observed, so it issues above every HLC it saved before the restart and, by the slack,
28
+ * above the ones it issued after its last save. A `load` that rejects starts the clock from the
29
+ * local clock alone, logged: refusing to start a node because its mark could not be read would turn
30
+ * a lost record into an outage.
31
+ */
32
+ static async start(options) {
33
+ const persistIntervalMs = nonNegative('persistIntervalMs', options.persistIntervalMs ?? DEFAULT_HLC_PERSIST_INTERVAL_MS);
34
+ const restartSlackMs = nonNegative('restartSlackMs', options.restartSlackMs ?? DEFAULT_HLC_RESTART_SLACK_MS);
35
+ const clock = new HlcClock(options);
36
+ const stored = await loadMark(options.store);
37
+ clock.persistence = new MarkPersistence(options.store, persistIntervalMs, stored?.wallMs);
38
+ if (stored !== undefined)
39
+ clock.observe(restartFloor(stored, restartSlackMs, clock.nodeId));
40
+ return clock;
41
+ }
42
+ /**
43
+ * The local wall clock's reading, the one this clock issues from, without issuing anything. What a
44
+ * peer's HLC is measured against (`SkewLedger`): the mark itself is no reference, since it has
45
+ * already moved up to whatever far-ahead HLC this clock observed.
46
+ */
47
+ localTime() {
48
+ return this.localClock();
49
+ }
50
+ /** Issue an HLC above every one this clock has issued or observed. */
51
+ now() {
52
+ const issued = this.next();
53
+ this.advance(issued);
54
+ return issued;
55
+ }
56
+ /**
57
+ * Raise the mark to `hlc` when it is above it; otherwise nothing. A value without the shape of an
58
+ * HLC ({@link isHlc}) is ignored: a wire field is checked, and reported, by the
59
+ * caller that read it.
60
+ */
61
+ observe(hlc) {
62
+ if (!isHlc(hlc))
63
+ return;
64
+ if (this.last !== undefined && compareHlc(hlc, this.last) <= 0)
65
+ return;
66
+ this.advance({ wallMs: hlc.wallMs, logical: hlc.logical, nodeId: hlc.nodeId });
67
+ }
68
+ /**
69
+ * The HLC after the mark at the local clock's reading. A logical count already at the top of the
70
+ * safe-integer range carries into `wallMs` instead: past it `logical + 1` stops changing the value,
71
+ * and the clock would issue one HLC twice after observing a peer's far-advanced count.
72
+ */
73
+ next() {
74
+ const prior = this.last;
75
+ const wallMs = Math.max(this.localClock(), prior?.wallMs ?? -Infinity);
76
+ if (prior === undefined || wallMs !== prior.wallMs)
77
+ return { wallMs, logical: 0, nodeId: this.nodeId };
78
+ if (prior.logical < Number.MAX_SAFE_INTEGER)
79
+ return { wallMs, logical: prior.logical + 1, nodeId: this.nodeId };
80
+ return { wallMs: wallMs + 1, logical: 0, nodeId: this.nodeId };
81
+ }
82
+ advance(hlc) {
83
+ const moved = hlc.wallMs !== this.last?.wallMs;
84
+ this.last = hlc;
85
+ if (moved)
86
+ this.persistence?.offer(hlc);
87
+ }
88
+ }
89
+ /**
90
+ * The clock for a transactor that declares none (`ITransactor.clock`): the reference peer's, the
91
+ * Quereus plugin's `local` and `test` transactors, a host's own. One per process, in the node id
92
+ * `'local'`, which is also the peer id `TransactionSession.create` stamps when none is given, so the
93
+ * two defaults agree. It has no store, so its mark does not survive a restart.
94
+ *
95
+ * This is the one place a second {@link HlcClock} can exist in a process beside a node's own;
96
+ * reach it only through {@link clockOf}.
97
+ */
98
+ let undeclaredClock;
99
+ /** The clock a write through `transactor` mints from: the transactor's own, or the process-wide fallback. */
100
+ export function clockOf(transactor) {
101
+ return transactor.clock ?? (undeclaredClock ??= new HlcClock({ nodeId: 'local' }));
102
+ }
103
+ async function loadMark(store) {
104
+ try {
105
+ const stored = await store.load();
106
+ if (stored === undefined || isHlc(stored))
107
+ return stored;
108
+ log('hlc:load-malformed value=%o', stored);
109
+ }
110
+ catch (error) {
111
+ log('hlc:load-failed error=%s', error instanceof Error ? error.message : String(error));
112
+ }
113
+ return undefined;
114
+ }
115
+ function nonNegative(name, value) {
116
+ if (!Number.isFinite(value) || value < 0) {
117
+ throw new RangeError(`HlcClock ${name} must be a finite number at or above 0, got ${value}`);
118
+ }
119
+ return value;
120
+ }
121
+ /**
122
+ * The clock's saves: one whenever `wallMs` has moved at least `intervalMs` past the last mark handed
123
+ * to the store, fire-and-forget, never awaited on a write path. At most one save is in flight; a mark
124
+ * offered meanwhile waits, replaced by any newer one. A rejection is logged and the next qualifying
125
+ * advance tries again.
126
+ */
127
+ class MarkPersistence {
128
+ store;
129
+ intervalMs;
130
+ lastOfferedWallMs;
131
+ inFlight = false;
132
+ queued;
133
+ constructor(store, intervalMs, storedWallMs) {
134
+ this.store = store;
135
+ this.intervalMs = intervalMs;
136
+ this.lastOfferedWallMs = storedWallMs;
137
+ }
138
+ offer(hlc) {
139
+ if (this.lastOfferedWallMs !== undefined && hlc.wallMs < this.lastOfferedWallMs + this.intervalMs)
140
+ return;
141
+ this.lastOfferedWallMs = hlc.wallMs;
142
+ if (this.inFlight) {
143
+ this.queued = hlc;
144
+ return;
145
+ }
146
+ this.save(hlc);
147
+ }
148
+ save(hlc) {
149
+ this.inFlight = true;
150
+ void this.callStore(hlc)
151
+ .catch(error => log('hlc:save-failed wallMs=%d logical=%d error=%s', hlc.wallMs, hlc.logical, error instanceof Error ? error.message : String(error)))
152
+ .finally(() => this.settled());
153
+ }
154
+ /** The store's `save`, with a synchronous throw turned into a rejection so it never reaches the caller of `now`. */
155
+ callStore(hlc) {
156
+ try {
157
+ return this.store.save(hlc);
158
+ }
159
+ catch (error) {
160
+ return Promise.reject(error);
161
+ }
162
+ }
163
+ settled() {
164
+ this.inFlight = false;
165
+ const next = this.queued;
166
+ this.queued = undefined;
167
+ if (next !== undefined)
168
+ this.save(next);
169
+ }
170
+ }
171
+ //# sourceMappingURL=clock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clock.js","sourceRoot":"","sources":["../../../src/hlc/clock.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,cA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AAC;IAED,oHAAoH;IAC5G,SAAS,CAAC,GAAQ;QACzB,IAAI,CAAC;YACJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;IACF,CAAC;IAEO,OAAO;QACd,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,IAAI,KAAK,SAAS;YAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;CACD"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * A hybrid logical clock (HLC) value: the Kulkarni-Demirbas construction docs/crdt-sync.md
3
+ * §HLC Generation and Causal Tracking describes, as one plain JSON-safe object, since it travels
4
+ * inside log blocks and pend requests, both plain JSON on the wire.
5
+ *
6
+ * Only `HlcClock` mints one. The other constructors are {@link legacyHlc}, for a record written
7
+ * before the field existed, and test fixtures.
8
+ */
9
+ export type Hlc = {
10
+ /** Wall-clock milliseconds, or higher: the max of the issuer's clock and the highest HLC it had observed. */
11
+ readonly wallMs: number;
12
+ /** Breaks ties among HLCs issued within one `wallMs`. 0 whenever `wallMs` moved. */
13
+ readonly logical: number;
14
+ /** The issuing node's peer id string. `''` means legacy: an entry written before the field existed. */
15
+ readonly nodeId: string;
16
+ };
17
+ /**
18
+ * The total order on HLCs: `wallMs`, then `logical`, then `nodeId` by plain string (code unit)
19
+ * comparison. Two HLCs from one node never compare equal, because its clock never issues the same
20
+ * `(wallMs, logical)` twice; two from different nodes are separated by `nodeId`.
21
+ */
22
+ export declare function compareHlc(a: Hlc, b: Hlc): number;
23
+ /** The later of `a` and `b` by {@link compareHlc}; `b` when `a` is absent. */
24
+ export declare function maxHlc(a: Hlc | undefined, b: Hlc): Hlc;
25
+ /**
26
+ * The HLC a reader derives for an entry or record written before the field existed: its plain
27
+ * timestamp, no logical count, and the empty node id. The empty id sorts before every real peer id,
28
+ * which is the only property anything relies on.
29
+ */
30
+ export declare function legacyHlc(timestamp: number): Hlc;
31
+ /**
32
+ * Whether a value read off the wire has the shape of an {@link Hlc}: a finite `wallMs`, a `logical`
33
+ * that is a non-negative safe integer (what a clock issues, and what it can count past without
34
+ * losing precision), and a string node id. How far `wallMs` may sit from local time is not a shape
35
+ * question and is not checked here.
36
+ */
37
+ export declare function isHlc(value: unknown): value is Hlc;
38
+ //# sourceMappingURL=hlc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hlc.d.ts","sourceRoot":"","sources":["../../../src/hlc/hlc.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,GAAG;IACjB,6GAA6G;IAC7G,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oFAAoF;IACpF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,uGAAuG;IACvG,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,MAAM,CAKjD;AAED,8EAA8E;AAC9E,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,GAAG,SAAS,EAAE,CAAC,EAAE,GAAG,GAAG,GAAG,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,GAAG,CAEhD;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,GAAG,CAMlD"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The total order on HLCs: `wallMs`, then `logical`, then `nodeId` by plain string (code unit)
3
+ * comparison. Two HLCs from one node never compare equal, because its clock never issues the same
4
+ * `(wallMs, logical)` twice; two from different nodes are separated by `nodeId`.
5
+ */
6
+ export function compareHlc(a, b) {
7
+ if (a.wallMs !== b.wallMs)
8
+ return a.wallMs < b.wallMs ? -1 : 1;
9
+ if (a.logical !== b.logical)
10
+ return a.logical < b.logical ? -1 : 1;
11
+ if (a.nodeId === b.nodeId)
12
+ return 0;
13
+ return a.nodeId < b.nodeId ? -1 : 1;
14
+ }
15
+ /** The later of `a` and `b` by {@link compareHlc}; `b` when `a` is absent. */
16
+ export function maxHlc(a, b) {
17
+ return a === undefined || compareHlc(b, a) > 0 ? b : a;
18
+ }
19
+ /**
20
+ * The HLC a reader derives for an entry or record written before the field existed: its plain
21
+ * timestamp, no logical count, and the empty node id. The empty id sorts before every real peer id,
22
+ * which is the only property anything relies on.
23
+ */
24
+ export function legacyHlc(timestamp) {
25
+ return { wallMs: timestamp, logical: 0, nodeId: '' };
26
+ }
27
+ /**
28
+ * Whether a value read off the wire has the shape of an {@link Hlc}: a finite `wallMs`, a `logical`
29
+ * that is a non-negative safe integer (what a clock issues, and what it can count past without
30
+ * losing precision), and a string node id. How far `wallMs` may sit from local time is not a shape
31
+ * question and is not checked here.
32
+ */
33
+ export function isHlc(value) {
34
+ if (typeof value !== 'object' || value === null)
35
+ return false;
36
+ const { wallMs, logical, nodeId } = value;
37
+ return typeof wallMs === 'number' && Number.isFinite(wallMs)
38
+ && Number.isSafeInteger(logical) && logical >= 0
39
+ && typeof nodeId === 'string';
40
+ }
41
+ //# sourceMappingURL=hlc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hlc.js","sourceRoot":"","sources":["../../../src/hlc/hlc.ts"],"names":[],"mappings":"AAiBA;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,CAAM,EAAE,CAAM;IACxC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC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export * from './hlc.js';
2
+ export * from './clock-store.js';
3
+ export * from './clock.js';
4
+ export * from './skew.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hlc/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './hlc.js';
2
+ export * from './clock-store.js';
3
+ export * from './clock.js';
4
+ export * from './skew.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hlc/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,35 @@
1
+ /** How many node ids a {@link SkewLedger} remembers; the least recently heard from is forgotten first. */
2
+ export declare const DEFAULT_SKEW_LEDGER_MAX_NODES = 256;
3
+ /**
4
+ * How far one node's HLCs have been from this node's wall clock, over the samples a
5
+ * {@link SkewLedger} kept for it. Both maxima are magnitudes, 0 when no sample fell on that side.
6
+ *
7
+ * A gap does not say which clock is wrong: a node whose own HLC jumped forward after it observed a
8
+ * far-ahead peer shows as ahead to everyone, and so does a node whose wall clock simply runs fast.
9
+ */
10
+ export type NodeSkew = {
11
+ readonly nodeId: string;
12
+ readonly samples: number;
13
+ /** The largest amount one of the node's HLCs sat ahead of local time, in milliseconds. */
14
+ readonly maxAheadMs: number;
15
+ /** The largest amount one of the node's HLCs sat behind local time, in milliseconds. */
16
+ readonly maxBehindMs: number;
17
+ };
18
+ /**
19
+ * The measurement docs/crdt-sync.md §Open Questions asks for before deciding whether members should
20
+ * refuse an HLC far from their own clock: per node id, how far the HLCs this node received from it
21
+ * sat from local time. Pure and in memory; it refuses nothing and nothing acts on it. A db-p2p
22
+ * storage repo records one sample per pend that carries an HLC (`StorageRepo.hlcSkew`).
23
+ */
24
+ export declare class SkewLedger {
25
+ private readonly nodes;
26
+ constructor(maxNodes?: number);
27
+ /**
28
+ * Add one sample: `gapMs` is the HLC's `wallMs` less local time, positive when the node's HLC was
29
+ * ahead. Returns the node's summary with the sample included.
30
+ */
31
+ record(nodeId: string, gapMs: number): NodeSkew;
32
+ /** Every node remembered, least recently heard from first. */
33
+ snapshot(): NodeSkew[];
34
+ }
35
+ //# sourceMappingURL=skew.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skew.d.ts","sourceRoot":"","sources":["../../../src/hlc/skew.ts"],"names":[],"mappings":"AAEA,0GAA0G;AAC1G,eAAO,MAAM,6BAA6B,MAAM,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,GAAG;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,0FAA0F;IAC1F,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,wFAAwF;IACxF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA2B;gBAErC,QAAQ,SAAgC;IAIpD;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,QAAQ;IAY/C,8DAA8D;IAC9D,QAAQ,IAAI,QAAQ,EAAE;CAGtB"}
@@ -0,0 +1,35 @@
1
+ import { LruMap } from '../utility/lru-map.js';
2
+ /** How many node ids a {@link SkewLedger} remembers; the least recently heard from is forgotten first. */
3
+ export const DEFAULT_SKEW_LEDGER_MAX_NODES = 256;
4
+ /**
5
+ * The measurement docs/crdt-sync.md §Open Questions asks for before deciding whether members should
6
+ * refuse an HLC far from their own clock: per node id, how far the HLCs this node received from it
7
+ * sat from local time. Pure and in memory; it refuses nothing and nothing acts on it. A db-p2p
8
+ * storage repo records one sample per pend that carries an HLC (`StorageRepo.hlcSkew`).
9
+ */
10
+ export class SkewLedger {
11
+ nodes;
12
+ constructor(maxNodes = DEFAULT_SKEW_LEDGER_MAX_NODES) {
13
+ this.nodes = new LruMap(maxNodes);
14
+ }
15
+ /**
16
+ * Add one sample: `gapMs` is the HLC's `wallMs` less local time, positive when the node's HLC was
17
+ * ahead. Returns the node's summary with the sample included.
18
+ */
19
+ record(nodeId, gapMs) {
20
+ const prior = this.nodes.get(nodeId);
21
+ const next = {
22
+ nodeId,
23
+ samples: (prior?.samples ?? 0) + 1,
24
+ maxAheadMs: Math.max(prior?.maxAheadMs ?? 0, gapMs),
25
+ maxBehindMs: Math.max(prior?.maxBehindMs ?? 0, -gapMs),
26
+ };
27
+ this.nodes.set(nodeId, next);
28
+ return next;
29
+ }
30
+ /** Every node remembered, least recently heard from first. */
31
+ snapshot() {
32
+ return [...this.nodes].map(([, skew]) => skew);
33
+ }
34
+ }
35
+ //# sourceMappingURL=skew.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skew.js","sourceRoot":"","sources":["../../../src/hlc/skew.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE/C,0GAA0G;AAC1G,MAAM,CAAC,MAAM,6BAA6B,GAAG,GAAG,CAAC;AAkBjD;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IACL,KAAK,CAA2B;IAEjD,YAAY,QAAQ,GAAG,6BAA6B;QACnD,IAAI,CAAC,KAAK,GAAG,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,MAAc,EAAE,KAAa;QACnC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACrC,MAAM,IAAI,GAAa;YACtB,MAAM;YACN,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC;YAClC,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,IAAI,CAAC,EAAE,KAAK,CAAC;YACnD,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;SACtD,CAAC;QACF,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,8DAA8D;IAC9D,QAAQ;QACP,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;CACD"}
@@ -5,6 +5,8 @@ export * from "./cluster/index.js";
5
5
  export * from "./cohort-topic/index.js";
6
6
  export * from "./collection/index.js";
7
7
  export * from "./collections/index.js";
8
+ export * from "./hlc/index.js";
9
+ export * from "./lane/index.js";
8
10
  export * from "./log/index.js";
9
11
  export * from "./logger-registry.js";
10
12
  export * from "./matchmaking/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CA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sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC"}
package/dist/src/index.js CHANGED
@@ -5,6 +5,8 @@ export * from "./cluster/index.js";
5
5
  export * from "./cohort-topic/index.js";
6
6
  export * from "./collection/index.js";
7
7
  export * from "./collections/index.js";
8
+ export * from "./hlc/index.js";
9
+ export * from "./lane/index.js";
8
10
  export * from "./log/index.js";
9
11
  export * from "./logger-registry.js";
10
12
  export * from "./matchmaking/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './lane.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lane/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './lane.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lane/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The lane of a write: how its conflicts with other machines' CONCURRENT writes are decided
3
+ * (docs/crdt-sync.md §Stage 5 — Tunable per-collection; docs/partition-healing.md §I-Confluence
4
+ * Classification).
5
+ *
6
+ * - `serializable` — the quorum lane. The write needs the collection's cohort to agree before it
7
+ * is committed, and the revision order decides every conflict: today's behaviour for every write.
8
+ * The tree entry guards (`TreeEntryGuard` in `packages/db-core/src/collections/tree/struct.ts`)
9
+ * are this lane's conflict rule, re-making every uniqueness and lost-update decision
10
+ * deterministically on replay.
11
+ * - `convergent` — the merge lane. The write may be committed by one machine without first agreeing
12
+ * with the others and merged into their concurrent writes later, because the invariants it is
13
+ * responsible for survive a merge: an append to an append-only set, an increment of an unbounded
14
+ * counter, a last-writer-wins field. The collection's merge declaration (`ConvergentMerge` in
15
+ * `packages/db-core/src/collection/collection.ts`) is this lane's conflict rule.
16
+ *
17
+ * Convergence is a property of an OPERATION, never of a system (the invariant-confluence result of
18
+ * Bailis et al., and Li et al.'s RedBlue consistency). **Idempotence is not the criterion**, and is
19
+ * the mistake a reader makes first: setting a column to a value is idempotent and still needs a
20
+ * tie-break under concurrency, while an increment is not idempotent and merges fine. The question is
21
+ * whether two machines applying the operation concurrently, each over its own view, leave the
22
+ * invariants the operation is responsible for true once the two views are merged.
23
+ *
24
+ * Nothing admits a write by its lane yet: the solo path, the offline queue and the conflict replay
25
+ * behave as they did before the field. The replay stage (backlog
26
+ * `feat-replay-mode-collections-merge-divergent-histories`) is what consumes it.
27
+ */
28
+ export type Lane = 'serializable' | 'convergent';
29
+ /**
30
+ * The lane of a write made of several parts: `serializable` if any part is. Convergence is a claim
31
+ * each part makes about itself, so a write is convergent only when every part claims it — and a
32
+ * write of NO parts claims nothing, so an empty set answers `serializable`, the lane every write
33
+ * takes without a declaration.
34
+ */
35
+ export declare function stricterLane(lanes: Iterable<Lane>): Lane;
36
+ /**
37
+ * Wraps a request's optional `lane` (`PendRequest.lane`, `CommitRequest.lane`) so the field spreads
38
+ * onto the request only when the sender has one: omitted rather than sent as `undefined`, because the
39
+ * request is hashed verbatim into every cohort signature preimage, so a request that states no lane
40
+ * must serialize exactly as it did before the field existed. Every producer of the field goes
41
+ * through here, as `refuseSoloField` does for its field.
42
+ */
43
+ export declare function laneField(lane: Lane | undefined): {
44
+ lane?: Lane;
45
+ };
46
+ //# sourceMappingURL=lane.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lane.d.ts","sourceRoot":"","sources":["../../../src/lane/lane.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,IAAI,GAAG,cAAc,GAAG,YAAY,CAAC;AAEjD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAOxD;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,GAAG;IAAE,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,CAEjE"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The lane of a write made of several parts: `serializable` if any part is. Convergence is a claim
3
+ * each part makes about itself, so a write is convergent only when every part claims it — and a
4
+ * write of NO parts claims nothing, so an empty set answers `serializable`, the lane every write
5
+ * takes without a declaration.
6
+ */
7
+ export function stricterLane(lanes) {
8
+ let seen = false;
9
+ for (const lane of lanes) {
10
+ if (lane === 'serializable')
11
+ return 'serializable';
12
+ seen = true;
13
+ }
14
+ return seen ? 'convergent' : 'serializable';
15
+ }
16
+ /**
17
+ * Wraps a request's optional `lane` (`PendRequest.lane`, `CommitRequest.lane`) so the field spreads
18
+ * onto the request only when the sender has one: omitted rather than sent as `undefined`, because the
19
+ * request is hashed verbatim into every cohort signature preimage, so a request that states no lane
20
+ * must serialize exactly as it did before the field existed. Every producer of the field goes
21
+ * through here, as `refuseSoloField` does for its field.
22
+ */
23
+ export function laneField(lane) {
24
+ return lane === undefined ? {} : { lane };
25
+ }
26
+ //# sourceMappingURL=lane.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lane.js","sourceRoot":"","sources":["../../../src/lane/lane.ts"],"names":[],"mappings":"AA6BA;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAqB;IACjD,IAAI,IAAI,GAAG,KAAK,CAAC;IACjB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAAI,KAAK,cAAc;YAAE,OAAO,cAAc,CAAC;QACnD,IAAI,GAAG,IAAI,CAAC;IACb,CAAC;IACD,OAAO,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC;AAC7C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,IAAsB;IAC/C,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;AAC3C,CAAC"}
@@ -3,6 +3,8 @@ import type { IBlock, BlockId, ActionId, CollectionId, ChainPath, ActionRev, Act
3
3
  import type { ChainDataNode } from '../chain/chain-nodes.js';
4
4
  import type { LogEntry, DisputeResolutionProof, InvalidationEntry, RevertedBlock } from "./index.js";
5
5
  import type { GetFromResult } from './struct.js';
6
+ import { type Hlc } from '../hlc/hlc.js';
7
+ import type { Lane } from '../lane/lane.js';
6
8
  export type LogBlock<TAction> = ChainDataNode<LogEntry<TAction>> & {
7
9
  /** Base64url encoded Sha256 hash of the prior (predecessor) block - present on every block except the head */
8
10
  priorHash?: string;
@@ -34,10 +36,36 @@ export type LogOpenOptions = {
34
36
  /** Supplies the id for each data block an append mints, in place of a fresh random one. */
35
37
  newDataBlockId?: () => BlockId;
36
38
  };
37
- /** The trailing, mostly-optional inputs to {@link Log.addActions}. An options object rather than
38
- * positional parameters because the two most interesting ones are the last two, and a caller that
39
- * needs only the timestamp should not have to spell out the two before it. */
40
- export type AddActionsOptions = {
39
+ /**
40
+ * The two per-write time values every entry kind carries (`LogEntry.hlc` and `LogEntry.timestamp`),
41
+ * taken by {@link Log.addActions}, {@link Log.addCheckpoint} and {@link Log.addInvalidation} alike.
42
+ *
43
+ * Both belong to the WRITE, not to the attempt. Storage identifies a saved block revision by
44
+ * `(action id, revision)` and accepts a retry of the same action at the same revision, so a value a
45
+ * retry minted afresh would store a second, different log block under one `(action, revision)` on
46
+ * every machine that missed the first attempt (docs/internals.md, "A write's log append is a fixed
47
+ * function of the write and the revision it is requesting").
48
+ */
49
+ export type EntryTimes = {
50
+ /** The HLC the writer minted for the write (`clockOf(transactor).now()`), re-sent verbatim by every
51
+ * retry. No default: the clock is the writer's, and an append has none of its own. */
52
+ hlc: Hlc;
53
+ /**
54
+ * When the entry was written. Nothing in the tree reads it for logic — it is informational —
55
+ * which is what makes it safe for a writer to supply the time of the WRITE rather than of the
56
+ * attempt, and arguably the more honest value. A writer whose attempt may be retried MUST supply
57
+ * it, for the reason above. Defaults to `Date.now()` for a caller that appends once.
58
+ */
59
+ timestamp?: number;
60
+ };
61
+ /** The trailing inputs to {@link Log.addActions}: the write's {@link EntryTimes} plus the two
62
+ * optional fields an action entry alone carries. An options object rather than positional
63
+ * parameters because a caller that needs only the times should not have to spell out the rest. */
64
+ export type AddActionsOptions = EntryTimes & {
65
+ /** The write's lane (`ActionEntry.lane`), minted once per write by the same rule as the times
66
+ * above and re-sent verbatim by every retry. No default: the lane is the write's, decided by the
67
+ * caller's classification or the collection's declaration, and an append has none of its own. */
68
+ lane: Lane;
41
69
  /** Every collection participating in the action this entry records. */
42
70
  collectionIds?: CollectionId[];
43
71
  /** The transaction's `(blockId, revision)` read set, persisted on the entry so the invalidation
@@ -45,18 +73,13 @@ export type AddActionsOptions = {
45
73
  * Omit it on the low-level direct-commit path that carries no `Transaction`; the cascade then
46
74
  * treats the entry as an unknown dependency. */
47
75
  reads?: ReadDependency[];
48
- /**
49
- * When the entry was written. Nothing in the tree reads it for logic — it is informational —
50
- * which is what makes it safe for a writer to supply the time of the WRITE rather than of the
51
- * attempt, and arguably the more honest value.
52
- *
53
- * A writer whose attempt may be retried MUST supply it. Storage identifies a saved block
54
- * revision by `(action id, revision)` and accepts a retry of the same action at the same
55
- * revision, so a retry that took a fresh timestamp here would store a second, different log
56
- * block under one `(action, revision)` on every machine that missed the first attempt.
57
- * Defaults to `Date.now()` for a caller that appends once.
58
- */
59
- timestamp?: number;
76
+ };
77
+ /** The trailing inputs to {@link Log.addInvalidation}: the write's {@link EntryTimes}, and the root
78
+ * invalidation when this entry is a cascade step. */
79
+ export type AddInvalidationOptions = EntryTimes & {
80
+ /** The root invalidation that triggered this cascade step; absent for a root invalidation
81
+ * (see {@link InvalidationEntry.cascadeRoot}). */
82
+ cascadeRoot?: ActionId;
60
83
  };
61
84
  export declare class Log<TAction> {
62
85
  private readonly chain;
@@ -79,28 +102,32 @@ export declare class Log<TAction> {
79
102
  * Adds a new action entry to the log.
80
103
  *
81
104
  * See {@link AddActionsOptions} for what the trailing options carry, and in particular why a
82
- * writer that may retry passes its own `timestamp` rather than letting each attempt take one.
105
+ * writer that may retry passes its own `hlc` and `timestamp` rather than letting each attempt
106
+ * take its own.
83
107
  */
84
- addActions(actions: TAction[], actionId: ActionId, rev: number, getBlockIds: () => BlockId[], options?: AddActionsOptions): Promise<{
108
+ addActions(actions: TAction[], actionId: ActionId, rev: number, getBlockIds: () => BlockId[], options: AddActionsOptions): Promise<{
85
109
  entry: {
86
110
  action: {
87
111
  blockIds: string[];
88
112
  actionId: ActionId;
89
113
  actions: TAction[];
90
114
  collectionIds?: CollectionId[];
115
+ lane: Lane;
91
116
  reads?: ReadDependency[];
92
117
  };
93
118
  timestamp: number;
119
+ hlc: Hlc;
94
120
  rev: number;
95
121
  checkpoint?: import("./struct.js").CheckpointEntry;
96
122
  invalidation?: InvalidationEntry;
97
123
  };
98
124
  tailPath: ChainPath<LogEntry<TAction>>;
99
125
  }>;
100
- /** Adds a checkpoint to the log. */
101
- addCheckpoint(pendings: ActionRev[], rev: number, timestamp?: number): Promise<{
126
+ /** Adds a checkpoint to the log, under the write's {@link EntryTimes}. */
127
+ addCheckpoint(pendings: ActionRev[], rev: number, times: EntryTimes): Promise<{
102
128
  entry: {
103
129
  timestamp: number;
130
+ hlc: Hlc;
104
131
  rev: number;
105
132
  checkpoint: {
106
133
  pendings: ActionRev[];
@@ -113,9 +140,10 @@ export declare class Log<TAction> {
113
140
  * invalid by dispute. The committed history is never rewritten — this is a new, append-only,
114
141
  * prior-hash-linked entry, recoverable on sync exactly like a commit. The caller has already
115
142
  * verified `resolution` is a challenger-wins certificate and computed the per-block `reverted`
116
- * targets (the new revisions restoring the as-if-`T_inv`-absent content).
143
+ * targets (the new revisions restoring the as-if-`T_inv`-absent content). The entry carries the
144
+ * applying node's {@link EntryTimes} like every other entry kind.
117
145
  */
118
- addInvalidation(invalidatedActionId: ActionId, invalidatedRev: number, resolution: DisputeResolutionProof, reverted: ReadonlyArray<RevertedBlock>, rev: number, cascadeRoot?: ActionId, timestamp?: number): Promise<{
146
+ addInvalidation(invalidatedActionId: ActionId, invalidatedRev: number, resolution: DisputeResolutionProof, reverted: ReadonlyArray<RevertedBlock>, rev: number, options: AddInvalidationOptions): Promise<{
119
147
  entry: LogEntry<TAction>;
120
148
  tailPath: ChainPath<LogEntry<TAction>>;
121
149
  }>;