-
Notifications
You must be signed in to change notification settings - Fork 4
/
peers.go
310 lines (248 loc) · 6.14 KB
/
peers.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package bitcoin_reader
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"math/rand"
"net"
"sync"
"time"
"github.com/tokenized/logger"
"github.com/tokenized/pkg/bitcoin"
"github.com/tokenized/pkg/storage"
"github.com/pkg/errors"
)
const (
peersDefaultPath = "peers"
peersVersion = uint8(0)
)
type Peer struct {
Address string
Score int32
LastTime uint32
}
type StoragePeerRepository struct {
store storage.Storage
path string
lookup map[string]*Peer
list PeerList
lastSaved time.Time
lock sync.Mutex
}
type PeerList []*Peer
func (l PeerList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func NewPeerRepository(store storage.Storage, path string) *StoragePeerRepository {
if len(path) == 0 {
path = peersDefaultPath
}
return &StoragePeerRepository{
store: store,
path: path,
lookup: make(map[string]*Peer),
}
}
func (repo *StoragePeerRepository) Count() int {
repo.lock.Lock()
defer repo.lock.Unlock()
return len(repo.list)
}
func (repo *StoragePeerRepository) Get(ctx context.Context,
minScore, maxScore int32) (PeerList, error) {
repo.lock.Lock()
defer repo.lock.Unlock()
result := make(PeerList, 0, 1000)
for _, peer := range repo.list {
if peer.Score >= minScore && (maxScore == -1 || peer.Score <= maxScore) {
result = append(result, peer)
}
}
// Shuffle the list
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(result), result.Swap)
return result, nil
}
func (repo *StoragePeerRepository) Add(ctx context.Context, address string) (bool, error) {
repo.lock.Lock()
defer repo.lock.Unlock()
_, exists := repo.lookup[address]
if exists {
return false, nil
}
// Add peer
peer := Peer{Address: address, Score: 0}
repo.list = append(repo.list, &peer)
repo.lookup[peer.Address] = &peer
return true, nil
}
func (repo *StoragePeerRepository) UpdateScore(ctx context.Context, address string,
delta int32) bool {
repo.lock.Lock()
defer repo.lock.Unlock()
peer, exists := repo.lookup[address]
if exists {
now := time.Now()
peer.LastTime = uint32(now.Unix())
peer.Score += delta
return true
}
return false
}
func (repo *StoragePeerRepository) UpdateTime(ctx context.Context, address string) bool {
repo.lock.Lock()
defer repo.lock.Unlock()
peer, exists := repo.lookup[address]
if exists {
now := time.Now()
peer.LastTime = uint32(now.Unix())
return true
}
return false
}
func (repo *StoragePeerRepository) LoadSeeds(ctx context.Context, network bitcoin.Network) {
logger.Info(ctx, "Loading %s peer seeds", network)
var seeds []Seed
if network == bitcoin.MainNet {
seeds = MainNetSeeds
} else {
seeds = TestNetSeeds
}
for _, seed := range seeds {
ip := net.IP(seed.Bytes)
peer := &Peer{
Address: fmt.Sprintf("[%s]:%d", ip.To16().String(), seed.Port),
}
repo.list = append(repo.list, peer)
repo.lookup[peer.Address] = peer
}
}
// Loads peers from storage
func (repo *StoragePeerRepository) Load(ctx context.Context) error {
repo.lock.Lock()
defer repo.lock.Unlock()
// Clear
repo.list = make(PeerList, 0)
repo.lookup = make(map[string]*Peer)
// Get current data
data, err := repo.store.Read(ctx, repo.path)
if err == storage.ErrNotFound {
return nil // Leave empty
}
if err != nil {
return err
}
// Parse peers
buffer := bytes.NewBuffer(data)
var version uint8
if err := binary.Read(buffer, binary.LittleEndian, &version); err != nil {
return errors.Wrap(err, "Failed to read peers version")
}
if version != 0 {
return errors.New("Unknown Version")
}
var count int32
if err := binary.Read(buffer, binary.LittleEndian, &count); err != nil {
return errors.Wrap(err, "Failed to read peers count")
}
// Reset
repo.list = make(PeerList, 0, count)
// Parse peers
for {
peer, err := readPeer(buffer, version)
if err != nil {
break
}
// Add peer
repo.list = append(repo.list, &peer)
repo.lookup[peer.Address] = &peer
}
repo.lastSaved = time.Now()
logger.Info(ctx, "Loaded %d peers", len(repo.list))
return nil
}
// Saves the peers to storage
func (repo *StoragePeerRepository) Save(ctx context.Context) error {
repo.lock.Lock()
defer repo.lock.Unlock()
start := time.Now()
var buffer bytes.Buffer
// Write version
if err := binary.Write(&buffer, binary.LittleEndian, peersVersion); err != nil {
return err
}
// Write count
if err := binary.Write(&buffer, binary.LittleEndian, int32(len(repo.list))); err != nil {
return err
}
// Write peers
for _, peer := range repo.list {
if err := peer.write(&buffer); err != nil {
return err
}
}
if err := repo.store.Write(ctx, repo.path, buffer.Bytes(), nil); err != nil {
return err
}
repo.lastSaved = time.Now()
logger.ElapsedWithFields(ctx, start, []logger.Field{
logger.Int("peer_count", len(repo.list)),
}, "Saved peers")
return nil
}
// Clears all peers from the database
func (repo *StoragePeerRepository) Clear(ctx context.Context) error {
repo.lock.Lock()
defer repo.lock.Unlock()
repo.list = make(PeerList, 0)
repo.lookup = make(map[string]*Peer)
repo.lastSaved = time.Now()
return repo.store.Remove(ctx, repo.path)
}
func readPeer(r io.Reader, version uint8) (Peer, error) {
result := Peer{}
// Read address
var addressSize int32
if err := binary.Read(r, binary.LittleEndian, &addressSize); err != nil {
return result, err
}
addressData := make([]byte, addressSize)
_, err := io.ReadFull(r, addressData) // Read until string terminator
if err != nil {
return result, err
}
result.Address = string(addressData)
// Read score
if err := binary.Read(r, binary.LittleEndian, &result.Score); err != nil {
return result, err
}
// Read score
if err := binary.Read(r, binary.LittleEndian, &result.LastTime); err != nil {
return result, err
}
return result, nil
}
func (peer *Peer) write(w io.Writer) error {
// Write address
err := binary.Write(w, binary.LittleEndian, int32(len(peer.Address)))
if err != nil {
return err
}
_, err = w.Write([]byte(peer.Address))
if err != nil {
return err
}
// Write score
err = binary.Write(w, binary.LittleEndian, peer.Score)
if err != nil {
return err
}
// Write time
err = binary.Write(w, binary.LittleEndian, peer.LastTime)
if err != nil {
return err
}
return nil
}