This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
distinct.go
187 lines (150 loc) · 3.85 KB
/
distinct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
qubic "github.com/qubic/go-node-connector"
"log"
"math/rand"
"net"
"sync"
"time"
)
type distinctPeers struct {
bp *blacklistedPeers
whitelistedPeers map[string]struct{}
peers map[string]struct{}
startingPeers []string
maxPeers int
db *pebble.DB
mux sync.RWMutex
exchangeConnectionTimeout time.Duration
}
func newDistinctPeers(startingPeers []string, whitelistedPeers []string, maxPeers int, exchangeConnectionTimeout time.Duration, bp *blacklistedPeers, db *pebble.DB) *distinctPeers {
dp := distinctPeers{
bp: bp,
peers: make(map[string]struct{}, maxPeers),
whitelistedPeers: createWhitelistedPeersMap(whitelistedPeers),
startingPeers: startingPeers,
maxPeers: maxPeers,
db: db,
exchangeConnectionTimeout: exchangeConnectionTimeout,
}
dp.setPeers(startingPeers)
return &dp
}
func createWhitelistedPeersMap(peers []string) map[string]struct{} {
peersMap := make(map[string]struct{})
if peers == nil || len(peers) == 0 {
return peersMap
}
for _, peer := range peers {
ip := net.ParseIP(peer)
if ip == nil {
continue
}
peersMap[peer] = struct{}{}
}
return peersMap
}
// isWhitelisted checks if a peer is whitelisted, if the whitelistedPeers list is empty, it returns true
func (p *distinctPeers) isWhitelisted(peer string) bool {
if len(p.whitelistedPeers) == 0 {
return true
}
_, ok := p.whitelistedPeers[peer]
return ok
}
func (p *distinctPeers) build(fixedPeerList bool) ([]string, error) {
if fixedPeerList {
return p.startingPeers, nil
}
peer, err := p.getRandomPeer()
if err != nil {
return nil, errors.Wrap(err, "getting random peer")
}
if len(peer) == 0 {
peer = p.get()[0]
}
err = p.exchangePeerList(peer)
if err != nil {
return nil, errors.Wrap(err, "exchanging peer list")
}
if p.isEmpty() {
if p.bp.isEmpty() {
log.Println("No distinct peers found, no blacklisted peers found")
}
return p.bp.get(), nil
}
err = storePeers(p.db, p.get())
if err != nil {
return nil, errors.Wrap(err, "storing peers")
}
return p.get(), nil
}
func (p *distinctPeers) exchangePeerList(peer string) error {
if p.isFull() {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), p.exchangeConnectionTimeout)
defer cancel()
qc, err := qubic.NewClient(ctx, peer, qubicPort)
if err != nil {
return errors.Wrap(err, "creating new connection")
}
qc.Close()
unmetPeers := p.getUnmetPeers(qc.Peers)
p.setPeers(unmetPeers)
for _, peer := range unmetPeers {
p.exchangePeerList(peer)
}
return nil
}
func (p *distinctPeers) getUnmetPeers(peers []string) []string {
var unmetPeers []string
for _, peer := range peers {
if _, ok := p.peers[peer]; !ok {
unmetPeers = append(unmetPeers, peer)
}
}
return unmetPeers
}
func (p *distinctPeers) get() []string {
p.mux.RLock()
defer p.mux.RUnlock()
var peers []string
for peer := range p.peers {
peers = append(peers, peer)
}
return peers
}
func (p *distinctPeers) setPeers(peers []string) {
p.mux.Lock()
defer p.mux.Unlock()
for _, peer := range peers {
if !p.isWhitelisted(peer) {
continue
}
p.peers[peer] = struct{}{}
}
}
func (p *distinctPeers) isFull() bool {
p.mux.RLock()
defer p.mux.RUnlock()
return len(p.peers) >= p.maxPeers
}
func (p *distinctPeers) isEmpty() bool {
p.mux.RLock()
defer p.mux.RUnlock()
return len(p.peers) == 0
}
func (p *distinctPeers) getRandomPeer() (string, error) {
peers, err := retrievePeers(p.db)
if err != nil {
return "", errors.Wrap(err, "retrieving peers from store")
}
if len(peers) == 0 {
return "", nil
}
return peers[rand.Intn(len(p.peers))], nil
}