Refactor to use atomic types (#15625)

* refactor to use atomic types

* added changlog fragment file

* update change log
This commit is contained in:
Sahil Sojitra
2025-08-27 01:27:14 +05:30
committed by GitHub
parent 4a1ab70929
commit e0aecb9c32
6 changed files with 17 additions and 13 deletions

View File

@@ -0,0 +1,3 @@
### Changed
- Changed old atomic functions to new atomic.Int for safer and clearer code.

View File

@@ -24,10 +24,10 @@ func ErrNotSupported(funcName string, ver int) error {
// ThreadSafeEnumerator is a thread-safe counter of all objects created since the node's start.
type ThreadSafeEnumerator struct {
counter uint64
counter atomic.Uint64
}
// Inc increments the enumerator and returns the new object count.
func (c *ThreadSafeEnumerator) Inc() uint64 {
return atomic.AddUint64(&c.counter, 1)
return c.counter.Add(1)
}

View File

@@ -10,18 +10,18 @@ import (
// Arbitrary start time.
var start = time.Date(1990, 1, 2, 0, 0, 0, 0, time.UTC).Round(0)
var elapsed int64
var elapsed atomic.Int64
// We provide atomic access to elapsed to avoid data races between multiple
// concurrent goroutines during the tests.
func getElapsed() time.Duration {
return time.Duration(atomic.LoadInt64(&elapsed))
return time.Duration(elapsed.Load())
}
func setElapsed(v time.Duration) {
atomic.StoreInt64(&elapsed, int64(v))
elapsed.Store(int64(v))
}
func addToElapsed(v time.Duration) {
atomic.AddInt64(&elapsed, int64(v))
elapsed.Add(int64(v))
}
func reset(t *testing.T, c *Collector) {

View File

@@ -3,7 +3,6 @@ package client
import (
"fmt"
"strconv"
"sync/atomic"
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
"github.com/OffchainLabs/prysm/v6/encoding/bytesutil"
@@ -138,9 +137,11 @@ func (v *validator) LogSubmittedAtts(slot primitives.Slot) {
// LogSubmittedSyncCommitteeMessages logs info about submitted sync committee messages.
func (v *validator) LogSubmittedSyncCommitteeMessages() {
if v.syncCommitteeStats.totalMessagesSubmitted > 0 {
log.WithField("messages", v.syncCommitteeStats.totalMessagesSubmitted).Debug("Submitted sync committee messages successfully to beacon node")
if count := v.syncCommitteeStats.totalMessagesSubmitted.Load(); count > 0 {
log.WithField("messages", count).
Debug("Submitted sync committee messages successfully to beacon node")
// Reset the amount.
atomic.StoreUint64(&v.syncCommitteeStats.totalMessagesSubmitted, 0)
v.syncCommitteeStats.totalMessagesSubmitted.Store(0)
}
}

View File

@@ -3,7 +3,6 @@ package client
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/altair"
@@ -94,7 +93,7 @@ func (v *validator) SubmitSyncCommitteeMessage(ctx context.Context, slot primiti
"blockRoot": fmt.Sprintf("%#x", bytesutil.Trunc(msg.BlockRoot)),
"validatorIndex": msg.ValidatorIndex,
}).Info("Submitted new sync message")
atomic.AddUint64(&v.syncCommitteeStats.totalMessagesSubmitted, 1)
v.syncCommitteeStats.totalMessagesSubmitted.Add(1)
}
// SubmitSignedContributionAndProof submits the signed sync committee contribution and proof to the beacon chain.

View File

@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/OffchainLabs/prysm/v6/api/client"
@@ -1571,5 +1572,5 @@ type voteStats struct {
// This tracks all validators' submissions for sync committees.
type syncCommitteeStats struct {
totalMessagesSubmitted uint64
totalMessagesSubmitted atomic.Uint64
}