tigerbeetle-node 0.8.1 → 0.9.143

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 (83) hide show
  1. package/README.md +584 -184
  2. package/dist/benchmark.js +59 -51
  3. package/dist/benchmark.js.map +1 -1
  4. package/dist/bin/aarch64-linux-gnu/client.node +0 -0
  5. package/dist/bin/aarch64-linux-musl/client.node +0 -0
  6. package/dist/bin/aarch64-macos/client.node +0 -0
  7. package/dist/bin/x86_64-linux-gnu/client.node +0 -0
  8. package/dist/bin/x86_64-linux-musl/client.node +0 -0
  9. package/dist/bin/x86_64-macos/client.node +0 -0
  10. package/dist/bin/x86_64-windows/client.node +0 -0
  11. package/dist/bindings.d.ts +141 -0
  12. package/dist/bindings.js +112 -0
  13. package/dist/bindings.js.map +1 -0
  14. package/dist/index.d.ts +2 -125
  15. package/dist/index.js +51 -101
  16. package/dist/index.js.map +1 -1
  17. package/dist/test.js +69 -55
  18. package/dist/test.js.map +1 -1
  19. package/package-lock.json +26 -0
  20. package/package.json +17 -28
  21. package/src/benchmark.ts +58 -49
  22. package/src/bindings.ts +631 -0
  23. package/src/index.ts +71 -163
  24. package/src/node.zig +169 -148
  25. package/src/test.ts +71 -57
  26. package/src/translate.zig +19 -36
  27. package/.yarn/releases/yarn-berry.cjs +0 -55
  28. package/.yarnrc.yml +0 -1
  29. package/scripts/download_node_headers.sh +0 -25
  30. package/scripts/postinstall.sh +0 -6
  31. package/src/tigerbeetle/scripts/benchmark.bat +0 -46
  32. package/src/tigerbeetle/scripts/benchmark.sh +0 -55
  33. package/src/tigerbeetle/scripts/install.sh +0 -6
  34. package/src/tigerbeetle/scripts/install_zig.bat +0 -109
  35. package/src/tigerbeetle/scripts/install_zig.sh +0 -84
  36. package/src/tigerbeetle/scripts/lint.zig +0 -199
  37. package/src/tigerbeetle/scripts/upgrade_ubuntu_kernel.sh +0 -39
  38. package/src/tigerbeetle/scripts/vopr.bat +0 -48
  39. package/src/tigerbeetle/scripts/vopr.sh +0 -33
  40. package/src/tigerbeetle/scripts/vr_state_enumerate +0 -46
  41. package/src/tigerbeetle/src/benchmark.zig +0 -290
  42. package/src/tigerbeetle/src/cli.zig +0 -244
  43. package/src/tigerbeetle/src/config.zig +0 -239
  44. package/src/tigerbeetle/src/demo.zig +0 -125
  45. package/src/tigerbeetle/src/demo_01_create_accounts.zig +0 -35
  46. package/src/tigerbeetle/src/demo_02_lookup_accounts.zig +0 -7
  47. package/src/tigerbeetle/src/demo_03_create_transfers.zig +0 -24
  48. package/src/tigerbeetle/src/demo_04_create_pending_transfers.zig +0 -61
  49. package/src/tigerbeetle/src/demo_05_post_pending_transfers.zig +0 -37
  50. package/src/tigerbeetle/src/demo_06_void_pending_transfers.zig +0 -24
  51. package/src/tigerbeetle/src/demo_07_lookup_transfers.zig +0 -7
  52. package/src/tigerbeetle/src/fifo.zig +0 -104
  53. package/src/tigerbeetle/src/io/benchmark.zig +0 -213
  54. package/src/tigerbeetle/src/io/darwin.zig +0 -793
  55. package/src/tigerbeetle/src/io/linux.zig +0 -1038
  56. package/src/tigerbeetle/src/io/test.zig +0 -643
  57. package/src/tigerbeetle/src/io/windows.zig +0 -1161
  58. package/src/tigerbeetle/src/io.zig +0 -34
  59. package/src/tigerbeetle/src/main.zig +0 -144
  60. package/src/tigerbeetle/src/message_bus.zig +0 -1000
  61. package/src/tigerbeetle/src/message_pool.zig +0 -142
  62. package/src/tigerbeetle/src/ring_buffer.zig +0 -289
  63. package/src/tigerbeetle/src/simulator.zig +0 -417
  64. package/src/tigerbeetle/src/state_machine.zig +0 -2470
  65. package/src/tigerbeetle/src/storage.zig +0 -308
  66. package/src/tigerbeetle/src/test/cluster.zig +0 -351
  67. package/src/tigerbeetle/src/test/message_bus.zig +0 -93
  68. package/src/tigerbeetle/src/test/network.zig +0 -179
  69. package/src/tigerbeetle/src/test/packet_simulator.zig +0 -387
  70. package/src/tigerbeetle/src/test/state_checker.zig +0 -145
  71. package/src/tigerbeetle/src/test/state_machine.zig +0 -76
  72. package/src/tigerbeetle/src/test/storage.zig +0 -438
  73. package/src/tigerbeetle/src/test/time.zig +0 -84
  74. package/src/tigerbeetle/src/tigerbeetle.zig +0 -222
  75. package/src/tigerbeetle/src/time.zig +0 -113
  76. package/src/tigerbeetle/src/unit_tests.zig +0 -14
  77. package/src/tigerbeetle/src/vsr/client.zig +0 -505
  78. package/src/tigerbeetle/src/vsr/clock.zig +0 -812
  79. package/src/tigerbeetle/src/vsr/journal.zig +0 -2293
  80. package/src/tigerbeetle/src/vsr/marzullo.zig +0 -309
  81. package/src/tigerbeetle/src/vsr/replica.zig +0 -5015
  82. package/src/tigerbeetle/src/vsr.zig +0 -1017
  83. package/yarn.lock +0 -42
@@ -1,1017 +0,0 @@
1
- const std = @import("std");
2
- const math = std.math;
3
- const Allocator = std.mem.Allocator;
4
- const assert = std.debug.assert;
5
- const log = std.log.scoped(.vsr);
6
-
7
- const config = @import("config.zig");
8
-
9
- /// The version of our Viewstamped Replication protocol in use, including customizations.
10
- /// For backwards compatibility through breaking changes (e.g. upgrading checksums/ciphers).
11
- pub const Version: u8 = 0;
12
-
13
- pub const Replica = @import("vsr/replica.zig").Replica;
14
- pub const Status = @import("vsr/replica.zig").Status;
15
- pub const Client = @import("vsr/client.zig").Client;
16
- pub const Clock = @import("vsr/clock.zig").Clock;
17
- pub const Journal = @import("vsr/journal.zig").Journal;
18
- pub const SlotRange = @import("vsr/journal.zig").SlotRange;
19
-
20
- pub const ProcessType = enum { replica, client };
21
-
22
- /// Viewstamped Replication protocol commands:
23
- pub const Command = enum(u8) {
24
- reserved,
25
-
26
- ping,
27
- pong,
28
-
29
- request,
30
- prepare,
31
- prepare_ok,
32
- reply,
33
- commit,
34
-
35
- start_view_change,
36
- do_view_change,
37
- start_view,
38
-
39
- recovery,
40
- recovery_response,
41
-
42
- request_start_view,
43
- request_headers,
44
- request_prepare,
45
- headers,
46
- nack_prepare,
47
-
48
- eviction,
49
- };
50
-
51
- /// This type exists to avoid making the Header type dependant on the state
52
- /// machine used, which would cause awkward circular type dependencies.
53
- pub const Operation = enum(u8) {
54
- /// Operations reserved by VR protocol (for all state machines):
55
- /// The value 0 is reserved to prevent a spurious zero from being interpreted as an operation.
56
- reserved = 0,
57
- /// The value 1 is reserved to initialize the cluster.
58
- root = 1,
59
- /// The value 2 is reserved to register a client session with the cluster.
60
- register = 2,
61
-
62
- /// Operations exported by the state machine (all other values are free):
63
- _,
64
-
65
- pub fn from(comptime StateMachine: type, op: StateMachine.Operation) Operation {
66
- check_state_machine_operations(StateMachine.Operation);
67
- return @intToEnum(Operation, @enumToInt(op));
68
- }
69
-
70
- pub fn cast(self: Operation, comptime StateMachine: type) StateMachine.Operation {
71
- check_state_machine_operations(StateMachine.Operation);
72
- return @intToEnum(StateMachine.Operation, @enumToInt(self));
73
- }
74
-
75
- fn check_state_machine_operations(comptime Op: type) void {
76
- if (!@hasField(Op, "reserved") or std.meta.fieldInfo(Op, .reserved).value != 0) {
77
- @compileError("StateMachine.Operation must have a 'reserved' field with value 0");
78
- }
79
- if (!@hasField(Op, "root") or std.meta.fieldInfo(Op, .root).value != 1) {
80
- @compileError("StateMachine.Operation must have an 'root' field with value 1");
81
- }
82
- if (!@hasField(Op, "register") or std.meta.fieldInfo(Op, .register).value != 2) {
83
- @compileError("StateMachine.Operation must have a 'register' field with value 2");
84
- }
85
- }
86
- };
87
-
88
- /// Network message and journal entry header:
89
- /// We reuse the same header for both so that prepare messages from the leader can simply be
90
- /// journalled as is by the followers without requiring any further modification.
91
- /// TODO Move from packed struct to extern struct for C ABI:
92
- pub const Header = packed struct {
93
- comptime {
94
- assert(@sizeOf(Header) == 128);
95
- }
96
- /// A checksum covering only the remainder of this header.
97
- /// This allows the header to be trusted without having to recv() or read() the associated body.
98
- /// This checksum is enough to uniquely identify a network message or journal entry.
99
- checksum: u128 = 0,
100
-
101
- /// A checksum covering only the associated body after this header.
102
- checksum_body: u128 = 0,
103
-
104
- /// A backpointer to the previous request or prepare checksum for hash chain verification.
105
- /// This provides a cryptographic guarantee for linearizability:
106
- /// 1. across our distributed log of prepares, and
107
- /// 2. across a client's requests and our replies.
108
- /// This may also be used as the initialization vector for AEAD encryption at rest, provided
109
- /// that the leader ratchets the encryption key every view change to ensure that prepares
110
- /// reordered through a view change never repeat the same IV for the same encryption key.
111
- parent: u128 = 0,
112
-
113
- /// Each client process generates a unique, random and ephemeral client ID at initialization.
114
- /// The client ID identifies connections made by the client to the cluster for the sake of
115
- /// routing messages back to the client.
116
- ///
117
- /// With the client ID in hand, the client then registers a monotonically increasing session
118
- /// number (committed through the cluster) to allow the client's session to be evicted safely
119
- /// from the client table if too many concurrent clients cause the client table to overflow.
120
- /// The monotonically increasing session number prevents duplicate client requests from being
121
- /// replayed.
122
- ///
123
- /// The problem of routing is therefore solved by the 128-bit client ID, and the problem of
124
- /// detecting whether a session has been evicted is solved by the session number.
125
- client: u128 = 0,
126
-
127
- /// The checksum of the message to which this message refers, or a unique recovery nonce.
128
- ///
129
- /// We use this cryptographic context in various ways, for example:
130
- ///
131
- /// * A `request` sets this to the client's session number.
132
- /// * A `prepare` sets this to the checksum of the client's request.
133
- /// * A `prepare_ok` sets this to the checksum of the prepare being acked.
134
- /// * A `commit` sets this to the checksum of the latest committed prepare.
135
- /// * A `request_prepare` sets this to the checksum of the prepare being requested.
136
- /// * A `nack_prepare` sets this to the checksum of the prepare being nacked.
137
- /// * A `recovery` and `recovery_response` sets this to the nonce.
138
- ///
139
- /// This allows for cryptographic guarantees beyond request, op, and commit numbers, which have
140
- /// low entropy and may otherwise collide in the event of any correctness bugs.
141
- context: u128 = 0,
142
-
143
- /// Each request is given a number by the client and later requests must have larger numbers
144
- /// than earlier ones. The request number is used by the replicas to avoid running requests more
145
- /// than once; it is also used by the client to discard duplicate responses to its requests.
146
- /// A client is allowed to have at most one request inflight at a time.
147
- request: u32 = 0,
148
-
149
- /// The cluster number binds intention into the header, so that a client or replica can indicate
150
- /// the cluster it believes it is speaking to, instead of accidentally talking to the wrong
151
- /// cluster (for example, staging vs production).
152
- cluster: u32,
153
-
154
- /// The cluster reconfiguration epoch number (for future use).
155
- epoch: u32 = 0,
156
-
157
- /// Every message sent from one replica to another contains the sending replica's current view.
158
- /// A `u32` allows for a minimum lifetime of 136 years at a rate of one view change per second.
159
- view: u32 = 0,
160
-
161
- /// The op number of the latest prepare that may or may not yet be committed. Uncommitted ops
162
- /// may be replaced by different ops if they do not survive through a view change.
163
- op: u64 = 0,
164
-
165
- /// The commit number of the latest committed prepare. Committed ops are immutable.
166
- commit: u64 = 0,
167
-
168
- /// This field is used in various ways:
169
- ///
170
- /// * A `prepare` sets this to the leader's state machine `prepare_timestamp`.
171
- /// For `create_accounts` and `create_transfers` this is the batch's highest timestamp.
172
- /// * A `do_view_change` sets this to the latest normal view number.
173
- /// * A `pong` sets this to the sender's wall clock value.
174
- /// * A `request_prepare` sets this to `1` when `context` is set to a checksum, and `0`
175
- /// otherwise.
176
- timestamp: u64 = 0,
177
-
178
- /// The size of the Header structure (always), plus any associated body.
179
- size: u32 = @sizeOf(Header),
180
-
181
- /// The index of the replica in the cluster configuration array that authored this message.
182
- /// This identifies only the ultimate author because messages may be forwarded amongst replicas.
183
- replica: u8 = 0,
184
-
185
- /// The Viewstamped Replication protocol command for this message.
186
- command: Command,
187
-
188
- /// The state machine operation to apply.
189
- operation: Operation = .reserved,
190
-
191
- /// The version of the protocol implementation that originated this message.
192
- version: u8 = Version,
193
-
194
- pub fn calculate_checksum(self: *const Header) u128 {
195
- const checksum_size = @sizeOf(@TypeOf(self.checksum));
196
- assert(checksum_size == 16);
197
- var target: [32]u8 = undefined;
198
- std.crypto.hash.Blake3.hash(std.mem.asBytes(self)[checksum_size..], target[0..], .{});
199
- return @bitCast(u128, target[0..checksum_size].*);
200
- }
201
-
202
- pub fn calculate_checksum_body(self: *const Header, body: []const u8) u128 {
203
- assert(self.size == @sizeOf(Header) + body.len);
204
- const checksum_size = @sizeOf(@TypeOf(self.checksum_body));
205
- assert(checksum_size == 16);
206
- var target: [32]u8 = undefined;
207
- std.crypto.hash.Blake3.hash(body[0..], target[0..], .{});
208
- return @bitCast(u128, target[0..checksum_size].*);
209
- }
210
-
211
- /// This must be called only after set_checksum_body() so that checksum_body is also covered:
212
- pub fn set_checksum(self: *Header) void {
213
- self.checksum = self.calculate_checksum();
214
- }
215
-
216
- pub fn set_checksum_body(self: *Header, body: []const u8) void {
217
- self.checksum_body = self.calculate_checksum_body(body);
218
- }
219
-
220
- pub fn valid_checksum(self: *const Header) bool {
221
- return self.checksum == self.calculate_checksum();
222
- }
223
-
224
- pub fn valid_checksum_body(self: *const Header, body: []const u8) bool {
225
- return self.checksum_body == self.calculate_checksum_body(body);
226
- }
227
-
228
- /// Returns null if all fields are set correctly according to the command, or else a warning.
229
- /// This does not verify that checksum is valid, and expects that this has already been done.
230
- pub fn invalid(self: *const Header) ?[]const u8 {
231
- if (self.version != Version) return "version != Version";
232
- if (self.size < @sizeOf(Header)) return "size < @sizeOf(Header)";
233
- if (self.epoch != 0) return "epoch != 0";
234
- return switch (self.command) {
235
- .reserved => self.invalid_reserved(),
236
- .ping => self.invalid_ping(),
237
- .pong => self.invalid_pong(),
238
- .request => self.invalid_request(),
239
- .prepare => self.invalid_prepare(),
240
- .prepare_ok => self.invalid_prepare_ok(),
241
- .reply => self.invalid_reply(),
242
- .commit => self.invalid_commit(),
243
- .start_view_change => self.invalid_start_view_change(),
244
- .do_view_change => self.invalid_do_view_change(),
245
- .start_view => self.invalid_start_view(),
246
- .recovery => self.invalid_recovery(),
247
- .recovery_response => self.invalid_recovery_response(),
248
- .request_start_view => self.invalid_request_start_view(),
249
- .request_headers => self.invalid_request_headers(),
250
- .request_prepare => self.invalid_request_prepare(),
251
- .headers => self.invalid_headers(),
252
- .nack_prepare => self.invalid_nack_prepare(),
253
- .eviction => self.invalid_eviction(),
254
- };
255
- }
256
-
257
- fn invalid_reserved(self: *const Header) ?[]const u8 {
258
- assert(self.command == .reserved);
259
- if (self.parent != 0) return "parent != 0";
260
- if (self.client != 0) return "client != 0";
261
- if (self.context != 0) return "context != 0";
262
- if (self.request != 0) return "request != 0";
263
- if (self.view != 0) return "view != 0";
264
- if (self.commit != 0) return "commit != 0";
265
- if (self.timestamp != 0) return "timestamp != 0";
266
- if (self.replica != 0) return "replica != 0";
267
- if (self.operation != .reserved) return "operation != .reserved";
268
- return null;
269
- }
270
-
271
- fn invalid_ping(self: *const Header) ?[]const u8 {
272
- assert(self.command == .ping);
273
- if (self.parent != 0) return "parent != 0";
274
- if (self.context != 0) return "context != 0";
275
- if (self.request != 0) return "request != 0";
276
- if (self.commit != 0) return "commit != 0";
277
- if (self.timestamp != 0) return "timestamp != 0";
278
- if (self.operation != .reserved) return "operation != .reserved";
279
- return null;
280
- }
281
-
282
- fn invalid_pong(self: *const Header) ?[]const u8 {
283
- assert(self.command == .pong);
284
- if (self.parent != 0) return "parent != 0";
285
- if (self.client != 0) return "client != 0";
286
- if (self.context != 0) return "context != 0";
287
- if (self.request != 0) return "request != 0";
288
- if (self.commit != 0) return "commit != 0";
289
- if (self.operation != .reserved) return "operation != .reserved";
290
- return null;
291
- }
292
-
293
- fn invalid_request(self: *const Header) ?[]const u8 {
294
- assert(self.command == .request);
295
- if (self.client == 0) return "client == 0";
296
- if (self.op != 0) return "op != 0";
297
- if (self.commit != 0) return "commit != 0";
298
- if (self.timestamp != 0) return "timestamp != 0";
299
- if (self.replica != 0) return "replica != 0";
300
- switch (self.operation) {
301
- .reserved => return "operation == .reserved",
302
- .root => return "operation == .root",
303
- .register => {
304
- // The first request a client makes must be to register with the cluster:
305
- if (self.parent != 0) return "parent != 0";
306
- if (self.context != 0) return "context != 0";
307
- if (self.request != 0) return "request != 0";
308
- // The .register operation carries no payload:
309
- if (self.size != @sizeOf(Header)) return "size != @sizeOf(Header)";
310
- },
311
- else => {
312
- // Thereafter, the client must provide the session number in the context:
313
- // These requests should set `parent` to the `checksum` of the previous reply.
314
- if (self.context == 0) return "context == 0";
315
- if (self.request == 0) return "request == 0";
316
- },
317
- }
318
- return null;
319
- }
320
-
321
- fn invalid_prepare(self: *const Header) ?[]const u8 {
322
- assert(self.command == .prepare);
323
- switch (self.operation) {
324
- .reserved => return "operation == .reserved",
325
- .root => {
326
- if (self.parent != 0) return "root: parent != 0";
327
- if (self.client != 0) return "root: client != 0";
328
- if (self.context != 0) return "root: context != 0";
329
- if (self.request != 0) return "root: request != 0";
330
- if (self.view != 0) return "root: view != 0";
331
- if (self.op != 0) return "root: op != 0";
332
- if (self.commit != 0) return "root: commit != 0";
333
- if (self.timestamp != 0) return "root: timestamp != 0";
334
- if (self.size != @sizeOf(Header)) return "root: size != @sizeOf(Header)";
335
- if (self.replica != 0) return "root: replica != 0";
336
- },
337
- else => {
338
- if (self.client == 0) return "client == 0";
339
- if (self.op == 0) return "op == 0";
340
- if (self.op <= self.commit) return "op <= commit";
341
- if (self.timestamp == 0) return "timestamp == 0";
342
- if (self.operation == .register) {
343
- // Client session numbers are replaced by the reference to the previous prepare.
344
- if (self.request != 0) return "request != 0";
345
- } else {
346
- // Client session numbers are replaced by the reference to the previous prepare.
347
- if (self.request == 0) return "request == 0";
348
- }
349
- },
350
- }
351
- return null;
352
- }
353
-
354
- fn invalid_prepare_ok(self: *const Header) ?[]const u8 {
355
- assert(self.command == .prepare_ok);
356
- if (self.size != @sizeOf(Header)) return "size != @sizeOf(Header)";
357
- switch (self.operation) {
358
- .reserved => return "operation == .reserved",
359
- .root => {
360
- if (self.parent != 0) return "root: parent != 0";
361
- if (self.client != 0) return "root: client != 0";
362
- if (self.context != 0) return "root: context != 0";
363
- if (self.request != 0) return "root: request != 0";
364
- if (self.view != 0) return "root: view != 0";
365
- if (self.op != 0) return "root: op != 0";
366
- if (self.commit != 0) return "root: commit != 0";
367
- if (self.timestamp != 0) return "root: timestamp != 0";
368
- if (self.replica != 0) return "root: replica != 0";
369
- },
370
- else => {
371
- if (self.client == 0) return "client == 0";
372
- if (self.op == 0) return "op == 0";
373
- if (self.op <= self.commit) return "op <= commit";
374
- if (self.operation == .register) {
375
- if (self.request != 0) return "request != 0";
376
- } else {
377
- if (self.request == 0) return "request == 0";
378
- }
379
- },
380
- }
381
- return null;
382
- }
383
-
384
- fn invalid_reply(self: *const Header) ?[]const u8 {
385
- assert(self.command == .reply);
386
- // Initialization within `client.zig` asserts that client `id` is greater than zero:
387
- if (self.client == 0) return "client == 0";
388
- if (self.context != 0) return "context != 0";
389
- if (self.op != self.commit) return "op != commit";
390
- if (self.timestamp != 0) return "timestamp != 0";
391
- if (self.operation == .register) {
392
- // In this context, the commit number is the newly registered session number.
393
- // The `0` commit number is reserved for cluster initialization.
394
- if (self.commit == 0) return "commit == 0";
395
- if (self.request != 0) return "request != 0";
396
- } else {
397
- if (self.commit == 0) return "commit == 0";
398
- if (self.request == 0) return "request == 0";
399
- }
400
- return null;
401
- }
402
-
403
- fn invalid_commit(self: *const Header) ?[]const u8 {
404
- assert(self.command == .commit);
405
- if (self.parent != 0) return "parent != 0";
406
- if (self.client != 0) return "client != 0";
407
- if (self.request != 0) return "request != 0";
408
- if (self.op != 0) return "op != 0";
409
- if (self.timestamp != 0) return "timestamp != 0";
410
- if (self.operation != .reserved) return "operation != .reserved";
411
- return null;
412
- }
413
-
414
- fn invalid_start_view_change(self: *const Header) ?[]const u8 {
415
- assert(self.command == .start_view_change);
416
- if (self.parent != 0) return "parent != 0";
417
- if (self.client != 0) return "client != 0";
418
- if (self.context != 0) return "context != 0";
419
- if (self.request != 0) return "request != 0";
420
- if (self.op != 0) return "op != 0";
421
- if (self.commit != 0) return "commit != 0";
422
- if (self.timestamp != 0) return "timestamp != 0";
423
- if (self.operation != .reserved) return "operation != .reserved";
424
- return null;
425
- }
426
-
427
- fn invalid_do_view_change(self: *const Header) ?[]const u8 {
428
- assert(self.command == .do_view_change);
429
- if (self.parent != 0) return "parent != 0";
430
- if (self.client != 0) return "client != 0";
431
- if (self.context != 0) return "context != 0";
432
- if (self.request != 0) return "request != 0";
433
- if (self.operation != .reserved) return "operation != .reserved";
434
- return null;
435
- }
436
-
437
- fn invalid_start_view(self: *const Header) ?[]const u8 {
438
- assert(self.command == .start_view);
439
- if (self.parent != 0) return "parent != 0";
440
- if (self.client != 0) return "client != 0";
441
- if (self.context != 0) return "context != 0";
442
- if (self.request != 0) return "request != 0";
443
- if (self.timestamp != 0) return "timestamp != 0";
444
- if (self.operation != .reserved) return "operation != .reserved";
445
- return null;
446
- }
447
-
448
- fn invalid_recovery(self: *const Header) ?[]const u8 {
449
- assert(self.command == .recovery);
450
- if (self.parent != 0) return "parent != 0";
451
- if (self.client != 0) return "client != 0";
452
- if (self.request != 0) return "request != 0";
453
- if (self.view != 0) return "view != 0";
454
- if (self.op != 0) return "op != 0";
455
- if (self.commit != 0) return "commit != 0";
456
- if (self.timestamp != 0) return "timestamp != 0";
457
- if (self.operation != .reserved) return "operation != .reserved";
458
- return null;
459
- }
460
-
461
- fn invalid_recovery_response(self: *const Header) ?[]const u8 {
462
- assert(self.command == .recovery_response);
463
- if (self.parent != 0) return "parent != 0";
464
- if (self.client != 0) return "client != 0";
465
- if (self.request != 0) return "request != 0";
466
- if (self.timestamp != 0) return "timestamp != 0";
467
- if (self.operation != .reserved) return "operation != .reserved";
468
- return null;
469
- }
470
-
471
- fn invalid_request_start_view(self: *const Header) ?[]const u8 {
472
- assert(self.command == .request_start_view);
473
- if (self.parent != 0) return "parent != 0";
474
- if (self.client != 0) return "client != 0";
475
- if (self.context != 0) return "context != 0";
476
- if (self.request != 0) return "request != 0";
477
- if (self.op != 0) return "op != 0";
478
- if (self.commit != 0) return "commit != 0";
479
- if (self.timestamp != 0) return "timestamp != 0";
480
- if (self.operation != .reserved) return "operation != .reserved";
481
- return null;
482
- }
483
-
484
- fn invalid_request_headers(self: *const Header) ?[]const u8 {
485
- assert(self.command == .request_headers);
486
- if (self.parent != 0) return "parent != 0";
487
- if (self.client != 0) return "client != 0";
488
- if (self.context != 0) return "context != 0";
489
- if (self.request != 0) return "request != 0";
490
- if (self.timestamp != 0) return "timestamp != 0";
491
- if (self.commit > self.op) return "op_min > op_max";
492
- if (self.operation != .reserved) return "operation != .reserved";
493
- return null;
494
- }
495
-
496
- fn invalid_request_prepare(self: *const Header) ?[]const u8 {
497
- assert(self.command == .request_prepare);
498
- if (self.parent != 0) return "parent != 0";
499
- if (self.client != 0) return "client != 0";
500
- if (self.request != 0) return "request != 0";
501
- if (self.commit != 0) return "commit != 0";
502
- switch (self.timestamp) {
503
- 0 => if (self.context != 0) return "context != 0",
504
- 1 => {}, // context is a checksum, which may be 0.
505
- else => return "timestamp > 1",
506
- }
507
- if (self.operation != .reserved) return "operation != .reserved";
508
- return null;
509
- }
510
-
511
- fn invalid_headers(self: *const Header) ?[]const u8 {
512
- assert(self.command == .headers);
513
- if (self.parent != 0) return "parent != 0";
514
- if (self.client != 0) return "client != 0";
515
- if (self.request != 0) return "request != 0";
516
- if (self.op != 0) return "op != 0";
517
- if (self.commit != 0) return "commit != 0";
518
- if (self.timestamp != 0) return "timestamp != 0";
519
- if (self.operation != .reserved) return "operation != .reserved";
520
- return null;
521
- }
522
-
523
- fn invalid_nack_prepare(self: *const Header) ?[]const u8 {
524
- assert(self.command == .nack_prepare);
525
- if (self.parent != 0) return "parent != 0";
526
- if (self.client != 0) return "client != 0";
527
- if (self.request != 0) return "request != 0";
528
- if (self.commit != 0) return "commit != 0";
529
- if (self.timestamp != 0) return "timestamp != 0";
530
- if (self.operation != .reserved) return "operation != .reserved";
531
- return null;
532
- }
533
-
534
- fn invalid_eviction(self: *const Header) ?[]const u8 {
535
- assert(self.command == .eviction);
536
- if (self.parent != 0) return "parent != 0";
537
- if (self.context != 0) return "context != 0";
538
- if (self.request != 0) return "request != 0";
539
- if (self.op != 0) return "op != 0";
540
- if (self.commit != 0) return "commit != 0";
541
- if (self.timestamp != 0) return "timestamp != 0";
542
- if (self.operation != .reserved) return "operation != .reserved";
543
- return null;
544
- }
545
-
546
- /// Returns whether the immediate sender is a replica or client (if this can be determined).
547
- /// Some commands such as .request or .prepare may be forwarded on to other replicas so that
548
- /// Header.replica or Header.client only identifies the ultimate origin, not the latest peer.
549
- pub fn peer_type(self: *const Header) enum { unknown, replica, client } {
550
- switch (self.command) {
551
- .reserved => unreachable,
552
- // These messages cannot always identify the peer as they may be forwarded:
553
- .request => switch (self.operation) {
554
- // However, we do not forward the first .register request sent by a client:
555
- .register => return .client,
556
- else => return .unknown,
557
- },
558
- .prepare => return .unknown,
559
- // These messages identify the peer as either a replica or a client:
560
- // TODO Assert that pong responses from a replica do not echo the pinging client's ID.
561
- .ping, .pong => {
562
- if (self.client > 0) {
563
- assert(self.replica == 0);
564
- return .client;
565
- } else {
566
- return .replica;
567
- }
568
- },
569
- // All other messages identify the peer as a replica:
570
- else => return .replica,
571
- }
572
- }
573
-
574
- pub fn reserved(cluster: u32, slot: u64) Header {
575
- assert(slot < config.journal_slot_count);
576
-
577
- var header = Header{
578
- .command = .reserved,
579
- .cluster = cluster,
580
- .op = slot,
581
- };
582
- header.set_checksum_body(&[0]u8{});
583
- header.set_checksum();
584
- assert(header.invalid() == null);
585
- return header;
586
- }
587
-
588
- pub fn root_prepare(cluster: u32) Header {
589
- var header = Header{
590
- .cluster = cluster,
591
- .size = @sizeOf(Header),
592
- .command = .prepare,
593
- .operation = .root,
594
- };
595
- header.set_checksum_body(&[0]u8{});
596
- header.set_checksum();
597
- assert(header.invalid() == null);
598
- return header;
599
- }
600
- };
601
-
602
- pub const Timeout = struct {
603
- name: []const u8,
604
- id: u128,
605
- after: u64,
606
- attempts: u8 = 0,
607
- rtt: u64 = config.rtt_ticks,
608
- rtt_multiple: u8 = config.rtt_multiple,
609
- ticks: u64 = 0,
610
- ticking: bool = false,
611
-
612
- /// Increments the attempts counter and resets the timeout with exponential backoff and jitter.
613
- /// Allows the attempts counter to wrap from time to time.
614
- /// The overflow period is kept short to surface any related bugs sooner rather than later.
615
- /// We do not saturate the counter as this would cause round-robin retries to get stuck.
616
- pub fn backoff(self: *Timeout, random: std.rand.Random) void {
617
- assert(self.ticking);
618
-
619
- self.ticks = 0;
620
- self.attempts +%= 1;
621
-
622
- log.debug("{}: {s} backing off", .{ self.id, self.name });
623
- self.set_after_for_rtt_and_attempts(random);
624
- }
625
-
626
- /// It's important to check that when fired() is acted on that the timeout is stopped/started,
627
- /// otherwise further ticks around the event loop may trigger a thundering herd of messages.
628
- pub fn fired(self: *Timeout) bool {
629
- if (self.ticking and self.ticks >= self.after) {
630
- log.debug("{}: {s} fired", .{ self.id, self.name });
631
- if (self.ticks > self.after) {
632
- log.err("{}: {s} is firing every tick", .{ self.id, self.name });
633
- @panic("timeout was not reset correctly");
634
- }
635
- return true;
636
- } else {
637
- return false;
638
- }
639
- }
640
-
641
- pub fn reset(self: *Timeout) void {
642
- self.attempts = 0;
643
- self.ticks = 0;
644
- assert(self.ticking);
645
- // TODO Use self.prng to adjust for rtt and attempts.
646
- log.debug("{}: {s} reset", .{ self.id, self.name });
647
- }
648
-
649
- /// Sets the value of `after` as a function of `rtt` and `attempts`.
650
- /// Adds exponential backoff and jitter.
651
- /// May be called only after a timeout has been stopped or reset, to prevent backward jumps.
652
- pub fn set_after_for_rtt_and_attempts(self: *Timeout, random: std.rand.Random) void {
653
- // If `after` is reduced by this function to less than `ticks`, then `fired()` will panic:
654
- assert(self.ticks == 0);
655
- assert(self.rtt > 0);
656
-
657
- const after = (self.rtt * self.rtt_multiple) + exponential_backoff_with_jitter(
658
- random,
659
- config.backoff_min_ticks,
660
- config.backoff_max_ticks,
661
- self.attempts,
662
- );
663
-
664
- // TODO Clamp `after` to min/max tick bounds for timeout.
665
-
666
- log.debug("{}: {s} after={}..{} (rtt={} min={} max={} attempts={})", .{
667
- self.id,
668
- self.name,
669
- self.after,
670
- after,
671
- self.rtt,
672
- config.backoff_min_ticks,
673
- config.backoff_max_ticks,
674
- self.attempts,
675
- });
676
-
677
- self.after = after;
678
- assert(self.after > 0);
679
- }
680
-
681
- pub fn set_rtt(self: *Timeout, rtt_ticks: u64) void {
682
- assert(self.rtt > 0);
683
- assert(rtt_ticks > 0);
684
-
685
- log.debug("{}: {s} rtt={}..{}", .{
686
- self.id,
687
- self.name,
688
- self.rtt,
689
- rtt_ticks,
690
- });
691
-
692
- self.rtt = rtt_ticks;
693
- }
694
-
695
- pub fn start(self: *Timeout) void {
696
- self.attempts = 0;
697
- self.ticks = 0;
698
- self.ticking = true;
699
- // TODO Use self.prng to adjust for rtt and attempts.
700
- log.debug("{}: {s} started", .{ self.id, self.name });
701
- }
702
-
703
- pub fn stop(self: *Timeout) void {
704
- self.attempts = 0;
705
- self.ticks = 0;
706
- self.ticking = false;
707
- log.debug("{}: {s} stopped", .{ self.id, self.name });
708
- }
709
-
710
- pub fn tick(self: *Timeout) void {
711
- if (self.ticking) self.ticks += 1;
712
- }
713
- };
714
-
715
- /// Calculates exponential backoff with jitter to prevent cascading failure due to thundering herds.
716
- pub fn exponential_backoff_with_jitter(
717
- random: std.rand.Random,
718
- min: u64,
719
- max: u64,
720
- attempt: u64,
721
- ) u64 {
722
- const range = max - min;
723
- assert(range > 0);
724
-
725
- // Do not use `@truncate(u6, attempt)` since that only discards the high bits:
726
- // We want a saturating exponent here instead.
727
- const exponent = @intCast(u6, std.math.min(std.math.maxInt(u6), attempt));
728
-
729
- // A "1" shifted left gives any power of two:
730
- // 1<<0 = 1, 1<<1 = 2, 1<<2 = 4, 1<<3 = 8
731
- const power = std.math.shlExact(u128, 1, exponent) catch unreachable; // Do not truncate.
732
-
733
- // Ensure that `backoff` is calculated correctly when min is 0, taking `std.math.max(1, min)`.
734
- // Otherwise, the final result will always be 0. This was an actual bug we encountered.
735
- const min_non_zero = std.math.max(1, min);
736
- assert(min_non_zero > 0);
737
- assert(power > 0);
738
-
739
- // Calculate the capped exponential backoff component, `min(range, min * 2 ^ attempt)`:
740
- const backoff = std.math.min(range, min_non_zero * power);
741
- const jitter = random.uintAtMostBiased(u64, backoff);
742
-
743
- const result = @intCast(u64, min + jitter);
744
- assert(result >= min);
745
- assert(result <= max);
746
-
747
- return result;
748
- }
749
-
750
- test "exponential_backoff_with_jitter" {
751
- const testing = std.testing;
752
-
753
- var prng = std.rand.DefaultPrng.init(0);
754
- const random = prng.random();
755
-
756
- const attempts = 1000;
757
- const max: u64 = std.math.maxInt(u64);
758
- const min = max - attempts;
759
-
760
- var attempt = max - attempts;
761
- while (attempt < max) : (attempt += 1) {
762
- const ebwj = exponential_backoff_with_jitter(random, min, max, attempt);
763
- try testing.expect(ebwj >= min);
764
- try testing.expect(ebwj <= max);
765
- }
766
- }
767
-
768
- /// Returns An array containing the remote or local addresses of each of the 2f + 1 replicas:
769
- /// Unlike the VRR paper, we do not sort the array but leave the order explicitly to the user.
770
- /// There are several advantages to this:
771
- /// * The operator may deploy a cluster with proximity in mind since replication follows order.
772
- /// * A replica's IP address may be changed without reconfiguration.
773
- /// This does require that the user specify the same order to all replicas.
774
- /// The caller owns the memory of the returned slice of addresses.
775
- pub fn parse_addresses(allocator: std.mem.Allocator, raw: []const u8) ![]std.net.Address {
776
- return parse_addresses_limit(allocator, raw, config.replicas_max);
777
- }
778
-
779
- fn parse_addresses_limit(allocator: std.mem.Allocator, raw: []const u8, max: usize) ![]std.net.Address {
780
- var addresses = try allocator.alloc(std.net.Address, max);
781
- errdefer allocator.free(addresses);
782
-
783
- var index: usize = 0;
784
- var comma_iterator = std.mem.split(u8, raw, ",");
785
- while (comma_iterator.next()) |raw_address| : (index += 1) {
786
- if (raw_address.len == 0) return error.AddressHasTrailingComma;
787
- if (index == max) return error.AddressLimitExceeded;
788
-
789
- var colon_iterator = std.mem.split(u8, raw_address, ":");
790
- // The split iterator will always return non-null once, even if the delimiter is not found:
791
- const raw_ipv4 = colon_iterator.next().?;
792
-
793
- if (colon_iterator.next()) |raw_port| {
794
- if (colon_iterator.next() != null) return error.AddressHasMoreThanOneColon;
795
-
796
- const port = std.fmt.parseUnsigned(u16, raw_port, 10) catch |err| switch (err) {
797
- error.Overflow => return error.PortOverflow,
798
- error.InvalidCharacter => return error.PortInvalid,
799
- };
800
- addresses[index] = std.net.Address.parseIp4(raw_ipv4, port) catch {
801
- return error.AddressInvalid;
802
- };
803
- } else {
804
- // There was no colon in the address so there are now two cases:
805
- // 1. an IPv4 address with the default port, or
806
- // 2. a port with the default IPv4 address.
807
-
808
- // Let's try parsing as a port first:
809
- if (std.fmt.parseUnsigned(u16, raw_address, 10)) |port| {
810
- addresses[index] = std.net.Address.parseIp4(config.address, port) catch unreachable;
811
- } else |err| switch (err) {
812
- error.Overflow => return error.PortOverflow,
813
- error.InvalidCharacter => {
814
- // Something was not a digit, let's try parsing as an IPv4 instead:
815
- addresses[index] = std.net.Address.parseIp4(raw_address, config.port) catch {
816
- return error.AddressInvalid;
817
- };
818
- },
819
- }
820
- }
821
- }
822
- return addresses[0..index];
823
- }
824
-
825
- test "parse_addresses" {
826
- const vectors_positive = &[_]struct {
827
- raw: []const u8,
828
- addresses: [3]std.net.Address,
829
- }{
830
- .{
831
- // Test the minimum/maximum address/port.
832
- .raw = "1.2.3.4:567,0.0.0.0:0,255.255.255.255:65535",
833
- .addresses = [3]std.net.Address{
834
- std.net.Address.initIp4([_]u8{ 1, 2, 3, 4 }, 567),
835
- std.net.Address.initIp4([_]u8{ 0, 0, 0, 0 }, 0),
836
- std.net.Address.initIp4([_]u8{ 255, 255, 255, 255 }, 65535),
837
- },
838
- },
839
- .{
840
- // Addresses are not reordered.
841
- .raw = "3.4.5.6:7777,200.3.4.5:6666,1.2.3.4:5555",
842
- .addresses = [3]std.net.Address{
843
- std.net.Address.initIp4([_]u8{ 3, 4, 5, 6 }, 7777),
844
- std.net.Address.initIp4([_]u8{ 200, 3, 4, 5 }, 6666),
845
- std.net.Address.initIp4([_]u8{ 1, 2, 3, 4 }, 5555),
846
- },
847
- },
848
- .{
849
- // Test default address and port.
850
- .raw = "1.2.3.4:5,4321,2.3.4.5",
851
- .addresses = [3]std.net.Address{
852
- std.net.Address.initIp4([_]u8{ 1, 2, 3, 4 }, 5),
853
- try std.net.Address.parseIp4(config.address, 4321),
854
- std.net.Address.initIp4([_]u8{ 2, 3, 4, 5 }, config.port),
855
- },
856
- },
857
- };
858
-
859
- const vectors_negative = &[_]struct {
860
- raw: []const u8,
861
- err: anyerror![]std.net.Address,
862
- }{
863
- .{ .raw = "", .err = error.AddressHasTrailingComma },
864
- .{ .raw = "1.2.3.4:5,2.3.4.5:6,4.5.6.7:8", .err = error.AddressLimitExceeded },
865
- .{ .raw = "1.2.3.4:7777,2.3.4.5:8888,", .err = error.AddressHasTrailingComma },
866
- .{ .raw = "1.2.3.4:7777,2.3.4.5::8888", .err = error.AddressHasMoreThanOneColon },
867
- .{ .raw = "1.2.3.4:5,A", .err = error.AddressInvalid }, // default port
868
- .{ .raw = "1.2.3.4:5,2.a.4.5", .err = error.AddressInvalid }, // default port
869
- .{ .raw = "1.2.3.4:5,2.a.4.5:6", .err = error.AddressInvalid }, // specified port
870
- .{ .raw = "1.2.3.4:5,2.3.4.5:", .err = error.PortInvalid },
871
- .{ .raw = "1.2.3.4:5,2.3.4.5:A", .err = error.PortInvalid },
872
- .{ .raw = "1.2.3.4:5,65536", .err = error.PortOverflow }, // default address
873
- .{ .raw = "1.2.3.4:5,2.3.4.5:65536", .err = error.PortOverflow },
874
- };
875
-
876
- for (vectors_positive) |vector| {
877
- const addresses_actual = try parse_addresses_limit(std.testing.allocator, vector.raw, 3);
878
- defer std.testing.allocator.free(addresses_actual);
879
-
880
- try std.testing.expectEqual(addresses_actual.len, 3);
881
- for (vector.addresses) |address_expect, i| {
882
- const address_actual = addresses_actual[i];
883
- try std.testing.expectEqual(address_expect.in.sa.family, address_actual.in.sa.family);
884
- try std.testing.expectEqual(address_expect.in.sa.port, address_actual.in.sa.port);
885
- try std.testing.expectEqual(address_expect.in.sa.addr, address_actual.in.sa.addr);
886
- try std.testing.expectEqual(address_expect.in.sa.zero, address_actual.in.sa.zero);
887
- }
888
- }
889
-
890
- for (vectors_negative) |vector| {
891
- try std.testing.expectEqual(vector.err, parse_addresses_limit(std.testing.allocator, vector.raw, 2));
892
- }
893
- }
894
-
895
- pub fn sector_floor(offset: u64) u64 {
896
- const sectors = math.divFloor(u64, offset, config.sector_size) catch unreachable;
897
- return sectors * config.sector_size;
898
- }
899
-
900
- pub fn sector_ceil(offset: u64) u64 {
901
- const sectors = math.divCeil(u64, offset, config.sector_size) catch unreachable;
902
- return sectors * config.sector_size;
903
- }
904
-
905
- /// Returns the number of bytes written to `target`.
906
- pub fn format_journal(cluster: u32, offset: u64, target: []u8) usize {
907
- assert(offset <= config.journal_size_max);
908
- assert(offset % config.sector_size == 0);
909
- assert(target.len > 0);
910
- assert(target.len % config.sector_size == 0);
911
-
912
- const sector_max = @divExact(config.journal_size_max, config.sector_size);
913
- var sectors = std.mem.bytesAsSlice([config.sector_size]u8, target);
914
- for (sectors) |*sector_data, i| {
915
- const sector = @divExact(offset, config.sector_size) + i;
916
- if (sector == sector_max) {
917
- if (i == 0) {
918
- assert(offset == config.journal_size_max);
919
- }
920
- return i * config.sector_size;
921
- } else {
922
- format_journal_sector(cluster, sector, sector_data);
923
- }
924
- }
925
- return target.len;
926
- }
927
-
928
- fn format_journal_sector(cluster: u32, sector: usize, sector_data: *[config.sector_size]u8) void {
929
- assert(sector < @divExact(config.journal_size_max, config.sector_size));
930
-
931
- const headers_per_sector = @divExact(config.sector_size, @sizeOf(Header));
932
- const slot_count = config.journal_slot_count;
933
- var sector_headers = std.mem.bytesAsSlice(Header, sector_data);
934
-
935
- if (sector * headers_per_sector < slot_count) {
936
- for (sector_headers) |*header, i| {
937
- const slot = sector * headers_per_sector + i;
938
- if (sector == 0 and i == 0) {
939
- header.* = Header.root_prepare(cluster);
940
- } else {
941
- header.* = Header.reserved(cluster, slot);
942
- }
943
- }
944
- return;
945
- }
946
-
947
- const sectors_per_message = config.message_size_max / config.sector_size;
948
- const sector_in_prepares = sector - slot_count / headers_per_sector;
949
- const message_slot = sector_in_prepares / sectors_per_message;
950
- if (message_slot < slot_count) {
951
- std.mem.set(u8, sector_data, 0);
952
- if (sector_in_prepares % sectors_per_message == 0) {
953
- // First sector of the message.
954
- if (message_slot == 0) {
955
- sector_headers[0] = Header.root_prepare(cluster);
956
- } else {
957
- sector_headers[0] = Header.reserved(cluster, message_slot);
958
- }
959
- }
960
- return;
961
- }
962
-
963
- unreachable;
964
- }
965
-
966
- test "format_journal" {
967
- const cluster = 123;
968
- const write_sizes = [_]usize{
969
- config.sector_size,
970
- config.sector_size * 2,
971
- config.sector_size * 3,
972
- config.journal_size_max,
973
- };
974
-
975
- for (write_sizes) |write_size_max| {
976
- var wal_data = try std.testing.allocator.alloc(u8, config.journal_size_max);
977
- defer std.testing.allocator.free(wal_data);
978
-
979
- var write_data = try std.testing.allocator.alloc(u8, write_size_max);
980
- defer std.testing.allocator.free(write_data);
981
-
982
- var headers_ring = std.mem.bytesAsSlice(Header, wal_data[0..config.journal_size_headers]);
983
- var prepare_ring = std.mem.bytesAsSlice([config.message_size_max]u8, wal_data[config.journal_size_headers..]);
984
- try std.testing.expectEqual(@as(usize, config.journal_slot_count), headers_ring.len);
985
- try std.testing.expectEqual(@as(usize, config.journal_slot_count), prepare_ring.len);
986
-
987
- var offset: u64 = 0;
988
- while (true) {
989
- const write_size = format_journal(cluster, offset, write_data);
990
- if (write_size == 0) break;
991
- std.mem.copy(u8, wal_data[offset..][0..write_size], write_data[0..write_size]);
992
- offset += write_size;
993
- }
994
-
995
- for (headers_ring) |*header, slot| {
996
- try std.testing.expect(header.valid_checksum());
997
- try std.testing.expect(header.valid_checksum_body(&[0]u8{}));
998
- try std.testing.expectEqual(header.invalid(), null);
999
- try std.testing.expectEqual(header.cluster, cluster);
1000
- try std.testing.expectEqual(header.op, slot);
1001
- try std.testing.expectEqual(header.size, @sizeOf(Header));
1002
- if (slot == 0) {
1003
- try std.testing.expectEqual(header.command, .prepare);
1004
- try std.testing.expectEqual(header.operation, .root);
1005
- } else {
1006
- try std.testing.expectEqual(header.command, .reserved);
1007
- }
1008
-
1009
- const prepare_bytes = prepare_ring[slot];
1010
- const prepare_header = std.mem.bytesAsValue(Header, prepare_bytes[0..@sizeOf(Header)]);
1011
- const prepare_body = prepare_bytes[@sizeOf(Header)..];
1012
-
1013
- try std.testing.expectEqual(header.*, prepare_header.*);
1014
- for (prepare_body) |byte| try std.testing.expectEqual(byte, 0);
1015
- }
1016
- }
1017
- }