add id representation

This commit is contained in:
Branden J Brown 2025-03-07 16:09:48 -05:00
parent 2c650804fd
commit ac31109615
2 changed files with 74 additions and 0 deletions

44
chord/id.go Normal file
View File

@ -0,0 +1,44 @@
package chord
import (
"bytes"
"crypto/sha1"
"net/netip"
)
type ID [20]byte
func addrID(addr netip.AddrPort) ID {
if !addr.IsValid() {
return ID{}
}
b := make([]byte, 0, len("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0]:65535"))
b = addr.AppendTo(b)
return keyID(b)
}
func keyID[T string | []byte](key T) ID {
s := sha1.Sum([]byte(key))
return s
}
// contains reports whether a clockwise traversal through the hash space
// starting immediately after left reaches x no later than it reaches right.
func contains(left, right, x ID) bool {
lr := bytes.Compare(left[:], right[:])
lx := bytes.Compare(left[:], x[:])
xr := bytes.Compare(x[:], right[:])
switch {
case lr < 0:
// left is less than right, so the interval does not cross the origin.
// | L-----R |
return (lx < 0) == (xr <= 0)
case lr > 0:
// The interval crosses the origin.
// |-----R L-----|
return (lx < 0) != (xr <= 0)
default:
// The interval is the entire hash space.
return true
}
}

30
chord/id_test.go Normal file
View File

@ -0,0 +1,30 @@
package chord
import "testing"
func TestContains(t *testing.T) {
true := [][3]ID{
{{1}, {1}, {0}},
{{1}, {1}, {1}},
{{1}, {1}, {2}},
{{1}, {2}, {2}},
{{1}, {3}, {2}},
{{3}, {1}, {1}},
{{3}, {1}, {0}},
}
false := [][3]ID{
{{1}, {2}, {0}},
{{1}, {2}, {3}},
{{3}, {1}, {2}},
}
for _, c := range true {
if !contains(c[0], c[1], c[2]) {
t.Errorf("%02x not found inside %02x - %02x", c[2], c[0], c[1])
}
}
for _, c := range false {
if contains(c[0], c[1], c[2]) {
t.Errorf("%02x found inside %02x - %02x", c[2], c[0], c[1])
}
}
}