-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouting_table.go
More file actions
198 lines (156 loc) · 3.75 KB
/
routing_table.go
File metadata and controls
198 lines (156 loc) · 3.75 KB
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
package dht
import (
"math/bits"
"net"
"sort"
)
const (
// K number of nodes in a bucket
K = 20
// KEY_BITS number of bits in a key
KEY_BITS = 160
// KEY_BYTES number of bytes in a key
KEY_BYTES = KEY_BITS / 8
)
// routing table stores buckets of every known node on the network
type routingTable struct {
localNode *node
// buckets of nodes active in the routing table
buckets []bucket
}
// newRoutingTable creates a new routing table
func newRoutingTable(localNode *node) *routingTable {
buckets := make([]bucket, KEY_BITS)
for i := range buckets {
buckets[i].nodes = make([]*node, K)
}
return &routingTable{
localNode: localNode,
buckets: buckets,
}
}
// insert a node to its corresponding bucket
func (t *routingTable) insert(id []byte, address *net.UDPAddr) {
t.buckets[bucketID(t.localNode.id, id)].insert(id, address)
}
// updates the timestamp of a node to seen
// returns true if the node exists and false
// if the node needs to be inserted into the
// routing table
func (t *routingTable) seen(id []byte) bool {
return t.buckets[bucketID(t.localNode.id, id)].seen(id)
}
// remove the node from the routing table
func (t *routingTable) remove(id []byte) {
t.buckets[bucketID(t.localNode.id, id)].remove(id, true)
}
// finds the closest known node for a given key
func (t *routingTable) closest(id []byte) *node {
offset := bucketID(t.localNode.id, id)
// scan outwardly from our selected bucket until we find a
// node that is close to the target key
var i int
var scanned int
for {
var cd int
var cn *node
if offset > -1 && offset < KEY_BITS {
t.buckets[offset].iterate(func(n *node) {
// find a node which has the most matching bits
nd := distance(n.id, id)
if cd == 0 || nd > cd {
cd = nd
cn = n
}
})
if cn != nil {
return cn
}
scanned++
}
if scanned >= KEY_BITS {
break
}
if i%2 == 0 {
offset = offset + i + 1
} else {
offset = offset - i - 1
}
i++
}
return nil
}
// finds the closest known nodes for a given key
func (t *routingTable) closestN(id []byte, count int) []*node {
offset := bucketID(t.localNode.id, id)
var nodes []*node
// scan outwardly from our selected bucket until we find a
// node that is close to the target key
var i int
var scanned int
for {
if offset > -1 && offset < KEY_BITS {
t.buckets[offset].iterate(func(n *node) {
nodes = append(nodes, n)
})
if len(nodes) >= count {
break
}
scanned++
}
if scanned >= KEY_BITS {
break
}
if i%2 == 0 {
offset = offset + i + 1
} else {
offset = offset - i - 1
}
i++
}
sort.Slice(nodes, func(i, j int) bool {
idst := distance(nodes[i].id, id)
jdst := distance(nodes[j].id, id)
return idst > jdst
})
if len(nodes) < count {
return nodes
}
return nodes[:count]
}
// neighbours the total number of nodes known to us
func (r *routingTable) neighbours() int {
var neighbours int
for i := range r.buckets {
r.buckets[i].mu.Lock()
neighbours = neighbours + r.buckets[i].size
r.buckets[i].mu.Unlock()
}
return neighbours
}
// bucketID gets the correct bucket id for a given node, based on it's xor distance from our node
func bucketID(localID, targetID []byte) int {
pfx := distance(localID, targetID)
d := (KEY_BITS - pfx)
if d == 0 {
return d
}
return d - 1
}
func distance(localID, targetID []byte) int {
var pfx int
// xor each byte and check for the number of 0 least significant bits
for i := 0; i < KEY_BYTES; i++ {
d := localID[i] ^ targetID[i]
if d == 0 {
// byte is all 0's, so we add all bits
pfx = pfx + 8
} else {
// there are some differences with this byte, so get the number
// of leading zero bits and add them to the prefix
pfx = pfx + bits.LeadingZeros8(d)
break
}
}
return pfx
}