@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"strconv"
24
24
"sync"
25
+ "sync/atomic"
25
26
"time"
26
27
27
28
"github.com/ethereum/go-ethereum/beacon/engine"
@@ -143,12 +144,9 @@ type ConsensusAPI struct {
143
144
// Geth can appear to be stuck or do strange things if the beacon client is
144
145
// offline or is sending us strange data. Stash some update stats away so
145
146
// 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
152
150
153
151
forkchoiceLock sync.Mutex // Lock for the forkChoiceUpdated method
154
152
newPayloadLock sync.Mutex // Lock for the NewPayload method
@@ -252,9 +250,7 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
252
250
return engine .STATUS_INVALID , nil // TODO(karalabe): Why does someone send us this?
253
251
}
254
252
// 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 ())
258
254
259
255
// Check whether we have the block yet in our database or not. If not, we'll
260
256
// 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
398
394
return nil , errors .New ("invalid terminal total difficulty" )
399
395
}
400
396
// 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 ())
404
398
405
399
ttd := api .config ().TerminalTotalDifficulty
406
400
if ttd == nil || ttd .Cmp (config .TerminalTotalDifficulty .ToInt ()) != 0 {
@@ -611,9 +605,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe
611
605
return api .invalid (err , nil ), nil
612
606
}
613
607
// 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 ())
617
609
618
610
// If we already have the block locally, ignore the entire execution and just
619
611
// return a fake success.
@@ -814,17 +806,9 @@ func (api *ConsensusAPI) heartbeat() {
814
806
// Sleep a bit and retrieve the last known consensus updates
815
807
time .Sleep (5 * time .Second )
816
808
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 )
828
812
829
813
// If there have been no updates for the past while, warn the user
830
814
// that the beacon client is probably offline
0 commit comments