Skip to content

Commit 23f07d8

Browse files
eth/catalyst: use atomics instead of locks (#31955)
1 parent 500ed3b commit 23f07d8

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

eth/catalyst/api.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"strconv"
2424
"sync"
25+
"sync/atomic"
2526
"time"
2627

2728
"github.com/ethereum/go-ethereum/beacon/engine"
@@ -143,12 +144,9 @@ type ConsensusAPI struct {
143144
// Geth can appear to be stuck or do strange things if the beacon client is
144145
// offline or is sending us strange data. Stash some update stats away so
145146
// that we can warn the user and not have them open issues on our tracker.
146-
lastTransitionUpdate time.Time
147-
lastTransitionLock sync.Mutex
148-
lastForkchoiceUpdate time.Time
149-
lastForkchoiceLock sync.Mutex
150-
lastNewPayloadUpdate time.Time
151-
lastNewPayloadLock sync.Mutex
147+
lastTransitionUpdate atomic.Int64
148+
lastForkchoiceUpdate atomic.Int64
149+
lastNewPayloadUpdate atomic.Int64
152150

153151
forkchoiceLock sync.Mutex // Lock for the forkChoiceUpdated method
154152
newPayloadLock sync.Mutex // Lock for the NewPayload method
@@ -252,9 +250,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
252250
return engine.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this?
253251
}
254252
// Stash away the last update to warn the user if the beacon client goes offline
255-
api.lastForkchoiceLock.Lock()
256-
api.lastForkchoiceUpdate = time.Now()
257-
api.lastForkchoiceLock.Unlock()
253+
api.lastForkchoiceUpdate.Store(time.Now().Unix())
258254

259255
// Check whether we have the block yet in our database or not. If not, we'll
260256
// 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
398394
return nil, errors.New("invalid terminal total difficulty")
399395
}
400396
// Stash away the last update to warn the user if the beacon client goes offline
401-
api.lastTransitionLock.Lock()
402-
api.lastTransitionUpdate = time.Now()
403-
api.lastTransitionLock.Unlock()
397+
api.lastTransitionUpdate.Store(time.Now().Unix())
404398

405399
ttd := api.config().TerminalTotalDifficulty
406400
if ttd == nil || ttd.Cmp(config.TerminalTotalDifficulty.ToInt()) != 0 {
@@ -611,9 +605,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe
611605
return api.invalid(err, nil), nil
612606
}
613607
// Stash away the last update to warn the user if the beacon client goes offline
614-
api.lastNewPayloadLock.Lock()
615-
api.lastNewPayloadUpdate = time.Now()
616-
api.lastNewPayloadLock.Unlock()
608+
api.lastNewPayloadUpdate.Store(time.Now().Unix())
617609

618610
// If we already have the block locally, ignore the entire execution and just
619611
// return a fake success.
@@ -814,17 +806,9 @@ func (api *ConsensusAPI) heartbeat() {
814806
// Sleep a bit and retrieve the last known consensus updates
815807
time.Sleep(5 * time.Second)
816808

817-
api.lastTransitionLock.Lock()
818-
lastTransitionUpdate := api.lastTransitionUpdate
819-
api.lastTransitionLock.Unlock()
820-
821-
api.lastForkchoiceLock.Lock()
822-
lastForkchoiceUpdate := api.lastForkchoiceUpdate
823-
api.lastForkchoiceLock.Unlock()
824-
825-
api.lastNewPayloadLock.Lock()
826-
lastNewPayloadUpdate := api.lastNewPayloadUpdate
827-
api.lastNewPayloadLock.Unlock()
809+
lastTransitionUpdate := time.Unix(api.lastTransitionUpdate.Load(), 0)
810+
lastForkchoiceUpdate := time.Unix(api.lastForkchoiceUpdate.Load(), 0)
811+
lastNewPayloadUpdate := time.Unix(api.lastNewPayloadUpdate.Load(), 0)
828812

829813
// If there have been no updates for the past while, warn the user
830814
// that the beacon client is probably offline

eth/catalyst/witness.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ func (api *ConsensusAPI) executeStatelessPayload(params engine.ExecutableData, v
280280
return engine.StatelessPayloadStatusV1{Status: engine.INVALID, ValidationError: &errorMsg}, nil
281281
}
282282
// Stash away the last update to warn the user if the beacon client goes offline
283-
api.lastNewPayloadLock.Lock()
284-
api.lastNewPayloadUpdate = time.Now()
285-
api.lastNewPayloadLock.Unlock()
283+
api.lastNewPayloadUpdate.Store(time.Now().Unix())
286284

287285
log.Trace("Executing block statelessly", "number", block.Number(), "hash", params.BlockHash)
288286
stateRoot, receiptRoot, err := core.ExecuteStateless(api.config(), vm.Config{}, block, witness)

0 commit comments

Comments
 (0)