From 4e951ed8fe2dd0b45288f32fb6edf5a1a18ca020 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:15 +0800 Subject: [PATCH] all: use github.com/deckarep/golang-set/v2 (generic set) (#26159) --- accounts/keystore/account_cache.go | 15 +++++++-------- accounts/keystore/file_cache.go | 12 ++++++------ consensus/ethash/consensus.go | 4 ++-- eth/peer.go | 30 +++++++++++++++--------------- go.mod | 2 +- go.sum | 4 ++-- miner/worker.go | 16 ++++++++-------- rpc/server.go | 14 +++++++------- rpc/websocket.go | 8 ++++---- 9 files changed, 52 insertions(+), 53 deletions(-) diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go index ad497d7e0f..aec9825bab 100644 --- a/accounts/keystore/account_cache.go +++ b/accounts/keystore/account_cache.go @@ -30,7 +30,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/accounts" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/log" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) // Minimum amount of time between cache reloads. This limit applies if the platform does @@ -79,7 +79,7 @@ func newAccountCache(keydir string) (*accountCache, chan struct{}) { keydir: keydir, byAddr: make(map[common.Address][]accounts.Account), notify: make(chan struct{}, 1), - fileC: fileCache{all: mapset.NewThreadUnsafeSet()}, + fileC: fileCache{all: mapset.NewThreadUnsafeSet[string]()}, } ac.watcher = newWatcher(ac) return ac, ac.notify @@ -283,16 +283,15 @@ func (ac *accountCache) scanAccounts() error { // Process all the file diffs start := time.Now() - for _, p := range creates.ToSlice() { - if a := readAccount(p.(string)); a != nil { + for _, path := range creates.ToSlice() { + if a := readAccount(path); a != nil { ac.add(*a) } } - for _, p := range deletes.ToSlice() { - ac.deleteByFile(p.(string)) + for _, path := range deletes.ToSlice() { + ac.deleteByFile(path) } - for _, p := range updates.ToSlice() { - path := p.(string) + for _, path := range updates.ToSlice() { ac.deleteByFile(path) if a := readAccount(path); a != nil { ac.add(*a) diff --git a/accounts/keystore/file_cache.go b/accounts/keystore/file_cache.go index f2bd7efbe6..59c047d1fa 100644 --- a/accounts/keystore/file_cache.go +++ b/accounts/keystore/file_cache.go @@ -24,19 +24,19 @@ import ( "time" "github.com/XinFinOrg/XDPoSChain/log" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) // fileCache is a cache of files seen during scan of keystore. type fileCache struct { - all mapset.Set // Set of all files from the keystore folder - lastMod time.Time // Last time instance when a file was modified + all mapset.Set[string] // Set of all files from the keystore folder + lastMod time.Time // Last time instance when a file was modified mu sync.Mutex } // scan performs a new scan on the given directory, compares against the already // cached filenames, and returns file sets: creates, deletes, updates. -func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) { +func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) { t0 := time.Now() // List all the files from the keystore folder @@ -50,8 +50,8 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er defer fc.mu.Unlock() // Iterate all the files and gather their metadata - all := mapset.NewThreadUnsafeSet() - mods := mapset.NewThreadUnsafeSet() + all := mapset.NewThreadUnsafeSet[string]() + mods := mapset.NewThreadUnsafeSet[string]() var newLastMod time.Time for _, fi := range files { diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index e6ecc9f7ab..6cf41dddeb 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -32,7 +32,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/params" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) // Ethash proof-of-work protocol constants. @@ -178,7 +178,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo return errTooManyUncles } // Gather the set of past uncles and ancestors - uncles, ancestors := mapset.NewSet(), make(map[common.Hash]*types.Header) + uncles, ancestors := mapset.NewSet[common.Hash](), make(map[common.Hash]*types.Header) number, parent := block.NumberU64()-1, block.ParentHash() for i := 0; i < 7; i++ { diff --git a/eth/peer.go b/eth/peer.go index eb7730b99a..458ae56fe4 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -27,7 +27,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/p2p" "github.com/XinFinOrg/XDPoSChain/rlp" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) var ( @@ -69,15 +69,15 @@ type peer struct { td *big.Int lock sync.RWMutex - knownTxs mapset.Set // Set of transaction hashes known to be known by this peer - knownBlocks mapset.Set // Set of block hashes known to be known by this peer + knownTxs mapset.Set[common.Hash] // Set of transaction hashes known to be known by this peer + knownBlocks mapset.Set[common.Hash] // Set of block hashes known to be known by this peer - knownOrderTxs mapset.Set // Set of order transaction hashes known to be known by this peer - knownLendingTxs mapset.Set // Set of lending transaction hashes known to be known by this peer + knownOrderTxs mapset.Set[common.Hash] // Set of order transaction hashes known to be known by this peer + knownLendingTxs mapset.Set[common.Hash] // Set of lending transaction hashes known to be known by this peer - knownVote mapset.Set // Set of BFT Vote known to be known by this peer - knownTimeout mapset.Set // Set of BFT timeout known to be known by this peer - knownSyncInfo mapset.Set // Set of BFT Sync Info known to be known by this peer + knownVote mapset.Set[common.Hash] // Set of BFT Vote known to be known by this peer + knownTimeout mapset.Set[common.Hash] // Set of BFT timeout known to be known by this peer + knownSyncInfo mapset.Set[common.Hash] // Set of BFT Sync Info known to be known by this peer } func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { @@ -88,14 +88,14 @@ func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { rw: rw, version: version, id: fmt.Sprintf("%x", id[:8]), - knownTxs: mapset.NewSet(), - knownBlocks: mapset.NewSet(), - knownOrderTxs: mapset.NewSet(), - knownLendingTxs: mapset.NewSet(), + knownTxs: mapset.NewSet[common.Hash](), + knownBlocks: mapset.NewSet[common.Hash](), + knownOrderTxs: mapset.NewSet[common.Hash](), + knownLendingTxs: mapset.NewSet[common.Hash](), - knownVote: mapset.NewSet(), - knownTimeout: mapset.NewSet(), - knownSyncInfo: mapset.NewSet(), + knownVote: mapset.NewSet[common.Hash](), + knownTimeout: mapset.NewSet[common.Hash](), + knownSyncInfo: mapset.NewSet[common.Hash](), } } diff --git a/go.mod b/go.mod index c53a1f35ec..b84e0ed811 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,7 @@ require ( require ( github.com/consensys/gnark-crypto v0.10.0 github.com/crate-crypto/go-kzg-4844 v0.7.0 - github.com/deckarep/golang-set v1.8.0 + github.com/deckarep/golang-set/v2 v2.7.0 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 github.com/dop251/goja v0.0.0-20200721192441-a695b0cdd498 github.com/ethereum/c-kzg-4844 v0.4.0 diff --git a/go.sum b/go.sum index 13b3389aa1..16558b1e15 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,8 @@ github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4= -github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo= +github.com/deckarep/golang-set/v2 v2.7.0 h1:gIloKvD7yH2oip4VLhsv3JyLLFnC0Y2mlusgcvJYW5k= +github.com/deckarep/golang-set/v2 v2.7.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= diff --git a/miner/worker.go b/miner/worker.go index 6514b48811..86822dda23 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -43,7 +43,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/event" "github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/params" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) const ( @@ -80,16 +80,16 @@ type Work struct { parentState *state.StateDB tradingState *tradingstate.TradingStateDB lendingState *lendingstate.LendingStateDB - ancestors mapset.Set // ancestor set (used for checking uncle parent validity) - family mapset.Set // family set (used for checking uncle invalidity) - uncles mapset.Set // uncle set - tcount int // tx count in cycle + ancestors mapset.Set[common.Hash] // ancestor set (used for checking uncle parent validity) + family mapset.Set[common.Hash] // family set (used for checking uncle invalidity) + tcount int // tx count in cycle Block *types.Block // the new block header *types.Header txs []*types.Transaction receipts []*types.Receipt + uncles map[common.Hash]*types.Header createdAt time.Time } @@ -575,10 +575,10 @@ func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error { parentState: state.Copy(), tradingState: XDCxState, lendingState: lendingState, - ancestors: mapset.NewSet(), - family: mapset.NewSet(), - uncles: mapset.NewSet(), + ancestors: mapset.NewSet[common.Hash](), + family: mapset.NewSet[common.Hash](), header: header, + uncles: make(map[common.Hash]*types.Header), createdAt: time.Now(), } diff --git a/rpc/server.go b/rpc/server.go index a1686bd008..5c1670e16f 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -22,7 +22,7 @@ import ( "sync/atomic" "github.com/XinFinOrg/XDPoSChain/log" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" ) const MetadataApi = "rpc" @@ -45,12 +45,12 @@ type Server struct { services serviceRegistry idgen func() ID run int32 - codecs mapset.Set + codecs mapset.Set[*ServerCodec] } // NewServer creates a new server instance with no registered handlers. func NewServer() *Server { - server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet(), run: 1} + server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet[*ServerCodec](), run: 1} // Register the default service providing meta information about the RPC service such // as the services and methods it offers. rpcService := &RPCService{server} @@ -80,8 +80,8 @@ func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) { } // Add the codec to the set so it can be closed by Stop. - s.codecs.Add(codec) - defer s.codecs.Remove(codec) + s.codecs.Add(&codec) + defer s.codecs.Remove(&codec) c := initClient(codec, s.idgen, &s.services) <-codec.closed() @@ -121,8 +121,8 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) { func (s *Server) Stop() { if atomic.CompareAndSwapInt32(&s.run, 1, 0) { log.Debug("RPC server shutting down") - s.codecs.Each(func(c interface{}) bool { - c.(ServerCodec).close() + s.codecs.Each(func(c *ServerCodec) bool { + (*c).close() return true }) } diff --git a/rpc/websocket.go b/rpc/websocket.go index 7c7cae4554..c916b79568 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -28,7 +28,7 @@ import ( "time" "github.com/XinFinOrg/XDPoSChain/log" - mapset "github.com/deckarep/golang-set" + mapset "github.com/deckarep/golang-set/v2" "github.com/gorilla/websocket" ) @@ -68,7 +68,7 @@ func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler { // websocket upgrade process. When a '*' is specified as an allowed origins all // connections are accepted. func wsHandshakeValidator(allowedOrigins []string) func(*http.Request) bool { - origins := mapset.NewSet() + origins := mapset.NewSet[string]() allowAllOrigins := false for _, origin := range allowedOrigins { @@ -121,10 +121,10 @@ func (e wsHandshakeError) Error() string { return s } -func originIsAllowed(allowedOrigins mapset.Set, browserOrigin string) bool { +func originIsAllowed(allowedOrigins mapset.Set[string], browserOrigin string) bool { it := allowedOrigins.Iterator() for origin := range it.C { - if ruleAllowsOrigin(origin.(string), browserOrigin) { + if ruleAllowsOrigin(origin, browserOrigin) { return true } }