From 883d76d8bb76445e7e8f391aa5ae037fdc0bbaf5 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Thu, 15 Mar 2018 14:42:57 -0400 Subject: [PATCH] fix bitmap prefix len --- dht/bitmap.go | 2 +- dht/bitmap_test.go | 23 +++++++++++++++++++++++ dht/decode_test.go | 3 +++ dht/dht.go | 2 +- dht/routing_table.go | 4 ++-- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/dht/bitmap.go b/dht/bitmap.go index 3125d6c..90f83fa 100644 --- a/dht/bitmap.go +++ b/dht/bitmap.go @@ -53,7 +53,7 @@ func (b bitmap) PrefixLen() (ret int) { } } } - return nodeIDLength*8 - 1 + return numBuckets } func (b *bitmap) UnmarshalBencode(encoded []byte) error { diff --git a/dht/bitmap_test.go b/dht/bitmap_test.go index 8b237ce..16a9bec 100644 --- a/dht/bitmap_test.go +++ b/dht/bitmap_test.go @@ -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) + } + } +} diff --git a/dht/decode_test.go b/dht/decode_test.go index 2ce78e7..70feab0 100644 --- a/dht/decode_test.go +++ b/dht/decode_test.go @@ -29,6 +29,9 @@ func TestDecode(t *testing.T) { continue } + t.Error("TODO") + continue + spew.Dump(decoded) } } diff --git a/dht/dht.go b/dht/dht.go index bc57393..03de2fa 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -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 diff --git a/dht/routing_table.go b/dht/routing_table.go index 32b86ab..138d310 100644 --- a/dht/routing_table.go +++ b/dht/routing_table.go @@ -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) }