/
crds.zig
816 lines (724 loc) · 25 KB
/
crds.zig
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
const std = @import("std");
const SocketAddr = @import("net.zig").SocketAddr;
const Tuple = std.meta.Tuple;
const Hash = @import("../core/hash.zig").Hash;
const Signature = @import("../core/signature.zig").Signature;
const Transaction = @import("../core/transaction.zig").Transaction;
const Slot = @import("../core/slot.zig").Slot;
const Option = @import("../option.zig").Option;
const ContactInfo = @import("node.zig").ContactInfo;
const bincode = @import("../bincode/bincode.zig");
const ArrayList = std.ArrayList;
const KeyPair = std.crypto.sign.Ed25519.KeyPair;
const Pubkey = @import("../core/pubkey.zig").Pubkey;
const sanitize_wallclock = @import("./protocol.zig").sanitize_wallclock;
const PACKET_DATA_SIZE = @import("./packet.zig").PACKET_DATA_SIZE;
/// returns current timestamp in milliseconds
pub fn get_wallclock() u64 {
return @intCast(std.time.milliTimestamp());
}
pub const MAX_EPOCH_SLOTS: u8 = 255;
pub const MAX_VOTES: u8 = 32;
pub const MAX_SLOT: u64 = 1_000_000_000_000_000;
pub const MAX_SLOT_PER_ENTRY: usize = 2048 * 8;
pub const MAX_DUPLICATE_SHREDS: u16 = 512;
pub const CrdsVersionedValue = struct {
value: CrdsValue,
value_hash: Hash,
timestamp_on_insertion: u64,
cursor_on_insertion: u64,
};
pub const CrdsValue = struct {
signature: Signature,
data: CrdsData,
const Self = @This();
pub fn init(data: CrdsData) Self {
return Self{
.signature = Signature{},
.data = data,
};
}
pub fn initSigned(data: CrdsData, keypair: *const KeyPair) !Self {
var self = Self{
.signature = Signature{},
.data = data,
};
try self.sign(keypair);
return self;
}
pub fn random(rng: std.rand.Random, keypair: *const KeyPair) !Self {
return try Self.initSigned(CrdsData.random(rng), keypair);
}
pub fn random_with_index(rng: std.rand.Random, keypair: *const KeyPair, index: usize) !Self {
return try Self.initSigned(CrdsData.random_from_index(rng, index), keypair);
}
pub fn sign(self: *Self, keypair: *const KeyPair) !void {
// should always be enough space or is invalid msg
var buf: [PACKET_DATA_SIZE]u8 = undefined;
var bytes = try bincode.writeToSlice(&buf, self.data, bincode.Params.standard);
var sig = try keypair.sign(bytes, null);
self.signature.data = sig.toBytes();
}
pub fn verify(self: *Self, pubkey: Pubkey) !bool {
// should always be enough space or is invalid msg
var buf: [PACKET_DATA_SIZE]u8 = undefined;
var msg = try bincode.writeToSlice(&buf, self.data, bincode.Params.standard);
return self.signature.verify(pubkey, msg);
}
pub fn id(self: *const Self) Pubkey {
return switch (self.data) {
.LegacyContactInfo => |*v| {
return v.id;
},
.Vote => |*v| {
return v[1].from;
},
.LowestSlot => |*v| {
return v[1].from;
},
.LegacySnapshotHashes => |*v| {
return v.from;
},
.AccountsHashes => |*v| {
return v.from;
},
.EpochSlots => |*v| {
return v[1].from;
},
.LegacyVersion => |*v| {
return v.from;
},
.Version => |*v| {
return v.from;
},
.NodeInstance => |*v| {
return v.from;
},
.DuplicateShred => |*v| {
return v[1].from;
},
.SnapshotHashes => |*v| {
return v.from;
},
.ContactInfo => |*v| {
return v.pubkey;
},
};
}
pub fn wallclock(self: *const Self) u64 {
return switch (self.data) {
.LegacyContactInfo => |*v| {
return v.wallclock;
},
.Vote => |*v| {
return v[1].wallclock;
},
.LowestSlot => |*v| {
return v[1].wallclock;
},
.LegacySnapshotHashes => |*v| {
return v.wallclock;
},
.AccountsHashes => |*v| {
return v.wallclock;
},
.EpochSlots => |*v| {
return v[1].wallclock;
},
.LegacyVersion => |*v| {
return v.wallclock;
},
.Version => |*v| {
return v.wallclock;
},
.NodeInstance => |*v| {
return v.wallclock;
},
.DuplicateShred => |*v| {
return v[1].wallclock;
},
.SnapshotHashes => |*v| {
return v.wallclock;
},
.ContactInfo => |*v| {
return v.wallclock;
},
};
}
pub fn label(self: *const Self) CrdsValueLabel {
return switch (self.data) {
.LegacyContactInfo => {
return CrdsValueLabel{ .LegacyContactInfo = self.id() };
},
.Vote => |*v| {
return CrdsValueLabel{ .Vote = .{ v[0], self.id() } };
},
.LowestSlot => {
return CrdsValueLabel{ .LowestSlot = self.id() };
},
.LegacySnapshotHashes => {
return CrdsValueLabel{ .LegacySnapshotHashes = self.id() };
},
.AccountsHashes => {
return CrdsValueLabel{ .AccountsHashes = self.id() };
},
.EpochSlots => |*v| {
return CrdsValueLabel{ .EpochSlots = .{ v[0], self.id() } };
},
.LegacyVersion => {
return CrdsValueLabel{ .LegacyVersion = self.id() };
},
.Version => {
return CrdsValueLabel{ .Version = self.id() };
},
.NodeInstance => {
return CrdsValueLabel{ .NodeInstance = self.id() };
},
.DuplicateShred => |*v| {
return CrdsValueLabel{ .DuplicateShred = .{ v[0], self.id() } };
},
.SnapshotHashes => {
return CrdsValueLabel{ .SnapshotHashes = self.id() };
},
.ContactInfo => {
return CrdsValueLabel{ .ContactInfo = self.id() };
},
};
}
};
pub const LegacyContactInfo = struct {
id: Pubkey,
/// gossip address
gossip: SocketAddr,
/// address to connect to for replication
tvu: SocketAddr,
/// address to forward shreds to
tvu_forwards: SocketAddr,
/// address to send repair responses to
repair: SocketAddr,
/// transactions address
tpu: SocketAddr,
/// address to forward unprocessed transactions to
tpu_forwards: SocketAddr,
/// address to which to send bank state requests
tpu_vote: SocketAddr,
/// address to which to send JSON-RPC requests
rpc: SocketAddr,
/// websocket for JSON-RPC push notifications
rpc_pubsub: SocketAddr,
/// address to send repair requests to
serve_repair: SocketAddr,
/// latest wallclock picked
wallclock: u64,
/// node shred version
shred_version: u16,
pub fn sanitize(self: *const LegacyContactInfo) !void {
try sanitize_wallclock(self.wallclock);
}
pub fn default(id: Pubkey) LegacyContactInfo {
const unspecified_addr = SocketAddr.init_ipv4(.{ 0, 0, 0, 0 }, 0);
const wallclock = get_wallclock();
return LegacyContactInfo{
.id = id,
.gossip = unspecified_addr,
.tvu = unspecified_addr,
.tvu_forwards = unspecified_addr,
.repair = unspecified_addr,
.tpu = unspecified_addr,
.tpu_forwards = unspecified_addr,
.tpu_vote = unspecified_addr,
.rpc = unspecified_addr,
.rpc_pubsub = unspecified_addr,
.serve_repair = unspecified_addr,
.wallclock = wallclock,
.shred_version = 0,
};
}
pub fn random(rng: std.rand.Random) LegacyContactInfo {
return LegacyContactInfo{
.id = Pubkey.random(rng, .{ .skip_encoding = false }),
.gossip = SocketAddr.random(rng),
.tvu = SocketAddr.random(rng),
.tvu_forwards = SocketAddr.random(rng),
.repair = SocketAddr.random(rng),
.tpu = SocketAddr.random(rng),
.tpu_forwards = SocketAddr.random(rng),
.tpu_vote = SocketAddr.random(rng),
.rpc = SocketAddr.random(rng),
.rpc_pubsub = SocketAddr.random(rng),
.serve_repair = SocketAddr.random(rng),
.wallclock = get_wallclock(),
.shred_version = rng.int(u16),
};
}
};
pub fn sanitize_socket(socket: *const SocketAddr) !void {
if (socket.port() == 0) {
return error.InvalidPort;
}
if (socket.is_unspecified()) {
return error.UnspecifiedAddress;
}
if (socket.is_multicast()) {
return error.MulticastAddress;
}
}
pub const CrdsValueLabel = union(enum) {
LegacyContactInfo: Pubkey,
Vote: struct { u8, Pubkey },
LowestSlot: Pubkey,
LegacySnapshotHashes: Pubkey,
EpochSlots: struct { u8, Pubkey },
AccountsHashes: Pubkey,
LegacyVersion: Pubkey,
Version: Pubkey,
NodeInstance: Pubkey,
DuplicateShred: struct { u16, Pubkey },
SnapshotHashes: Pubkey,
ContactInfo: Pubkey,
};
pub const CrdsData = union(enum(u32)) {
LegacyContactInfo: LegacyContactInfo,
Vote: struct { u8, Vote },
LowestSlot: struct { u8, LowestSlot },
LegacySnapshotHashes: LegacySnapshotHashes,
AccountsHashes: AccountsHashes,
EpochSlots: struct { u8, EpochSlots },
LegacyVersion: LegacyVersion,
Version: Version,
NodeInstance: NodeInstance,
DuplicateShred: struct { u16, DuplicateShred },
SnapshotHashes: SnapshotHashes,
ContactInfo: ContactInfo,
pub fn sanitize(self: *const CrdsData) !void {
switch (self.*) {
.LegacyContactInfo => |*v| {
try v.sanitize();
},
.Vote => |*v| {
const index = v[0];
if (index >= MAX_VOTES) {
return error.ValueOutOfBounds;
}
const vote: Vote = v[1];
try vote.sanitize();
},
.EpochSlots => |*v| {
const index = v[0];
if (index >= MAX_EPOCH_SLOTS) {
return error.ValueOutOfBounds;
}
const value: EpochSlots = v[1];
try value.sanitize();
},
.DuplicateShred => |*v| {
const index = v[0];
if (index >= MAX_DUPLICATE_SHREDS) {
return error.ValueOutOfBounds;
}
const value: DuplicateShred = v[1];
try value.sanitize();
},
.LowestSlot => |*v| {
const index = v[0];
if (index >= 1) {
return error.ValueOutOfBounds;
}
const value: LowestSlot = v[1];
try value.sanitize();
},
.LegacySnapshotHashes => |*v| {
try v.sanitize();
},
.AccountsHashes => |*v| {
try v.sanitize();
},
else => {
std.debug.print("sanitize not implemented for type: {any}\n", .{@tagName(self.*)});
return error.NotImplemented;
},
}
}
// only used in tests
pub fn set_id(self: *CrdsData, id: Pubkey) void {
switch (self.*) {
.LegacyContactInfo => |*v| {
v.id = id;
},
.Vote => |*v| {
v[1].from = id;
},
.EpochSlots => |*v| {
v[1].from = id;
},
.DuplicateShred => |*v| {
v[1].from = id;
},
else => {
// tmp
@panic("set_id not implemented for the given type\n");
},
}
}
pub fn random(rng: std.rand.Random) CrdsData {
const v = rng.intRangeAtMost(u16, 0, 3);
return CrdsData.random_from_index(rng, v);
}
pub fn random_from_index(rng: std.rand.Random, index: usize) CrdsData {
switch (index) {
0 => {
return CrdsData{ .LegacyContactInfo = LegacyContactInfo.random(rng) };
},
1 => {
return CrdsData{ .Vote = .{ rng.intRangeAtMost(u8, 0, MAX_VOTES), Vote.random(rng) } };
},
2 => {
return CrdsData{ .EpochSlots = .{ rng.intRangeAtMost(u8, 0, MAX_EPOCH_SLOTS), EpochSlots.random(rng) } };
},
else => {
return CrdsData{ .DuplicateShred = .{ rng.intRangeAtMost(u16, 0, MAX_DUPLICATE_SHREDS), DuplicateShred.random(rng) } };
},
}
}
};
pub const Vote = struct {
from: Pubkey,
transaction: Transaction,
wallclock: u64,
slot: Slot = Slot.default(),
pub const @"!bincode-config:slot" = bincode.FieldConfig(Slot){ .skip = true };
pub fn random(rng: std.rand.Random) Vote {
return Vote{
.from = Pubkey.random(rng, .{ .skip_encoding = true }),
.transaction = Transaction.default(),
.wallclock = get_wallclock(),
.slot = Slot.init(rng.int(u64)),
};
}
pub fn sanitize(self: *const Vote) !void {
try sanitize_wallclock(self.wallclock);
try self.transaction.sanitize();
}
};
pub const LowestSlot = struct {
from: Pubkey,
root: u64, //deprecated
lowest: u64,
slots: []u64, //deprecated
stash: []DeprecatedEpochIncompleteSlots, //deprecated
wallclock: u64,
pub fn sanitize(value: *const LowestSlot) !void {
try sanitize_wallclock(value.wallclock);
if (value.lowest >= MAX_SLOT) {
return error.ValueOutOfBounds;
}
if (value.root != 0) {
return error.InvalidValue;
}
if (value.slots.len != 0) {
return error.InvalidValue;
}
if (value.stash.len != 0) {
return error.InvalidValue;
}
}
};
pub const DeprecatedEpochIncompleteSlots = struct {
first: u64,
compression: CompressionType,
compressed_list: []u8,
};
pub const CompressionType = enum {
Uncompressed,
GZip,
BZip2,
};
pub const LegacySnapshotHashes = AccountsHashes;
pub const AccountsHashes = struct {
from: Pubkey,
hashes: []struct { u64, Hash },
wallclock: u64,
pub fn sanitize(value: *const AccountsHashes) !void {
try sanitize_wallclock(value.wallclock);
for (value.hashes) |*snapshot_hash| {
const slot = snapshot_hash[0];
if (slot >= MAX_SLOT) {
return error.ValueOutOfBounds;
}
}
}
};
pub const EpochSlots = struct {
from: Pubkey,
slots: []CompressedSlots,
wallclock: u64,
pub fn random(rng: std.rand.Random) EpochSlots {
var slice: [0]CompressedSlots = .{};
return EpochSlots{
.from = Pubkey.random(rng, .{ .skip_encoding = true }),
.slots = &slice,
.wallclock = get_wallclock(),
};
}
pub fn sanitize(value: *const EpochSlots) !void {
try sanitize_wallclock(value.wallclock);
for (value.slots) |slot| {
try slot.sanitize();
}
}
};
pub const CompressedSlots = union(enum(u32)) {
Flate2: Flate2,
Uncompressed: Uncompressed,
pub fn sanitize(self: *const CompressedSlots) !void {
switch (self.*) {
.Flate2 => |*v| try v.sanitize(),
.Uncompressed => |*v| try v.sanitize(),
}
}
};
pub const Flate2 = struct {
first_slot: Slot,
num: usize,
compressed: []u8,
pub fn sanitize(self: *const Flate2) !void {
if (self.first_slot.value >= MAX_SLOT) {
return error.ValueOutOfBounds;
}
if (self.num >= MAX_SLOT_PER_ENTRY) {
return error.ValueOutOfBounds;
}
}
};
pub const Uncompressed = struct {
first_slot: Slot,
num: usize,
slots: BitVec(u8),
pub fn sanitize(self: *const Uncompressed) !void {
if (self.first_slot.value >= MAX_SLOT) {
return error.ValueOutOfBounds;
}
if (self.num >= MAX_SLOT_PER_ENTRY) {
return error.ValueOutOfBounds;
}
if (self.slots.len % 8 != 0) {
return error.InvalidValue;
}
// TODO: check BitVec.capacity()
}
};
pub fn BitVec(comptime T: type) type {
return struct {
bits: Option([]T),
len: usize,
};
}
pub const LegacyVersion = struct {
from: Pubkey,
wallclock: u64,
version: LegacyVersion1,
};
pub const LegacyVersion1 = struct {
major: u16,
minor: u16,
patch: u16,
commit: Option(u32), // first 4 bytes of the sha1 commit hash
};
pub const Version = struct {
from: Pubkey,
wallclock: u64,
version: LegacyVersion2,
const Self = @This();
pub fn init(from: Pubkey, wallclock: u64, version: LegacyVersion2) Self {
return Self{
.from = from,
.wallclock = wallclock,
.version = version,
};
}
pub fn default(from: Pubkey) Self {
return Self{
.from = from,
.wallclock = get_wallclock(),
.version = LegacyVersion2.CURRENT,
};
}
};
pub const LegacyVersion2 = struct {
major: u16,
minor: u16,
patch: u16,
commit: Option(u32), // first 4 bytes of the sha1 commit hash
feature_set: u32, // first 4 bytes of the FeatureSet identifier
const Self = @This();
pub const CURRENT = LegacyVersion2.init(1, 14, 17, Option(u32).Some(2996451279), 3488713414);
pub fn init(major: u16, minor: u16, patch: u16, commit: Option(u32), feature_set: u32) Self {
return Self{
.major = major,
.minor = minor,
.patch = patch,
.commit = commit,
.feature_set = feature_set,
};
}
};
pub const NodeInstance = struct {
from: Pubkey,
wallclock: u64,
timestamp: u64, // Timestamp when the instance was created.
token: u64, // Randomly generated value at node instantiation.
const Self = @This();
pub fn init(from: Pubkey, wallclock: u64) Self {
var rng = std.rand.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
return Self{
.from = from,
.wallclock = wallclock,
.timestamp = @intCast(std.time.microTimestamp()),
.token = rng.random().int(u64),
};
}
pub fn withWallclock(self: *Self, wallclock: u64) Self {
return Self{
.from = self.from,
.wallclock = wallclock,
.timestamp = self.timestamp,
.token = self.token,
};
}
};
pub const ShredType = enum(u32) {
Data = 0b1010_0101,
Code = 0b0101_1010,
};
pub const DuplicateShred = struct {
from: Pubkey,
wallclock: u64,
slot: Slot,
shred_index: u32,
shred_type: ShredType,
// Serialized DuplicateSlotProof split into chunks.
num_chunks: u8,
chunk_index: u8,
chunk: []u8,
pub fn random(rng: std.rand.Random) DuplicateShred {
var slice = [_]u8{0} ** 32;
return DuplicateShred{
.from = Pubkey.random(rng, .{ .skip_encoding = true }),
.wallclock = get_wallclock(),
.slot = Slot.init(rng.int(u64)),
.shred_index = rng.int(u32),
.shred_type = ShredType.Data,
.num_chunks = rng.int(u8),
.chunk_index = rng.int(u8),
.chunk = &slice,
};
}
pub fn sanitize(value: *const DuplicateShred) !void {
try sanitize_wallclock(value.wallclock);
if (value.chunk_index >= value.num_chunks) {
return error.ValueOutOfBounds;
}
}
};
pub const SnapshotHashes = struct {
from: Pubkey,
full: struct { Slot, Hash },
incremental: []struct { Slot, Hash },
wallclock: u64,
};
test "gossip.crds: test sanitize CrdsData" {
var rng = std.rand.DefaultPrng.init(0);
var rand = rng.random();
for (0..4) |i| {
const data = CrdsData.random_from_index(rand, i);
data.sanitize() catch {};
}
}
test "gossip.crds: test CrdsValue label() and id() methods" {
var kp_bytes = [_]u8{1} ** 32;
var kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
var id = Pubkey.fromPublicKey(&pk, true);
var legacy_contact_info = LegacyContactInfo.default(id);
legacy_contact_info.wallclock = 0;
var crds_value = try CrdsValue.initSigned(CrdsData{
.LegacyContactInfo = legacy_contact_info,
}, &kp);
try std.testing.expect(crds_value.id().equals(&id));
try std.testing.expect(crds_value.label().LegacyContactInfo.equals(&id));
}
test "gossip.crds: pubkey matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const rust_bytes = [_]u8{ 138, 136, 227, 221, 116, 9, 241, 149, 253, 82, 219, 45, 60, 186, 93, 114, 202, 103, 9, 191, 29, 148, 18, 27, 243, 116, 136, 1, 180, 15, 111, 92 };
var buf = [_]u8{0} ** 1024;
var bytes = try bincode.writeToSlice(buf[0..], id, bincode.Params.standard);
try std.testing.expectEqualSlices(u8, rust_bytes[0..], bytes[0..bytes.len]);
var out = try bincode.readFromSlice(std.testing.allocator, Pubkey, buf[0..], bincode.Params.standard);
try std.testing.expectEqual(id, out);
}
test "gossip.crds: contact info serialization matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const gossip_addr = SocketAddr.init_ipv4(.{ 127, 0, 0, 1 }, 1234);
var buf = [_]u8{0} ** 1024;
var legacy_contact_info = LegacyContactInfo.default(id);
legacy_contact_info.gossip = gossip_addr;
legacy_contact_info.id = id;
legacy_contact_info.wallclock = 0;
var contact_info_rust = [_]u8{ 138, 136, 227, 221, 116, 9, 241, 149, 253, 82, 219, 45, 60, 186, 93, 114, 202, 103, 9, 191, 29, 148, 18, 27, 243, 116, 136, 1, 180, 15, 111, 92, 0, 0, 0, 0, 127, 0, 0, 1, 210, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
var bytes = try bincode.writeToSlice(buf[0..], legacy_contact_info, bincode.Params.standard);
try std.testing.expectEqualSlices(u8, bytes[0..bytes.len], &contact_info_rust);
}
test "gossip.crds: crds data serialization matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const gossip_addr = SocketAddr.init_ipv4(.{ 127, 0, 0, 1 }, 1234);
var legacy_contact_info = LegacyContactInfo.default(id);
legacy_contact_info.gossip = gossip_addr;
legacy_contact_info.wallclock = 0;
var crds_data = CrdsData{
.LegacyContactInfo = legacy_contact_info,
};
var buf = [_]u8{0} ** 1024;
var rust_crds_data = [_]u8{ 0, 0, 0, 0, 138, 136, 227, 221, 116, 9, 241, 149, 253, 82, 219, 45, 60, 186, 93, 114, 202, 103, 9, 191, 29, 148, 18, 27, 243, 116, 136, 1, 180, 15, 111, 92, 0, 0, 0, 0, 127, 0, 0, 1, 210, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
var bytes = try bincode.writeToSlice(buf[0..], crds_data, bincode.Params.standard);
try std.testing.expectEqualSlices(u8, bytes[0..bytes.len], rust_crds_data[0..bytes.len]);
}
test "gossip.crds: random crds data" {
var seed: u64 = @intCast(std.time.milliTimestamp());
var rand = std.rand.DefaultPrng.init(seed);
const rng = rand.random();
var buf: [1000]u8 = undefined;
{
const data = LegacyContactInfo.random(rng);
const result = try bincode.writeToSlice(&buf, data, bincode.Params.standard);
_ = result;
}
{
const data = EpochSlots.random(rng);
const result = try bincode.writeToSlice(&buf, data, bincode.Params.standard);
_ = result;
}
{
const data = Vote.random(rng);
const result = try bincode.writeToSlice(&buf, data, bincode.Params.standard);
_ = result;
}
{
const data = DuplicateShred.random(rng);
const result = try bincode.writeToSlice(&buf, data, bincode.Params.standard);
_ = result;
}
{
const data = CrdsData.random(rng);
const result = try bincode.writeToSlice(&buf, data, bincode.Params.standard);
_ = result;
}
}