Skip to content

Commit

Permalink
beacon/light: optimize lock usage in HeadTracker (ethereum#30485)
Browse files Browse the repository at this point in the history
minimizes the time when the lock is held
  • Loading branch information
zhiqiangxu authored Oct 2, 2024
1 parent 56c4f2b commit 84a8021
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions beacon/light/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 84a8021

Please sign in to comment.