-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdata.zig
2108 lines (1851 loc) · 78.5 KB
/
data.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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const network = @import("zig-network");
const sig = @import("../sig.zig");
const testing = std.testing;
const bincode = sig.bincode;
const ArrayList = std.ArrayList;
const KeyPair = std.crypto.sign.Ed25519.KeyPair;
const UdpSocket = network.Socket;
const TcpListener = network.Socket;
const SocketAddr = sig.net.SocketAddr;
const Hash = sig.core.Hash;
const Signature = sig.core.Signature;
const Transaction = sig.core.Transaction;
const Slot = sig.core.Slot;
const Pubkey = sig.core.Pubkey;
const IpAddr = sig.net.IpAddr;
const ClientVersion = sig.version.ClientVersion;
const DynamicArrayBitSet = sig.bloom.bit_set.DynamicArrayBitSet;
const SlotAndHash = sig.core.hash.SlotAndHash;
const getWallclockMs = sig.time.getWallclockMs;
const BitVecConfig = sig.bloom.bit_vec.BitVecConfig;
const sanitizeWallclock = sig.gossip.message.sanitizeWallclock;
const PACKET_DATA_SIZE = sig.net.packet.PACKET_DATA_SIZE;
const var_int_config_u16 = sig.bincode.varint.var_int_config_u16;
const var_int_config_u64 = sig.bincode.varint.var_int_config_u64;
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;
/// Analogous to [VersionedCrdsValue](https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/gossip/src/crds.rs#L122)
pub const GossipVersionedData = struct {
value: SignedGossipData,
value_hash: Hash,
timestamp_on_insertion: u64,
cursor_on_insertion: u64,
pub fn clone(self: *const GossipVersionedData, allocator: std.mem.Allocator) error{OutOfMemory}!GossipVersionedData {
return .{
.value = try self.value.clone(allocator),
.value_hash = self.value_hash,
.timestamp_on_insertion = self.timestamp_on_insertion,
.cursor_on_insertion = self.cursor_on_insertion,
};
}
pub fn deinit(self: *const GossipVersionedData, allocator: std.mem.Allocator) void {
self.value.deinit(allocator);
}
pub fn overwrites(new_value: *const @This(), old_value: *const @This()) bool {
// labels must match
std.debug.assert(@intFromEnum(new_value.value.label()) == @intFromEnum(old_value.value.label()));
const new_ts = new_value.value.wallclock();
const old_ts = old_value.value.wallclock();
// TODO: improve the return type here
if (new_ts > old_ts) {
return true;
} else if (new_ts < old_ts) {
return false;
} else {
return old_value.value_hash.order(&new_value.value_hash) == .lt;
}
}
};
/// Analogous to [CrdsValue](https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/gossip/src/crds_value.rs#L45)
pub const SignedGossipData = struct {
signature: Signature,
data: GossipData,
const Self = @This();
pub fn initSigned(
/// Assumed to be a valid & strong keypair, passing a bad or invalid keypair is illegal.
keypair: *const KeyPair,
/// Assumed to be valid, passing invalid data is illegal.
data: GossipData,
) Self {
// should always be enough space or is invalid msg
var buf: [PACKET_DATA_SIZE]u8 = undefined;
const bytes = bincode.writeToSlice(&buf, data, bincode.Params.standard) catch |err| {
// should never be possible for a valid gossip value
std.debug.panic("Unexpected bincode failure: {}", .{err});
};
const signature = keypair.sign(bytes, null) catch |err| switch (err) {
error.KeyMismatch => unreachable, // the keypair must match, passing a mismatched keypair is illegal
error.IdentityElement => unreachable, // this would only be possible with a weak or invalid keypair, which is illegal
error.WeakPublicKey => unreachable, // this would only be possible with a weak or invalid keypair, which is illegal
// TODO: inspecting the code reveals this error is never actually reached from this function, despite being part of the error set
// we should upstream a fix to zig's stdlib that amends this or documents why it's part of the error set at all.
error.NonCanonical => unreachable,
};
return .{
.signature = Signature.init(signature.toBytes()),
.data = data,
};
}
pub fn clone(self: *const Self, allocator: std.mem.Allocator) error{OutOfMemory}!Self {
return .{
.signature = self.signature,
.data = try self.data.clone(allocator),
};
}
pub fn deinit(self: *const Self, allocator: std.mem.Allocator) void {
self.data.deinit(allocator);
}
pub fn verify(self: *const Self, pubkey: Pubkey) !bool {
// should always be enough space or is invalid msg
var buf: [PACKET_DATA_SIZE]u8 = undefined;
const msg = try bincode.writeToSlice(&buf, self.data, bincode.Params.standard);
return self.signature.verify(pubkey, msg);
}
pub fn id(self: *const Self) Pubkey {
return self.data.id();
}
pub fn label(self: *const Self) GossipKey {
return self.data.label();
}
pub fn wallclock(self: *const Self) u64 {
return self.data.wallclock();
}
/// only used in tests.
pub fn initRandom(
random: std.rand.Random,
/// Assumed to be a valid & strong keypair, passing a bad or invalid keypair is illegal.
keypair: *const KeyPair,
) Self {
return initSigned(keypair, GossipData.initRandom(random));
}
/// only used in tests
pub fn randomWithIndex(
random: std.rand.Random,
/// Assumed to be a valid & strong keypair, passing a bad or invalid keypair is illegal.
keypair: *const KeyPair,
index: usize,
) !Self {
var data = GossipData.randomFromIndex(random, index);
const pubkey = Pubkey.fromPublicKey(&keypair.public_key);
data.setId(pubkey);
return initSigned(keypair, data);
}
};
/// Analogous to [CrdsValueLabel](https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/gossip/src/crds_value.rs#L500)
pub const GossipKey = union(GossipDataTag) {
LegacyContactInfo: Pubkey,
Vote: struct { u8, Pubkey },
LowestSlot: Pubkey,
LegacySnapshotHashes: Pubkey,
AccountsHashes: Pubkey,
EpochSlots: struct { u8, Pubkey },
LegacyVersion: Pubkey,
Version: Pubkey,
NodeInstance: Pubkey,
DuplicateShred: struct { u16, Pubkey },
SnapshotHashes: Pubkey,
ContactInfo: Pubkey,
RestartLastVotedForkSlots: Pubkey,
RestartHeaviestFork: Pubkey,
};
const GossipDataTag = enum(u32) {
LegacyContactInfo,
Vote,
LowestSlot,
LegacySnapshotHashes,
AccountsHashes,
EpochSlots,
LegacyVersion,
Version,
NodeInstance,
DuplicateShred,
SnapshotHashes,
ContactInfo,
RestartLastVotedForkSlots,
RestartHeaviestFork,
};
/// Analogous to [CrdsData](https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/gossip/src/crds_value.rs#L85)
pub const GossipData = union(GossipDataTag) {
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,
// https://github.com/anza-xyz/agave/commit/0a3810854fa4a11b0841c548dcbc0ada311b8830
RestartLastVotedForkSlots: RestartLastVotedForkSlots,
// https://github.com/anza-xyz/agave/commit/4a2871f38419b4d9b303254273b19a2e41707c47
RestartHeaviestFork: RestartHeaviestFork,
pub fn clone(self: *const GossipData, allocator: std.mem.Allocator) error{OutOfMemory}!GossipData {
return switch (self.*) {
.LegacyContactInfo => |*v| .{ .LegacyContactInfo = v.* },
.Vote => |*v| .{ .Vote = .{ v[0], try v[1].clone(allocator) } },
.LowestSlot => |*v| .{ .LowestSlot = .{ v[0], try v[1].clone(allocator) } },
.LegacySnapshotHashes => |*v| .{ .LegacySnapshotHashes = try v.clone(allocator) },
.AccountsHashes => |*v| .{ .AccountsHashes = try v.clone(allocator) },
.EpochSlots => |*v| .{ .EpochSlots = .{ v[0], try v[1].clone(allocator) } },
.LegacyVersion => |*v| .{ .LegacyVersion = v.* },
.Version => |*v| .{ .Version = v.* },
.NodeInstance => |*v| .{ .NodeInstance = v.* },
.DuplicateShred => |*v| .{ .DuplicateShred = .{ v[0], try v[1].clone(allocator) } },
.SnapshotHashes => |*v| .{ .SnapshotHashes = try v.clone(allocator) },
.ContactInfo => |*v| .{ .ContactInfo = try v.clone() },
.RestartLastVotedForkSlots => |*v| .{ .RestartLastVotedForkSlots = try v.clone(allocator) },
.RestartHeaviestFork => |*v| .{ .RestartHeaviestFork = v.* },
};
}
pub fn deinit(self: *const GossipData, allocator: std.mem.Allocator) void {
switch (self.*) {
.LegacyContactInfo => {},
.Vote => |*v| v[1].deinit(allocator),
.LowestSlot => |*v| v[1].deinit(allocator),
.LegacySnapshotHashes => |*v| v.deinit(allocator),
.AccountsHashes => |*v| v.deinit(allocator),
.EpochSlots => |*v| v[1].deinit(allocator),
.LegacyVersion => {},
.Version => {},
.NodeInstance => {},
.DuplicateShred => |*v| v[1].deinit(allocator),
.SnapshotHashes => |*v| v.deinit(allocator),
.ContactInfo => |*v| v.deinit(),
.RestartLastVotedForkSlots => |*v| v.deinit(allocator),
.RestartHeaviestFork => {},
}
}
pub fn sanitize(self: *const GossipData) !void {
switch (self.*) {
inline .LegacyContactInfo,
.ContactInfo,
.AccountsHashes,
.LegacySnapshotHashes,
.SnapshotHashes,
.NodeInstance,
.Version,
.RestartLastVotedForkSlots,
.RestartHeaviestFork,
=> |*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();
},
else => {
std.debug.print("sanitize not implemented for type: {s}\n", .{@tagName(self.*)});
return error.NotImplemented;
},
}
}
pub fn gossipAddr(self: *const @This()) ?SocketAddr {
return switch (self.*) {
.LegacyContactInfo => |*v| if (v.gossip.isUnspecified()) null else v.gossip,
.ContactInfo => |*v| v.getSocket(.gossip),
else => null,
};
}
pub fn shredVersion(self: *const @This()) ?u16 {
return switch (self.*) {
.LegacyContactInfo => |*v| v.shred_version,
.ContactInfo => |*v| v.shred_version,
else => null,
};
}
pub fn id(self: *const GossipData) Pubkey {
return switch (self.*) {
// zig fmt: off
.LegacyContactInfo => |v| v.id,
.Vote => |v| v[1].from,
.LowestSlot => |v| v[1].from,
.LegacySnapshotHashes => |v| v.from,
.AccountsHashes => |v| v.from,
.EpochSlots => |v| v[1].from,
.LegacyVersion => |v| v.from,
.Version => |v| v.from,
.NodeInstance => |v| v.from,
.DuplicateShred => |v| v[1].from,
.SnapshotHashes => |v| v.from,
.ContactInfo => |v| v.pubkey,
.RestartLastVotedForkSlots => |v| v.from,
.RestartHeaviestFork => |v| v.from,
// zig fmt: on
};
}
pub fn label(self: *const GossipData) GossipKey {
return switch (self.*) {
// zig fmt: off
.LegacyContactInfo => |v|.{ .LegacyContactInfo = v.id },
.Vote => |v|.{ .Vote = .{ v[0], v[1].from } },
.LowestSlot => |v|.{ .LowestSlot = v[1].from },
.LegacySnapshotHashes => |v|.{ .LegacySnapshotHashes = v.from },
.AccountsHashes => |v|.{ .AccountsHashes = v.from },
.EpochSlots => |v|.{ .EpochSlots = .{ v[0], v[1].from } },
.LegacyVersion => |v|.{ .LegacyVersion = v.from },
.Version => |v|.{ .Version = v.from },
.NodeInstance => |v|.{ .NodeInstance = v.from },
.DuplicateShred => |v|.{ .DuplicateShred = .{ v[0], v[1].from } },
.SnapshotHashes => |v|.{ .SnapshotHashes = v.from },
.ContactInfo => |v|.{ .ContactInfo = v.pubkey },
.RestartLastVotedForkSlots => |v|.{ .RestartLastVotedForkSlots= v.from },
.RestartHeaviestFork => |v|.{ .RestartHeaviestFork = v.from },
// zig fmt: on
};
}
pub fn wallclockPtr(self: *GossipData) *u64 {
return switch (self.*) {
// zig fmt: off
.LegacyContactInfo => |*v| &v.wallclock,
.Vote => |*v| &v[1].wallclock,
.LowestSlot => |*v| &v[1].wallclock,
.LegacySnapshotHashes => |*v| &v.wallclock,
.AccountsHashes => |*v| &v.wallclock,
.EpochSlots => |*v| &v[1].wallclock,
.LegacyVersion => |*v| &v.wallclock,
.Version => |*v| &v.wallclock,
.NodeInstance => |*v| &v.wallclock,
.DuplicateShred => |*v| &v[1].wallclock,
.SnapshotHashes => |*v| &v.wallclock,
.ContactInfo => |*v| &v.wallclock,
.RestartLastVotedForkSlots => |*v| &v.wallclock,
.RestartHeaviestFork => |*v| &v.wallclock,
// zig fmt: on
};
}
pub fn wallclock(self: *const GossipData) u64 {
return switch (self.*) {
// zig fmt: off
.LegacyContactInfo => |v| v.wallclock,
.Vote => |v| v[1].wallclock,
.LowestSlot => |v| v[1].wallclock,
.LegacySnapshotHashes => |v| v.wallclock,
.AccountsHashes => |v| v.wallclock,
.EpochSlots => |v| v[1].wallclock,
.LegacyVersion => |v| v.wallclock,
.Version => |v| v.wallclock,
.NodeInstance => |v| v.wallclock,
.DuplicateShred => |v| v[1].wallclock,
.SnapshotHashes => |v| v.wallclock,
.ContactInfo => |v| v.wallclock,
.RestartLastVotedForkSlots => |v| v.wallclock,
.RestartHeaviestFork => |v| v.wallclock,
// zig fmt: on
};
}
/// only used in tests
pub fn setId(self: *GossipData, new_id: Pubkey) void {
switch (self.*) {
// zig fmt: off
.LegacyContactInfo => |*v| v.id = new_id,
.Vote => |*v| v[1].from = new_id,
.LowestSlot => |*v| v[1].from = new_id,
.LegacySnapshotHashes => |*v| v.from = new_id,
.AccountsHashes => |*v| v.from = new_id,
.EpochSlots => |*v| v[1].from = new_id,
.LegacyVersion => |*v| v.from = new_id,
.Version => |*v| v.from = new_id,
.NodeInstance => |*v| v.from = new_id,
.DuplicateShred => |*v| v[1].from = new_id,
.SnapshotHashes => |*v| v.from = new_id,
.ContactInfo => |*v| v.pubkey = new_id,
.RestartLastVotedForkSlots => |*v| v.from = new_id,
.RestartHeaviestFork => |*v| v.from = new_id,
// zig fmt: on
}
}
/// only used in tests
pub fn initRandom(random: std.rand.Random) GossipData {
const v = random.intRangeAtMost(u16, 0, 10);
return GossipData.randomFromIndex(random, v);
}
pub fn randomFromIndex(random: std.rand.Random, index: usize) GossipData {
return switch (index) {
0 => .{ .LegacyContactInfo = LegacyContactInfo.initRandom(random) },
1 => .{ .Vote = .{ random.intRangeAtMost(u8, 0, MAX_VOTES - 1), Vote.initRandom(random) } },
2 => .{ .EpochSlots = .{ random.intRangeAtMost(u8, 0, MAX_EPOCH_SLOTS - 1), EpochSlots.initRandom(random) } },
3 => .{ .LowestSlot = .{ 0, LowestSlot.initRandom(random) } },
4 => .{ .LegacySnapshotHashes = LegacySnapshotHashes.initRandom(random) },
5 => .{ .AccountsHashes = AccountsHashes.initRandom(random) },
6 => .{ .LegacyVersion = LegacyVersion.initRandom(random) },
7 => .{ .Version = Version.initRandom(random) },
8 => .{ .NodeInstance = NodeInstance.initRandom(random) },
9 => .{ .SnapshotHashes = SnapshotHashes.initRandom(random) },
// 10 => .{ .ContactInfo = ContactInfo.initRandom(random) },
else => .{ .DuplicateShred = .{ random.intRangeAtMost(u16, 0, MAX_DUPLICATE_SHREDS - 1), DuplicateShred.initRandom(random) } },
};
}
};
/// analogous to [LegactContactInfo](https://github.com/anza-xyz/agave/blob/0d34a1a160129c4293dac248e14231e9e773b4ce/gossip/src/legacy_contact_info.rs#L26)
pub const LegacyContactInfo = struct {
id: Pubkey,
/// gossip address
gossip: SocketAddr,
/// address to connect to for replication
/// analogous to `tvu` in agave
turbine_recv: SocketAddr,
/// address to forward shreds to
/// analogous to `tvu_quic` in agave
turbine_recv_quic: 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 sanitizeWallclock(self.wallclock);
}
pub fn default(id: Pubkey) LegacyContactInfo {
const unspecified_addr = SocketAddr.initIpv4(.{ 0, 0, 0, 0 }, 0);
const wallclock = getWallclockMs();
return LegacyContactInfo{
.id = id,
.gossip = unspecified_addr,
.turbine_recv = unspecified_addr,
.turbine_recv_quic = 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 initRandom(random: std.rand.Random) LegacyContactInfo {
return LegacyContactInfo{
.id = Pubkey.initRandom(random),
.gossip = SocketAddr.initRandom(random),
.turbine_recv = SocketAddr.initRandom(random),
.turbine_recv_quic = SocketAddr.initRandom(random),
.repair = SocketAddr.initRandom(random),
.tpu = SocketAddr.initRandom(random),
.tpu_forwards = SocketAddr.initRandom(random),
.tpu_vote = SocketAddr.initRandom(random),
.rpc = SocketAddr.initRandom(random),
.rpc_pubsub = SocketAddr.initRandom(random),
.serve_repair = SocketAddr.initRandom(random),
.wallclock = getWallclockMs(),
.shred_version = random.int(u16),
};
}
/// call ContactInfo.deinit to free
pub fn toContactInfo(self: *const LegacyContactInfo, allocator: std.mem.Allocator) !ContactInfo {
var ci = ContactInfo.init(allocator, self.id, self.wallclock, self.shred_version);
try ci.setSocket(.gossip, self.gossip);
try ci.setSocket(.turbine_recv, self.turbine_recv);
try ci.setSocket(.turbine_recv_quic, self.turbine_recv_quic);
try ci.setSocket(.repair, self.repair);
try ci.setSocket(.tpu, self.tpu);
try ci.setSocket(.tpu_forwards, self.tpu_forwards);
try ci.setSocket(.tpu_vote, self.tpu_vote);
try ci.setSocket(.rpc, self.rpc);
try ci.setSocket(.rpc_pubsub, self.rpc_pubsub);
try ci.setSocket(.serve_repair, self.serve_repair);
return ci;
}
pub fn fromContactInfo(ci: *const ContactInfo) LegacyContactInfo {
return .{
.id = ci.pubkey,
.gossip = ci.getSocket(.gossip) orelse SocketAddr.UNSPECIFIED,
.turbine_recv = ci.getSocket(.turbine_recv) orelse SocketAddr.UNSPECIFIED,
.turbine_recv_quic = ci.getSocket(.turbine_recv_quic) orelse SocketAddr.UNSPECIFIED,
.repair = ci.getSocket(.repair) orelse SocketAddr.UNSPECIFIED,
.tpu = ci.getSocket(.tpu) orelse SocketAddr.UNSPECIFIED,
.tpu_forwards = ci.getSocket(.tpu_forwards) orelse SocketAddr.UNSPECIFIED,
.tpu_vote = ci.getSocket(.tpu_vote) orelse SocketAddr.UNSPECIFIED,
.rpc = ci.getSocket(.rpc) orelse SocketAddr.UNSPECIFIED,
.rpc_pubsub = ci.getSocket(.rpc_pubsub) orelse SocketAddr.UNSPECIFIED,
.serve_repair = ci.getSocket(.serve_repair) orelse SocketAddr.UNSPECIFIED,
.wallclock = ci.wallclock,
.shred_version = ci.shred_version,
};
}
};
pub const Vote = struct {
from: Pubkey,
transaction: Transaction,
wallclock: u64,
slot: Slot = 0,
pub const @"!bincode-config:slot" = bincode.FieldConfig(Slot){ .skip = true };
pub fn clone(self: *const Vote, allocator: std.mem.Allocator) error{OutOfMemory}!Vote {
return .{
.from = self.from,
.transaction = try self.transaction.clone(allocator),
.wallclock = self.wallclock,
.slot = self.slot,
};
}
pub fn deinit(self: *const Vote, allocator: std.mem.Allocator) void {
self.transaction.deinit(allocator);
}
pub fn initRandom(random: std.rand.Random) Vote {
return Vote{
.from = Pubkey.initRandom(random),
.transaction = Transaction.EMPTY,
.wallclock = getWallclockMs(),
.slot = random.int(u64),
};
}
pub fn sanitize(self: *const Vote) !void {
try sanitizeWallclock(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 clone(self: *const LowestSlot, allocator: std.mem.Allocator) error{OutOfMemory}!LowestSlot {
const stash = try allocator.alloc(DeprecatedEpochIncompleteSlots, self.stash.len);
for (stash, 0..) |*item, i| item.* = try self.stash[i].clone(allocator);
return .{
.from = self.from,
.root = self.root,
.lowest = self.lowest,
.slots = try allocator.dupe(u64, self.slots),
.stash = stash,
.wallclock = self.wallclock,
};
}
pub fn deinit(self: *const LowestSlot, allocator: std.mem.Allocator) void {
allocator.free(self.slots);
for (self.stash) |*item| item.deinit(allocator);
allocator.free(self.stash);
}
pub fn sanitize(value: *const LowestSlot) !void {
try sanitizeWallclock(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 fn initRandom(random: std.rand.Random) LowestSlot {
var slots: [0]u64 = .{};
var stash: [0]DeprecatedEpochIncompleteSlots = .{};
return LowestSlot{
.from = Pubkey.initRandom(random),
.root = 0,
.lowest = random.int(u64),
.slots = &slots,
.stash = &stash,
.wallclock = getWallclockMs(),
};
}
};
pub const DeprecatedEpochIncompleteSlots = struct {
first: u64,
compression: CompressionType,
compressed_list: []u8,
pub fn clone(self: *const DeprecatedEpochIncompleteSlots, allocator: std.mem.Allocator) error{OutOfMemory}!DeprecatedEpochIncompleteSlots {
return .{
.first = self.first,
.compression = self.compression,
.compressed_list = try allocator.dupe(u8, self.compressed_list),
};
}
pub fn deinit(self: *const DeprecatedEpochIncompleteSlots, allocator: std.mem.Allocator) void {
allocator.free(self.compressed_list);
}
};
pub const CompressionType = enum {
Uncompressed,
GZip,
BZip2,
};
pub const LegacySnapshotHashes = AccountsHashes;
pub const AccountsHashes = struct {
from: Pubkey,
hashes: []const SlotAndHash,
wallclock: u64,
pub fn clone(self: *const AccountsHashes, allocator: std.mem.Allocator) error{OutOfMemory}!AccountsHashes {
return .{
.from = self.from,
.hashes = try allocator.dupe(SlotAndHash, self.hashes),
.wallclock = self.wallclock,
};
}
pub fn deinit(self: *const AccountsHashes, allocator: std.mem.Allocator) void {
allocator.free(self.hashes);
}
pub fn initRandom(random: std.rand.Random) AccountsHashes {
return .{
.from = Pubkey.initRandom(random),
.hashes = &.{},
.wallclock = getWallclockMs(),
};
}
pub fn sanitize(value: *const AccountsHashes) !void {
try sanitizeWallclock(value.wallclock);
for (value.hashes) |*snapshot_hash| {
if (snapshot_hash.slot >= MAX_SLOT) {
return error.ValueOutOfBounds;
}
}
}
};
pub const EpochSlots = struct {
from: Pubkey,
slots: []CompressedSlots,
wallclock: u64,
pub fn clone(self: *const EpochSlots, allocator: std.mem.Allocator) error{OutOfMemory}!EpochSlots {
const slots = try allocator.alloc(CompressedSlots, self.slots.len);
for (slots, 0..) |*slot, i| slot.* = try self.slots[i].clone(allocator);
return .{
.from = self.from,
.slots = slots,
.wallclock = self.wallclock,
};
}
pub fn deinit(self: *const EpochSlots, allocator: std.mem.Allocator) void {
for (self.slots) |*slot| slot.deinit(allocator);
allocator.free(self.slots);
}
pub fn initRandom(random: std.rand.Random) EpochSlots {
var slice: [0]CompressedSlots = .{};
return EpochSlots{
.from = Pubkey.initRandom(random),
.slots = &slice,
.wallclock = getWallclockMs(),
};
}
pub fn sanitize(value: *const EpochSlots) !void {
try sanitizeWallclock(value.wallclock);
for (value.slots) |slot| {
try slot.sanitize();
}
}
};
pub const CompressedSlots = union(enum(u32)) {
Flate2: Flate2,
Uncompressed: Uncompressed,
pub fn clone(self: *const CompressedSlots, allocator: std.mem.Allocator) error{OutOfMemory}!CompressedSlots {
return switch (self.*) {
.Flate2 => |*v| .{ .Flate2 = try v.clone(allocator) },
.Uncompressed => |*v| .{ .Uncompressed = try v.clone(allocator) },
};
}
pub fn deinit(self: *const CompressedSlots, allocator: std.mem.Allocator) void {
switch (self.*) {
.Flate2 => |*v| v.deinit(allocator),
.Uncompressed => |*v| v.deinit(allocator),
}
}
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 clone(self: *const Flate2, allocator: std.mem.Allocator) error{OutOfMemory}!Flate2 {
return .{
.first_slot = self.first_slot,
.num = self.num,
.compressed = try allocator.dupe(u8, self.compressed),
};
}
pub fn deinit(self: *const Flate2, allocator: std.mem.Allocator) void {
allocator.free(self.compressed);
}
pub fn sanitize(self: *const Flate2) !void {
if (self.first_slot >= 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 clone(self: *const Uncompressed, allocator: std.mem.Allocator) error{OutOfMemory}!Uncompressed {
return .{
.first_slot = self.first_slot,
.num = self.num,
.slots = try self.slots.clone(allocator),
};
}
pub fn deinit(self: *const Uncompressed, allocator: std.mem.Allocator) void {
self.slots.deinit(allocator);
}
pub fn sanitize(self: *const Uncompressed) !void {
if (self.first_slot >= 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()
}
};
// TODO: replace logic with another library
pub fn BitVec(comptime T: type) type {
return struct {
bits: ?[]T,
len: usize,
pub fn clone(self: *const BitVec(T), allocator: std.mem.Allocator) error{OutOfMemory}!BitVec(T) {
return .{
.bits = if (self.bits == null) null else try allocator.dupe(T, self.bits.?),
.len = self.len,
};
}
pub fn deinit(self: *const BitVec(T), allocator: std.mem.Allocator) void {
allocator.free(self.bits.?);
}
};
}
pub const LegacyVersion = struct {
from: Pubkey,
wallclock: u64,
version: LegacyVersion1,
pub fn initRandom(random: std.rand.Random) LegacyVersion {
return LegacyVersion{
.from = Pubkey.initRandom(random),
.wallclock = getWallclockMs(),
.version = LegacyVersion1.initRandom(random),
};
}
};
pub const LegacyVersion1 = struct {
major: u16,
minor: u16,
patch: u16,
commit: ?u32, // first 4 bytes of the sha1 commit hash
pub fn initRandom(random: std.rand.Random) LegacyVersion1 {
return LegacyVersion1{
.major = random.int(u16),
.minor = random.int(u16),
.patch = random.int(u16),
.commit = random.int(u32),
};
}
};
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 = getWallclockMs(),
.version = LegacyVersion2.CURRENT,
};
}
pub fn initRandom(random: std.rand.Random) Version {
return Version{
.from = Pubkey.initRandom(random),
.wallclock = getWallclockMs(),
.version = LegacyVersion2.initRandom(random),
};
}
pub fn sanitize(self: *const Self) !void {
try sanitizeWallclock(self.wallclock);
}
};
pub const LegacyVersion2 = struct {
major: u16,
minor: u16,
patch: u16,
commit: ?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, 2996451279, 3488713414);
pub fn initRandom(random: std.rand.Random) Self {
return Self{
.major = random.int(u16),
.minor = random.int(u16),
.patch = random.int(u16),
.commit = random.int(u32),
.feature_set = random.int(u32),
};
}
pub fn init(major: u16, minor: u16, patch: u16, commit: ?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 initRandom(random: std.rand.Random) Self {
return Self{
.from = Pubkey.initRandom(random),
.wallclock = getWallclockMs(),
.timestamp = random.int(u64),
.token = random.int(u64),
};
}
pub fn init(random: std.Random, from: Pubkey, wallclock: u64) Self {
return Self{
.from = from,
.wallclock = wallclock,
.timestamp = @intCast(std.time.microTimestamp()),
.token = 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 fn sanitize(self: *const Self) !void {
try sanitizeWallclock(self.wallclock);
}
};
fn ShredTypeConfig() bincode.FieldConfig(ShredType) {
const S = struct {
pub fn serialize(writer: anytype, data: anytype, params: bincode.Params) !void {
try bincode.write(writer, @intFromEnum(data), params);
return;