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. // ThreadSafeEnumerator is a thread-safe counter of all objects created since the node's start.
type ThreadSafeEnumerator struct { type ThreadSafeEnumerator struct {
counter uint64 counter atomic.Uint64
} }
// Inc increments the enumerator and returns the new object count. // Inc increments the enumerator and returns the new object count.
func (c *ThreadSafeEnumerator) Inc() uint64 { 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. // Arbitrary start time.
var start = time.Date(1990, 1, 2, 0, 0, 0, 0, time.UTC).Round(0) 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 // We provide atomic access to elapsed to avoid data races between multiple
// concurrent goroutines during the tests. // concurrent goroutines during the tests.
func getElapsed() time.Duration { func getElapsed() time.Duration {
return time.Duration(atomic.LoadInt64(&elapsed)) return time.Duration(elapsed.Load())
} }
func setElapsed(v time.Duration) { func setElapsed(v time.Duration) {
atomic.StoreInt64(&elapsed, int64(v)) elapsed.Store(int64(v))
} }
func addToElapsed(v time.Duration) { func addToElapsed(v time.Duration) {
atomic.AddInt64(&elapsed, int64(v)) elapsed.Add(int64(v))
} }
func reset(t *testing.T, c *Collector) { func reset(t *testing.T, c *Collector) {

View File

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

View File

@@ -3,7 +3,6 @@ package client
import ( import (
"context" "context"
"fmt" "fmt"
"sync/atomic"
"time" "time"
"github.com/OffchainLabs/prysm/v6/beacon-chain/core/altair" "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)), "blockRoot": fmt.Sprintf("%#x", bytesutil.Trunc(msg.BlockRoot)),
"validatorIndex": msg.ValidatorIndex, "validatorIndex": msg.ValidatorIndex,
}).Info("Submitted new sync message") }).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. // SubmitSignedContributionAndProof submits the signed sync committee contribution and proof to the beacon chain.

View File

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