@ -0,0 +1,5 @@ |
|||||
|
# go-dht |
||||
|
|
||||
|
Kademlia DHT Go implementation. |
||||
|
|
||||
|
Following the specification from https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf and http://xlattice.sourceforge.net/components/protocol/kademlia/specs.html |
@ -0,0 +1,5 @@ |
|||||
|
module go-dht |
||||
|
|
||||
|
go 1.12 |
||||
|
|
||||
|
require github.com/stretchr/testify v1.4.0 |
@ -0,0 +1,10 @@ |
|||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= |
||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= |
||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= |
||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
@ -0,0 +1,51 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"crypto/rand" |
||||
|
"encoding/hex" |
||||
|
) |
||||
|
|
||||
|
type ID [B]byte |
||||
|
|
||||
|
func NewID() (ID, error) { |
||||
|
b := make([]byte, 32) |
||||
|
_, err := rand.Read(b) |
||||
|
if err != nil { |
||||
|
return ID{}, err |
||||
|
} |
||||
|
var id ID |
||||
|
copy(id[:], b[:B]) |
||||
|
return id, nil |
||||
|
} |
||||
|
|
||||
|
func (id ID) String() string { |
||||
|
return hex.EncodeToString(id[:]) |
||||
|
} |
||||
|
|
||||
|
func IDFromString(s string) (ID, error) { |
||||
|
b, err := hex.DecodeString(s) |
||||
|
if err != nil { |
||||
|
return ID{}, err |
||||
|
} |
||||
|
var id ID |
||||
|
copy(id[:], b[:B]) |
||||
|
return id, nil |
||||
|
} |
||||
|
|
||||
|
// Cmp returns true if idA > idB
|
||||
|
func (idA ID) Cmp(idB ID) bool { |
||||
|
for i := 0; i < len(idA); i++ { |
||||
|
if idA[i] != idB[i] { |
||||
|
return idA[i] > idB[i] |
||||
|
} |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
func (idA ID) Distance(idB ID) ID { |
||||
|
var d ID |
||||
|
for i := 0; i < B; i++ { |
||||
|
d[i] = idA[i] ^ idB[i] |
||||
|
} |
||||
|
return d |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestNewID(t *testing.T) { |
||||
|
_, err := NewID() |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7") |
||||
|
assert.Nil(t, err) |
||||
|
assert.Equal(t, "0fd85ddddf15aeec2d5d8b01b013dbca030a18d7", idA.String()) |
||||
|
} |
||||
|
|
||||
|
func TestIDCmp(t *testing.T) { |
||||
|
idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7") |
||||
|
assert.Nil(t, err) |
||||
|
idB, err := IDFromString("c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b") |
||||
|
assert.Nil(t, err) |
||||
|
assert.True(t, !idA.Cmp(idB)) |
||||
|
} |
||||
|
|
||||
|
func TestIDDistance(t *testing.T) { |
||||
|
idA, err := IDFromString("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7") |
||||
|
assert.Nil(t, err) |
||||
|
idB, err := IDFromString("c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b") |
||||
|
assert.Nil(t, err) |
||||
|
assert.Equal(t, "cb55d68e04fa18e5c0131fd236ce80e8ecc1348c", idA.Distance(idB).String()) |
||||
|
} |