kademlia

package
v1.0.0-rc2.17 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Package kademlia provides an implementation of the topology.Driver interface in a way that a kademlia connectivity is actively maintained by the node.

A thorough explanation of the logic in the `manage()` forever loop: The `manageC` channel gets triggered every time there's a change in the information regarding peers we know about. This can be a result of: (1) A peer has disconnected from us (2) A peer has been added to the list of known peers (from discovery, debugapi, bootnode flag or just because it was persisted in the address book and the node has been restarted).

So the information has been changed, and potentially upon disconnection, the depth can travel to a shallower depth in result. If a peer gets added through AddPeers, this does not necessarily infer an immediate depth change, since the peer might end up in the backlog for a long time until we actually need to connect to her.

The `manage()` forever-loop will connect to peers in order from shallower to deeper depths. This is because of depth calculation method that prioritizes empty bins That are shallower than depth. An in-depth look at `recalcDepth()` method will clarify this (more below). So if we will connect to peers from deeper to shallower depths, all peers in all bins will qualify as peers we'd like to connect to (see `binSaturated` method), ending up connecting to everyone we know about.

Another important notion one must observe while inspecting how `manage()` works, is that when we connect to peers depth can only move in one direction, which is deeper. So this becomes our strategy and we operate with this in mind, this is also why we iterate from shallower to deeper - since additional connections to peers for whatever reason can only result in increasing depth.

Empty intermediate bins should be eliminated by the `binSaturated` method indicating a bin size too short, which in turn means that connections should be established within this bin. Empty bins have special status in terms of depth calculation and as mentioned before they are prioritized over deeper, non empty bins and they constitute as the node's depth when the latter is recalculated. For the rationale behind this please refer to the appropriate chapters in the book of Swarm.

A special case of the `manage()` functionality is that when we iterate over peers and we come across a peer that has PO >= depth, we would always like to connect to that peer. This should always be enforced within the bounds of the `binSaturated` function and guarantees an ever increasing kademlia depth in an ever-increasing size of Swarm, resulting in smaller areas of responsibility for the nodes, maintaining a general upper bound of the assigned nominal area of responsibility in terms of actual storage requirement. See book of Swarm for more details.

Worth to note is that `manage()` will always try to initiate connections when a bin is not saturated, however currently it will not try to eliminate connections on bins which might be over-saturated. Ideally it should be very cheap to maintain a connection to a peer in a bin, so we should theoretically not aspire to eliminate connections prematurely. It is also safe to assume we will always have more than the lower bound of peers in a bin, why? (1) Initially, we will always try to satisfy our own connectivity requirement to saturate the bin (2) Later on, other peers will get notified about our advertised address and will try to connect to us in order to satisfy their own connectivity thresholds

We should allow other nodes to dial in, in order to help them maintain a healthy topolgy. It could be, however, that we would need to mark-and-sweep certain connections once a theorical upper bound has been reached.

Depth calculation explained: When we calculate depth we must keep in mind the following constraints: (1) A nearest-neighborhood constitutes of an arbitrary lower bound of the closest peers we know about, this is defined in `nnLowWatermark` and is currently set to `2` (2) Empty bins which are shallower than depth constitute as the node's area of responsibility

As of such, we would calculate depth in the following manner: (1) Iterate over all peers we know about, from deepest (closest) to shallowest, and count until we reach `nnLowWatermark` (2) Once we reach `nnLowWatermark`, mark current bin as depth candidate (3) Iterate over all bins from shallowest to deepest, and look for the shallowest empty bin (4) If the shallowest empty bin is shallower than the depth candidate - select shallowest bin as depth, otherwise select the candidate

Note: when we are connected to less or equal to `nnLowWatermark` peers, the depth will always be considered `0`, thus a short-circuit is handling this edge case explicitly in the `recalcDepth` method.

TODO: add pseudo-code how to calculate depth.

A few examples to depth calculation:

1. empty kademlia bin | nodes ------------- ==DEPTH== 0 0 1 0 2 0 3 0 4 0 depth: 0

2. less or equal to two peers (nnLowWatermark=2) (a) bin | nodes ------------- ==DEPTH== 0 1 1 1 2 0 3 0 4 0 depth: 0

3. less or equal to two peers (nnLowWatermark=2) (b) bin | nodes ------------- ==DEPTH== 0 1 1 0 2 1 3 0 4 0 depth: 0

4. less or equal to two peers (nnLowWatermark=2) (c) bin | nodes ------------- ==DEPTH== 0 2 1 0 2 0 3 0 4 0 depth: 0

5. empty shallow bin bin | nodes ------------- 0 1 ==DEPTH== 1 0 2 1 3 1 4 0 depth: 1 (depth candidate is 2, but 1 is shallower and empty)

6. no empty shallower bin, depth after nnLowerWatermark found bin | nodes ------------- 0 1 1 1 ==DEPTH== 2 1 3 1 4 0 depth: 2 (depth candidate is 2, shallowest empty bin is 4)

7. last bin size >= nnLowWatermark bin | nodes ------------- 0 1 1 1 2 1 ==DEPTH== 3 3 4 0 depth: 3 (depth candidate is 3, shallowest empty bin is 4)

8. all bins full bin | nodes ------------- 0 1 1 1 2 1 3 3 ==DEPTH== 4 2 depth: 4 (depth candidate is 4, no empty bins)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kad

type Kad struct {
	// contains filtered or unexported fields
}

Kad is the Swarm forwarding kademlia implementation.

func New

func New(
	base swarm.Address,
	addressbook addressbook.Interface,
	discovery discovery.Driver,
	p2p p2p.Service,
	metricsDB *shed.DB,
	logger logging.Logger,
	o Options,
) *Kad

New returns a new Kademlia.

func (*Kad) AddPeers

func (k *Kad) AddPeers(addrs ...swarm.Address)

AddPeers adds peers to the knownPeers list. This does not guarantee that a connection will immediately be made to the peer.

func (*Kad) Announce

func (k *Kad) Announce(ctx context.Context, peer swarm.Address, fullnode bool) error

Announce a newly connected peer to our connected peers, but also notify the peer about our already connected peers

func (*Kad) Close

func (k *Kad) Close() error

Close shuts down kademlia.

func (*Kad) ClosestPeer

func (k *Kad) ClosestPeer(addr swarm.Address, includeSelf bool, skipPeers ...swarm.Address) (swarm.Address, error)

ClosestPeer returns the closest peer to a given address.

func (*Kad) Connected

func (k *Kad) Connected(ctx context.Context, peer p2p.Peer) error

Connected is called when a peer has dialed in.

func (*Kad) Disconnected

func (k *Kad) Disconnected(peer p2p.Peer)

Disconnected is called when peer disconnects.

func (*Kad) EachNeighbor

func (k *Kad) EachNeighbor(f topology.EachPeerFunc) error

EachNeighbor iterates from closest bin to farthest of the neighborhood peers.

func (*Kad) EachNeighborRev

func (k *Kad) EachNeighborRev(f topology.EachPeerFunc) error

EachNeighborRev iterates from farthest bin to closest of the neighborhood peers.

func (*Kad) EachPeer

func (k *Kad) EachPeer(f topology.EachPeerFunc) error

EachPeer iterates from closest bin to farthest.

func (*Kad) EachPeerRev

func (k *Kad) EachPeerRev(f topology.EachPeerFunc) error

EachPeerRev iterates from farthest bin to closest.

func (*Kad) Halt

func (k *Kad) Halt()

Halt stops outgoing connections from happening. This is needed while we shut down, so that further topology changes do not happen while we shut down.

func (*Kad) IsBalanced

func (k *Kad) IsBalanced(bin uint8) bool

IsBalanced returns if Kademlia is balanced to bin.

func (*Kad) IsWithinDepth

func (k *Kad) IsWithinDepth(addr swarm.Address) bool

IsWithinDepth returns if an address is within the neighborhood depth of a node.

func (*Kad) Metrics

func (k *Kad) Metrics() []prometheus.Collector

Metrics returns set of prometheus collectors.

func (*Kad) NeighborhoodDepth

func (k *Kad) NeighborhoodDepth() uint8

NeighborhoodDepth returns the current Kademlia depth.

func (*Kad) Pick

func (k *Kad) Pick(peer p2p.Peer) bool

func (*Kad) SetRadius

func (k *Kad) SetRadius(r uint8)

func (*Kad) Snapshot

func (k *Kad) Snapshot() *topology.KadParams

func (*Kad) Start

func (k *Kad) Start(_ context.Context) error

func (*Kad) String

func (k *Kad) String() string

String returns a string represenstation of Kademlia.

func (*Kad) SubscribePeersChange

func (k *Kad) SubscribePeersChange() (c <-chan struct{}, unsubscribe func())

SubscribePeersChange returns the channel that signals when the connected peers set changes. Returned function is safe to be called multiple times.

type Options

type Options struct {
	SaturationFunc  binSaturationFunc
	Bootnodes       []ma.Multiaddr
	StandaloneMode  bool
	BootnodeMode    bool
	BitSuffixLength int
}

Options for injecting services to Kademlia.

Directories

Path Synopsis
internal
metrics
Package metrics provides service for collecting various metrics about peers.
Package metrics provides service for collecting various metrics about peers.
waitnext
Package metrics provides service for collecting various metrics about peers.
Package metrics provides service for collecting various metrics about peers.

Jump to

Keyboard shortcuts

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