Commit 955487f2 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet Committed by Brian Tiger Chow

kbucket use new keyspace

parent 6aca2ab2
...@@ -69,7 +69,7 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket { ...@@ -69,7 +69,7 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket {
e := b.list.Front() e := b.list.Front()
for e != nil { for e != nil {
peerID := ConvertPeerID(e.Value.(*peer.Peer).ID) peerID := ConvertPeerID(e.Value.(*peer.Peer).ID)
peerCPL := prefLen(peerID, target) peerCPL := commonPrefixLen(peerID, target)
if peerCPL > cpl { if peerCPL > cpl {
cur := e cur := e
out.PushBack(e.Value) out.PushBack(e.Value)
......
...@@ -44,7 +44,7 @@ func (rt *RoutingTable) Update(p *peer.Peer) *peer.Peer { ...@@ -44,7 +44,7 @@ func (rt *RoutingTable) Update(p *peer.Peer) *peer.Peer {
rt.tabLock.Lock() rt.tabLock.Lock()
defer rt.tabLock.Unlock() defer rt.tabLock.Unlock()
peerID := ConvertPeerID(p.ID) peerID := ConvertPeerID(p.ID)
cpl := xor(peerID, rt.local).commonPrefixLen() cpl := commonPrefixLen(peerID, rt.local)
bucketID := cpl bucketID := cpl
if bucketID >= len(rt.Buckets) { if bucketID >= len(rt.Buckets) {
...@@ -145,7 +145,7 @@ func (rt *RoutingTable) NearestPeer(id ID) *peer.Peer { ...@@ -145,7 +145,7 @@ func (rt *RoutingTable) NearestPeer(id ID) *peer.Peer {
func (rt *RoutingTable) NearestPeers(id ID, count int) []*peer.Peer { func (rt *RoutingTable) NearestPeers(id ID, count int) []*peer.Peer {
rt.tabLock.RLock() rt.tabLock.RLock()
defer rt.tabLock.RUnlock() defer rt.tabLock.RUnlock()
cpl := prefLen(id, rt.local) cpl := commonPrefixLen(id, rt.local)
// Get bucket at cpl index or last bucket // Get bucket at cpl index or last bucket
var bucket *Bucket var bucket *Bucket
......
...@@ -48,7 +48,7 @@ func TestBucket(t *testing.T) { ...@@ -48,7 +48,7 @@ func TestBucket(t *testing.T) {
llist := b.list llist := b.list
for e := llist.Front(); e != nil; e = e.Next() { for e := llist.Front(); e != nil; e = e.Next() {
p := ConvertPeerID(e.Value.(*peer.Peer).ID) p := ConvertPeerID(e.Value.(*peer.Peer).ID)
cpl := xor(p, localID).commonPrefixLen() cpl := commonPrefixLen(p, localID)
if cpl > 0 { if cpl > 0 {
t.Fatalf("Split failed. found id with cpl > 0 in 0 bucket") t.Fatalf("Split failed. found id with cpl > 0 in 0 bucket")
} }
...@@ -57,7 +57,7 @@ func TestBucket(t *testing.T) { ...@@ -57,7 +57,7 @@ func TestBucket(t *testing.T) {
rlist := spl.list rlist := spl.list
for e := rlist.Front(); e != nil; e = e.Next() { for e := rlist.Front(); e != nil; e = e.Next() {
p := ConvertPeerID(e.Value.(*peer.Peer).ID) p := ConvertPeerID(e.Value.(*peer.Peer).ID)
cpl := xor(p, localID).commonPrefixLen() cpl := commonPrefixLen(p, localID)
if cpl == 0 { if cpl == 0 {
t.Fatalf("Split failed. found id with cpl == 0 in non 0 bucket") t.Fatalf("Split failed. found id with cpl == 0 in non 0 bucket")
} }
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"errors" "errors"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
ks "github.com/jbenet/go-ipfs/routing/keyspace"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
...@@ -13,8 +14,7 @@ import ( ...@@ -13,8 +14,7 @@ import (
// behaviour // behaviour
var ErrLookupFailure = errors.New("failed to find any peer in table") var ErrLookupFailure = errors.New("failed to find any peer in table")
// ID for IpfsDHT should be a byte slice, to allow for simpler operations // ID for IpfsDHT is in the XORKeySpace
// (xor). DHT ids are based on the peer.IDs.
// //
// The type dht.ID signifies that its contents have been hashed from either a // The type dht.ID signifies that its contents have been hashed from either a
// peer.ID or a util.Key. This unifies the keyspace // peer.ID or a util.Key. This unifies the keyspace
...@@ -25,55 +25,17 @@ func (id ID) equal(other ID) bool { ...@@ -25,55 +25,17 @@ func (id ID) equal(other ID) bool {
} }
func (id ID) less(other ID) bool { func (id ID) less(other ID) bool {
a, b := equalizeSizes(id, other) a := ks.Key{Space: ks.XORKeySpace, Adjusted: id}
for i := 0; i < len(a); i++ { b := ks.Key{Space: ks.XORKeySpace, Adjusted: other}
if a[i] != b[i] { return a.Less(b)
return a[i] < b[i]
}
}
return len(a) < len(b)
}
func (id ID) commonPrefixLen() int {
for i := 0; i < len(id); i++ {
for j := 0; j < 8; j++ {
if (id[i]>>uint8(7-j))&0x1 != 0 {
return i*8 + j
}
}
}
return len(id)*8 - 1
}
func prefLen(a, b ID) int {
return xor(a, b).commonPrefixLen()
} }
func xor(a, b ID) ID { func xor(a, b ID) ID {
a, b = equalizeSizes(a, b) return ID(ks.XOR(a, b))
c := make(ID, len(a))
for i := 0; i < len(a); i++ {
c[i] = a[i] ^ b[i]
}
return c
} }
func equalizeSizes(a, b ID) (ID, ID) { func commonPrefixLen(a, b ID) int {
la := len(a) return ks.ZeroPrefixLen(ks.XOR(a, b))
lb := len(b)
if la < lb {
na := make([]byte, lb)
copy(na, a)
a = na
} else if lb < la {
nb := make([]byte, la)
copy(nb, b)
b = nb
}
return a, b
} }
// ConvertPeerID creates a DHT ID by hashing a Peer ID (Multihash) // ConvertPeerID creates a DHT ID by hashing a Peer ID (Multihash)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment