From 3bd9a2395c377345ad2297bfe7bcd45bed296cb7 Mon Sep 17 00:00:00 2001 From: buddho Date: Tue, 20 Aug 2024 23:06:49 +0800 Subject: [PATCH] core: improve readability of the fork choice logic (#2658) --- core/forkchoice.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/forkchoice.go b/core/forkchoice.go index bea7652fa..4e931ccf5 100644 --- a/core/forkchoice.go +++ b/core/forkchoice.go @@ -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 }