forked from prettymuchbryce/kademlia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
141 lines (121 loc) · 2.75 KB
/
node.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
package kademlia
import (
"bytes"
"math/big"
"net"
"strconv"
)
// NetworkNode is the over-the-wire representation of a node
type NetworkNode struct {
// ID is a 20 byte unique identifier
ID []byte
// IP is the IPv4 address of the node
IP net.IP
// Port is the port of the node
Port int
}
// node represents a node in the network locally
// a separate struct due to the fact that we may want to add some metadata
// here later such as RTT, or LastSeen time
type node struct {
*NetworkNode
}
// NewNetworkNode creates a new NetworkNode for bootstrapping
func NewNetworkNode(ip string, port string) *NetworkNode {
p, _ := strconv.Atoi(port)
ips, err := net.LookupIP(ip)
if len(ips) <= 0 || err != nil {
return nil
}
return &NetworkNode{
IP: ips[0],
Port: p,
}
}
func newNode(networkNode *NetworkNode) *node {
n := &node{}
n.NetworkNode = networkNode
return n
}
// nodeList is used in order to sort a list of arbitrary nodes against a
// comparator. These nodes are sorted by xor distance
type shortList struct {
// Nodes are a list of nodes to be compared
Nodes []*NetworkNode
// Comparator is the ID to compare to
Comparator []byte
}
func areNodesEqual(n1 *NetworkNode, n2 *NetworkNode, allowNilID bool) bool {
if n1 == nil || n2 == nil {
return false
}
if !allowNilID {
if n1.ID == nil || n2.ID == nil {
return false
}
if bytes.Compare(n1.ID, n2.ID) != 0 {
return false
}
}
if !n1.IP.Equal(n2.IP) {
return false
}
if n1.Port != n2.Port {
return false
}
return true
}
func (n *shortList) RemoveNode(node *NetworkNode) {
for i := 0; i < n.Len(); i++ {
if bytes.Compare(n.Nodes[i].ID, node.ID) == 0 {
n.Nodes = append(n.Nodes[:i], n.Nodes[i+1:]...)
return
}
}
}
func (n *shortList) AppendUniqueNetworkNodes(nodes []*NetworkNode) {
for _, vv := range nodes {
exists := false
for _, v := range n.Nodes {
if bytes.Compare(v.ID, vv.ID) == 0 {
exists = true
}
}
if !exists {
n.Nodes = append(n.Nodes, vv)
}
}
}
func (n *shortList) AppendUnique(nodes []*node) {
for _, vv := range nodes {
exists := false
for _, v := range n.Nodes {
if bytes.Compare(v.ID, vv.ID) == 0 {
exists = true
}
}
if !exists {
n.Nodes = append(n.Nodes, vv.NetworkNode)
}
}
}
func (n *shortList) Len() int {
return len(n.Nodes)
}
func (n *shortList) Swap(i, j int) {
n.Nodes[i], n.Nodes[j] = n.Nodes[j], n.Nodes[i]
}
func (n *shortList) Less(i, j int) bool {
iDist := getDistance(n.Nodes[i].ID, n.Comparator)
jDist := getDistance(n.Nodes[j].ID, n.Comparator)
if iDist.Cmp(jDist) == -1 {
return true
}
return false
}
func getDistance(id1 []byte, id2 []byte) *big.Int {
buf1 := new(big.Int).SetBytes(id1)
buf2 := new(big.Int).SetBytes(id2)
result := new(big.Int).Xor(buf1, buf2)
return result
}