routing

package
v0.0.0-...-20ef9fc Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0, MIT Imports: 13 Imported by: 0

README

Routing Table

Table defines a generic Kademlia routing table interface.

// Table is the interface for Kademlia Routing Tables
type Table interface {
	// Self returns the local node's Kademlia key
	Self() key.KadKey
	// AddPeer tries to add a peer to the routing table
	AddPeer(context.Context, kad.NodeID) (bool, error)
	// RemovePeer tries to remove a peer identified by its Kademlia key from the
	// routing table
	RemoveKey(context.Context, key.KadKey) (bool, error)
	// NearestPeers returns the closest peers to a given key
	NearestPeers(context.Context, key.KadKey, int) ([]kad.NodeID, error)
}

In go-libp2p-kad-dht, the Routing Table periodically refreshes. This operation consists in looking for its own Kademlia key, to be aware of one's closest neighbors at all time, and making sure that the buckets are as full as possible with reachable peers. So a node will make sure that all peers that are in its routing table are still online, and will replace the offline peers with fresh ones.

Implementations

  • SimpleRT a very simple routing table implementation that should NOT be used in production.
  • ClientRT (doesn't exist yet) a routing table implementation that is optimized for nodes in client mode only
  • TrieRT (doesn't exist yet) a routing table implementation based on a binary trie to store Kademlia keys and optimize distance computations.
  • FullRT (not migrated yet) a routing table implementation that periodically crawls the network and stores all nodes.
  • LazyRT (doesn't exist yet) a routing table implementation keeping all peers it has heard of in its routing table, but only refreshes a subset of them periodically. Some peers may be unreachable.

Challenges

2023-05-23: We want to keep track of the remote Clients that are close to us. So we want to add them in our routing table. However, we don't want to give them as closer peers when answering a FIND_NODE request. They should remain in the RT (as long as there is space in the buckets), but not be shared. They should be kept in the routing table, but they aren't prioritary compared with other DHT servers.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNodeValueList

func NewNodeValueList[K kad.Key[K]]() *nodeValueList[K]

Types

type Bootstrap

type Bootstrap[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

func NewBootstrap

func NewBootstrap[K kad.Key[K], A kad.Address[A]](self kad.NodeID[K], cfg *BootstrapConfig[K, A]) (*Bootstrap[K, A], error)

func (*Bootstrap[K, A]) Advance

func (b *Bootstrap[K, A]) Advance(ctx context.Context, ev BootstrapEvent) BootstrapState

Advance advances the state of the bootstrap by attempting to advance its query if running.

type BootstrapConfig

type BootstrapConfig[K kad.Key[K], A kad.Address[A]] struct {
	Timeout            time.Duration // the time to wait before terminating a query that is not making progress
	RequestConcurrency int           // the maximum number of concurrent requests that each query may have in flight
	RequestTimeout     time.Duration // the timeout queries should use for contacting a single node
	Clock              clock.Clock   // a clock that may replaced by a mock when testing
}

BootstrapConfig specifies optional configuration for a Bootstrap

func DefaultBootstrapConfig

func DefaultBootstrapConfig[K kad.Key[K], A kad.Address[A]]() *BootstrapConfig[K, A]

DefaultBootstrapConfig returns the default configuration options for a Bootstrap. Options may be overridden before passing to NewBootstrap

func (*BootstrapConfig[K, A]) Validate

func (cfg *BootstrapConfig[K, A]) Validate() error

Validate checks the configuration options and returns an error if any have invalid values.

type BootstrapEvent

type BootstrapEvent interface {
	// contains filtered or unexported methods
}

BootstrapEvent is an event intended to advance the state of a bootstrap.

type BootstrapState

type BootstrapState interface {
	// contains filtered or unexported methods
}

BootstrapState is the state of a bootstrap.

type EventBootstrapMessageFailure

type EventBootstrapMessageFailure[K kad.Key[K]] struct {
	NodeID kad.NodeID[K] // the node the message was sent to
	Error  error         // the error that caused the failure, if any
}

EventBootstrapMessageFailure notifiesa bootstrap that an attempt to send a message has failed.

type EventBootstrapMessageResponse

type EventBootstrapMessageResponse[K kad.Key[K], A kad.Address[A]] struct {
	NodeID   kad.NodeID[K]      // the node the message was sent to
	Response kad.Response[K, A] // the message response sent by the node
}

EventBootstrapMessageResponse notifies a bootstrap that a sent message has received a successful response.

type EventBootstrapPoll

type EventBootstrapPoll struct{}

EventBootstrapPoll is an event that signals the bootstrap that it can perform housekeeping work such as time out queries.

type EventBootstrapStart

type EventBootstrapStart[K kad.Key[K], A kad.Address[A]] struct {
	ProtocolID        address.ProtocolID
	Message           kad.Request[K, A]
	KnownClosestNodes []kad.NodeID[K]
}

EventBootstrapStart is an event that attempts to start a new bootstrap

type EventIncludeAddCandidate

type EventIncludeAddCandidate[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the candidate node
}

EventIncludeAddCandidate notifies that a node should be added to the candidate list

type EventIncludeMessageFailure

type EventIncludeMessageFailure[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the node the message was sent to
	Error    error              // the error that caused the failure, if any
}

EventIncludeMessageFailure notifiesa include that an attempt to send a message has failed.

type EventIncludeMessageResponse

type EventIncludeMessageResponse[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the node the message was sent to
	Response kad.Response[K, A] // the message response sent by the node
}

EventIncludeMessageResponse notifies a include that a sent message has received a successful response.

type EventIncludePoll

type EventIncludePoll struct{}

EventIncludePoll is an event that signals the include that it can perform housekeeping work such as time out queries.

type EventProbeAdd

type EventProbeAdd[K kad.Key[K]] struct {
	NodeID kad.NodeID[K] // the node to be probed
}

EventProbeAdd notifies a probe that a node should be added to its list of nodes.

type EventProbeMessageFailure

type EventProbeMessageFailure[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the node the message was sent to
	Error    error              // the error that caused the failure, if any
}

EventProbeMessageFailure notifiesa probe that an attempt to send a message has failed.

type EventProbeMessageResponse

type EventProbeMessageResponse[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the node the message was sent to
	Response kad.Response[K, A] // the message response sent by the node
}

EventProbeMessageResponse notifies a probe that a sent message has received a successful response.

type EventProbeNotifyConnectivity

type EventProbeNotifyConnectivity[K kad.Key[K]] struct {
	NodeID kad.NodeID[K]
}

EventProbeNotifyConnectivity notifies a probe that a node has confirmed connectivity from another source such as a query.

type EventProbePoll

type EventProbePoll struct{}

EventProbePoll is an event that signals the probe that it can perform housekeeping work such as time out queries.

type EventProbeRemove

type EventProbeRemove[K kad.Key[K]] struct {
	NodeID kad.NodeID[K] // the node to be removed
}

EventProbeRemove notifies a probe that a node should be removed from its list of nodes and the routing table.

type Include

type Include[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

func NewInclude

func NewInclude[K kad.Key[K], A kad.Address[A]](rt kad.RoutingTable[K, kad.NodeID[K]], cfg *IncludeConfig) (*Include[K, A], error)

func (*Include[K, A]) Advance

func (b *Include[K, A]) Advance(ctx context.Context, ev IncludeEvent) IncludeState

Advance advances the state of the include state machine by attempting to advance its query if running.

type IncludeConfig

type IncludeConfig struct {
	QueueCapacity int           // the maximum number of nodes that can be in the candidate queue
	Concurrency   int           // the maximum number of include checks that may be in progress at any one time
	Timeout       time.Duration // the time to wait before terminating a check that is not making progress
	Clock         clock.Clock   // a clock that may replaced by a mock when testing
}

IncludeConfig specifies optional configuration for an Include

func DefaultIncludeConfig

func DefaultIncludeConfig() *IncludeConfig

DefaultIncludeConfig returns the default configuration options for an Include. Options may be overridden before passing to NewInclude

func (*IncludeConfig) Validate

func (cfg *IncludeConfig) Validate() error

Validate checks the configuration options and returns an error if any have invalid values.

type IncludeEvent

type IncludeEvent interface {
	// contains filtered or unexported methods
}

IncludeEvent is an event intended to advance the state of a include.

type IncludeState

type IncludeState interface {
	// contains filtered or unexported methods
}

IncludeState is the state of a include.

type Probe

type Probe[K kad.Key[K], A kad.Address[A]] struct {
	// contains filtered or unexported fields
}

The Probe state machine performs regular connectivity checks for nodes in a routing table.

The state machine is notified of a new entry in the routing table via the EventProbeAdd event. This adds the node to an internal list and sets a time for a check to be performed, based on the current time plus a configurable interval.

Connectivity checks are performed in time order, so older nodes are processed first. The connectivity check performed is the same as for the Include state machine: ask the node for closest nodes to itself and confirm that the node returns at least one node in the list of closer nodes. The state machine emits the StateProbeConnectivityCheck state when it wants to check the status of a node.

The state machine expects to be notified either with the EventProbeMessageResponse or the EventProbeMessageFailure events to determine the outcome of the check. If neither are received within a configurable timeout the node is marked as failed.

Nodes that receive a successful response have their next check time updated to the current time plus the configured [ProbeConfig.CheckInterval].

Nodes that fail a connectivity check, or are timed out, are removed from the routing table and from the list of nodes to check. The state machine emits the StateProbeNodeFailure state to notify callers of this event.

The state machine accepts a EventProbePoll event to check for outstanding work such as initiating a new check or timing out an existing one.

The EventProbeRemove event may be used to remove a node from the check list and from the routing table.

The state machine accepts the EventProbeNotifyConnectivity event as a notification that an external system has performed a suitable connectivity check, such as when the node responds to a query. The probe state machine treats these events as if a successful response had been received from a check by advancing the time of the next check.

func NewProbe

func NewProbe[K kad.Key[K], A kad.Address[A]](rt RoutingTableCpl[K, kad.NodeID[K]], cfg *ProbeConfig) (*Probe[K, A], error)

func (*Probe[K, A]) Advance

func (p *Probe[K, A]) Advance(ctx context.Context, ev ProbeEvent) ProbeState

Advance advances the state of the probe state machine by attempting to advance its query if running.

type ProbeConfig

type ProbeConfig struct {
	CheckInterval time.Duration // the minimum time interval between checks for a node
	Concurrency   int           // the maximum number of probe checks that may be in progress at any one time
	Timeout       time.Duration // the time to wait before terminating a check that is not making progress
	Clock         clock.Clock   // a clock that may be replaced by a mock when testing
}

ProbeConfig specifies optional configuration for a Probe

func DefaultProbeConfig

func DefaultProbeConfig() *ProbeConfig

DefaultProbeConfig returns the default configuration options for a Probe. Options may be overridden before passing to NewProbe

func (*ProbeConfig) Validate

func (cfg *ProbeConfig) Validate() error

Validate checks the configuration options and returns an error if any have invalid values.

type ProbeEvent

type ProbeEvent interface {
	// contains filtered or unexported methods
}

ProbeEvent is an event intended to advance the state of a probe.

type ProbeState

type ProbeState interface {
	// contains filtered or unexported methods
}

ProbeState is the state of the Probe state machine.

type RoutingTableCpl

type RoutingTableCpl[K kad.Key[K], N kad.NodeID[K]] interface {
	kad.RoutingTable[K, N]

	// Cpl returns the longest common prefix length the supplied key shares with the table's key.
	Cpl(kk K) int

	// CplSize returns the number of nodes in the table whose longest common prefix with the table's key is of length cpl.
	CplSize(cpl int) int
}

type StateBootstrapFinished

type StateBootstrapFinished struct {
	Stats query.QueryStats
}

StateBootstrapFinished indicates that the bootstrap has finished.

type StateBootstrapIdle

type StateBootstrapIdle struct{}

StateBootstrapIdle indicates that the bootstrap is not running its query.

type StateBootstrapMessage

type StateBootstrapMessage[K kad.Key[K], A kad.Address[A]] struct {
	QueryID    query.QueryID
	NodeID     kad.NodeID[K]
	ProtocolID address.ProtocolID
	Message    kad.Request[K, A]
	Stats      query.QueryStats
}

StateBootstrapMessage indicates that the bootstrap query is waiting to message a node.

type StateBootstrapTimeout

type StateBootstrapTimeout struct {
	Stats query.QueryStats
}

StateBootstrapTimeout indicates that the bootstrap query has timed out.

type StateBootstrapWaiting

type StateBootstrapWaiting struct {
	Stats query.QueryStats
}

StateBootstrapWaiting indicates that the bootstrap query is waiting for a response.

type StateIncludeFindNodeMessage

type StateIncludeFindNodeMessage[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A] // the node to send the mssage to
}

StateIncludeFindNodeMessage indicates that the include subsystem is waiting to send a find node message a node. A find node message should be sent to the node, with the target being the node's key.

type StateIncludeIdle

type StateIncludeIdle struct{}

StateIncludeIdle indicates that the include is not running its query.

type StateIncludeRoutingUpdated

type StateIncludeRoutingUpdated[K kad.Key[K], A kad.Address[A]] struct {
	NodeInfo kad.NodeInfo[K, A]
}

StateIncludeRoutingUpdated indicates the routing table has been updated with a new node.

type StateIncludeWaitingAtCapacity

type StateIncludeWaitingAtCapacity struct{}

StateIncludeWaitingAtCapacity indicates that the include subsystem is waiting for responses for checks and that the maximum number of concurrent checks has been reached.

type StateIncludeWaitingFull

type StateIncludeWaitingFull struct{}

StateIncludeWaitingFull indicates that the include subsystem is waiting for responses for checks and that the maximum number of queued candidates has been reached.

type StateIncludeWaitingWithCapacity

type StateIncludeWaitingWithCapacity struct{}

StateIncludeWaitingWithCapacity indicates that the include subsystem is waiting for responses for checks but has capacity to perform more.

type StateProbeConnectivityCheck

type StateProbeConnectivityCheck[K kad.Key[K]] struct {
	NodeID kad.NodeID[K] // the node to send the message to
}

StateProbeConnectivityCheck indicates that the probe subsystem is waiting to send a connectivity check to a node. A find node message should be sent to the node, with the target being the node's key.

type StateProbeIdle

type StateProbeIdle struct{}

StateProbeIdle indicates that the probe state machine is not running any checks.

type StateProbeNodeFailure

type StateProbeNodeFailure[K kad.Key[K]] struct {
	NodeID kad.NodeID[K]
}

StateProbeNodeFailure indicates a node has failed a connectivity check been removed from the routing table and the probe list

type StateProbeWaitingAtCapacity

type StateProbeWaitingAtCapacity struct{}

StateProbeWaitingAtCapacity indicates that the probe state machine is waiting for responses for checks and the maximum number of concurrent checks has been reached.

type StateProbeWaitingWithCapacity

type StateProbeWaitingWithCapacity struct{}

StateProbeWaitingWithCapacity indicates that the probe state machine is waiting for responses for checks but has capacity to perform more.

Directories

Path Synopsis
Package triert provides a routing table implemented using a XOR trie.
Package triert provides a routing table implemented using a XOR trie.

Jump to

Keyboard shortcuts

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