forked from blockblaz/zeam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.zig
More file actions
1319 lines (1133 loc) · 58.3 KB
/
node.zig
File metadata and controls
1319 lines (1133 loc) · 58.3 KB
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 enr_lib = @import("enr");
const ENR = enr_lib.ENR;
const utils_lib = @import("@zeam/utils");
const Yaml = @import("yaml").Yaml;
const configs = @import("@zeam/configs");
const api = @import("@zeam/api");
const api_server = @import("api_server.zig");
const event_broadcaster = api.event_broadcaster;
const ChainConfig = configs.ChainConfig;
const Chain = configs.Chain;
const ChainOptions = configs.ChainOptions;
const sft = @import("@zeam/state-transition");
const xev = @import("xev");
const networks = @import("@zeam/network");
const Multiaddr = @import("multiformats").multiaddr.Multiaddr;
const node_lib = @import("@zeam/node");
const key_manager_lib = @import("@zeam/key-manager");
const xmss = @import("@zeam/xmss");
const Clock = node_lib.Clock;
const BeamNode = node_lib.BeamNode;
const types = @import("@zeam/types");
const LoggerConfig = utils_lib.ZeamLoggerConfig;
const NodeCommand = @import("main.zig").NodeCommand;
const zeam_utils = @import("@zeam/utils");
const constants = @import("constants.zig");
const database = @import("@zeam/database");
const json = std.json;
const utils = @import("@zeam/utils");
const ssz = @import("ssz");
const zeam_metrics = @import("@zeam/metrics");
const build_options = @import("build_options");
// Structure to hold parsed ENR fields from validator-config.yaml
const EnrFields = struct {
ip: ?[]const u8 = null,
ip6: ?[]const u8 = null,
tcp: ?u16 = null,
udp: ?u16 = null,
quic: ?u16 = null,
seq: ?u64 = null,
// Allow for custom fields
custom_fields: std.StringHashMap([]const u8),
pub fn deinit(self: *EnrFields, allocator: std.mem.Allocator) void {
if (self.ip) |ip_str| allocator.free(ip_str);
if (self.ip6) |ip6_str| allocator.free(ip6_str);
var iterator = self.custom_fields.iterator();
while (iterator.next()) |entry| {
allocator.free(entry.key_ptr.*);
allocator.free(entry.value_ptr.*);
}
self.custom_fields.deinit();
}
};
/// Represents a validator assignment from annotated_validators.yaml
pub const ValidatorAssignment = struct {
index: usize,
pubkey_hex: []const u8,
privkey_file: []const u8,
pub fn deinit(self: *ValidatorAssignment, allocator: std.mem.Allocator) void {
allocator.free(self.pubkey_hex);
allocator.free(self.privkey_file);
}
};
pub const NodeOptions = struct {
network_id: u32,
node_key: []const u8,
node_key_index: usize,
// 1. a special value of "genesis_bootnode" for validator config means its a genesis bootnode and so
// the configuration is to be picked from genesis
// 2. otherwise validator_config is dir path to this nodes's validator_config.yaml and annotated_validators.yaml
// and one must use all the nodes in genesis nodes.yaml as peers
validator_config: []const u8,
bootnodes: []const []const u8,
validator_assignments: []ValidatorAssignment,
genesis_spec: types.GenesisSpec,
metrics_enable: bool,
api_port: u16,
local_priv_key: []const u8,
logger_config: *LoggerConfig,
database_path: []const u8,
hash_sig_key_dir: []const u8,
node_registry: *node_lib.NodeNameRegistry,
checkpoint_sync_url: ?[]const u8 = null,
pub fn deinit(self: *NodeOptions, allocator: std.mem.Allocator) void {
for (self.bootnodes) |b| allocator.free(b);
allocator.free(self.bootnodes);
for (self.validator_assignments) |*assignment| {
@constCast(assignment).deinit(allocator);
}
allocator.free(self.validator_assignments);
allocator.free(self.local_priv_key);
allocator.free(self.hash_sig_key_dir);
self.node_registry.deinit();
allocator.destroy(self.node_registry);
}
pub fn getValidatorIndices(self: *const NodeOptions, allocator: std.mem.Allocator) ![]usize {
var indices = try allocator.alloc(usize, self.validator_assignments.len);
for (self.validator_assignments, 0..) |assignment, i| {
indices[i] = assignment.index;
}
return indices;
}
};
/// A Node that encapsulates the networking, blockchain, and validator functionalities.
/// It manages the event loop, network interface, clock, and beam node.
pub const Node = struct {
loop: xev.Loop,
network: networks.EthLibp2p,
beam_node: BeamNode,
clock: Clock,
enr: ENR,
options: *const NodeOptions,
allocator: std.mem.Allocator,
logger: zeam_utils.ModuleLogger,
db: database.Db,
key_manager: key_manager_lib.KeyManager,
anchor_state: *types.BeamState,
const Self = @This();
pub fn init(
self: *Self,
allocator: std.mem.Allocator,
options: *const NodeOptions,
) !void {
self.allocator = allocator;
self.options = options;
// Initialize event broadcaster
try event_broadcaster.initGlobalBroadcaster(allocator);
// some base mainnet spec would be loaded to build this up
const chain_spec =
\\{"preset": "mainnet", "name": "devnet0"}
;
const json_options = json.ParseOptions{
.ignore_unknown_fields = true,
.allocate = .alloc_if_needed,
};
var chain_options = (try json.parseFromSlice(ChainOptions, allocator, chain_spec, json_options)).value;
chain_options.genesis_time = options.genesis_spec.genesis_time;
// Set validator_pubkeys from genesis_spec (read from config.yaml via genesisConfigFromYAML)
chain_options.validator_pubkeys = options.genesis_spec.validator_pubkeys;
// transfer ownership of the chain_options to ChainConfig
const chain_config = try ChainConfig.init(Chain.custom, chain_options);
// TODO we seem to be needing one loop because then the events added to loop are not being fired
// in the order to which they have been added even with the an appropriate delay added
// behavior of this further needs to be investigated but for now we will share the same loop
self.loop = try xev.Loop.init(.{});
const addresses = try self.constructMultiaddrs();
self.network = try networks.EthLibp2p.init(allocator, &self.loop, .{
.networkId = options.network_id,
.network_name = chain_config.spec.name,
.listen_addresses = addresses.listen_addresses,
.connect_peers = addresses.connect_peers,
.local_private_key = options.local_priv_key,
.node_registry = options.node_registry,
}, options.logger_config.logger(.network));
errdefer self.network.deinit();
self.clock = try Clock.init(allocator, chain_config.genesis.genesis_time, &self.loop);
errdefer self.clock.deinit(allocator);
var db = try database.Db.open(allocator, options.logger_config.logger(.database), options.database_path);
errdefer db.deinit();
self.logger = options.logger_config.logger(.node);
const anchorState: *types.BeamState = try allocator.create(types.BeamState);
errdefer allocator.destroy(anchorState);
self.anchor_state = anchorState;
// Initialize anchor state with priority: checkpoint URL > database > genesis
var checkpoint_sync_succeeded = false;
if (options.checkpoint_sync_url) |checkpoint_url| {
self.logger.info("checkpoint sync enabled, downloading state from: {s}", .{checkpoint_url});
// Try checkpoint sync, fall back to database/genesis on failure
if (downloadCheckpointState(allocator, checkpoint_url, self.logger)) |downloaded_state| {
self.anchor_state.* = downloaded_state;
// Verify state against genesis config
if (verifyCheckpointState(allocator, self.anchor_state, &chain_config.genesis, self.logger)) {
self.logger.info("checkpoint sync completed successfully, using state at slot {d} as anchor", .{self.anchor_state.slot});
checkpoint_sync_succeeded = true;
} else |verify_err| {
self.logger.warn("checkpoint state verification failed: {}, falling back to database/genesis", .{verify_err});
self.anchor_state.deinit();
}
} else |download_err| {
self.logger.warn("checkpoint sync failed: {}, falling back to database/genesis", .{download_err});
}
}
// Fall back to database/genesis if checkpoint sync was not attempted or failed
if (!checkpoint_sync_succeeded) {
// Try to load the latest finalized state from the database, fallback to genesis
db.loadLatestFinalizedState(self.anchor_state) catch |err| {
self.logger.warn("failed to load latest finalized state from database: {any}", .{err});
try self.anchor_state.genGenesisState(allocator, chain_config.genesis);
};
}
errdefer self.anchor_state.deinit();
const num_validators: usize = @intCast(chain_config.genesis.numValidators());
self.key_manager = key_manager_lib.KeyManager.init(allocator);
errdefer self.key_manager.deinit();
try self.loadValidatorKeypairs(num_validators);
const validator_ids = try options.getValidatorIndices(allocator);
errdefer allocator.free(validator_ids);
try self.beam_node.init(allocator, .{
.nodeId = @intCast(options.node_key_index),
.config = chain_config,
.anchorState = self.anchor_state,
.backend = self.network.getNetworkInterface(),
.clock = &self.clock,
.validator_ids = validator_ids,
.key_manager = &self.key_manager,
.db = db,
.logger_config = options.logger_config,
.node_registry = options.node_registry,
});
// Start API server after chain is initialized so we can pass the chain pointer
if (options.metrics_enable) {
try api.init(allocator);
// Set node lifecycle metrics
zeam_metrics.metrics.lean_node_info.set(.{ .name = "zeam", .version = build_options.version }, 1) catch {};
zeam_metrics.metrics.lean_node_start_time_seconds.set(@intCast(std.time.timestamp()));
try api_server.startAPIServer(allocator, options.api_port, options.logger_config, self.beam_node.chain);
}
}
pub fn deinit(self: *Self) void {
self.clock.deinit(self.allocator);
self.beam_node.deinit();
self.key_manager.deinit();
self.network.deinit();
self.enr.deinit();
self.db.deinit();
self.loop.deinit();
event_broadcaster.deinitGlobalBroadcaster();
self.anchor_state.deinit();
self.allocator.destroy(self.anchor_state);
}
pub fn run(self: *Node) !void {
try self.network.run();
try self.beam_node.run();
const ascii_art =
\\ ███████████████████████████████████████████████████████
\\ ██████████████ ████ ██████████
\\ ███████████ ████████████████ █████████████
\\ █████████ ████████████████████████ ███████████
\\ ██████ █████████████████████████████████ ███████
\\ █████ █████████████████████ █████████████ ██████
\\ ███ ██████████ █ █████ █████████ █████
\\ ███ ███████████ █████ █ █ █ ███████████████ ███
\\ ██ ██████████ ██ ██ ████ ███ ██ ██████████ ████
\\ ██ ██████████ ███ ████ █████████ ███
\\ ██ ███████████ █ ██████ ████ █████████ ███
\\ █ █████████ ████ █████ █████ █████████████ ███
\\ █ ██████████ █ ████ ██ █████ ███████ ███
\\ ██ ██████████ ████████ ██ █ ██████ ███
\\ ██ █████████ ███████ █ ██████████ ███
\\ ███ ██████████ ███ ███ █████████ █ ██
\\ ███ ████████████ ███ ██ ███ █ █ ███████ █████
\\ ███ ████████████ ████ █████ █████████ ██████
\\ █████ █████████ ███ █████ ████████ ███████
\\ ████████ ██████████████████████████ ██████████
\\ ████████ █ ████████████████████ ████████████
\\ █████ ██████ ██████████ ██████████████
\\ █████████████████ ██████████████████
\\ ███████████████████████████████████████████████████████
\\
\\ ███████╗███████╗ █████╗ ███╗ ███╗
\\ ╚══███╔╝██╔════╝██╔══██╗████╗ ████║
\\ ███╔╝ █████╗ ███████║██╔████╔██║
\\ ███╔╝ ██╔══╝ ██╔══██║██║╚██╔╝██║
\\ ███████╗███████╗██║ ██║██║ ╚═╝ ██║
\\ ╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
\\
\\ A blazing fast lean consensus client
;
var encoded_txt_buf: [1000]u8 = undefined;
const encoded_txt = try self.enr.encodeToTxt(&encoded_txt_buf);
const quic_port = try self.enr.getQUIC();
// Use logger.info instead of std.debug.print
self.logger.info("\n{s}", .{ascii_art});
self.logger.info("════════════════════════════════════════════════════════", .{});
self.logger.info(" 🚀 Zeam Lean Node Started Successfully!", .{});
self.logger.info("════════════════════════════════════════════════════════", .{});
self.logger.info(" Node ID: {d}", .{self.options.node_key_index});
self.logger.info(" Listening on QUIC port: {?d}", .{quic_port});
self.logger.info(" ENR: {s}", .{encoded_txt});
self.logger.info("────────────────────────────────────────────────────────", .{});
try self.clock.run();
}
fn constructMultiaddrs(self: *Self) !struct { listen_addresses: []const Multiaddr, connect_peers: []const Multiaddr } {
if (std.mem.eql(u8, self.options.validator_config, "genesis_bootnode")) {
try ENR.decodeTxtInto(&self.enr, self.options.bootnodes[self.options.node_key_index]);
} else {
// Parse validator config to get ENR fields
const validator_config_filepath = try std.mem.concat(self.allocator, u8, &[_][]const u8{
self.options.validator_config,
"/validator-config.yaml",
});
defer self.allocator.free(validator_config_filepath);
var parsed_validator_config = try utils_lib.loadFromYAMLFile(self.allocator, validator_config_filepath);
defer parsed_validator_config.deinit(self.allocator);
// Get ENR fields from validator config
var enr_fields = try getEnrFieldsFromValidatorConfig(self.allocator, self.options.node_key, parsed_validator_config);
defer enr_fields.deinit(self.allocator);
// Construct ENR from fields and private key
self.enr = try constructENRFromFields(self.allocator, self.options.local_priv_key, enr_fields);
}
// Overriding the IP to 0.0.0.0 to listen on all interfaces
try self.enr.kvs.put("ip", "\x00\x00\x00\x00");
var node_multiaddrs = try self.enr.multiaddrP2PQUIC(self.allocator);
defer node_multiaddrs.deinit(self.allocator);
// move the ownership to the `EthLibp2p`, will be freed in its deinit
const listen_addresses = try node_multiaddrs.toOwnedSlice(self.allocator);
errdefer {
for (listen_addresses) |addr| addr.deinit();
self.allocator.free(listen_addresses);
}
var connect_peer_list: std.ArrayListUnmanaged(Multiaddr) = .empty;
defer connect_peer_list.deinit(self.allocator);
for (self.options.bootnodes, 0..) |n, i| {
// don't exclude any entry from nodes.yaml if this is not a genesis bootnode
if (i != self.options.node_key_index or !std.mem.eql(u8, self.options.validator_config, "genesis_bootnode")) {
var n_enr: ENR = undefined;
try ENR.decodeTxtInto(&n_enr, n);
var peer_multiaddr_list = try n_enr.multiaddrP2PQUIC(self.allocator);
defer peer_multiaddr_list.deinit(self.allocator);
const peer_multiaddrs = try peer_multiaddr_list.toOwnedSlice(self.allocator);
defer self.allocator.free(peer_multiaddrs);
try connect_peer_list.appendSlice(self.allocator, peer_multiaddrs);
}
}
// move the ownership to the `EthLibp2p`, will be freed in its deinit
const connect_peers = try connect_peer_list.toOwnedSlice(self.allocator);
errdefer {
for (connect_peers) |addr| addr.deinit();
self.allocator.free(connect_peers);
}
return .{ .listen_addresses = listen_addresses, .connect_peers = connect_peers };
}
fn loadValidatorKeypairs(
self: *Self,
num_validators: usize,
) !void {
if (self.options.validator_assignments.len == 0) {
return error.NoValidatorAssignments;
}
const hash_sig_key_dir = self.options.hash_sig_key_dir;
for (self.options.validator_assignments) |assignment| {
if (assignment.index >= num_validators) {
return error.HashSigValidatorIndexOutOfRange;
}
const privkey_file = assignment.privkey_file;
if (!std.mem.endsWith(u8, privkey_file, "_sk.ssz")) {
return error.InvalidPrivkeyFileFormat;
}
const base = privkey_file[0 .. privkey_file.len - 7]; // Remove "_sk.ssz"
const sk_path = try std.fmt.allocPrint(self.allocator, "{s}/{s}_sk.ssz", .{ hash_sig_key_dir, base });
defer self.allocator.free(sk_path);
const pk_path = try std.fmt.allocPrint(self.allocator, "{s}/{s}_pk.ssz", .{ hash_sig_key_dir, base });
defer self.allocator.free(pk_path);
// Read secret key
var sk_file = std.fs.cwd().openFile(sk_path, .{}) catch |err| switch (err) {
error.FileNotFound => return error.HashSigSecretKeyMissing,
else => return err,
};
defer sk_file.close();
const secret_ssz = try sk_file.readToEndAlloc(self.allocator, constants.MAX_HASH_SIG_ENCODED_KEY_SIZE);
defer self.allocator.free(secret_ssz);
// Read public key
var pk_file = std.fs.cwd().openFile(pk_path, .{}) catch |err| switch (err) {
error.FileNotFound => return error.HashSigPublicKeyMissing,
else => return err,
};
defer pk_file.close();
const public_ssz = try pk_file.readToEndAlloc(self.allocator, constants.MAX_HASH_SIG_ENCODED_KEY_SIZE);
defer self.allocator.free(public_ssz);
var keypair = try xmss.KeyPair.fromSsz(
self.allocator,
secret_ssz,
public_ssz,
);
errdefer keypair.deinit();
try self.key_manager.addKeypair(assignment.index, keypair);
}
}
};
/// Builds the start options for a node based on the provided command and options.
/// It loads the necessary configuration files, parses them, and populates the
/// `StartNodeOptions` structure.
/// The caller is responsible for freeing the allocated resources in `StartNodeOptions`.
pub fn buildStartOptions(
allocator: std.mem.Allocator,
node_cmd: NodeCommand,
opts: *NodeOptions,
) !void {
try utils_lib.checkDIRExists(node_cmd.custom_genesis);
const config_filepath = try std.mem.concat(allocator, u8, &[_][]const u8{ node_cmd.custom_genesis, "/config.yaml" });
defer allocator.free(config_filepath);
const bootnodes_filepath = try std.mem.concat(allocator, u8, &[_][]const u8{ node_cmd.custom_genesis, "/nodes.yaml" });
defer allocator.free(bootnodes_filepath);
const validators_filepath = try std.mem.concat(allocator, u8, &[_][]const u8{
if (std.mem.eql(u8, node_cmd.validator_config, "genesis_bootnode"))
//
node_cmd.custom_genesis
else
node_cmd.validator_config,
"/annotated_validators.yaml",
});
defer allocator.free(validators_filepath);
const validator_config_filepath = try std.mem.concat(allocator, u8, &[_][]const u8{
if (std.mem.eql(u8, node_cmd.validator_config, "genesis_bootnode"))
//
node_cmd.custom_genesis
else
node_cmd.validator_config,
"/validator-config.yaml",
});
defer allocator.free(validator_config_filepath);
// TODO: support genesis file loading when ssz library supports it
// const genesis_filepath = try std.mem.concat(allocator, &[_][]const u8{custom_genesis, "/genesis.ssz"});
// defer allocator.free(genesis_filepath);
var parsed_bootnodes = try utils_lib.loadFromYAMLFile(allocator, bootnodes_filepath);
defer parsed_bootnodes.deinit(allocator);
var parsed_config = try utils_lib.loadFromYAMLFile(allocator, config_filepath);
defer parsed_config.deinit(allocator);
var parsed_validators = try utils_lib.loadFromYAMLFile(allocator, validators_filepath);
defer parsed_validators.deinit(allocator);
var parsed_validator_config = try utils_lib.loadFromYAMLFile(allocator, validator_config_filepath);
defer parsed_validator_config.deinit(allocator);
const bootnodes = try nodesFromYAML(allocator, parsed_bootnodes);
errdefer {
for (bootnodes) |b| allocator.free(b);
allocator.free(bootnodes);
}
if (bootnodes.len == 0) {
return error.InvalidNodesConfig;
}
const genesis_spec = try configs.genesisConfigFromYAML(allocator, parsed_config, node_cmd.override_genesis_time);
const validator_assignments = try validatorAssignmentsFromYAML(allocator, opts.node_key, parsed_validators);
errdefer {
for (validator_assignments) |*a| {
@constCast(a).deinit(allocator);
}
allocator.free(validator_assignments);
}
if (validator_assignments.len == 0) {
return error.InvalidValidatorConfig;
}
const local_priv_key = try getPrivateKeyFromValidatorConfig(allocator, opts.node_key, parsed_validator_config);
const node_key_index = try nodeKeyIndexFromYaml(opts.node_key, parsed_validator_config);
const hash_sig_key_dir = try std.mem.concat(allocator, u8, &[_][]const u8{
node_cmd.custom_genesis,
"/",
node_cmd.@"sig-keys-dir",
});
// Populate node name registry with peer information
populateNodeNameRegistry(allocator, opts.node_registry, validator_config_filepath, validators_filepath) catch |err| {
std.log.warn("Failed to populate node name registry: {any}", .{err});
};
opts.bootnodes = bootnodes;
opts.validator_assignments = validator_assignments;
opts.local_priv_key = local_priv_key;
opts.genesis_spec = genesis_spec;
opts.node_key_index = node_key_index;
opts.hash_sig_key_dir = hash_sig_key_dir;
opts.checkpoint_sync_url = node_cmd.@"checkpoint-sync-url";
}
/// Downloads finalized checkpoint state from the given URL and deserializes it
/// Returns the deserialized state. The caller is responsible for calling deinit on it.
fn downloadCheckpointState(
allocator: std.mem.Allocator,
url: []const u8,
logger: zeam_utils.ModuleLogger,
) !types.BeamState {
logger.info("downloading checkpoint state from: {s}", .{url});
// Parse URL using std.Uri
const uri = std.Uri.parse(url) catch return error.InvalidUrl;
// Initialize HTTP client
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
// Buffer for server response headers
var server_header_buffer: [16 * 1024]u8 = undefined;
// Open HTTP request
var req = client.open(.GET, uri, .{
.server_header_buffer = &server_header_buffer,
}) catch |err| {
logger.err("failed to open HTTP connection: {}", .{err});
return error.ConnectionFailed;
};
defer req.deinit();
// Send the request
req.send() catch |err| {
logger.err("failed to send HTTP request: {}", .{err});
return error.RequestFailed;
};
// Wait for response
req.wait() catch |err| {
logger.err("failed to receive HTTP response: {}", .{err});
return error.ResponseFailed;
};
// Check HTTP status
if (req.response.status != .ok) {
logger.err("checkpoint sync failed: HTTP {d}", .{@intFromEnum(req.response.status)});
return error.HttpError;
}
// Read response body
var ssz_data = std.ArrayList(u8).init(allocator);
errdefer ssz_data.deinit();
var buffer: [8192]u8 = undefined;
while (true) {
const bytes_read = req.reader().read(&buffer) catch |err| {
logger.err("failed to read response body: {}", .{err});
return error.ReadFailed;
};
if (bytes_read == 0) break;
try ssz_data.appendSlice(buffer[0..bytes_read]);
}
logger.info("downloaded checkpoint state: {d} bytes", .{ssz_data.items.len});
// Deserialize SSZ state
// Use arena allocator for deserialization as SSZ types may allocate
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var checkpoint_state: types.BeamState = undefined;
try ssz.deserialize(types.BeamState, ssz_data.items, &checkpoint_state, arena.allocator());
logger.info("successfully deserialized checkpoint state at slot {d}", .{checkpoint_state.slot});
// Clone the state to move it out of the arena using the proper cloning function
var cloned_state: types.BeamState = undefined;
try types.sszClone(allocator, types.BeamState, checkpoint_state, &cloned_state);
return cloned_state;
}
/// Verifies checkpoint state against the genesis configuration
/// Validates that the downloaded state is consistent with expected genesis parameters
/// Also computes and logs the state root and block root
fn verifyCheckpointState(
allocator: std.mem.Allocator,
state: *const types.BeamState,
genesis_spec: *const types.GenesisSpec,
logger: zeam_utils.ModuleLogger,
) !void {
// Verify genesis timestamp matches
if (state.config.genesis_time != genesis_spec.genesis_time) {
logger.err("checkpoint state verification failed: genesis time mismatch (expected={d}, got={d})", .{
genesis_spec.genesis_time,
state.config.genesis_time,
});
return error.GenesisTimeMismatch;
}
// Verify number of validators matches genesis config
const expected_validators = genesis_spec.numValidators();
const actual_validators = state.validators.len();
if (actual_validators != expected_validators) {
logger.err("checkpoint state verification failed: validator count mismatch (expected={d}, got={d})", .{
expected_validators,
actual_validators,
});
return error.ValidatorCountMismatch;
}
// Verify state has validators
if (actual_validators == 0) {
logger.err("checkpoint state verification failed: no validators in state", .{});
return error.NoValidators;
}
// Verify each validator pubkey matches genesis config
const state_validators = state.validators.constSlice();
for (genesis_spec.validator_pubkeys, 0..) |expected_pubkey, i| {
const actual_pubkey = state_validators[i].pubkey;
if (!std.mem.eql(u8, &expected_pubkey, &actual_pubkey)) {
logger.err("checkpoint state verification failed: validator pubkey mismatch at index {d}", .{i});
return error.ValidatorPubkeyMismatch;
}
}
// Generate state block header with correct state_root
// (latest_block_header.state_root is zero; genStateBlockHeader computes and sets it)
const state_block_header = try state.genStateBlockHeader(allocator);
// Calculate the block root from the properly constructed block header
var block_root: types.Root = undefined;
try ssz.hashTreeRoot(types.BeamBlockHeader, state_block_header, &block_root, allocator);
logger.info("checkpoint state verified: slot={d}, genesis_time={d}, validators={d}, state_root=0x{s}, block_root=0x{s}", .{
state.slot,
state.config.genesis_time,
actual_validators,
std.fmt.fmtSliceHexLower(&state_block_header.state_root),
std.fmt.fmtSliceHexLower(&block_root),
});
}
/// Parses the nodes from a YAML configuration.
/// Expects a YAML structure like:
/// ```yaml
/// - enr1...
/// - enr2...
/// ```
/// Returns a set of ENR strings. The caller is responsible for freeing the returned slice.
fn nodesFromYAML(allocator: std.mem.Allocator, nodes_config: Yaml) ![]const []const u8 {
const temp_nodes = try nodes_config.parse(allocator, [][]const u8);
defer allocator.free(temp_nodes);
var nodes = try allocator.alloc([]const u8, temp_nodes.len);
errdefer {
for (nodes) |node| allocator.free(node);
allocator.free(nodes);
}
for (temp_nodes, 0..) |temp_node, i| {
nodes[i] = try allocator.dupe(u8, temp_node);
}
return nodes;
}
/// Parses the validator indices for a given node from a YAML configuration.
/// Expects a YAML structure like:
/// ```yaml
/// node_0:
/// - 0
/// - 1
/// node_1:
/// Parses the validator assignments for a given node from a YAML configuration.
/// Expects a YAML structure like:
/// ```yaml
/// zeam_0:
/// - index: 0
/// pubkey_hex: 812f8540481ce70515d43b451cedcf6b4e3177312821fa541df6375c20ced55196ad245a00335d425e86817bdbde75536c986c25
/// privkey_file: validator_0_sk.json
/// - index: 3
/// pubkey_hex: a47e01144cd43e3efddef56da069f418d9aac406c29589314f74b00158437375e51b1213ecbbf23d47fd9e05933cfb24ac84e72d
/// privkey_file: validator_3_sk.json
/// ```
/// where `node_key` (e.g., "zeam_0") is the key for the node's validator assignments.
/// Returns a slice of ValidatorAssignment. The caller is responsible for freeing the returned slice.
fn validatorAssignmentsFromYAML(allocator: std.mem.Allocator, node_key: []const u8, validators: Yaml) ![]ValidatorAssignment {
var assignments: std.ArrayListUnmanaged(ValidatorAssignment) = .empty;
defer assignments.deinit(allocator);
errdefer {
for (assignments.items) |*a| {
@constCast(a).deinit(allocator);
}
}
const node_validators = validators.docs.items[0].map.get(node_key) orelse return error.InvalidNodeKey;
if (node_validators != .list) return error.InvalidValidatorConfig;
for (node_validators.list) |item| {
if (item != .map) return error.InvalidValidatorConfig;
const index_value = item.map.get("index") orelse return error.InvalidValidatorConfig;
if (index_value != .int) return error.InvalidValidatorConfig;
const pubkey_value = item.map.get("pubkey_hex") orelse return error.InvalidValidatorConfig;
if (pubkey_value != .string) return error.InvalidValidatorConfig;
const privkey_value = item.map.get("privkey_file") orelse return error.InvalidValidatorConfig;
if (privkey_value != .string) return error.InvalidValidatorConfig;
const assignment = ValidatorAssignment{
.index = @intCast(index_value.int),
.pubkey_hex = try allocator.dupe(u8, pubkey_value.string),
.privkey_file = try allocator.dupe(u8, privkey_value.string),
};
try assignments.append(allocator, assignment);
}
return try assignments.toOwnedSlice(allocator);
}
// Parses the index for a given node key from a YAML configuration.
// ```yaml
// shuffle: roundrobin
// validators:
// - name: "zeam_0"
// # node id 7d0904dc6d8d7130e0e68d5d3175d0c3cf470f8725f67bd8320882f5b9753cc0
// # peer id 16Uiu2HAkvi2sxT75Bpq1c7yV2FjnSQJJ432d6jeshbmfdJss1i6f
// privkey: "bdf953adc161873ba026330c56450453f582e3c4ee6cb713644794bcfdd85fe5"
// enrFields:
// # verify /ip4/127.0.0.1/udp/9000/quic-v1/p2p/16Uiu2HAkvi2sxT75Bpq1c7yV2FjnSQJJ432d6jeshbmfdJss1i6f
// ip: "127.0.0.1"
// quic: 9000
// count: 1 # number of indices for this node
//```
fn nodeKeyIndexFromYaml(node_key: []const u8, validator_config: Yaml) !usize {
var index: usize = 0;
for (validator_config.docs.items[0].map.get("validators").?.list) |entry| {
const name_value = entry.map.get("name").?;
if (name_value == .string and std.mem.eql(u8, name_value.string, node_key)) {
return index;
}
index += 1;
}
return error.InvalidNodeKey;
}
fn getPrivateKeyFromValidatorConfig(allocator: std.mem.Allocator, node_key: []const u8, validator_config: Yaml) ![]const u8 {
for (validator_config.docs.items[0].map.get("validators").?.list) |entry| {
const name_value = entry.map.get("name").?;
if (name_value == .string and std.mem.eql(u8, name_value.string, node_key)) {
const privkey_value = entry.map.get("privkey").?;
if (privkey_value == .string) {
return try allocator.dupe(u8, privkey_value.string);
} else {
return error.InvalidPrivateKeyFormat;
}
}
}
return error.InvalidNodeKey;
}
fn getEnrFieldsFromValidatorConfig(allocator: std.mem.Allocator, node_key: []const u8, validator_config: Yaml) !EnrFields {
for (validator_config.docs.items[0].map.get("validators").?.list) |entry| {
const name_value = entry.map.get("name").?;
if (name_value == .string and std.mem.eql(u8, name_value.string, node_key)) {
const enr_fields_value = entry.map.get("enrFields");
if (enr_fields_value == null) {
return error.MissingEnrFields;
}
var enr_fields = EnrFields{
.custom_fields = std.StringHashMap([]const u8).init(allocator),
};
errdefer enr_fields.deinit(allocator);
const fields_map = enr_fields_value.?.map;
// Parse known fields
if (fields_map.get("ip")) |ip_value| {
if (ip_value == .string) {
enr_fields.ip = try allocator.dupe(u8, ip_value.string);
}
}
if (fields_map.get("ip6")) |ip6_value| {
if (ip6_value == .string) {
enr_fields.ip6 = try allocator.dupe(u8, ip6_value.string);
}
}
if (fields_map.get("tcp")) |tcp_value| {
if (tcp_value == .int) {
enr_fields.tcp = @intCast(tcp_value.int);
}
}
if (fields_map.get("udp")) |udp_value| {
if (udp_value == .int) {
enr_fields.udp = @intCast(udp_value.int);
}
}
if (fields_map.get("quic")) |quic_value| {
if (quic_value == .int) {
enr_fields.quic = @intCast(quic_value.int);
}
}
if (fields_map.get("seq")) |seq_value| {
if (seq_value == .int) {
enr_fields.seq = @intCast(seq_value.int);
}
}
// Parse custom fields
var iterator = fields_map.iterator();
while (iterator.next()) |kv| {
const key = kv.key_ptr.*;
const value = kv.value_ptr.*;
// Skip known fields
if (std.mem.eql(u8, key, "ip") or
std.mem.eql(u8, key, "ip6") or
std.mem.eql(u8, key, "tcp") or
std.mem.eql(u8, key, "udp") or
std.mem.eql(u8, key, "quic") or
std.mem.eql(u8, key, "seq"))
{
continue;
}
// Handle custom field based on type
if (value == .string) {
const key_copy = try allocator.dupe(u8, key);
const value_copy = try allocator.dupe(u8, value.string);
try enr_fields.custom_fields.put(key_copy, value_copy);
} else if (value == .int) {
// Convert integer to string for custom fields with proper padding
const value_str = try std.fmt.allocPrint(allocator, "0x{x:0>8}", .{@as(u32, @intCast(value.int))});
const key_copy = try allocator.dupe(u8, key);
try enr_fields.custom_fields.put(key_copy, value_str);
}
}
return enr_fields;
}
}
return error.InvalidNodeKey;
}
fn constructENRFromFields(allocator: std.mem.Allocator, private_key: []const u8, enr_fields: EnrFields) !ENR {
// Clean up private key (remove 0x prefix if present)
const secret_key_str = if (std.mem.startsWith(u8, private_key, "0x"))
private_key[2..]
else
private_key;
if (secret_key_str.len != 64) {
return error.InvalidSecretKeyLength;
}
// Create SignableENR from private key
var signable_enr = enr_lib.SignableENR.fromSecretKeyString(secret_key_str) catch {
return error.ENRCreationFailed;
};
// Set IP address (IPv4)
if (enr_fields.ip) |ip_str| {
const ip_addr = std.net.Ip4Address.parse(ip_str, 0) catch {
return error.InvalidIPAddress;
};
const ip_addr_bytes = std.mem.asBytes(&ip_addr.sa.addr);
signable_enr.set("ip", ip_addr_bytes) catch {
return error.ENRSetIPFailed;
};
}
// Set IP address (IPv6)
if (enr_fields.ip6) |ip6_str| {
const ip6_addr = std.net.Ip6Address.parse(ip6_str, 0) catch {
return error.InvalidIP6Address;
};
const ip6_addr_bytes = std.mem.asBytes(&ip6_addr.sa.addr);
signable_enr.set("ip6", ip6_addr_bytes) catch {
return error.ENRSetIP6Failed;
};
}
// Set TCP port
if (enr_fields.tcp) |tcp_port| {
var tcp_bytes: [2]u8 = undefined;
std.mem.writeInt(u16, &tcp_bytes, tcp_port, .big);
signable_enr.set("tcp", &tcp_bytes) catch {
return error.ENRSetTCPFailed;
};
}
// Set UDP port
if (enr_fields.udp) |udp_port| {
var udp_bytes: [2]u8 = undefined;
std.mem.writeInt(u16, &udp_bytes, udp_port, .big);
signable_enr.set("udp", &udp_bytes) catch {
return error.ENRSetUDPFailed;
};
}
// Set QUIC port
if (enr_fields.quic) |quic_port| {
var quic_bytes: [2]u8 = undefined;
std.mem.writeInt(u16, &quic_bytes, quic_port, .big);
signable_enr.set("quic", &quic_bytes) catch {
return error.ENRSetQUICFailed;
};
}
// Set sequence number
if (enr_fields.seq) |seq_num| {
var seq_bytes: [8]u8 = undefined;
std.mem.writeInt(u64, &seq_bytes, seq_num, .big);
signable_enr.set("seq", &seq_bytes) catch {
return error.ENRSetSEQFailed;
};
}
// Set custom fields
var custom_iterator = enr_fields.custom_fields.iterator();
while (custom_iterator.next()) |kv| {
const key = kv.key_ptr.*;
const value = kv.value_ptr.*;
// Try to parse as hex if it starts with 0x
if (std.mem.startsWith(u8, value, "0x")) {
const hex_value = value[2..];
if (hex_value.len % 2 != 0) {
return error.InvalidHexValue;
}
const bytes = try allocator.alloc(u8, hex_value.len / 2);
defer allocator.free(bytes);
_ = std.fmt.hexToBytes(bytes, hex_value) catch {
return error.InvalidHexFormat;
};
signable_enr.set(key, bytes) catch {
return error.ENRSetCustomFieldFailed;
};
} else {
// Treat as string
signable_enr.set(key, value) catch {
return error.ENRSetCustomFieldFailed;
};
}
}
// Convert SignableENR to ENR
var buffer: [1024]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
const writer = fbs.writer();
try enr_lib.writeSignableENR(writer, &signable_enr);
const enr_text = fbs.getWritten();
var enr: ENR = undefined;
try ENR.decodeTxtInto(&enr, enr_text);
return enr;
}
/// Populate a NodeNameRegistry from validator-config.yaml and validators.yaml
/// This creates mappings from peer IDs and validator indices to node names
pub fn populateNodeNameRegistry(
allocator: std.mem.Allocator,