dht

package module
v0.0.0-...-27fdafd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2021 License: MIT Imports: 9 Imported by: 0

README

DHT Lite

This code is a fork from https://github.com/james-lawrence/kademlia and https://github.com/prettymuchbryce/kademlia with modifications for proper abstraction. All networking code was removed from the original one. This package shall only provide DHT fuctionality.

The following functions are not handled here and must be done by the caller, if desired:

  • Remove nodes that are deemed inactive via dht.RemoveNode.
  • Provide a function ShouldEvict to determine if a node shall be evicted in favor of another one.
  • Refresh buckets via dht.RefreshBuckets.
  • The actual store data functions (and associated replication/expiration) are not provided, only the functionality to traverse through the network.

Documentation

Index

Constants

View Source
const (
	ActionFindNode  = iota // Find a node
	ActionFindValue        // Find a value
)

Actions for performing the information request

Variables

This section is empty.

Functions

This section is empty.

Types

type DHT

type DHT struct {

	// ShouldEvict determines whether node 1 shall be evicted in favor of node 2
	ShouldEvict func(node1, node2 *Node) bool

	// SendRequestStore sends an announcement-store message to the remote node. It informs the remote node that the local one stores the given key-value.
	SendRequestStore func(node *Node, key []byte, dataSize uint64)

	// SendRequestFindNode sends an information request to find a particular node. nodes are the nodes to send the request to.
	SendRequestFindNode func(request *InformationRequest)

	// SendRequestFindValue sends an information request to find data. nodes are the nodes to send the request to.
	SendRequestFindValue func(request *InformationRequest)

	// The maximum time to wait for a response to any message in Store, Get, FindNode
	TMsgTimeout time.Duration
	// contains filtered or unexported fields
}

DHT represents the state of the local node in the distributed hash table

func NewDHT

func NewDHT(self *Node, bits, bucketSize, alpha int) *DHT

NewDHT initializes a new DHT node with default values.

func (*DHT) AddNode

func (dht *DHT) AddNode(node *Node)

AddNode adds a node into the appropriate k bucket. These buckets are stored in big-endian order so we look at the bits from right to left in order to find the appropriate bucket.

func (*DHT) FindNode

func (dht *DHT) FindNode(key []byte) (value []byte, found bool, err error)

FindNode finds the target node in the network

func (*DHT) Get

func (dht *DHT) Get(key []byte) (value []byte, senderID []byte, found bool, err error)

Get retrieves data from the network using key

func (*DHT) GetClosestContacts

func (dht *DHT) GetClosestContacts(count int, target []byte, filterFunc NodeFilterFunc, ignoredNodes ...[]byte) []*Node

GetClosestContacts returns the closes contacts in the hash table

func (*DHT) GetSelfID

func (dht *DHT) GetSelfID() []byte

GetSelfID returns the identifier of the local node

func (*DHT) IsNodeCloser

func (dht *DHT) IsNodeCloser(node1, node2 []byte) bool

IsNodeCloser compares 2 nodes to self. If true, the first node is closer (= smaller distance) to self than the second.

func (*DHT) MarkNodeAsSeen

func (dht *DHT) MarkNodeAsSeen(ID []byte)

MarkNodeAsSeen marks a node as seen, which pushes it to the top in the bucket list.

func (*DHT) NewInformationRequest

func (dht *DHT) NewInformationRequest(Action int, Key []byte, Nodes []*Node) (ir *InformationRequest)

NewInformationRequest creates a new information request and adds it to the list. It marks the count of nodes as active, meaning the caller should later decrease it via ActiveNodesSub.

func (*DHT) Nodes

func (dht *DHT) Nodes() []*Node

Nodes returns the nodes themselves sotred in the routing table.

func (*DHT) NumNodes

func (dht *DHT) NumNodes() int

NumNodes returns the total number of nodes stored in the local routing table

func (*DHT) RefreshBuckets

func (dht *DHT) RefreshBuckets(target int)

RefreshBuckets refreshes all buckets not meeting the target node number. 0 to refresh all.

func (*DHT) RemoveNode

func (dht *DHT) RemoveNode(ID []byte)

RemoveNode removes a node

func (*DHT) Store

func (dht *DHT) Store(key []byte, dataSize uint64) (err error)

Store informs the network about data stored locally.

type InformationRequest

type InformationRequest struct {
	Action          int               // ActionX
	Key             []byte            // Key that is being queried
	ResultChan      chan *NodeMessage // Result channel
	ActiveNodes     uint64            // Number of nodes actively handling the request.
	Nodes           []*Node           // Nodes that are receiving the request.
	IsTerminated    bool              // If true, it was signaled for termination
	TerminateSignal chan struct{}     // gets closed on termination signal, can be used in select via "case _ = <- network.terminateSignal:"
	sync.Mutex                        // for sychronized closing
}

InformationRequest is an asynchronous request sent to nodes. It tracks any asynchronous replies and handles timeouts.

func (*InformationRequest) CollectResults

func (ir *InformationRequest) CollectResults(timeout time.Duration) (results []*NodeMessage)

CollectResults collects all information request responses within the given timeout.

func (*InformationRequest) Done

func (ir *InformationRequest) Done()

Done is called when a remote node is done.

func (*InformationRequest) QueueResult

func (ir *InformationRequest) QueueResult(message *NodeMessage)

QueueResult accepts incoming results

func (*InformationRequest) Terminate

func (ir *InformationRequest) Terminate()

Terminate sends the termination signal to all workers. It is safe to call Terminate multiple times.

type Node

type Node struct {
	// ID is the unique identifier
	ID []byte

	// LastSeen when was this node last considered seen by the DHT
	LastSeen time.Time

	// Info is an arbitrary pointer specified by the caller
	Info interface{}
}

Node is the over-the-wire representation of a node

type NodeFilterFunc

type NodeFilterFunc func(node *Node) (accept bool)

NodeFilterFunc is called to filter nodes based on the callers choice

type NodeMessage

type NodeMessage struct {
	SenderID []byte  // Sender of the message
	Data     []byte  // FIND_VALUE: Actual data
	Closest  []*Node // FIND_VALUE, FIND_NODE: Closest nodes to the requested key
	Storing  []*Node // FIND_VALUE: Nodes known to store the value
	Error    error   // To be removed
}

NodeMessage is a message sent by a node

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL