forked from blockblaz/zeam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.zig
More file actions
1821 lines (1588 loc) · 80.5 KB
/
chain.zig
File metadata and controls
1821 lines (1588 loc) · 80.5 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 Allocator = std.mem.Allocator;
const json = std.json;
const configs = @import("@zeam/configs");
const types = @import("@zeam/types");
const stf = @import("@zeam/state-transition");
const ssz = @import("ssz");
const networks = @import("@zeam/network");
const params = @import("@zeam/params");
const api = @import("@zeam/api");
const zeam_metrics = @import("@zeam/metrics");
const database = @import("@zeam/database");
const event_broadcaster = api.event_broadcaster;
const zeam_utils = @import("@zeam/utils");
const keymanager = @import("@zeam/key-manager");
const utils = @import("./utils.zig");
pub const fcFactory = @import("./forkchoice.zig");
const constants = @import("./constants.zig");
const networkFactory = @import("./network.zig");
const PeerInfo = networkFactory.PeerInfo;
const NodeNameRegistry = networks.NodeNameRegistry;
const ZERO_SIGBYTES = types.ZERO_SIGBYTES;
pub const BlockProductionParams = struct {
slot: usize,
proposer_index: usize,
};
pub const AttestationConstructionParams = struct {
slot: types.Slot,
};
pub const ChainOpts = struct {
config: configs.ChainConfig,
anchorState: *types.BeamState,
nodeId: u32,
logger_config: *zeam_utils.ZeamLoggerConfig,
db: database.Db,
node_registry: *const NodeNameRegistry,
force_block_production: bool = false,
};
pub const CachedProcessedBlockInfo = struct {
postState: ?*types.BeamState = null,
blockRoot: ?types.Root = null,
pruneForkchoice: bool = true,
};
pub const GossipProcessingResult = struct {
processed_block_root: ?types.Root = null,
missing_attestation_roots: []types.Root = &[_]types.Root{},
};
pub const ProducedBlock = struct {
block: types.BeamBlock,
blockRoot: types.Root,
// Aggregated signatures corresponding to attestations in the block body.
attestation_signatures: types.AttestationSignatures,
pub fn deinit(self: *ProducedBlock) void {
self.block.deinit();
for (self.attestation_signatures.slice()) |*sig_group| {
sig_group.deinit();
}
self.attestation_signatures.deinit();
}
};
pub const BeamChain = struct {
config: configs.ChainConfig,
anchor_state: *types.BeamState,
forkChoice: fcFactory.ForkChoice,
allocator: Allocator,
// from finalized onwards to recent
states: std.AutoHashMap(types.Root, *types.BeamState),
nodeId: u32,
// This struct needs to contain the zeam_logger_config to be able to call `maybeRotate`
// For all other modules, we just need module_logger
zeam_logger_config: *zeam_utils.ZeamLoggerConfig,
module_logger: zeam_utils.ModuleLogger,
stf_logger: zeam_utils.ModuleLogger,
block_building_logger: zeam_utils.ModuleLogger,
registered_validator_ids: []usize = &[_]usize{},
db: database.Db,
// Track last-emitted checkpoints to avoid duplicate SSE events (e.g., genesis spam)
last_emitted_justified: types.Checkpoint,
last_emitted_finalized: types.Checkpoint,
connected_peers: *const std.StringHashMap(PeerInfo),
node_registry: *const NodeNameRegistry,
force_block_production: bool,
// Cached finalized state loaded from database (separate from states map to avoid affecting pruning)
cached_finalized_state: ?*types.BeamState = null,
const Self = @This();
pub fn init(
allocator: Allocator,
opts: ChainOpts,
connected_peers: *const std.StringHashMap(PeerInfo),
) !Self {
const logger_config = opts.logger_config;
const fork_choice = try fcFactory.ForkChoice.init(allocator, .{
.config = opts.config,
.anchorState = opts.anchorState,
.logger = logger_config.logger(.forkchoice),
});
var states = std.AutoHashMap(types.Root, *types.BeamState).init(allocator);
const cloned_anchor_state = try allocator.create(types.BeamState);
try types.sszClone(allocator, types.BeamState, opts.anchorState.*, cloned_anchor_state);
try states.put(fork_choice.head.blockRoot, cloned_anchor_state);
return Self{
.nodeId = opts.nodeId,
.config = opts.config,
.forkChoice = fork_choice,
.allocator = allocator,
.states = states,
.anchor_state = opts.anchorState,
.zeam_logger_config = logger_config,
.module_logger = logger_config.logger(.chain),
.stf_logger = logger_config.logger(.state_transition),
.block_building_logger = logger_config.logger(.state_transition_block_building),
.db = opts.db,
.last_emitted_justified = fork_choice.fcStore.latest_justified,
.last_emitted_finalized = fork_choice.fcStore.latest_finalized,
.connected_peers = connected_peers,
.node_registry = opts.node_registry,
.force_block_production = opts.force_block_production,
};
}
pub fn deinit(self: *Self) void {
var it = self.states.iterator();
while (it.next()) |entry| {
entry.value_ptr.*.deinit();
self.allocator.destroy(entry.value_ptr.*);
}
self.states.deinit();
// Clean up cached finalized state if present
if (self.cached_finalized_state) |cached_state| {
cached_state.deinit();
self.allocator.destroy(cached_state);
}
// assume the allocator of config is same as self.allocator
self.config.deinit(self.allocator);
// self.anchor_state.deinit();
}
pub fn registerValidatorIds(self: *Self, validator_ids: []usize) void {
// right now it's simple assignment but eventually it should be a set
// tacking registrations and keeping it alive for 3*2=6 slots
self.registered_validator_ids = validator_ids;
zeam_metrics.metrics.lean_validators_count.set(self.registered_validator_ids.len);
}
pub fn onInterval(self: *Self, time_intervals: usize) !void {
// see if the node has a proposal this slot to properly tick
// forkchoice head
const slot = @divFloor(time_intervals, constants.INTERVALS_PER_SLOT);
const interval = time_intervals % constants.INTERVALS_PER_SLOT;
// Update current slot metric (wall-clock time slot)
zeam_metrics.metrics.lean_current_slot.set(slot);
var has_proposal = false;
if (interval == 0) {
const num_validators: usize = @intCast(self.config.genesis.numValidators());
const slot_proposer_id = slot % num_validators;
if (std.mem.indexOfScalar(usize, self.registered_validator_ids, slot_proposer_id)) |index| {
_ = index;
has_proposal = true;
}
}
self.module_logger.debug("ticking chain to time(intervals)={d} = slot={d} interval={d} has_proposal={} ", .{
time_intervals,
slot,
interval,
has_proposal,
});
try self.forkChoice.onInterval(time_intervals, has_proposal);
if (interval == 1) {
// interval to attest so we should put out the chain status information to the user along with
// latest head which most likely should be the new block received and processed
const islot: isize = @intCast(slot);
self.printSlot(islot, self.connected_peers.count());
// Periodic pruning: prune old non-canonical states every N slots
// This ensures we prune even when finalization doesn't advance
if (slot > 0 and slot % constants.FORKCHOICE_PRUNING_INTERVAL_SLOTS == 0) {
const finalized = self.forkChoice.fcStore.latest_finalized;
// no need to work extra if finalization is not far behind
if (finalized.slot + 2 * constants.FORKCHOICE_PRUNING_INTERVAL_SLOTS < slot) {
self.module_logger.warn("finalization slot={d} too far behind the current slot={d}", .{ finalized.slot, slot });
const pruningAnchor = try self.forkChoice.getCanonicalAncestorAtDepth(constants.FORKCHOICE_PRUNING_INTERVAL_SLOTS);
// prune if finalization hasn't happened since a long time
if (pruningAnchor.slot > finalized.slot) {
self.module_logger.info("periodic pruning triggered at slot {d} (finalized slot={d} pruning anchor={d})", .{
slot,
finalized.slot,
pruningAnchor.slot,
});
const analysis_result = try self.forkChoice.getCanonicalityAnalysis(pruningAnchor.blockRoot, finalized.root, null);
const depth_confirmed_roots = analysis_result[0];
const non_finalized_descendants = analysis_result[1];
const non_canonical_roots = analysis_result[2];
defer self.allocator.free(depth_confirmed_roots);
defer self.allocator.free(non_finalized_descendants);
defer self.allocator.free(non_canonical_roots);
const states_count_before: isize = self.states.count();
_ = self.pruneStates(depth_confirmed_roots[1..depth_confirmed_roots.len], "confirmed ancestors");
_ = self.pruneStates(non_canonical_roots, "confirmed non canonical");
const pruned_count = states_count_before - self.states.count();
self.module_logger.info("pruned states={d} at slot={d} (finalized slot={d} pruning anchor={d})", .{
//
pruned_count,
slot,
finalized.slot,
pruningAnchor.slot,
});
} else {
self.module_logger.info("skipping periodic pruning at slot={d} since finalization not behind pruning anchor (finalized slot={d} pruning anchor={d})", .{
slot,
finalized.slot,
pruningAnchor.slot,
});
}
} else {
self.module_logger.info("skipping periodic pruning at current slot={d} since finalization slot={d} not behind", .{
slot,
finalized.slot,
});
}
}
}
// check if log rotation is needed
self.zeam_logger_config.maybeRotate() catch |err| {
self.module_logger.err("error rotating log file: {any}", .{err});
};
}
pub fn produceBlock(self: *Self, opts: BlockProductionParams) !ProducedBlock {
// dump the vote tracker, letting this stay here commented for handy debugging activation
// var iterator = self.forkChoice.attestations.iterator();
// while (iterator.next()) |entry| {
// var latest_new: []const u8 = "null";
// if (entry.value_ptr.latestNew) |latest_new_in| {
// if (latest_new_in.attestation) |latest_new_att| {
// latest_new = try latest_new_att.message.toJsonString(self.allocator);
// }
// }
// self.module_logger.warn("validator id={d} vote is={s}", .{ entry.key_ptr.*, latest_new });
// }
// right now with integrated validator into node produceBlock is always gurranteed to be
// called post ticking the chain to the correct time, but once validator is separated
// one must make the forkchoice tick to the right time if there is a race condition
// however in that scenario forkchoice also needs to be protected by mutex/kept thread safe
const chainHead = try self.forkChoice.updateHead();
const signed_attestations = try self.forkChoice.getProposalAttestations();
defer self.allocator.free(signed_attestations);
const parent_root = chainHead.blockRoot;
const pre_state = self.states.get(parent_root) orelse return BlockProductionError.MissingPreState;
var post_state_opt: ?*types.BeamState = try self.allocator.create(types.BeamState);
errdefer if (post_state_opt) |post_state_ptr| {
post_state_ptr.deinit();
self.allocator.destroy(post_state_ptr);
};
const post_state = post_state_opt.?;
try types.sszClone(self.allocator, types.BeamState, pre_state.*, post_state);
var aggregation_opt: ?types.AggregatedAttestationsResult = try types.aggregateSignedAttestations(self.allocator, signed_attestations);
errdefer if (aggregation_opt) |*aggregation| aggregation.deinit();
const aggregation = &aggregation_opt.?;
// keeping for later when execution will be integrated into lean
// const timestamp = self.config.genesis.genesis_time + opts.slot * params.SECONDS_PER_SLOT;
var block = types.BeamBlock{
.slot = opts.slot,
.proposer_index = opts.proposer_index,
.parent_root = parent_root,
.state_root = undefined,
.body = types.BeamBlockBody{
// .execution_payload_header = .{ .timestamp = timestamp },
.attestations = aggregation.attestations,
},
};
errdefer block.deinit();
var attestation_signatures = aggregation.attestation_signatures;
errdefer {
for (attestation_signatures.slice()) |*sig_group| {
sig_group.deinit();
}
attestation_signatures.deinit();
}
// Ownership moved into `block` + `attestation_signatures`.
aggregation_opt = null;
const block_str = try block.toJsonString(self.allocator);
defer self.allocator.free(block_str);
self.module_logger.debug("node-{d}::going for block production opts={any} raw block={s}", .{ self.nodeId, opts, block_str });
// 2. apply STF to get post state & update post state root & cache it
try stf.apply_raw_block(self.allocator, post_state, &block, self.block_building_logger);
const block_str_2 = try block.toJsonString(self.allocator);
defer self.allocator.free(block_str_2);
self.module_logger.debug("applied raw block opts={any} raw block={s}", .{ opts, block_str_2 });
// 3. cache state to save recompute while adding the block on publish
var block_root: [32]u8 = undefined;
try ssz.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator);
try self.states.put(block_root, post_state);
post_state_opt = null;
var forkchoice_added = false;
errdefer if (!forkchoice_added) {
if (self.states.fetchRemove(block_root)) |entry| {
entry.value.deinit();
self.allocator.destroy(entry.value);
}
};
// 4. Add the block to directly forkchoice as this proposer will next need to construct its vote
// note - attestations packed in the block are already in the knownVotes so we don't need to re-import
// them in the forkchoice
_ = try self.forkChoice.onBlock(block, post_state, .{
.currentSlot = block.slot,
.blockDelayMs = 0,
.blockRoot = block_root,
// confirmed in publish
.confirmed = false,
});
forkchoice_added = true;
_ = try self.forkChoice.updateHead();
return .{
.block = block,
.blockRoot = block_root,
.attestation_signatures = attestation_signatures,
};
}
pub fn constructAttestationData(self: *Self, opts: AttestationConstructionParams) !types.AttestationData {
const slot = opts.slot;
// const head = try self.forkChoice.getProposalHead(slot);
const head_proto = self.forkChoice.head;
const head: types.Checkpoint = .{
.root = head_proto.blockRoot,
.slot = head_proto.slot,
};
const head_str = try head.toJsonString(self.allocator);
defer self.allocator.free(head_str);
const safe_target_proto = self.forkChoice.safeTarget;
const safe_target: types.Checkpoint = .{
.root = safe_target_proto.blockRoot,
.slot = safe_target_proto.slot,
};
const safe_target_str = try safe_target.toJsonString(self.allocator);
defer self.allocator.free(safe_target_str);
self.module_logger.info("constructing attestation data at slot={d} with chain head={s} safe_target={s}", .{
slot,
head_str,
safe_target_str,
});
const target = try self.forkChoice.getAttestationTarget();
const target_str = try target.toJsonString(self.allocator);
defer self.allocator.free(target_str);
self.module_logger.info("calculated target for attestations at slot={d}: {s}", .{ slot, target_str });
const attestation_data = types.AttestationData{
.slot = slot,
.head = head,
.target = target,
.source = self.forkChoice.fcStore.latest_justified,
};
return attestation_data;
}
pub fn printSlot(self: *Self, islot: isize, peer_count: usize) void {
// head should be auto updated if receieved a block or block proposal done
// however it doesn't get updated unless called updatehead even though process block
// logs show it has been updated. debug and fix the call below
const fc_head = if (islot > 0)
self.forkChoice.updateHead() catch |err| {
self.module_logger.err("forkchoice updatehead error={any}", .{err});
return;
}
else
self.forkChoice.head;
// Get additional chain information
const justified = self.forkChoice.fcStore.latest_justified;
const finalized = self.forkChoice.fcStore.latest_finalized;
// Calculate chain progress
const slot: usize = if (islot < 0) 0 else @intCast(islot);
const blocks_behind = if (slot > fc_head.slot) slot - fc_head.slot else 0;
const is_timely = fc_head.timeliness;
const states_count = self.states.count();
const fc_nodes_count = self.forkChoice.protoArray.nodes.items.len;
self.module_logger.debug("cached states={d}, forkchoice nodes={d}", .{ states_count, fc_nodes_count });
self.module_logger.info(
\\
\\+===============================================================+
\\ CHAIN STATUS: Current Slot: {d} | Head Slot: {d} | Behind: {d}
\\+---------------------------------------------------------------+
\\ Connected Peers: {d}
\\+---------------------------------------------------------------+
\\ Head Block Root: 0x{any}
\\ Parent Block Root: 0x{any}
\\ State Root: 0x{any}
\\ Timely: {s}
\\+---------------------------------------------------------------+
\\ Latest Justified: Slot {d:>6} | Root: 0x{any}
\\ Latest Finalized: Slot {d:>6} | Root: 0x{any}
\\+===============================================================+
\\
, .{
islot,
fc_head.slot,
blocks_behind,
peer_count,
std.fmt.fmtSliceHexLower(&fc_head.blockRoot),
std.fmt.fmtSliceHexLower(&fc_head.parentRoot),
std.fmt.fmtSliceHexLower(&fc_head.stateRoot),
if (is_timely) "YES" else "NO",
justified.slot,
std.fmt.fmtSliceHexLower(&justified.root),
finalized.slot,
std.fmt.fmtSliceHexLower(&finalized.root),
});
}
pub fn onGossip(self: *Self, data: *const networks.GossipMessage, sender_peer_id: []const u8) !GossipProcessingResult {
switch (data.*) {
.block => |signed_block| {
const block = signed_block.message.block;
var block_root: [32]u8 = undefined;
try ssz.hashTreeRoot(types.BeamBlock, block, &block_root, self.allocator);
//check if we have the block already in forkchoice
const hasBlock = self.forkChoice.hasBlock(block_root);
self.module_logger.debug("chain received gossip block for slot={any} blockroot={any} proposer={d}{} hasBlock={any} from peer={s}{}", .{
block.slot,
std.fmt.fmtSliceHexLower(&block_root),
block.proposer_index,
self.node_registry.getNodeNameFromValidatorIndex(block.proposer_index),
hasBlock,
sender_peer_id,
self.node_registry.getNodeNameFromPeerId(sender_peer_id),
});
if (!hasBlock) {
self.validateBlock(block, true) catch |err| {
self.module_logger.warn("gossip block validation failed: {any}", .{err});
return .{}; // Drop invalid gossip attestations
};
const missing_roots = self.onBlock(signed_block, .{
.blockRoot = block_root,
}) catch |err| {
self.module_logger.err("error processing block for slot={any} root={any}: {any}", .{
//
block.slot,
std.fmt.fmtSliceHexLower(&block_root),
err,
});
return err;
};
// followup with additional housekeeping tasks
self.onBlockFollowup(true);
// NOTE: ownership of `missing_roots` is transferred to the caller (BeamNode),
// which is responsible for freeing it after optionally fetching those roots.
// Return both the block root and missing attestation roots so the node can:
// 1. Call processCachedDescendants(block_root) to retry any cached children
// 2. Fetch missing attestation head blocks via RPC
return .{
.processed_block_root = block_root,
.missing_attestation_roots = missing_roots,
};
} else {
self.module_logger.debug("skipping processing the already present block slot={any} blockroot={any}", .{
//
block.slot,
std.fmt.fmtSliceHexLower(&block_root),
});
}
return .{};
},
.attestation => |signed_attestation| {
const slot = signed_attestation.message.slot;
const validator_id = signed_attestation.validator_id;
const validator_node_name = self.node_registry.getNodeNameFromValidatorIndex(validator_id);
const sender_node_name = self.node_registry.getNodeNameFromPeerId(sender_peer_id);
self.module_logger.debug("chain received gossip attestation for slot={d} validator={d}{} from peer={s}{}", .{
slot,
validator_id,
validator_node_name,
sender_peer_id,
sender_node_name,
});
// Validate attestation before processing (gossip = not from block)
self.validateAttestation(signed_attestation.toAttestation(), false) catch |err| {
self.module_logger.warn("gossip attestation validation failed: {any}", .{err});
zeam_metrics.metrics.lean_attestations_invalid_total.incr(.{ .source = "gossip" }) catch {};
return .{}; // Drop invalid gossip attestations
};
// Process validated attestation
self.onAttestation(signed_attestation) catch |err| {
zeam_metrics.metrics.lean_attestations_invalid_total.incr(.{ .source = "gossip" }) catch {};
self.module_logger.err("attestation processing error: {any}", .{err});
return err;
};
self.module_logger.info("processed gossip attestation for slot={d} validator={d}{}", .{
slot,
validator_id,
validator_node_name,
});
zeam_metrics.metrics.lean_attestations_valid_total.incr(.{ .source = "gossip" }) catch {};
return .{};
},
}
}
// import block assuming it is gossip validated or synced
// this onBlock corresponds to spec's forkchoice's onblock with some functionality split between this and
// our implemented forkchoice's onblock. this is to parallelize "apply transition" with other verifications
// Returns a list of missing block roots that need to be fetched from the network
pub fn onBlock(self: *Self, signedBlock: types.SignedBlockWithAttestation, blockInfo: CachedProcessedBlockInfo) ![]types.Root {
const onblock_timer = zeam_metrics.chain_onblock_duration_seconds.start();
const block = signedBlock.message.block;
const block_root: types.Root = blockInfo.blockRoot orelse computedroot: {
var cblock_root: [32]u8 = undefined;
try ssz.hashTreeRoot(types.BeamBlock, block, &cblock_root, self.allocator);
break :computedroot cblock_root;
};
const post_state = if (blockInfo.postState) |post_state_ptr| post_state_ptr else computedstate: {
// 1. get parent state
const pre_state = self.states.get(block.parent_root) orelse return BlockProcessingError.MissingPreState;
const cpost_state = try self.allocator.create(types.BeamState);
try types.sszClone(self.allocator, types.BeamState, pre_state.*, cpost_state);
// 2. verify XMSS signatures (independent step; placed before STF for now, parallelizable later)
try stf.verifySignatures(self.allocator, pre_state, &signedBlock);
// 3. apply state transition assuming signatures are valid (STF does not re-verify)
try stf.apply_transition(self.allocator, cpost_state, block, .{
//
.logger = self.stf_logger,
.validSignatures = true,
});
break :computedstate cpost_state;
};
var missing_roots = std.ArrayList(types.Root).init(self.allocator);
errdefer missing_roots.deinit();
// 3. fc onblock if the block was not pre added by the block production
const fcBlock = self.forkChoice.getBlock(block_root) orelse fcprocessing: {
const freshFcBlock = try self.forkChoice.onBlock(block, post_state, .{
.currentSlot = block.slot,
.blockDelayMs = 0,
.blockRoot = block_root,
// confirmed in next steps post written to db
.confirmed = false,
});
// 4. fc onattestations
self.module_logger.debug("processing attestations of block with root=0x{s} slot={d}", .{
std.fmt.fmtSliceHexLower(&freshFcBlock.blockRoot),
block.slot,
});
const aggregated_attestations = block.body.attestations.constSlice();
const signature_groups = signedBlock.signature.attestation_signatures.constSlice();
if (aggregated_attestations.len != signature_groups.len) {
self.module_logger.err(
"signature group count mismatch for block root=0x{s}: attestations={d} signature_groups={d}",
.{ std.fmt.fmtSliceHexLower(&freshFcBlock.blockRoot), aggregated_attestations.len, signature_groups.len },
);
}
for (aggregated_attestations, 0..) |aggregated_attestation, index| {
var validator_indices = try types.aggregationBitsToValidatorIndices(&aggregated_attestation.aggregation_bits, self.allocator);
defer validator_indices.deinit();
const group_signatures = if (index < signature_groups.len)
signature_groups[index].constSlice()
else
&[_]types.SIGBYTES{};
if (validator_indices.items.len != group_signatures.len) {
zeam_metrics.metrics.lean_attestations_invalid_total.incr(.{ .source = "block" }) catch {};
self.module_logger.err(
"attestation signature mismatch index={d} validators={d} signatures={d}",
.{ index, validator_indices.items.len, group_signatures.len },
);
continue;
}
for (validator_indices.items, group_signatures) |validator_index, signature| {
const validator_id: types.ValidatorIndex = @intCast(validator_index);
const attestation = types.Attestation{
.validator_id = validator_id,
.data = aggregated_attestation.data,
};
self.validateAttestation(attestation, true) catch |e| {
zeam_metrics.metrics.lean_attestations_invalid_total.incr(.{ .source = "block" }) catch {};
if (e == AttestationValidationError.UnknownHeadBlock) {
try missing_roots.append(attestation.data.head.root);
}
self.module_logger.err("invalid attestation in block: validator={d} error={any}", .{
validator_index,
e,
});
continue;
};
const signed_attestation = types.SignedAttestation{
.validator_id = validator_id,
.message = attestation.data,
.signature = signature,
};
self.forkChoice.onAttestation(signed_attestation, true) catch |e| {
zeam_metrics.metrics.lean_attestations_invalid_total.incr(.{ .source = "block" }) catch {};
self.module_logger.err("error processing block attestation={any} e={any}", .{ signed_attestation, e });
continue;
};
zeam_metrics.metrics.lean_attestations_valid_total.incr(.{ .source = "block" }) catch {};
}
}
// 5. fc update head
_ = try self.forkChoice.updateHead();
break :fcprocessing freshFcBlock;
};
try self.states.put(fcBlock.blockRoot, post_state);
// 6. proposer attestation
const proposer_signature = signedBlock.signature.proposer_signature;
const signed_proposer_attestation = types.SignedAttestation{
.validator_id = signedBlock.message.proposer_attestation.validator_id,
.message = signedBlock.message.proposer_attestation.data,
.signature = proposer_signature,
};
self.forkChoice.onAttestation(signed_proposer_attestation, false) catch |e| {
self.module_logger.err("error processing proposer attestation={any} e={any}", .{ signed_proposer_attestation, e });
};
const processing_time = onblock_timer.observe();
// 7. Save block and state to database and confirm the block in forkchoice
self.updateBlockDb(signedBlock, fcBlock.blockRoot, post_state.*, block.slot) catch |err| {
self.module_logger.err("failed to update block database for block root=0x{s}: {any}", .{
std.fmt.fmtSliceHexLower(&fcBlock.blockRoot),
err,
});
};
try self.forkChoice.confirmBlock(block_root);
self.module_logger.info("processed block with root=0x{s} slot={d} processing time={d} (computed root={} computed state={})", .{
std.fmt.fmtSliceHexLower(&fcBlock.blockRoot),
block.slot,
processing_time,
blockInfo.blockRoot == null,
blockInfo.postState == null,
});
return missing_roots.toOwnedSlice();
}
pub fn onBlockFollowup(self: *Self, pruneForkchoice: bool) void {
// 8. Asap emit new events via SSE (use forkchoice ProtoBlock directly)
const new_head = self.forkChoice.head;
if (api.events.NewHeadEvent.fromProtoBlock(self.allocator, new_head)) |head_event| {
var chain_event = api.events.ChainEvent{ .new_head = head_event };
event_broadcaster.broadcastGlobalEvent(&chain_event) catch |err| {
self.module_logger.warn("failed to broadcast head event: {any}", .{err});
chain_event.deinit(self.allocator);
};
} else |err| {
self.module_logger.warn("failed to create head event: {any}", .{err});
}
const store = self.forkChoice.fcStore;
const latest_justified = store.latest_justified;
const latest_finalized = store.latest_finalized;
// 9. Asap emit justification/finalization events based on forkchoice store
// Emit justification event only when slot increases beyond last emitted
if (latest_justified.slot > self.last_emitted_justified.slot) {
if (api.events.NewJustificationEvent.fromCheckpoint(self.allocator, latest_justified, new_head.slot)) |just_event| {
var chain_event = api.events.ChainEvent{ .new_justification = just_event };
event_broadcaster.broadcastGlobalEvent(&chain_event) catch |err| {
self.module_logger.warn("failed to broadcast justification event: {any}", .{err});
chain_event.deinit(self.allocator);
};
self.last_emitted_justified = latest_justified;
} else |err| {
self.module_logger.warn("failed to create justification event: {any}", .{err});
}
}
// Emit finalization event only when slot increases beyond last emitted
const last_emitted_finalized = self.last_emitted_finalized;
if (latest_finalized.slot > last_emitted_finalized.slot) {
if (api.events.NewFinalizationEvent.fromCheckpoint(self.allocator, latest_finalized, new_head.slot)) |final_event| {
var chain_event = api.events.ChainEvent{ .new_finalization = final_event };
event_broadcaster.broadcastGlobalEvent(&chain_event) catch |err| {
self.module_logger.warn("failed to broadcast finalization event: {any}", .{err});
chain_event.deinit(self.allocator);
};
self.last_emitted_finalized = latest_finalized;
} else |err| {
self.module_logger.warn("failed to create finalization event: {any}", .{err});
}
}
// Update finalized slot indices and cleanup if finalization has advanced
// note use presaved local last_emitted_finalized as self.last_emitted_finalized has been updated above
if (latest_finalized.slot > last_emitted_finalized.slot) {
self.processFinalizationAdvancement(last_emitted_finalized, latest_finalized, pruneForkchoice) catch |err| {
// Record failed finalization attempt
zeam_metrics.metrics.lean_finalizations_total.incr(.{ .result = "error" }) catch {};
self.module_logger.err("failed to process finalization advancement from slot {d} to {d}: {any}", .{
last_emitted_finalized.slot,
latest_finalized.slot,
err,
});
};
}
const states_count_after_block = self.states.count();
const fc_nodes_count_after_block = self.forkChoice.protoArray.nodes.items.len;
self.module_logger.info("completed on block followup with states_count={d} fc_nodes_count={d}", .{
states_count_after_block,
fc_nodes_count_after_block,
});
zeam_metrics.metrics.lean_latest_justified_slot.set(latest_justified.slot);
zeam_metrics.metrics.lean_latest_finalized_slot.set(latest_finalized.slot);
}
/// Update block database with block, state, and slot indices
fn updateBlockDb(self: *Self, signedBlock: types.SignedBlockWithAttestation, blockRoot: types.Root, postState: types.BeamState, slot: types.Slot) !void {
var batch = self.db.initWriteBatch();
defer batch.deinit();
// Store block and state
batch.putBlock(database.DbBlocksNamespace, blockRoot, signedBlock);
batch.putState(database.DbStatesNamespace, blockRoot, postState);
// TODO: uncomment this code if there is a need of slot to unfinalized index
_ = slot;
// primarily this is served by the forkchoice
// update unfinalized slot index
// if (slot > finalizedSlot) {
// const existing_blockroots = self.db.loadUnfinalizedSlotIndex(database.DbUnfinalizedSlotsNamespace, slot) orelse &[_]types.Root{};
// if (existing_blockroots.len > 0) {
// defer self.allocator.free(existing_blockroots);
// }
// var updated_blockroots = std.ArrayList(types.Root).init(self.allocator);
// defer updated_blockroots.deinit();
// updated_blockroots.appendSlice(existing_blockroots) catch {};
// updated_blockroots.append(blockRoot) catch {};
// batch.putUnfinalizedSlotIndex(database.DbUnfinalizedSlotsNamespace, slot, updated_blockroots.items);
// }
self.db.commit(&batch);
}
/// Prune old non-canonical states from memory
/// canonical_blocks: set of block roots that should be kept (e.g., canonical chain from finalized to head)
/// All states in canonical_blocks are kept, all others are pruned
fn pruneStates(self: *Self, roots: []types.Root, pruneType: []const u8) usize {
const states_count_before = self.states.count();
self.module_logger.debug("pruning for {s} (states_count={d}, roots={d})", .{
pruneType,
states_count_before,
roots.len,
});
// We keep the canonical chain from finalized to head, so we can safely prune all non-canonical states
// Actually remove and deallocate the pruned states
for (roots) |root| {
if (self.states.fetchRemove(root)) |entry| {
const state_ptr = entry.value;
state_ptr.deinit();
self.allocator.destroy(state_ptr);
self.module_logger.debug("pruned state for root 0x{s}", .{
std.fmt.fmtSliceHexLower(&root),
});
}
}
const states_count_after = self.states.count();
const pruned_count = states_count_before - states_count_after;
self.module_logger.debug("pruning completed for {s} removed {d} states (states: {d} -> {d})", .{
pruneType,
pruned_count,
states_count_before,
states_count_after,
});
return pruned_count;
}
/// Process finalization advancement: move canonical blocks to finalized index and cleanup unfinalized indices
fn processFinalizationAdvancement(self: *Self, previousFinalized: types.Checkpoint, latestFinalized: types.Checkpoint, pruneForkchoice: bool) !void {
var batch = self.db.initWriteBatch();
defer batch.deinit();
self.module_logger.debug("processing finalization advancement from slot={d} to slot={d}", .{ previousFinalized.slot, latestFinalized.slot });
// 1. Do canonoical analysis to segment forkchoice
var canonical_view = std.AutoHashMap(types.Root, void).init(self.allocator);
defer canonical_view.deinit();
try self.forkChoice.getCanonicalView(&canonical_view, latestFinalized.root, null);
const analysis_result = try self.forkChoice.getCanonicalityAnalysis(latestFinalized.root, null, &canonical_view);
const finalized_roots = analysis_result[0];
const non_finalized_descendants = analysis_result[1];
const non_canonical_roots = analysis_result[2];
defer self.allocator.free(finalized_roots);
defer self.allocator.free(non_finalized_descendants);
defer self.allocator.free(non_canonical_roots);
// finalized_ancestor_roots has the previous finalized included
const newly_finalized_count = finalized_roots.len - 1;
self.module_logger.info("finalization canonicality analysis (previousFinalized slot={d} to latestFinalized slot={d}): newly finalized={d}, orphaned/missing={d}, non finalized descendants={d} & finalized non canonical={d}", .{
previousFinalized.slot,
//
latestFinalized.slot,
newly_finalized_count,
latestFinalized.slot - previousFinalized.slot - newly_finalized_count,
non_finalized_descendants.len,
non_canonical_roots.len,
});
// 2. Put all newly finalized roots in DbFinalizedSlotsNamespace
for (finalized_roots) |root| {
const idx = self.forkChoice.protoArray.indices.get(root) orelse return error.FinalizedBlockNotInForkChoice;
const node = self.forkChoice.protoArray.nodes.items[idx];
batch.putFinalizedSlotIndex(database.DbFinalizedSlotsNamespace, node.slot, root);
self.module_logger.debug("added block 0x{s} at slot {d} to finalized index", .{
std.fmt.fmtSliceHexLower(&root),
node.slot,
});
}
// Update the latest finalized slot metadata
batch.putLatestFinalizedSlot(database.DbDefaultNamespace, latestFinalized.slot);
// 3. commit all batch ops for finalized indices before we prune
self.db.commit(&batch);
// 4. Prunestates from memory
// Get all canonical blocks from finalized to head (not just newly finalized)
const states_count_before: isize = self.states.count();
// first root is the new finalized, we need to retain it and will be pruned in the next round
_ = self.pruneStates(finalized_roots[1..finalized_roots.len], "finalized ancestors");
_ = self.pruneStates(non_canonical_roots, "finalized non canonical");
const pruned_count = states_count_before - self.states.count();
self.module_logger.info("state pruning completed (slots latestFinalized={d} to latestFinalized={d}) removed {d} states", .{
previousFinalized.slot,
latestFinalized.slot,
pruned_count,
});
// 5 Rebase forkchouce
if (pruneForkchoice)
try self.forkChoice.rebase(latestFinalized.root, &canonical_view);
// TODO:
// 6. Remove orphaned blocks from database and cleanup unfinalized indices of there are any
// for (previousFinalizedSlot + 1..finalizedSlot + 1) |slot| {
// var slot_orphaned_count: usize = 0;
// // Get all unfinalized blocks at this slot before deleting the index
// if (self.db.loadUnfinalizedSlotIndex(database.DbUnfinalizedSlotsNamespace, slot)) |unfinalized_blockroots| {
// defer self.allocator.free(unfinalized_blockroots);
// // Remove blocks not in the canonical finalized chain
// for (unfinalized_blockroots) |blockroot| {
// if (!canonical_blocks.contains(blockroot)) {
// // This block is orphaned - remove it from database
// batch.delete(database.DbBlocksNamespace, &blockroot);
// batch.delete(database.DbStatesNamespace, &blockroot);
// slot_orphaned_count += 1;
// }
// }
// if (slot_orphaned_count > 0) {
// self.module_logger.debug("Removed {d} orphaned block at slot {d} from database", .{
// slot_orphaned_count,
// slot,
// });
// }
// // Remove the unfinalized slot index
// batch.deleteUnfinalizedSlotIndexFromBatch(database.DbUnfinalizedSlotsNamespace, slot);
// self.module_logger.debug("Removed {d} unfinalized index for slot {d}", .{ unfinalized_blockroots.len, slot });
// }
// }
// Record successful finalization
zeam_metrics.metrics.lean_finalizations_total.incr(.{ .result = "success" }) catch {};
self.module_logger.debug("finalization advanced previousFinalized slot={d} to latestFinalized slot={d}", .{ previousFinalized.slot, latestFinalized.slot });
}
pub fn validateBlock(self: *Self, block: types.BeamBlock, is_from_gossip: bool) !void {
_ = is_from_gossip;
const hasParentBlock = self.forkChoice.hasBlock(block.parent_root);
if (!hasParentBlock) {
self.module_logger.warn("gossip block validation failed slot={any} with unknown parent={any}", .{
//
block.slot,
std.fmt.fmtSliceHexLower(&block.parent_root),
});
return BlockValidationError.UnknownParentBlock;
}
}
/// Validate incoming attestation before processing.
///
/// is_from_block: true if attestation came from a block, false if from network gossip
///
/// Per leanSpec:
/// - Gossip attestations (is_from_block=false): attestation.slot <= current_slot (no future tolerance)
/// - Block attestations (is_from_block=true): attestation.slot <= current_slot + 1 (lenient)
pub fn validateAttestation(self: *Self, attestation: types.Attestation, is_from_block: bool) !void {
const timer = zeam_metrics.lean_attestation_validation_time_seconds.start();
defer _ = timer.observe();
const data = attestation.data;
// 1. Validate that source, target, and head blocks exist in proto array
const source_idx = self.forkChoice.protoArray.indices.get(data.source.root) orelse {
self.module_logger.debug("attestation validation failed: unknown source block root=0x{s}", .{
std.fmt.fmtSliceHexLower(&data.source.root),
});
return AttestationValidationError.UnknownSourceBlock;
};
const target_idx = self.forkChoice.protoArray.indices.get(data.target.root) orelse {
self.module_logger.debug("attestation validation failed: unknown target block slot={d} root=0x{s}", .{
data.target.slot,
std.fmt.fmtSliceHexLower(&data.target.root),
});
return AttestationValidationError.UnknownTargetBlock;
};
const head_idx = self.forkChoice.protoArray.indices.get(data.head.root) orelse {
self.module_logger.debug("attestation validation failed: unknown head block slot={d} root=0x{s}", .{
data.head.slot,
std.fmt.fmtSliceHexLower(&data.head.root),
});
return AttestationValidationError.UnknownHeadBlock;