Skip to content

Commit a3b2ad5

Browse files
authored
Track current epoch observed justified and previous / current slot head (#7983)
* Track current epoch observed justified and previous / current slot head Fast Confirmation Rule needs to track a consistent checkpoint per epoch and the heads that were selected during current / previous slots. These hooks give comparable data to `update_fast_confirmation_variables` in the spec but are slightly optimized to not trigger extra load. - ethereum/consensus-specs#4747 * Copy current_slot_head instead of DAG head * Export from type module * Implicit result
1 parent 62800ff commit a3b2ad5

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

beacon_chain/fork_choice/fork_choice.nim

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ func init*(
6262
proto_array: ProtoArray.init(finalized.checkpoint, currentSlot),
6363
confirmed: BlockId(
6464
slot: finalized.checkpoint.epoch.start_slot,
65-
root: finalized.checkpoint.root))
65+
root: finalized.checkpoint.root),
66+
current_epoch_observed_justified: finalized,
67+
previous_slot_head: finalized.checkpoint.root,
68+
current_slot_head: finalized.checkpoint.root)
6669

6770
proc init*(
6871
T: type ForkChoice, confirmation_byzantine_threshold: uint64,
@@ -157,10 +160,17 @@ proc on_tick(
157160
# Reset store.proposer_boost_root
158161
self.checkpoints.proposer_boost_root = ZERO_HASH
159162

163+
# Update prev slot head
164+
self.backend.previous_slot_head = self.backend.current_slot_head
165+
160166
if current_slot.is_epoch:
161167
# Pull-up unrealized justified / finalized checkpoints from previous epoch
162168
for realized in self.backend.proto_array.realizePendingCheckpoints():
163169
? self.checkpoints.update_checkpoints(dag, realized, current_slot)
170+
171+
# Update observed justified checkpoint before any attestations from the
172+
# last slot of the previous epoch become processable
173+
self.backend.current_epoch_observed_justified = self.checkpoints.justified
164174
ok()
165175

166176
func process_attestation(
@@ -371,17 +381,18 @@ proc get_head*(
371381
self: var ForkChoice, dag: ChainDAGRef,
372382
wallTime: BeaconTime): FcResult[Eth2Digest] =
373383
? self.update_time(dag, wallTime)
374-
result = self.backend.find_head(
384+
self.backend.find_head(
375385
self.checkpoints.time.slotOrZero(dag.timeParams),
376386
self.checkpoints)
377-
self.backend.update_confirmed BlockId(
378-
slot: self.checkpoints.justified.checkpoint.epoch.start_slot,
379-
root: self.checkpoints.justified.checkpoint.root)
380387

381388
proc will_select_head*(
382389
self: var ForkChoice, dag: ChainDAGRef,
383390
blckRef: BlockRef, wallTime: BeaconTime): FcResult[void] =
384391
? self.update_time(dag, wallTime)
392+
self.backend.current_slot_head = dag.head.root
393+
self.backend.update_confirmed BlockId(
394+
slot: self.checkpoints.justified.checkpoint.epoch.start_slot,
395+
root: self.checkpoints.justified.checkpoint.root)
385396
ok()
386397

387398
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-beta.0/fork_choice/safe-block.md#get_safe_beacon_block_root

beacon_chain/fork_choice/fork_choice_types.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ type
137137
confirmation_byzantine_threshold*: uint64
138138
proto_array*: ProtoArray
139139
confirmed*: BlockId
140+
current_epoch_observed_justified*: BalanceCheckpoint
141+
previous_slot_head*, current_slot_head*: Eth2Digest
140142
votes*: seq[VoteTracker]
141143
balances*: seq[ForkChoiceBalance]
142144

beacon_chain/rpc/rest_debug_api.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ proc installDebugApiHandlers*(router: var RestRouter, node: BeaconNode) =
103103
var response = GetForkChoiceResponse(
104104
justified_checkpoint: forkChoice.checkpoints.justified.checkpoint,
105105
finalized_checkpoint: forkChoice.checkpoints.finalized,
106-
extra_data: RestExtraData())
106+
extra_data: RestExtraData(
107+
confirmed_root: forkChoice.get_safe_beacon_block_root,
108+
current_epoch_observed_justified_checkpoint:
109+
forkChoice.backend.current_epoch_observed_justified.checkpoint,
110+
previous_slot_head: forkChoice.backend.previous_slot_head,
111+
current_slot_head: forkChoice.backend.current_slot_head))
107112

108113
for item in forkChoice.backend.proto_array:
109114
let

beacon_chain/spec/eth2_apis/rest_types.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ type
590590
extra_data*: Opt[RestNodeExtraData]
591591

592592
RestExtraData* = object
593-
discard
593+
confirmed_root*: Eth2Digest
594+
current_epoch_observed_justified_checkpoint*: Checkpoint
595+
previous_slot_head*, current_slot_head*: Eth2Digest
594596

595597
GetForkChoiceResponse* = object
596598
justified_checkpoint*: Checkpoint

0 commit comments

Comments
 (0)