From 84a80216c6481efca1a761fb98827478a0589c09 Mon Sep 17 00:00:00 2001 From: zhiqiangxu <652732310@qq.com> Date: Wed, 2 Oct 2024 22:00:36 +0800 Subject: [PATCH] beacon/light: optimize lock usage in `HeadTracker` (#30485) minimizes the time when the lock is held --- beacon/light/head_tracker.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/beacon/light/head_tracker.go b/beacon/light/head_tracker.go index 7ef93fecce..010e548ddb 100644 --- a/beacon/light/head_tracker.go +++ b/beacon/light/head_tracker.go @@ -69,12 +69,13 @@ func (h *HeadTracker) ValidatedFinality() (types.FinalityUpdate, bool) { // slot or same slot and more signers) then ValidatedOptimistic is updated. // The boolean return flag signals if ValidatedOptimistic has been changed. func (h *HeadTracker) ValidateOptimistic(update types.OptimisticUpdate) (bool, error) { - h.lock.Lock() - defer h.lock.Unlock() - if err := update.Validate(); err != nil { return false, err } + + h.lock.Lock() + defer h.lock.Unlock() + replace, err := h.validate(update.SignedHeader(), h.optimisticUpdate.SignedHeader()) if replace { h.optimisticUpdate, h.hasOptimisticUpdate = update, true @@ -88,12 +89,13 @@ func (h *HeadTracker) ValidateOptimistic(update types.OptimisticUpdate) (bool, e // slot or same slot and more signers) then ValidatedFinality is updated. // The boolean return flag signals if ValidatedFinality has been changed. func (h *HeadTracker) ValidateFinality(update types.FinalityUpdate) (bool, error) { - h.lock.Lock() - defer h.lock.Unlock() - if err := update.Validate(); err != nil { return false, err } + + h.lock.Lock() + defer h.lock.Unlock() + replace, err := h.validate(update.SignedHeader(), h.finalityUpdate.SignedHeader()) if replace { h.finalityUpdate, h.hasFinalityUpdate = update, true