eth/catalyst: use atomics instead of locks (#31955)
Some checks are pending
/ Linux Build (arm) (push) Waiting to run
/ Linux Build (push) Waiting to run
/ Docker Image (push) Waiting to run

This commit is contained in:
Marius van der Wijden 2025-06-04 06:18:20 +02:00 committed by GitHub
parent 500ed3b22c
commit 23f07d8c93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 29 deletions

View file

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/beacon/engine"
@ -143,12 +144,9 @@ type ConsensusAPI struct {
// Geth can appear to be stuck or do strange things if the beacon client is // Geth can appear to be stuck or do strange things if the beacon client is
// offline or is sending us strange data. Stash some update stats away so // offline or is sending us strange data. Stash some update stats away so
// that we can warn the user and not have them open issues on our tracker. // that we can warn the user and not have them open issues on our tracker.
lastTransitionUpdate time.Time lastTransitionUpdate atomic.Int64
lastTransitionLock sync.Mutex lastForkchoiceUpdate atomic.Int64
lastForkchoiceUpdate time.Time lastNewPayloadUpdate atomic.Int64
lastForkchoiceLock sync.Mutex
lastNewPayloadUpdate time.Time
lastNewPayloadLock sync.Mutex
forkchoiceLock sync.Mutex // Lock for the forkChoiceUpdated method forkchoiceLock sync.Mutex // Lock for the forkChoiceUpdated method
newPayloadLock sync.Mutex // Lock for the NewPayload method newPayloadLock sync.Mutex // Lock for the NewPayload method
@ -252,9 +250,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
return engine.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this? return engine.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this?
} }
// Stash away the last update to warn the user if the beacon client goes offline // Stash away the last update to warn the user if the beacon client goes offline
api.lastForkchoiceLock.Lock() api.lastForkchoiceUpdate.Store(time.Now().Unix())
api.lastForkchoiceUpdate = time.Now()
api.lastForkchoiceLock.Unlock()
// Check whether we have the block yet in our database or not. If not, we'll // Check whether we have the block yet in our database or not. If not, we'll
// need to either trigger a sync, or to reject this forkchoice update for a // need to either trigger a sync, or to reject this forkchoice update for a
@ -398,9 +394,7 @@ func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config engine.Transit
return nil, errors.New("invalid terminal total difficulty") return nil, errors.New("invalid terminal total difficulty")
} }
// Stash away the last update to warn the user if the beacon client goes offline // Stash away the last update to warn the user if the beacon client goes offline
api.lastTransitionLock.Lock() api.lastTransitionUpdate.Store(time.Now().Unix())
api.lastTransitionUpdate = time.Now()
api.lastTransitionLock.Unlock()
ttd := api.config().TerminalTotalDifficulty ttd := api.config().TerminalTotalDifficulty
if ttd == nil || ttd.Cmp(config.TerminalTotalDifficulty.ToInt()) != 0 { if ttd == nil || ttd.Cmp(config.TerminalTotalDifficulty.ToInt()) != 0 {
@ -611,9 +605,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe
return api.invalid(err, nil), nil return api.invalid(err, nil), nil
} }
// Stash away the last update to warn the user if the beacon client goes offline // Stash away the last update to warn the user if the beacon client goes offline
api.lastNewPayloadLock.Lock() api.lastNewPayloadUpdate.Store(time.Now().Unix())
api.lastNewPayloadUpdate = time.Now()
api.lastNewPayloadLock.Unlock()
// If we already have the block locally, ignore the entire execution and just // If we already have the block locally, ignore the entire execution and just
// return a fake success. // return a fake success.
@ -814,17 +806,9 @@ func (api *ConsensusAPI) heartbeat() {
// Sleep a bit and retrieve the last known consensus updates // Sleep a bit and retrieve the last known consensus updates
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
api.lastTransitionLock.Lock() lastTransitionUpdate := time.Unix(api.lastTransitionUpdate.Load(), 0)
lastTransitionUpdate := api.lastTransitionUpdate lastForkchoiceUpdate := time.Unix(api.lastForkchoiceUpdate.Load(), 0)
api.lastTransitionLock.Unlock() lastNewPayloadUpdate := time.Unix(api.lastNewPayloadUpdate.Load(), 0)
api.lastForkchoiceLock.Lock()
lastForkchoiceUpdate := api.lastForkchoiceUpdate
api.lastForkchoiceLock.Unlock()
api.lastNewPayloadLock.Lock()
lastNewPayloadUpdate := api.lastNewPayloadUpdate
api.lastNewPayloadLock.Unlock()
// If there have been no updates for the past while, warn the user // If there have been no updates for the past while, warn the user
// that the beacon client is probably offline // that the beacon client is probably offline

View file

@ -280,9 +280,7 @@ func (api *ConsensusAPI) executeStatelessPayload(params engine.ExecutableData, v
return engine.StatelessPayloadStatusV1{Status: engine.INVALID, ValidationError: &errorMsg}, nil return engine.StatelessPayloadStatusV1{Status: engine.INVALID, ValidationError: &errorMsg}, nil
} }
// Stash away the last update to warn the user if the beacon client goes offline // Stash away the last update to warn the user if the beacon client goes offline
api.lastNewPayloadLock.Lock() api.lastNewPayloadUpdate.Store(time.Now().Unix())
api.lastNewPayloadUpdate = time.Now()
api.lastNewPayloadLock.Unlock()
log.Trace("Executing block statelessly", "number", block.Number(), "hash", params.BlockHash) log.Trace("Executing block statelessly", "number", block.Number(), "hash", params.BlockHash)
stateRoot, receiptRoot, err := core.ExecuteStateless(api.config(), vm.Config{}, block, witness) stateRoot, receiptRoot, err := core.ExecuteStateless(api.config(), vm.Config{}, block, witness)