}
// Below are 2 versions of idDistance. Only one can be active.
+var maxDistance big.Int
+func init() {
+ var zero big.Int
+ maxDistance.SetBit(&zero, 160, 1)
+}
+
+// If we don't know the ID for a node, then its distance is more than the
+// furthest possible distance otherwise.
func idDistance(a, b string) (ret bigIntDistance) {
+ if a == "" && b == "" {
+ return
+ }
+ if a == "" {
+ if len(b) != 20 {
+ panic(b)
+ }
+ ret.Set(&maxDistance)
+ return
+ }
+ if b == "" {
+ if len(a) != 20 {
+ panic(a)
+ }
+ ret.Set(&maxDistance)
+ return
+ }
if len(a) != 20 {
panic(a)
}
"\x03" + zeroID[1:18] + "\x55\xf0",
"\x55" + zeroID[1:17] + "\xff\x55\x0f",
"\x54" + zeroID[1:18] + "\x50\x0f",
+ "",
}
func TestDistances(t *testing.T) {
if idDistance(testIDs[3], testIDs[2]).BitCount() != 4+8+8 {
t.FailNow()
}
+ for i := 0; i < 5; i++ {
+ dist := idDistance(testIDs[i], testIDs[5]).Int
+ if dist.Cmp(&maxDistance) != 0 {
+ t.FailNow()
+ }
+ }
+}
+
+func TestMaxDistanceString(t *testing.T) {
+ if string(maxDistance.Bytes()) != "\x01"+zeroID {
+ t.FailNow()
+ }
}
func TestBadIdStrings(t *testing.T) {
var a, b string
+ idDistance(a, b)
+ idDistance(a, zeroID)
+ idDistance(zeroID, b)
recoverPanicOrDie(t, func() {
- idDistance(a, b)
+ idDistance("when", a)
})
recoverPanicOrDie(t, func() {
- idDistance(a, zeroID)
+ idDistance(a, "bad")
})
recoverPanicOrDie(t, func() {
- idDistance(zeroID, b)
+ idDistance("meets", "evil")
})
- if !idDistance(zeroID, zeroID).IsZero() {
- t.Fatal("identical IDs should have distance 0")
+ for _, id := range testIDs {
+ if !idDistance(id, id).IsZero() {
+ t.Fatal("identical IDs should have distance 0")
+ }
}
a = "\x03" + zeroID[1:]
b = zeroID