mirror of
https://github.com/arnaucube/go-dht.git
synced 2026-02-06 19:06:44 +01:00
add update KBucket table
This commit is contained in:
22
kademlia.go
22
kademlia.go
@@ -75,13 +75,15 @@ func (n *Node) Update(o ListedNode) {
|
||||
k := n.KBucket(o.ID)
|
||||
kb := n.KBuckets[k]
|
||||
if len(kb) >= KBucketSize {
|
||||
// TODO ping the kb[0], and if no response, delete it, and add the current o (ID)
|
||||
// if n.KBuckets[k] is alrady full, perform ping of the first element
|
||||
n.Ping(k, o)
|
||||
return
|
||||
}
|
||||
// check that is not already in the list
|
||||
exist, _ := existsInListedNodes(n.KBuckets[k], o)
|
||||
exist, pos := existsInListedNodes(n.KBuckets[k], o)
|
||||
if exist {
|
||||
// update position to the bottom
|
||||
// update position of o to the bottom
|
||||
n.KBuckets[k] = moveToBottom(n.KBuckets[k], pos)
|
||||
return
|
||||
}
|
||||
// not exists, add it to the kBucket
|
||||
@@ -89,6 +91,13 @@ func (n *Node) Update(o ListedNode) {
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Node) Ping(k int, o ListedNode) {
|
||||
// TODO when rpc layer is done
|
||||
// ping the n.KBuckets[k][0] (using goroutine)
|
||||
// if no response (timeout), delete it and add 'o'
|
||||
// n.KBuckets[k][0] = o
|
||||
}
|
||||
|
||||
func existsInListedNodes(lns []ListedNode, ln ListedNode) (bool, int) {
|
||||
for i, v := range lns {
|
||||
if v.ID.Equal(ln.ID) {
|
||||
@@ -97,3 +106,10 @@ func existsInListedNodes(lns []ListedNode, ln ListedNode) (bool, int) {
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func moveToBottom(kb []ListedNode, k int) []ListedNode {
|
||||
e := kb[k]
|
||||
kb = append(kb[:k], kb[k+1:]...)
|
||||
kb = append(kb[:], e)
|
||||
return kb
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var debug = true
|
||||
var debug = false
|
||||
|
||||
func TestCountZeros(t *testing.T) {
|
||||
zeroes := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||
@@ -51,10 +51,7 @@ func TestNodeKBucket(t *testing.T) {
|
||||
assert.Equal(t, 159, d)
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
nodeA, err := LoadNode("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
|
||||
assert.Nil(t, err)
|
||||
|
||||
func prepareTestListedNodes() []ListedNode {
|
||||
lnIDs := []string{
|
||||
"c48d8b53dbefb609ed4e94d386dd5b22efcb2c5b",
|
||||
"420bfebd44fc62615253224328f40f29c9b225fa",
|
||||
@@ -76,13 +73,35 @@ func TestUpdate(t *testing.T) {
|
||||
"0750931c40a52a2facd220a02851f7d34f95e1fa",
|
||||
"ebfba615ac50bcd0f5c2420741d032e885abf576",
|
||||
}
|
||||
var lns []ListedNode
|
||||
for i := 0; i < len(lnIDs); i++ {
|
||||
idI, err := IDFromString(lnIDs[i])
|
||||
assert.Nil(t, err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
lnI := ListedNode{
|
||||
ID: idI,
|
||||
Addr: "",
|
||||
}
|
||||
lns = append(lns, lnI)
|
||||
}
|
||||
return lns
|
||||
}
|
||||
|
||||
func TestMoveToBottom(t *testing.T) {
|
||||
lns := prepareTestListedNodes()
|
||||
movedElem := lns[3]
|
||||
assert.NotEqual(t, movedElem, lns[len(lns)-1])
|
||||
lns = moveToBottom(lns, 3)
|
||||
assert.Equal(t, movedElem, lns[len(lns)-1])
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
nodeA, err := LoadNode("0fd85ddddf15aeec2d5d8b01b013dbca030a18d7")
|
||||
assert.Nil(t, err)
|
||||
|
||||
lns := prepareTestListedNodes()
|
||||
for _, lnI := range lns {
|
||||
nodeA.Update(lnI)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user