probe-lab / go-kademlia

Generic Go Kademlia implementation
Other
18 stars 4 forks source link

Introduce generic Kademlia key interface #68

Closed iand closed 1 year ago

iand commented 1 year ago

This is a revised attempt to introduce a generic Kademlia key interface to improve type safety in the codebase. It draws on previous attempts (generic-kadkey, generic-key-constraint and kadkey-interface)

The current implementation of keys as byte slices has several disadvantages including possible aliasing and mutation due to reference semantics and requiring every consumer of a key to check its length for compatibly.

This implementation overcomes these disadvantages by introducing a parameterized Key interface which enforces type safety on basic operations such as Xor, Compare and CommonPrefixLength. Three key implementations are included for differing bit lengths: Key256, Key32 and Key8. These are implemented as values with copy semantics which avoids mutations and accidental sharing of backing arrays.

A consuming type can declare that it accepts a Kademlia key type using the generic signature [K kad.Key[K]] which is interpreted as K is a type that implements the kad.Key interface.

Much of this PR is mechanical adjustment, weaving the generic type declarations through all consuming types.

The principal changes to review are:

A new kad package is also introduced as the location for the fundamental Kademlia interfaces such as Key. No other interfaces have been moved yet but candidates include address.NodeID, address.NodeAddr, routing.Table and message.MinKadMessage. This may be done in a follow up after discussion.

dennis-tra commented 1 year ago

Let's delete the outdated branches from this repository after we've merged this 👍