Skip to content

Commit 5d30be4

Browse files
deckarepkaralabe
authored andcommitted
all: switch out defunct set library to different one (#16873)
* keystore, ethash, eth, miner, rpc, whisperv6: tech debt with now defunct set. * whisperv5: swap out gopkg.in/fatih/set.v0 with supported set
1 parent eb7f901 commit 5d30be4

25 files changed

Lines changed: 1069 additions & 843 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,4 @@ xiekeyang <xiekeyang@users.noreply.github.com>
171171
yoza <yoza.is12s@gmail.com>
172172
ΞTHΞЯSPHΞЯΞ <{viktor.tron,nagydani,zsfelfoldi}@gmail.com>
173173
Максим Чусовлянов <mchusovlianov@gmail.com>
174+
Ralph Caraveo <deckarep@gmail.com>

‎accounts/keystore/account_cache.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"sync"
2828
"time"
2929

30+
mapset "github.com/deckarep/golang-set"
3031
"github.com/ethereum/go-ethereum/accounts"
3132
"github.com/ethereum/go-ethereum/common"
3233
"github.com/ethereum/go-ethereum/log"
33-
"gopkg.in/fatih/set.v0"
3434
)
3535

3636
// 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{}) {
7979
keydir: keydir,
8080
byAddr: make(map[common.Address][]accounts.Account),
8181
notify: make(chan struct{}, 1),
82-
fileC: fileCache{all: set.NewNonTS()},
82+
fileC: fileCache{all: mapset.NewThreadUnsafeSet()},
8383
}
8484
ac.watcher = newWatcher(ac)
8585
return ac, ac.notify
@@ -237,7 +237,7 @@ func (ac *accountCache) scanAccounts() error {
237237
log.Debug("Failed to reload keystore contents", "err", err)
238238
return err
239239
}
240-
if creates.Size() == 0 && deletes.Size() == 0 && updates.Size() == 0 {
240+
if creates.Cardinality() == 0 && deletes.Cardinality() == 0 && updates.Cardinality() == 0 {
241241
return nil
242242
}
243243
// Create a helper method to scan the contents of the key files
@@ -272,15 +272,15 @@ func (ac *accountCache) scanAccounts() error {
272272
// Process all the file diffs
273273
start := time.Now()
274274

275-
for _, p := range creates.List() {
275+
for _, p := range creates.ToSlice() {
276276
if a := readAccount(p.(string)); a != nil {
277277
ac.add(*a)
278278
}
279279
}
280-
for _, p := range deletes.List() {
280+
for _, p := range deletes.ToSlice() {
281281
ac.deleteByFile(p.(string))
282282
}
283-
for _, p := range updates.List() {
283+
for _, p := range updates.ToSlice() {
284284
path := p.(string)
285285
ac.deleteByFile(path)
286286
if a := readAccount(path); a != nil {

‎accounts/keystore/file_cache.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ import (
2424
"sync"
2525
"time"
2626

27+
mapset "github.com/deckarep/golang-set"
2728
"github.com/ethereum/go-ethereum/log"
28-
set "gopkg.in/fatih/set.v0"
2929
)
3030

3131
// fileCache is a cache of files seen during scan of keystore.
3232
type fileCache struct {
33-
all *set.SetNonTS // Set of all files from the keystore folder
34-
lastMod time.Time // Last time instance when a file was modified
33+
all mapset.Set // Set of all files from the keystore folder
34+
lastMod time.Time // Last time instance when a file was modified
3535
mu sync.RWMutex
3636
}
3737

3838
// scan performs a new scan on the given directory, compares against the already
3939
// cached filenames, and returns file sets: creates, deletes, updates.
40-
func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
40+
func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
4141
t0 := time.Now()
4242

4343
// List all the failes from the keystore folder
@@ -51,8 +51,8 @@ func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Inte
5151
defer fc.mu.Unlock()
5252

5353
// Iterate all the files and gather their metadata
54-
all := set.NewNonTS()
55-
mods := set.NewNonTS()
54+
all := mapset.NewThreadUnsafeSet()
55+
mods := mapset.NewThreadUnsafeSet()
5656

5757
var newLastMod time.Time
5858
for _, fi := range files {
@@ -76,9 +76,9 @@ func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Inte
7676
t2 := time.Now()
7777

7878
// Update the tracked files and return the three sets
79-
deletes := set.Difference(fc.all, all) // Deletes = previous - current
80-
creates := set.Difference(all, fc.all) // Creates = current - previous
81-
updates := set.Difference(mods, creates) // Updates = modified - creates
79+
deletes := fc.all.Difference(all) // Deletes = previous - current
80+
creates := all.Difference(fc.all) // Creates = current - previous
81+
updates := mods.Difference(creates) // Updates = modified - creates
8282

8383
fc.all, fc.lastMod = all, newLastMod
8484
t3 := time.Now()

‎consensus/ethash/consensus.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import (
2424
"runtime"
2525
"time"
2626

27+
mapset "github.com/deckarep/golang-set"
2728
"github.com/ethereum/go-ethereum/common"
2829
"github.com/ethereum/go-ethereum/common/math"
2930
"github.com/ethereum/go-ethereum/consensus"
3031
"github.com/ethereum/go-ethereum/consensus/misc"
3132
"github.com/ethereum/go-ethereum/core/state"
3233
"github.com/ethereum/go-ethereum/core/types"
3334
"github.com/ethereum/go-ethereum/params"
34-
set "gopkg.in/fatih/set.v0"
3535
)
3636

3737
// Ethash proof-of-work protocol constants.
@@ -177,7 +177,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
177177
return errTooManyUncles
178178
}
179179
// Gather the set of past uncles and ancestors
180-
uncles, ancestors := set.New(), make(map[common.Hash]*types.Header)
180+
uncles, ancestors := mapset.NewSet(), make(map[common.Hash]*types.Header)
181181

182182
number, parent := block.NumberU64()-1, block.ParentHash()
183183
for i := 0; i < 7; i++ {
@@ -198,7 +198,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
198198
for _, uncle := range block.Uncles() {
199199
// Make sure every uncle is rewarded only once
200200
hash := uncle.Hash()
201-
if uncles.Has(hash) {
201+
if uncles.Contains(hash) {
202202
return errDuplicateUncle
203203
}
204204
uncles.Add(hash)

‎eth/peer.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
"sync"
2424
"time"
2525

26+
mapset "github.com/deckarep/golang-set"
2627
"github.com/ethereum/go-ethereum/common"
2728
"github.com/ethereum/go-ethereum/core/types"
2829
"github.com/ethereum/go-ethereum/p2p"
2930
"github.com/ethereum/go-ethereum/rlp"
30-
"gopkg.in/fatih/set.v0"
3131
)
3232

3333
var (
@@ -85,8 +85,8 @@ type peer struct {
8585
td *big.Int
8686
lock sync.RWMutex
8787

88-
knownTxs *set.Set // Set of transaction hashes known to be known by this peer
89-
knownBlocks *set.Set // Set of block hashes known to be known by this peer
88+
knownTxs mapset.Set // Set of transaction hashes known to be known by this peer
89+
knownBlocks mapset.Set // Set of block hashes known to be known by this peer
9090
queuedTxs chan []*types.Transaction // Queue of transactions to broadcast to the peer
9191
queuedProps chan *propEvent // Queue of blocks to broadcast to the peer
9292
queuedAnns chan *types.Block // Queue of blocks to announce to the peer
@@ -99,8 +99,8 @@ func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
9999
rw: rw,
100100
version: version,
101101
id: fmt.Sprintf("%x", p.ID().Bytes()[:8]),
102-
knownTxs: set.New(),
103-
knownBlocks: set.New(),
102+
knownTxs: mapset.NewSet(),
103+
knownBlocks: mapset.NewSet(),
104104
queuedTxs: make(chan []*types.Transaction, maxQueuedTxs),
105105
queuedProps: make(chan *propEvent, maxQueuedProps),
106106
queuedAnns: make(chan *types.Block, maxQueuedAnns),
@@ -177,7 +177,7 @@ func (p *peer) SetHead(hash common.Hash, td *big.Int) {
177177
// never be propagated to this particular peer.
178178
func (p *peer) MarkBlock(hash common.Hash) {
179179
// If we reached the memory allowance, drop a previously known block hash
180-
for p.knownBlocks.Size() >= maxKnownBlocks {
180+
for p.knownBlocks.Cardinality() >= maxKnownBlocks {
181181
p.knownBlocks.Pop()
182182
}
183183
p.knownBlocks.Add(hash)
@@ -187,7 +187,7 @@ func (p *peer) MarkBlock(hash common.Hash) {
187187
// will never be propagated to this particular peer.
188188
func (p *peer) MarkTransaction(hash common.Hash) {
189189
// If we reached the memory allowance, drop a previously known transaction hash
190-
for p.knownTxs.Size() >= maxKnownTxs {
190+
for p.knownTxs.Cardinality() >= maxKnownTxs {
191191
p.knownTxs.Pop()
192192
}
193193
p.knownTxs.Add(hash)
@@ -470,7 +470,7 @@ func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer {
470470

471471
list := make([]*peer, 0, len(ps.peers))
472472
for _, p := range ps.peers {
473-
if !p.knownBlocks.Has(hash) {
473+
if !p.knownBlocks.Contains(hash) {
474474
list = append(list, p)
475475
}
476476
}
@@ -485,7 +485,7 @@ func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer {
485485

486486
list := make([]*peer, 0, len(ps.peers))
487487
for _, p := range ps.peers {
488-
if !p.knownTxs.Has(hash) {
488+
if !p.knownTxs.Contains(hash) {
489489
list = append(list, p)
490490
}
491491
}

‎miner/worker.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sync/atomic"
2525
"time"
2626

27+
mapset "github.com/deckarep/golang-set"
2728
"github.com/ethereum/go-ethereum/common"
2829
"github.com/ethereum/go-ethereum/consensus"
2930
"github.com/ethereum/go-ethereum/consensus/misc"
@@ -35,7 +36,6 @@ import (
3536
"github.com/ethereum/go-ethereum/event"
3637
"github.com/ethereum/go-ethereum/log"
3738
"github.com/ethereum/go-ethereum/params"
38-
"gopkg.in/fatih/set.v0"
3939
)
4040

4141
const (
@@ -67,9 +67,9 @@ type Work struct {
6767
signer types.Signer
6868

6969
state *state.StateDB // apply state changes here
70-
ancestors *set.Set // ancestor set (used for checking uncle parent validity)
71-
family *set.Set // family set (used for checking uncle invalidity)
72-
uncles *set.Set // uncle set
70+
ancestors mapset.Set // ancestor set (used for checking uncle parent validity)
71+
family mapset.Set // family set (used for checking uncle invalidity)
72+
uncles mapset.Set // uncle set
7373
tcount int // tx count in cycle
7474
gasPool *core.GasPool // available gas used to pack transactions
7575

@@ -362,9 +362,9 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error
362362
config: self.config,
363363
signer: types.NewEIP155Signer(self.config.ChainID),
364364
state: state,
365-
ancestors: set.New(),
366-
family: set.New(),
367-
uncles: set.New(),
365+
ancestors: mapset.NewSet(),
366+
family: mapset.NewSet(),
367+
uncles: mapset.NewSet(),
368368
header: header,
369369
createdAt: time.Now(),
370370
}
@@ -492,13 +492,13 @@ func (self *worker) commitNewWork() {
492492

493493
func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
494494
hash := uncle.Hash()
495-
if work.uncles.Has(hash) {
495+
if work.uncles.Contains(hash) {
496496
return fmt.Errorf("uncle not unique")
497497
}
498-
if !work.ancestors.Has(uncle.ParentHash) {
498+
if !work.ancestors.Contains(uncle.ParentHash) {
499499
return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4])
500500
}
501-
if work.family.Has(hash) {
501+
if work.family.Contains(hash) {
502502
return fmt.Errorf("uncle already in family (%x)", hash)
503503
}
504504
work.uncles.Add(uncle.Hash())

‎rpc/server.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"sync"
2626
"sync/atomic"
2727

28+
mapset "github.com/deckarep/golang-set"
2829
"github.com/ethereum/go-ethereum/log"
29-
"gopkg.in/fatih/set.v0"
3030
)
3131

3232
const MetadataApi = "rpc"
@@ -46,7 +46,7 @@ const (
4646
func NewServer() *Server {
4747
server := &Server{
4848
services: make(serviceRegistry),
49-
codecs: set.New(),
49+
codecs: mapset.NewSet(),
5050
run: 1,
5151
}
5252

‎rpc/types.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"strings"
2424
"sync"
2525

26+
mapset "github.com/deckarep/golang-set"
2627
"github.com/ethereum/go-ethereum/common/hexutil"
27-
"gopkg.in/fatih/set.v0"
2828
)
2929

3030
// API describes the set of methods offered over the RPC interface
@@ -73,7 +73,7 @@ type Server struct {
7373

7474
run int32
7575
codecsMu sync.Mutex
76-
codecs *set.Set
76+
codecs mapset.Set
7777
}
7878

7979
// rpcRequest represents a raw incoming RPC request

‎rpc/websocket.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import (
2929
"strings"
3030
"time"
3131

32+
mapset "github.com/deckarep/golang-set"
3233
"github.com/ethereum/go-ethereum/log"
3334
"golang.org/x/net/websocket"
34-
"gopkg.in/fatih/set.v0"
3535
)
3636

3737
// websocketJSONCodec is a custom JSON codec with payload size enforcement and
@@ -84,7 +84,7 @@ func NewWSServer(allowedOrigins []string, srv *Server) *http.Server {
8484
// websocket upgrade process. When a '*' is specified as an allowed origins all
8585
// connections are accepted.
8686
func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http.Request) error {
87-
origins := set.New()
87+
origins := mapset.NewSet()
8888
allowAllOrigins := false
8989

9090
for _, origin := range allowedOrigins {
@@ -97,18 +97,18 @@ func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http
9797
}
9898

9999
// allow localhost if no allowedOrigins are specified.
100-
if len(origins.List()) == 0 {
100+
if len(origins.ToSlice()) == 0 {
101101
origins.Add("http://localhost")
102102
if hostname, err := os.Hostname(); err == nil {
103103
origins.Add("http://" + strings.ToLower(hostname))
104104
}
105105
}
106106

107-
log.Debug(fmt.Sprintf("Allowed origin(s) for WS RPC interface %v\n", origins.List()))
107+
log.Debug(fmt.Sprintf("Allowed origin(s) for WS RPC interface %v\n", origins.ToSlice()))
108108

109109
f := func(cfg *websocket.Config, req *http.Request) error {
110110
origin := strings.ToLower(req.Header.Get("Origin"))
111-
if allowAllOrigins || origins.Has(origin) {
111+
if allowAllOrigins || origins.Contains(origin) {
112112
return nil
113113
}
114114
log.Warn(fmt.Sprintf("origin '%s' not allowed on WS-RPC interface\n", origin))

vendor/gopkg.in/fatih/set.v0/LICENSE.md renamed to vendor/github.com/deckarep/golang-set/LICENSE

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)