Documentation
¶
Overview ¶
KBucket ¶
Kademlia DHT K-bucket implementation as a binary tree. KBucket was ported from Tristan Slominski's k-bucket: github.com/tristanls/k-bucket
A Distributed Hash Table (DHT) is a decentralized distributed system that provides a lookup table similar to a hash table.
KBucket is an implementation of a storage mechanism for keys within a DHT. It stores Contact objects which represent locations and addresses of nodes in the decentralized distributed system. Contact objects are typically identified by a SHA-1 hash, however this restriction is lifted in this implementation. Additionally, node ids of different lengths can be compared.
This Kademlia DHT k-bucket implementation is meant to be as minimal as possible. It assumes that Contact objects consist only of Id. It is useful, and necessary, to attach other properties to a Contact. For example, one may want to attach ip and port properties, which allow an application to send IP traffic to the Contact. However, this information is extraneous and irrelevant to the operation of a k-bucket.
KBucket events:
kbucket.added newContact Contact: The new contact that was added. Emitted only when "newContact" was added to bucket and it was not stored in the bucket before. kbucket.ping old Contacts: The slice of contacts to ping. new Contact: The new contact to be added if one of old contacts does not respond. Emitted every time a contact is added that would exceed the capacity of a "don't split" k-bucket it belongs to. kbucket.removed contact Contact: The contact that was removed. Emitted when "contact" was removed from the bucket. kbucket.updated old Contact: The contact that was stored prior to the update. new Contact: The new contact that is now stored after the update. Emitted when a previously existing ("previously existing" means "oldContact.id" equals "newContact.id") contact was added to the bucket and it was replaced with "newContact".
Index ¶
- func CompareAddrPorts(a, b netip.AddrPort) bool
- func GenerateId() ([]byte, error)
- func GenerateRandomBytes(n int) ([]byte, error)
- type Contact
- type Contacts
- type KBucket
- func (b *KBucket) Add(contact Contact) *KBucket
- func (b *KBucket) Clear()
- func (b *KBucket) Closest(id []byte, n uint) Contacts
- func (b *KBucket) Count() int
- func (b *KBucket) Distance(fid, sid []byte) int
- func (b *KBucket) Get(id []byte) Contact
- func (b *KBucket) GetId() []byte
- func (b *KBucket) Has(id []byte) bool
- func (b *KBucket) Remove(id []byte)
- func (b *KBucket) Seen(id []byte) bool
- func (b *KBucket) SetArbiterFn(arbiterFn func(Contact, Contact) Contact)
- func (b *KBucket) SetDistanceFn(distanceFn func([]byte, []byte) int)
- func (b *KBucket) ToSlice() Contacts
- type KBucketOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareAddrPorts ¶
CompareAddrPorts compares two netip.AddrPorts. It will return true if the two AddrPorts are equal, false otherwise.
func GenerateId ¶
GenerateId generates a random 160-bit ID (SHA-1). It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.
func GenerateRandomBytes ¶
GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.
Types ¶
type Contact ¶
type Contact struct { Id []byte // The node id. AddrPort netip.AddrPort // The address and port of the node. SeenAt time.Time // SeenAt is the time this node was last seen. VectorClock int Metadata map[string]any // Optional satellite data to include with the Contact. // contains filtered or unexported fields }
type KBucket ¶
type KBucket struct { // Optional satellite data to include with the KBucket. Metadata property is // guaranteed not be altered, it is provided as an explicit container for users // of KBucket to store implementation-specific data. Metadata map[string]any // contains filtered or unexported fields }
func NewKBucket ¶
func NewKBucket(options KBucketOptions, emitter *eventemitter.Emitter) (*KBucket, error)
Implementation of a Kademlia DHT k-bucket used for storing contact (peer node) information. NewKBucket creates a new KBucket with the given options.
func (*KBucket) Closest ¶
Get the n closest contacts to the provided node id. "Closest" here means: closest according to the XOR metric of the contact node id. Return the maximum of n closest contacts to the node id.
func (*KBucket) Distance ¶
Default distance function. Finds the XOR distance between first id and second id. Returns the XOR distance between first id and second id.
func (*KBucket) Get ¶
Get a contact by its exact id. Returns an empty Contact if the contact is not found.
func (*KBucket) Has ¶
Has returns true if the contact with the given id is in the KBucket, false otherwise.
func (*KBucket) Seen ¶
Seen updates the contact's last seen time. Returns true if the contact was found, false otherwise.
func (*KBucket) SetArbiterFn ¶
SetArbiterFn overrides the default arbiter function.
func (*KBucket) SetDistanceFn ¶
SetDistanceFn overrides the default distance function.
type KBucketOptions ¶
type KBucketOptions struct { // An optional byte slice representing the local node id. If not provided, a // local node id will be created via "GenerateId()". (Default: randomly generated) LocalNodeId []byte // The number of nodes that a KBucket can contain before being full or split. // (Default: 20) NodesPerKBucket int // The number of nodes to ping when a bucket that should not be split becomes // full. KBucket will emit a "kbucket.ping" event that contains "NodesToPing" // nodes that have not been contacted the longest. (Default: 3) NodesToPing int }