Skip to content

Commit

Permalink
core: improve readability of the fork choice logic
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Aug 20, 2024
1 parent 5d19f21 commit 67ea946
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions core/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,19 @@ func (f *ForkChoice) ReorgNeeded(current *types.Header, extern *types.Header) (b
if f.preserve != nil {
currentPreserve, externPreserve = f.preserve(current), f.preserve(extern)
}
doubleSign := (extern.Coinbase == current.Coinbase)
reorg = !currentPreserve && (externPreserve ||
extern.Time < current.Time ||
extern.Time == current.Time &&
((doubleSign && extern.Hash().Cmp(current.Hash()) < 0) ||
(!doubleSign && f.rand.Float64() < 0.5)))
choiceRules := func() bool {
if extern.Time == current.Time {
doubleSign := (extern.Coinbase == current.Coinbase)
if doubleSign {
return extern.Hash().Cmp(current.Hash()) < 0
} else {
return f.rand.Float64() < 0.5
}
} else {
return extern.Time < current.Time
}
}
reorg = !currentPreserve && (externPreserve || choiceRules())
}
return reorg, nil
}
Expand Down

0 comments on commit 67ea946

Please sign in to comment.