Skip to content

Commit ddb93b3

Browse files
committed
Update get head
1 parent c2b6115 commit ddb93b3

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

beacon_chain/fork_choice/fork_choice.nim

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,10 @@ proc process_block*(
361361
self.backend.process_attestation(
362362
validator_index,
363363
attestation.data.beacon_block_root,
364-
attestation.data.target.epoch,
365364
attestation.data.slot,
366365
attestation.data.index == 1,
367366
dag.cfg)
368-
367+
369368
# Process payload attestations
370369
when typeof(blck).kind >= ConsensusFork.Gloas:
371370
if dag.isGloasEnabled(blckRef.slot):
@@ -430,8 +429,7 @@ template getPhysicalNode(
430429
if physicalIdx >= 0 and
431430
physicalIdx < self.backend.proto_array.nodes.buf.len:
432431
addr self.backend.proto_array.nodes.buf[physicalIdx]
433-
else:
434-
nil
432+
else: nil
435433

436434
# https://github.com/ethereum/consensus-specs/blob/v1.6.1/specs/gloas/fork-choice.md#new-get_node_children
437435
func get_node_children(
@@ -660,7 +658,7 @@ func is_payload_timely(self: ForkChoiceBackend, root: Eth2Digest): bool =
660658

661659
#https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.2/specs/gloas/fork-choice.md#new-should_extend_payload
662660
func should_extend_payload*(
663-
self: var ForkChoice, root: Eth2Digest, dag: ChainDAGRef): bool =
661+
self: var ForkChoice, root: Eth2Digest): bool =
664662
if self.backend.is_payload_timely(root):
665663
return true
666664

@@ -687,7 +685,7 @@ func should_extend_payload*(
687685
if parent_node.bid.root != root:
688686
return true
689687

690-
proposer_node.parentPayloadStatus == PARENT_PAYLOAD_STATUS_FULL
688+
proposer_node.parentPayloadStatus == PAYLOAD_STATUS_FULL
691689

692690
# https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.0/specs/gloas/fork-choice.md#new-get_payload_status_tiebreaker
693691
func get_payload_status_tiebreaker(
@@ -704,14 +702,10 @@ func get_payload_status_tiebreaker(
704702
if proto_node == nil:
705703
return node.payloadStatus
706704

707-
# Are we deciding on previous slot's payload
708-
let is_deciding_on_previous = (proto_node.bid.slot + 1 == current_slot)
709-
710705
if node.payloadStatus == PAYLOAD_STATUS_PENDING or
711-
not is_deciding_on_previous:
706+
not (proto_node.bid.slot + 1 == current_slot):
712707
return node.payloadStatus
713708

714-
# Deciding on previous slot's payload
715709
if node.payloadStatus == PAYLOAD_STATUS_EMPTY:
716710
1'u8
717711
elif node.payloadStatus == PAYLOAD_STATUS_FULL:
@@ -774,21 +768,14 @@ proc get_head*(
774768
payloadStatus: PAYLOAD_STATUS_PENDING)
775769

776770
var iterations = 0
777-
const MAX_ITERATIONS = 1000
778771

779-
while iterations < MAX_ITERATIONS:
772+
while iterations < 1000:
780773
inc iterations
774+
781775
let children = self.get_node_children(head, dag)
782-
783776
if children.len == 0:
784777
return ok(head.root)
785778

786-
# Log all children with their weights
787-
for i, child in children:
788-
let
789-
child_weight = self.get_weight(child, current_slot, dag)
790-
child_tiebreaker =
791-
self.get_payload_status_tiebreaker(child, current_slot, dag)
792779
var
793780
best = children[0]
794781
best_weight = self.get_weight(best, current_slot, dag)
@@ -802,9 +789,10 @@ proc get_head*(
802789
child_tiebreaker =
803790
self.get_payload_status_tiebreaker(child, current_slot, dag)
804791

805-
var should_update = false
806792
if child_weight > best_weight:
807-
should_update = true
793+
best = child
794+
best_weight = child_weight
795+
best_tiebreaker = child_tiebreaker
808796
elif child_weight == best_weight:
809797
var root_cmp = 0
810798
for j in 0..<32:
@@ -815,20 +803,15 @@ proc get_head*(
815803
root_cmp = -1
816804
break
817805

818-
if root_cmp > 0:
819-
should_update = true
820-
elif root_cmp == 0:
821-
if child_tiebreaker > best_tiebreaker:
822-
should_update = true
823-
824-
if should_update:
825-
best = child
826-
best_weight = child_weight
827-
best_tiebreaker = child_tiebreaker
806+
if root_cmp > 0 or
807+
(root_cmp == 0 and child_tiebreaker > best_tiebreaker):
808+
best = child
809+
best_weight = child_weight
810+
best_tiebreaker = child_tiebreaker
828811

829812
head = best
830-
831-
ok(head.root)
813+
814+
err ForkChoiceError(kind: fcInvalidBestNode)
832815

833816
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-beta.0/fork_choice/safe-block.md#get_safe_beacon_block_root
834817
func get_safe_beacon_block_root*(self: ForkChoice): Eth2Digest =

0 commit comments

Comments
 (0)