*: replace go.uber.org/atomic with sync/atomic

Use sync/atomic everywhere and exclude go.uber.org/atomic from go.mod.
Close #2626.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-10-11 13:24:16 +03:00
parent 7ca4ce0f79
commit dc3d1300dd
24 changed files with 57 additions and 70 deletions

View file

@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@ -42,7 +43,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
// Keep contract NEFs consistent between runs.

2
go.mod
View file

@ -25,7 +25,6 @@ require (
github.com/twmb/murmur3 v1.1.5
github.com/urfave/cli v1.22.5
go.etcd.io/bbolt v1.3.7
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.12.0
golang.org/x/term v0.11.0
@ -64,6 +63,7 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/mod v0.12.0 // indirect

View file

@ -18,7 +18,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
uatomic "go.uber.org/atomic"
)
// FakeChain implements the Blockchainer interface, but does not provide real functionality.
@ -26,7 +25,7 @@ type FakeChain struct {
config.Blockchain
*mempool.Pool
blocksCh []chan *block.Block
Blockheight uint32
Blockheight atomic.Uint32
PoolTxF func(*transaction.Transaction) error
poolTxWithData func(*transaction.Transaction, any, *mempool.Pool) error
blocks map[util.Uint256]*block.Block
@ -42,9 +41,9 @@ type FakeChain struct {
// FakeStateSync implements the StateSync interface.
type FakeStateSync struct {
IsActiveFlag uatomic.Bool
IsInitializedFlag uatomic.Bool
RequestHeaders uatomic.Bool
IsActiveFlag atomic.Bool
IsInitializedFlag atomic.Bool
RequestHeaders atomic.Bool
InitFunc func(h uint32) error
TraverseFunc func(root util.Uint256, process func(node mpt.Node, nodeBytes []byte) bool) error
AddMPTNodesFunc func(nodes [][]byte) error
@ -76,7 +75,7 @@ func NewFakeChainWithCustomCfg(protocolCfg func(c *config.Blockchain)) *FakeChai
func (chain *FakeChain) PutBlock(b *block.Block) {
chain.blocks[b.Hash()] = b
chain.hdrHashes[b.Index] = b.Hash()
atomic.StoreUint32(&chain.Blockheight, b.Index)
chain.Blockheight.Store(b.Index)
}
// PutHeader implements the Blockchainer interface.
@ -185,7 +184,7 @@ func (chain *FakeChain) AddHeaders(...*block.Header) error {
// AddBlock implements the Blockchainer interface.
func (chain *FakeChain) AddBlock(block *block.Block) error {
if block.Index == atomic.LoadUint32(&chain.Blockheight)+1 {
if block.Index == chain.Blockheight.Load()+1 {
chain.PutBlock(block)
}
return nil
@ -193,12 +192,12 @@ func (chain *FakeChain) AddBlock(block *block.Block) error {
// BlockHeight implements the Feer interface.
func (chain *FakeChain) BlockHeight() uint32 {
return atomic.LoadUint32(&chain.Blockheight)
return chain.Blockheight.Load()
}
// HeaderHeight implements the Blockchainer interface.
func (chain *FakeChain) HeaderHeight() uint32 {
return atomic.LoadUint32(&chain.Blockheight)
return chain.Blockheight.Load()
}
// GetAppExecResults implements the Blockchainer interface.

View file

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sort"
"sync/atomic"
"time"
"github.com/nspcc-dev/dbft"
@ -26,7 +27,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -100,7 +100,7 @@ type service struct {
wallet *wallet.Wallet
// started is a flag set with Start method that runs an event handling
// goroutine.
started *atomic.Bool
started atomic.Bool
quit chan struct{}
finished chan struct{}
// lastTimestamp contains timestamp for the last processed block.
@ -155,7 +155,6 @@ func NewService(cfg Config) (Service, error) {
transactions: make(chan *transaction.Transaction, 100),
blockEvents: make(chan *coreb.Block, 1),
started: atomic.NewBool(false),
quit: make(chan struct{}),
finished: make(chan struct{}),
}

View file

@ -6,12 +6,12 @@ import (
"math/bits"
"sort"
"sync"
"sync/atomic"
"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/core/mempoolevent"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic"
)
var (

View file

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -18,7 +19,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic"
"go.uber.org/zap"
)

View file

@ -2,9 +2,9 @@ package bqueue
import (
"sync"
"sync/atomic"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -24,7 +24,7 @@ type Queue struct {
checkBlocks chan struct{}
chain Blockqueuer
relayF func(*block.Block)
discarded *atomic.Bool
discarded atomic.Bool
len int
lenUpdateF func(int)
}
@ -49,7 +49,6 @@ func New(bc Blockqueuer, log *zap.Logger, relayer func(*block.Block), lenMetrics
checkBlocks: make(chan struct{}, 1),
chain: bc,
relayF: relayer,
discarded: atomic.NewBool(false),
lenUpdateF: lenMetricsUpdater,
}
}

View file

@ -12,13 +12,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
atomic2 "go.uber.org/atomic"
)
type fakeTransp struct {
retFalse int32
started atomic2.Bool
closed atomic2.Bool
retFalse atomic.Int32
started atomic.Bool
closed atomic.Bool
dialCh chan string
host string
port string
@ -58,7 +57,7 @@ func newFakeTransp(s *Server, addr string) Transporter {
func (ft *fakeTransp) Dial(addr string, timeout time.Duration) (AddressablePeer, error) {
var ret error
if atomic.LoadInt32(&ft.retFalse) > 0 {
if ft.retFalse.Load() > 0 {
ret = errors.New("smth bad happened")
}
ft.dialCh <- addr
@ -174,7 +173,7 @@ func TestDefaultDiscoverer(t *testing.T) {
require.Equal(t, 2, d.PoolCount())
// Now make Dial() fail and wait to see addresses in the bad list.
atomic.StoreInt32(&ts.retFalse, 1)
ts.retFalse.Store(1)
assert.Equal(t, len(set1), d.PoolCount())
set1D := d.UnconnectedPeers()
sort.Strings(set1D)
@ -216,7 +215,7 @@ func TestSeedDiscovery(t *testing.T) {
var seeds = []string{"1.1.1.1:10333", "2.2.2.2:10333"}
ts := &fakeTransp{}
ts.dialCh = make(chan string)
atomic.StoreInt32(&ts.retFalse, 1) // Fail all dial requests.
ts.retFalse.Store(1) // Fail all dial requests.
sort.Strings(seeds)
d := NewDefaultDiscovery(seeds, time.Second/10, ts)

View file

@ -13,7 +13,7 @@ import (
"sort"
"strconv"
"sync"
satomic "sync/atomic"
"sync/atomic"
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -29,7 +29,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/network/extpool"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -112,7 +111,7 @@ type (
services map[string]Service
extensHandlers map[string]func(*payload.Extensible) error
txCallback func(*transaction.Transaction)
txCbList satomic.Value
txCbList atomic.Value
txInLock sync.RWMutex
txin chan *transaction.Transaction
@ -134,7 +133,7 @@ type (
transactions chan *transaction.Transaction
syncReached *atomic.Bool
syncReached atomic.Bool
stateSync StateSync
@ -186,7 +185,6 @@ func newServerFromConstructors(config ServerConfig, chain Ledger, stSync StateSy
handshake: make(chan Peer),
txInMap: make(map[util.Uint256]struct{}),
peers: make(map[Peer]bool),
syncReached: atomic.NewBool(false),
mempool: chain.GetMemPool(),
extensiblePool: extpool.New(chain, config.ExtensiblePoolSize),
log: log,

View file

@ -7,7 +7,7 @@ import (
"net"
"strconv"
"sync"
atomic2 "sync/atomic"
"sync/atomic"
"testing"
"time"
@ -27,7 +27,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap/zaptest"
)
@ -223,7 +222,7 @@ func testGetBlocksByIndex(t *testing.T, cmd CommandType) {
checkPingRespond(t, 3, 5000, 1+3*payload.MaxHashesCount)
// Receive some blocks.
s.chain.(*fakechain.FakeChain).Blockheight = 2123
s.chain.(*fakechain.FakeChain).Blockheight.Store(2123)
// Minimum chunk has priority.
checkPingRespond(t, 5, 5000, 2124)
@ -399,7 +398,7 @@ func startWithCleanup(t *testing.T, s *Server) {
func TestBlock(t *testing.T) {
s := startTestServer(t)
atomic2.StoreUint32(&s.chain.(*fakechain.FakeChain).Blockheight, 12344)
s.chain.(*fakechain.FakeChain).Blockheight.Store(12344)
require.Equal(t, uint32(12344), s.chain.BlockHeight())
b := block.New(false)
@ -414,7 +413,7 @@ func TestConsensus(t *testing.T) {
s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction)
startWithCleanup(t, s)
atomic2.StoreUint32(&s.chain.(*fakechain.FakeChain).Blockheight, 4)
s.chain.(*fakechain.FakeChain).Blockheight.Store(4)
p := newLocalPeer(t, s)
p.handshaked = 1
s.register <- p

View file

@ -7,12 +7,12 @@ import (
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/capability"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"go.uber.org/atomic"
)
type handShakeStage uint8
@ -494,12 +494,12 @@ func (p *TCPPeer) HandlePong(pong *payload.Ping) error {
// AddGetAddrSent increments internal outstanding getaddr requests counter. Then,
// the peer can only send one addr reply per getaddr request.
func (p *TCPPeer) AddGetAddrSent() {
p.getAddrSent.Inc()
p.getAddrSent.Add(1)
}
// CanProcessAddr decrements internal outstanding getaddr requests counter and
// answers whether the addr command from the peer can be safely processed.
func (p *TCPPeer) CanProcessAddr() bool {
v := p.getAddrSent.Dec()
v := p.getAddrSent.Add(-1)
return v >= 0
}

View file

@ -3,6 +3,7 @@ package actor
import (
"context"
"errors"
"sync/atomic"
"testing"
"github.com/google/uuid"
@ -15,7 +16,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
type RPCClient struct {

View file

@ -10,13 +10,13 @@ import (
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/atomic"
)
const (
@ -48,7 +48,7 @@ type Client struct {
// during regular Client lifecycle.
cache cache
latestReqID *atomic.Uint64
latestReqID atomic.Uint64
// getNextRequestID returns an ID to be used for the subsequent request creation.
// It is defined on Client, so that our testing code can override this method
// for the sake of more predictable request IDs generation behavior.
@ -126,7 +126,7 @@ func initClient(ctx context.Context, cl *Client, endpoint string, opts Options)
cl.cache = cache{
nativeHashes: make(map[string]util.Uint160),
}
cl.latestReqID = atomic.NewUint64(0)
cl.latestReqID = atomic.Uint64{}
cl.getNextRequestID = (cl).getRequestID
cl.opts = opts
cl.requestF = cl.makeHTTPRequest
@ -135,7 +135,7 @@ func initClient(ctx context.Context, cl *Client, endpoint string, opts Options)
}
func (c *Client) getRequestID() uint64 {
return c.latestReqID.Inc()
return c.latestReqID.Add(1)
}
// Init sets magic of the network client connected to, stateRootInHeader option

View file

@ -4,7 +4,6 @@ import (
"context"
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"go.uber.org/atomic"
)
// InternalHook is a function signature that is required to create a local client
@ -31,7 +30,6 @@ func NewInternal(ctx context.Context, register InternalHook) (*Internal, error)
shutdown: make(chan struct{}),
done: make(chan struct{}),
closeCalled: *atomic.NewBool(false),
subscriptions: make(map[string]notificationReceiver),
receivers: make(map[any][]string),
},

View file

@ -7,6 +7,7 @@ import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@ -16,7 +17,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
"github.com/nspcc-dev/neo-go/pkg/neorpc/rpcevent"
"go.uber.org/atomic"
)
// WSClient is a websocket-enabled RPC client that can be used with appropriate
@ -426,7 +426,6 @@ func NewWS(ctx context.Context, endpoint string, opts WSOptions) (*WSClient, err
wsOpts: opts,
shutdown: make(chan struct{}),
done: make(chan struct{}),
closeCalled: *atomic.NewBool(false),
respChannels: make(map[uint64]chan *neorpc.Response),
requests: make(chan *neorpc.Request),
subscriptions: make(map[string]notificationReceiver),

View file

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@ -26,7 +27,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
func TestWSClientClose(t *testing.T) {
@ -677,23 +677,23 @@ func TestWSConcurrentAccess(t *testing.T) {
wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), WSOptions{})
require.NoError(t, err)
batchCount := 100
completed := atomic.NewInt32(0)
completed := &atomic.Int32{}
for i := 0; i < batchCount; i++ {
go func() {
_, err := wsc.GetBlockCount()
require.NoError(t, err)
completed.Inc()
completed.Add(1)
}()
go func() {
_, err := wsc.GetBlockHash(123)
require.NoError(t, err)
completed.Inc()
completed.Add(1)
}()
go func() {
_, err := wsc.GetVersion()
require.NoError(t, err)
completed.Inc()
completed.Add(1)
}()
}
require.Eventually(t, func() bool {

View file

@ -6,9 +6,9 @@ import (
"fmt"
"net"
"net/http"
"sync/atomic"
"github.com/nspcc-dev/neo-go/pkg/config"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -18,7 +18,7 @@ type Service struct {
config config.BasicService
log *zap.Logger
serviceType string
started *atomic.Bool
started atomic.Bool
}
// NewService configures logger and returns new service instance.
@ -28,7 +28,6 @@ func NewService(name string, httpServers []*http.Server, cfg config.BasicService
config: cfg,
serviceType: name,
log: log.With(zap.String("service", name)),
started: atomic.NewBool(false),
}
}

View file

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
@ -23,7 +24,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -50,7 +50,7 @@ type (
// to be processed in an `onTransaction` callback.
newTxs chan txHashPair
// started is a status bool to protect from double start/shutdown.
started *atomic.Bool
started atomic.Bool
// reqMtx protects requests list.
reqMtx sync.RWMutex
@ -152,7 +152,6 @@ func NewNotary(cfg Config, net netmode.Magic, mp *mempool.Pool, onTransaction fu
requests: make(map[util.Uint256]*request),
Config: cfg,
Network: net,
started: atomic.NewBool(false),
wallet: wallet,
onTransaction: onTransaction,
newTxs: make(chan txHashPair, defaultTxChannelCapacity),

View file

@ -16,6 +16,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
@ -56,7 +57,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -140,7 +140,7 @@ type (
oracle *atomic.Value
log *zap.Logger
shutdown chan struct{}
started *atomic.Bool
started atomic.Bool
errChan chan<- error
sessionsLock sync.Mutex
@ -339,7 +339,6 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server,
log: log,
oracle: oracleWrapped,
shutdown: make(chan struct{}),
started: atomic.NewBool(false),
errChan: errChan,
sessions: make(map[string]*session),

View file

@ -15,6 +15,7 @@ import (
"sort"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
@ -53,7 +54,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap/zapcore"
)

View file

@ -1,9 +1,10 @@
package rpcsrv
import (
"sync/atomic"
"github.com/gorilla/websocket"
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"go.uber.org/atomic"
)
type (

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strings"
"sync/atomic"
"testing"
"time"
@ -15,7 +16,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/neorpc"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
const testOverflow = false
@ -68,7 +68,7 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso
// Use buffered channel to read server's messages and then read expected
// responses from it.
respMsgs := make(chan []byte, 16)
finishedFlag := atomic.NewBool(false)
finishedFlag := &atomic.Bool{}
go wsReader(t, ws, respMsgs, finishedFlag)
return chain, rpcSrv, ws, respMsgs, finishedFlag
}

View file

@ -3,6 +3,7 @@ package stateroot
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -14,7 +15,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/atomic"
"go.uber.org/zap"
)
@ -50,7 +50,7 @@ type (
Network netmode.Magic
log *zap.Logger
started *atomic.Bool
started atomic.Bool
accMtx sync.RWMutex
accHeight uint32
myIndex byte
@ -83,7 +83,6 @@ func New(cfg config.StateRoot, sm *stateroot.Module, log *zap.Logger, bc Ledger,
s := &service{
Module: sm,
Network: bcConf.Magic,
started: atomic.NewBool(false),
chain: bc,
log: log,
incompleteRoots: make(map[uint32]*incompleteRoot),

View file

@ -4,6 +4,7 @@ import (
"crypto/elliptic"
"path/filepath"
"sort"
"sync/atomic"
"testing"
"time"
@ -32,7 +33,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap/zaptest"
)