-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for kbucket and some code cleanup
- Loading branch information
1 parent
35a4086
commit bade1aa
Showing
4 changed files
with
249 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package dht | ||
|
||
import ( | ||
"container/list" | ||
|
||
peer "github.com/jbenet/go-ipfs/peer" | ||
) | ||
// Bucket holds a list of peers. | ||
type Bucket list.List | ||
|
||
func (b *Bucket) Find(id peer.ID) *list.Element { | ||
bucket_list := (*list.List)(b) | ||
for e := bucket_list.Front(); e != nil; e = e.Next() { | ||
if e.Value.(*peer.Peer).ID.Equal(id) { | ||
return e | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *Bucket) MoveToFront(e *list.Element) { | ||
bucket_list := (*list.List)(b) | ||
bucket_list.MoveToFront(e) | ||
} | ||
|
||
func (b *Bucket) PushFront(p *peer.Peer) { | ||
bucket_list := (*list.List)(b) | ||
bucket_list.PushFront(p) | ||
} | ||
|
||
func (b *Bucket) PopBack() *peer.Peer { | ||
bucket_list := (*list.List)(b) | ||
last := bucket_list.Back() | ||
bucket_list.Remove(last) | ||
return last.Value.(*peer.Peer) | ||
} | ||
|
||
func (b *Bucket) Len() int { | ||
bucket_list := (*list.List)(b) | ||
return bucket_list.Len() | ||
} | ||
|
||
// Splits a buckets peers into two buckets, the methods receiver will have | ||
// peers with CPL equal to cpl, the returned bucket will have peers with CPL | ||
// greater than cpl (returned bucket has closer peers) | ||
func (b *Bucket) Split(cpl int, target ID) *Bucket { | ||
bucket_list := (*list.List)(b) | ||
out := list.New() | ||
e := bucket_list.Front() | ||
for e != nil { | ||
peer_id := convertPeerID(e.Value.(*peer.Peer).ID) | ||
peer_cpl := xor(peer_id, target).commonPrefixLen() | ||
if peer_cpl > cpl { | ||
cur := e | ||
out.PushBack(e.Value) | ||
e = e.Next() | ||
bucket_list.Remove(cur) | ||
continue | ||
} | ||
e = e.Next() | ||
} | ||
return (*Bucket)(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dht | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"crypto/sha256" | ||
"math/rand" | ||
"container/list" | ||
"testing" | ||
|
||
peer "github.com/jbenet/go-ipfs/peer" | ||
) | ||
|
||
func _randPeer() *peer.Peer { | ||
p := new(peer.Peer) | ||
p.ID = make(peer.ID, 16) | ||
crand.Read(p.ID) | ||
return p | ||
} | ||
|
||
func _randID() ID { | ||
buf := make([]byte, 16) | ||
crand.Read(buf) | ||
|
||
hash := sha256.Sum256(buf) | ||
return ID(hash[:]) | ||
} | ||
|
||
// Test basic features of the bucket struct | ||
func TestBucket(t *testing.T) { | ||
b := new(Bucket) | ||
|
||
peers := make([]*peer.Peer, 100) | ||
for i := 0; i < 100; i++ { | ||
peers[i] = _randPeer() | ||
b.PushFront(peers[i]) | ||
} | ||
|
||
local := _randPeer() | ||
local_id := convertPeerID(local.ID) | ||
|
||
i := rand.Intn(len(peers)) | ||
e := b.Find(peers[i].ID) | ||
if e == nil { | ||
t.Errorf("Failed to find peer: %v", peers[i]) | ||
} | ||
|
||
spl := b.Split(0, convertPeerID(local.ID)) | ||
llist := (*list.List)(b) | ||
for e := llist.Front(); e != nil; e = e.Next() { | ||
p := convertPeerID(e.Value.(*peer.Peer).ID) | ||
cpl := xor(p, local_id).commonPrefixLen() | ||
if cpl > 0 { | ||
t.Fatalf("Split failed. found id with cpl > 0 in 0 bucket") | ||
} | ||
} | ||
|
||
rlist := (*list.List)(spl) | ||
for e := rlist.Front(); e != nil; e = e.Next() { | ||
p := convertPeerID(e.Value.(*peer.Peer).ID) | ||
cpl := xor(p, local_id).commonPrefixLen() | ||
if cpl == 0 { | ||
t.Fatalf("Split failed. found id with cpl == 0 in non 0 bucket") | ||
} | ||
} | ||
} | ||
|
||
// Right now, this just makes sure that it doesnt hang or crash | ||
func TestTableUpdate(t *testing.T) { | ||
local := _randPeer() | ||
rt := NewRoutingTable(10, convertPeerID(local.ID)) | ||
|
||
peers := make([]*peer.Peer, 100) | ||
for i := 0; i < 100; i++ { | ||
peers[i] = _randPeer() | ||
} | ||
|
||
// Testing Update | ||
for i := 0; i < 10000; i++ { | ||
p := rt.Update(peers[rand.Intn(len(peers))]) | ||
if p != nil { | ||
t.Log("evicted peer.") | ||
} | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
id := _randID() | ||
ret := rt.NearestPeers(id, 5) | ||
if len(ret) == 0 { | ||
t.Fatal("Failed to find node near ID.") | ||
} | ||
} | ||
} |
Oops, something went wrong.
@whyrusleeping wait, shouldnt we be calculating "Sort by distance to
id
" here?