1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// ░▀█▀░█▀▀░█▀█░█▀▄░█▀█░█▀▀░█░░░█▀▀
// ░░█░░▀▀█░█░█░█▀▄░█▀█░█░░░█░░░█▀▀
// ░░▀░░▀▀▀░▀▀▀░▀░▀░▀░▀░▀▀▀░▀▀▀░▀▀▀
//
// tsoracle — Distributed Timestamp Oracle
// https://www.tsoracle.rs
//
// Copyright (c) 2026 Prisma Risk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//! Binary wire codec for openraft RPC payloads and storage records.
//!
//! Every payload is encoded as `[version_byte | postcard(value)]`. The leading
//! byte lets us evolve the wire format without an explicit migration when both
//! sides of an upgrade run mixed versions briefly.
//!
//! The framing itself lives in the shared [`tsoracle_codec`] crate, re-used
//! verbatim by the paxos toolkit. The version constants ([`MIN_READABLE_VERSION`],
//! [`MAX_READABLE_VERSION`], [`BASELINE_WRITE_VERSION`]) and the runtime
//! [`ActiveWriteVersion`] cell are owned here, so this toolkit's wire/on-disk
//! format versions independently of the others.
use Arc;
use ;
pub use ;
/// Oldest on-disk/wire format version this binary still ships a parser for.
/// The decode side (disk now, peer RPC in P3) accepts any version in the
/// inclusive range `[MIN_READABLE_VERSION, MAX_READABLE_VERSION]`. Under the
/// never-remove decoder policy this only ever stays put or moves down; it never
/// rises. Seeded at the feature baseline (4): the historical 1->2->3->4 bumps
/// were stop-the-world, so no pre-v4 on-disk data can rolling-coexist in any
/// cluster this feature touches. A future release may lower it (re-adding
/// older parsers) if a real pre-v4 rolling-read need ever surfaces.
pub const MIN_READABLE_VERSION: u8 = 4;
/// Newest on-disk/wire format version this binary has a parser for. Only ever
/// grows. Decode accepts `[MIN_READABLE_VERSION, MAX_READABLE_VERSION]`.
///
/// Today this is 6 (`BATCH_WRITE_VERSION`): a v6-capable binary can read the
/// v4 baseline layout, the v5 dense layout, and the v6 batch layout. A node
/// writes `BASELINE_WRITE_VERSION` (4) until a committed `SetFormatVersion`
/// activation advances the active write version through the all-members gate.
pub const MAX_READABLE_VERSION: u8 = 6;
// Compile-time guard: the readable range must be non-empty (`MIN <= MAX`) or
// every decode rejects every record. Catches a future inverted-constants edit
// at build time rather than at test runtime.
const _: = assert!;
/// Default active write version — the single version this node emits before any
/// activation barrier advances it. It is independent of [`MIN_READABLE_VERSION`]
/// and is the assumed version of any unframed legacy peer payload (P3). Held at
/// runtime in an [`ActiveWriteVersion`] cell that defaults to this value; the
/// cell only ever advances via a successful, committed activation apply (P5),
/// so in this release every framed record still leads with this byte.
pub const BASELINE_WRITE_VERSION: u8 = 4;
/// The write version that introduces the `AdvanceDense` log command and the
/// dense fields in the state-machine snapshot. A leader must not append an
/// `AdvanceDense` entry (and `GetSeq` is refused) until the active write
/// version has been activated to at least this value through the all-members
/// gate — otherwise an older member could receive an entry it cannot decode.
pub const DENSE_WRITE_VERSION: u8 = 5;
/// The write version that introduces the `AdvanceDenseBatch` log command. The
/// state-machine snapshot shape is unchanged from `DENSE_WRITE_VERSION` — this
/// version gates which *commands* may be appended, not the snapshot layout, so
/// a v6 snapshot is byte-identical to a v5 one.
pub const BATCH_WRITE_VERSION: u8 = 6;
/// Process-shared, runtime-mutable active write version.
///
/// A thin newtype over `Arc<AtomicU8>` so the three writers — the log store
/// (stamps appended records), the state machine (stamps snapshots), and the peer
/// RPC sender (P3) — share **one** cell, the single source of truth for "the
/// version this node currently emits". Constructed once at bootstrap, seeded by
/// [`recover_active_write_version`], and cloned (cheap refcount bump) into each
/// writer. It is mutated only by a successful, committed activation apply (P5);
/// in this release it never leaves [`BASELINE_WRITE_VERSION`].
///
/// There is deliberately no persisted copy of this value. The state machine
/// reaches storage only through its opaque snapshot store and cannot write the
/// log store's meta column family, and a separate non-synced counter would
/// reintroduce the barrier-seq durability hazard. Durability comes from the raft
/// log (a committed `SetFormatVersion` entry is fsynced and re-applied
/// deterministically on restart) and the snapshot's leading frame byte; recovery
/// re-seeds the cell from that durable evidence via [`recover_active_write_version`].
;
/// Compute the bootstrap seed for the [`ActiveWriteVersion`] cell from
/// fsync-durable lower bounds.
///
/// Returns `max(BASELINE_WRITE_VERSION, snapshot_leading_byte?,
/// highest_log_record_byte?)` when every present input is within the readable
/// range `[MIN_READABLE_VERSION, MAX_READABLE_VERSION]`, and
/// [`CodecError::VersionUnsupported`] when any present input exceeds
/// `MAX_READABLE_VERSION`. Both inputs are `Option` because a fresh store has
/// neither a persisted snapshot nor any log record. Taking the max of
/// durably-*written* evidence is monotone and safe: a rejected or no-op
/// activation never caused any record to be written at the higher version, so it
/// cannot be resurrected here, and a node never recovers a version below what it
/// actually emitted.
///
/// The upper-bound check matters when this binary is older than the on-disk
/// data — for example a rolling-restart that downgrades a node to a release
/// whose [`MAX_READABLE_VERSION`] is lower than a leading byte already stamped
/// in the log or snapshot by a newer release. Without the check the cell would
/// be seeded to a value the decode side cannot handle, and the writers would
/// stamp every new record at that unreadable version (a persistent availability
/// failure). Failing the recovery fast surfaces the version mismatch at the
/// boundary between durable bytes and the in-memory cell, instead of silently
/// poisoning later reads.
///
/// This deliberately does NOT read any meta-CF counter and does NOT inspect the
/// mere presence of a committed `SetFormatVersion` log entry. Durability of an
/// activation is the raft log itself (the entry is fsynced and re-applied
/// deterministically on restart, re-establishing the cell through P5's apply) and
/// the snapshot's leading frame byte (snapshots are encoded at the active write
/// version, carrying it across log purge). This mirrors the project's barrier-seq
/// rule: a safety-critical recovery value derives from fsynced written state, not
/// a non-synced side counter or unapplied log presence.