fix bitmap prefix len
This commit is contained in:
parent
211dbd7c50
commit
883d76d8bb
5 changed files with 30 additions and 4 deletions
|
@ -53,7 +53,7 @@ func (b bitmap) PrefixLen() (ret int) {
|
|||
}
|
||||
}
|
||||
}
|
||||
return nodeIDLength*8 - 1
|
||||
return numBuckets
|
||||
}
|
||||
|
||||
func (b *bitmap) UnmarshalBencode(encoded []byte) error {
|
||||
|
|
|
@ -96,3 +96,26 @@ func TestBitmapMarshalEmbedded2(t *testing.T) {
|
|||
t.Error("encoding does not match expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitmap_PrefixLen(t *testing.T) {
|
||||
tt := []struct {
|
||||
str string
|
||||
len int
|
||||
}{
|
||||
{len: 0, str: "F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{len: 0, str: "800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{len: 1, str: "700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{len: 1, str: "400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{len: 384, str: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
|
||||
{len: 383, str: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"},
|
||||
{len: 382, str: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"},
|
||||
{len: 382, str: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003"},
|
||||
}
|
||||
|
||||
for _, test := range tt {
|
||||
len := newBitmapFromHex(test.str).PrefixLen()
|
||||
if len != test.len {
|
||||
t.Errorf("got prefix len %d; expected %d for %s", len, test.len, test.str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,9 @@ func TestDecode(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
|
||||
t.Error("TODO")
|
||||
continue
|
||||
|
||||
spew.Dump(decoded)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ const network = "udp4"
|
|||
|
||||
const alpha = 3 // this is the constant alpha in the spec
|
||||
const nodeIDLength = 48 // bytes. this is the constant B in the spec
|
||||
const bucketSize = 20 // this is the constant k in the spec
|
||||
const bucketSize = 8 // this is the constant k in the spec
|
||||
|
||||
const tExpire = 86400 * time.Second // the time after which a key/value pair expires; this is a time-to-live (TTL) from the original publication date
|
||||
const tRefresh = 3600 * time.Second // the time after which an otherwise unaccessed bucket must be refreshed
|
||||
|
|
|
@ -135,12 +135,12 @@ func (rt *RoutingTable) FindClosest(target bitmap, count int) []*Node {
|
|||
bucket := rt.buckets[prefixLength]
|
||||
toSort = appendNodes(toSort, bucket.Front(), nil, target)
|
||||
|
||||
for i := 1; (prefixLength-i >= 0 || prefixLength+i < nodeIDLength*8) && len(toSort) < count; i++ {
|
||||
for i := 1; (prefixLength-i >= 0 || prefixLength+i < numBuckets) && len(toSort) < count; i++ {
|
||||
if prefixLength-i >= 0 {
|
||||
bucket = rt.buckets[prefixLength-i]
|
||||
toSort = appendNodes(toSort, bucket.Front(), nil, target)
|
||||
}
|
||||
if prefixLength+i < nodeIDLength*8 {
|
||||
if prefixLength+i < numBuckets {
|
||||
bucket = rt.buckets[prefixLength+i]
|
||||
toSort = appendNodes(toSort, bucket.Front(), nil, target)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue