forked from nebulasio/go-nebulas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_table.go
377 lines (320 loc) · 9.41 KB
/
route_table.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (C) 2018 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package net
import (
"bufio"
"errors"
"fmt"
"math/rand"
"os"
"path"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/multiformats/go-multiaddr"
netpb "github.com/nebulasio/go-nebulas/net/pb"
"github.com/nebulasio/go-nebulas/util/logging"
kbucket "github.com/libp2p/go-libp2p-kbucket"
peer "github.com/libp2p/go-libp2p-peer"
peerstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
)
// Route Table Errors
var (
ErrExceedMaxSyncRouteResponse = errors.New("too many sync route table response")
)
// RouteTable route table struct.
type RouteTable struct {
quitCh chan bool
peerStore peerstore.Peerstore
routeTable *kbucket.RoutingTable
maxPeersCountForSyncResp int
maxPeersCountToSync int
cacheFilePath string
seedNodes []ma.Multiaddr
node *Node
streamManager *StreamManager
latestUpdatedAt int64
}
// NewRouteTable new route table.
func NewRouteTable(config *Config, node *Node) *RouteTable {
table := &RouteTable{
quitCh: make(chan bool, 1),
peerStore: peerstore.NewPeerstore(),
maxPeersCountForSyncResp: MaxPeersCountForSyncResp,
maxPeersCountToSync: config.MaxSyncNodes,
cacheFilePath: path.Join(config.RoutingTableDir, RouteTableCacheFileName),
seedNodes: config.BootNodes,
node: node,
streamManager: node.streamManager,
latestUpdatedAt: 0,
}
table.routeTable = kbucket.NewRoutingTable(
config.Bucketsize,
kbucket.ConvertPeerID(node.id),
config.Latency,
table.peerStore,
)
table.routeTable.Update(node.id)
table.peerStore.AddPubKey(node.id, node.networkKey.GetPublic())
table.peerStore.AddPrivKey(node.id, node.networkKey)
return table
}
// Start start route table syncLoop.
func (table *RouteTable) Start() {
logging.CLog().Info("Starting NetService RouteTable Sync...")
go table.syncLoop()
}
// Stop quit route table syncLoop.
func (table *RouteTable) Stop() {
logging.CLog().Info("Stopping NetService RouteTable Sync...")
table.quitCh <- true
}
// Peers return peers in route table.
func (table *RouteTable) Peers() map[peer.ID][]ma.Multiaddr {
peers := make(map[peer.ID][]ma.Multiaddr)
for _, pid := range table.peerStore.Peers() {
peers[pid] = table.peerStore.Addrs(pid)
}
return peers
}
func (table *RouteTable) syncLoop() {
// Load Route Table.
table.LoadSeedNodes()
table.LoadRouteTableFromFile()
// trigger first sync.
table.SyncRouteTable()
logging.CLog().Info("Started NetService RouteTable Sync.")
syncLoopTicker := time.NewTicker(RouteTableSyncLoopInterval)
saveRouteTableToDiskTicker := time.NewTicker(RouteTableSaveToDiskInterval)
latestUpdatedAt := table.latestUpdatedAt
for {
select {
case <-table.quitCh:
logging.CLog().Info("Stopped NetService RouteTable Sync.")
return
case <-syncLoopTicker.C:
table.SyncRouteTable()
case <-saveRouteTableToDiskTicker.C:
if latestUpdatedAt < table.latestUpdatedAt {
table.SaveRouteTableToFile()
latestUpdatedAt = table.latestUpdatedAt
}
}
}
}
// AddPeerInfo add peer to route table.
func (table *RouteTable) AddPeerInfo(prettyID string, addrStr []string) error {
pid, err := peer.IDB58Decode(prettyID)
if err != nil {
return nil
}
addrs := make([]ma.Multiaddr, len(addrStr))
for i, v := range addrStr {
addrs[i], err = multiaddr.NewMultiaddr(v)
if err != nil {
return err
}
}
if table.routeTable.Find(pid) != "" {
table.peerStore.SetAddrs(pid, addrs, peerstore.PermanentAddrTTL)
} else {
table.peerStore.AddAddrs(pid, addrs, peerstore.PermanentAddrTTL)
}
table.routeTable.Update(pid)
table.onRouteTableChange()
return nil
}
// AddPeer add peer to route table.
func (table *RouteTable) AddPeer(pid peer.ID, addr ma.Multiaddr) {
logging.VLog().Debugf("Adding Peer: %s,%s", pid.Pretty(), addr.String())
table.peerStore.AddAddr(pid, addr, peerstore.PermanentAddrTTL)
table.routeTable.Update(pid)
table.onRouteTableChange()
}
// AddPeers add peers to route table
func (table *RouteTable) AddPeers(pid string, peers *netpb.Peers) {
// recv too many peers info. say Bye.
if len(peers.Peers) > table.maxPeersCountForSyncResp {
table.streamManager.CloseStream(pid, ErrExceedMaxSyncRouteResponse)
}
for _, v := range peers.Peers {
table.AddPeerInfo(v.Id, v.Addrs)
}
}
// AddIPFSPeerAddr add a peer to route table with ipfs address.
func (table *RouteTable) AddIPFSPeerAddr(addr ma.Multiaddr) {
id, addr, err := ParseFromIPFSAddr(addr)
if err != nil {
return
}
table.AddPeer(id, addr)
}
// AddPeerStream add peer stream to peerStore.
func (table *RouteTable) AddPeerStream(s *Stream) {
table.peerStore.AddAddr(
s.pid,
s.addr,
peerstore.PermanentAddrTTL,
)
table.routeTable.Update(s.pid)
table.onRouteTableChange()
}
// RemovePeerStream remove peerStream from peerStore.
func (table *RouteTable) RemovePeerStream(s *Stream) {
table.peerStore.AddAddr(s.pid, s.addr, 0)
table.routeTable.Remove(s.pid)
table.onRouteTableChange()
}
func (table *RouteTable) onRouteTableChange() {
table.latestUpdatedAt = time.Now().Unix()
}
// GetRandomPeers get random peers
func (table *RouteTable) GetRandomPeers(pid peer.ID) []peerstore.PeerInfo {
// change sync route algorithm from `NearestPeers` to `randomPeers`
var peers []peer.ID
allPeers := shufflePeerID(table.routeTable.ListPeers())
if len(allPeers) <= table.maxPeersCountForSyncResp {
peers = allPeers
} else {
peers = allPeers[:table.maxPeersCountForSyncResp]
}
ret := make([]peerstore.PeerInfo, len(peers))
for i, v := range peers {
ret[i] = table.peerStore.PeerInfo(v)
}
return ret
}
func shufflePeerID(pids []peer.ID) []peer.ID {
r := rand.New(rand.NewSource(time.Now().Unix()))
ret := make([]peer.ID, len(pids))
perm := r.Perm(len(pids))
for i, randIndex := range perm {
ret[i] = pids[randIndex]
}
return ret
}
// LoadSeedNodes load seed nodes.
func (table *RouteTable) LoadSeedNodes() {
for _, ipfsAddr := range table.seedNodes {
table.AddIPFSPeerAddr(ipfsAddr)
}
}
// LoadRouteTableFromFile load route table from file.
func (table *RouteTable) LoadRouteTableFromFile() {
file, err := os.Open(table.cacheFilePath)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"cacheFilePath": table.cacheFilePath,
"err": err,
}).Warn("Failed to open Route Table Cache file.")
return
}
defer file.Close()
// read line by line.
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
continue
}
addr, err := ma.NewMultiaddr(line)
if err != nil {
// ignore.
logging.VLog().WithFields(logrus.Fields{
"err": err,
"text": line,
}).Warn("Invalid address in Route Table Cache file.")
continue
}
table.AddIPFSPeerAddr(addr)
}
}
// SaveRouteTableToFile save route table to file.
func (table *RouteTable) SaveRouteTableToFile() {
file, err := os.Create(table.cacheFilePath)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"cacheFilePath": table.cacheFilePath,
"err": err,
}).Warn("Failed to open Route Table Cache file.")
return
}
defer file.Close()
// write header.
file.WriteString(fmt.Sprintf("# %s\n", time.Now().String()))
peers := table.routeTable.ListPeers()
for _, v := range peers {
for _, addr := range table.peerStore.Addrs(v) {
line := fmt.Sprintf("%s/ipfs/%s\n", addr, v.Pretty())
file.WriteString(line)
}
}
}
// SyncRouteTable sync route table.
func (table *RouteTable) SyncRouteTable() {
syncedPeers := make(map[peer.ID]bool)
// sync with seed nodes.
for _, ipfsAddr := range table.seedNodes {
pid, _, err := ParseFromIPFSAddr(ipfsAddr)
if err != nil {
continue
}
table.SyncWithPeer(pid)
syncedPeers[pid] = true
}
// random peer selection.
peers := table.routeTable.ListPeers()
peersCount := len(peers)
if peersCount <= 1 {
return
}
peersCountToSync := table.maxPeersCountToSync
if peersCount < peersCountToSync {
peersCountToSync = peersCount
}
selectedPeersIdx := make(map[int]bool)
for i := 0; i < peersCountToSync/2; i++ {
ri := 0
for {
ri = rand.Intn(peersCountToSync)
if selectedPeersIdx[ri] == false {
break
}
}
selectedPeersIdx[ri] = true
pid := peers[ri]
if syncedPeers[pid] == false {
table.SyncWithPeer(pid)
syncedPeers[pid] = true
}
}
}
// SyncWithPeer sync route table with a peer.
func (table *RouteTable) SyncWithPeer(pid peer.ID) {
if pid == table.node.id {
return
}
stream := table.streamManager.Find(pid)
if stream == nil {
stream = NewStreamFromPID(pid, table.node)
table.streamManager.AddStream(stream)
}
stream.SyncRoute()
}