kbucket

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2019 License: MIT Imports: 16 Imported by: 294

README

go-libp2p-kbucket

Discourse posts

A kbucket implementation for use as a routing table in go-libp2p-kad-dht

Documenation

See https://godoc.org/github.com/libp2p/go-libp2p-kbucket.

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the libp2p Code of Conduct.

Want to hack on libp2p?

License

MIT


The last gx published version of this module was: 2.2.23: QmSNE1XryoCMnZCbRaj1D23k6YKCaTQ386eJciu1pAfu8M

Documentation

Overview

package kbucket implements a kademlia 'k-bucket' routing table.

Index

Constants

This section is empty.

Variables

View Source
var ErrLookupFailure = errors.New("failed to find any peer in table")

Returned if a routing table query returns no results. This is NOT expected behaviour

View Source
var ErrPeerRejectedHighLatency = errors.New("peer rejected; latency too high")
View Source
var ErrPeerRejectedNoCapacity = errors.New("peer rejected; insufficient capacity")

Functions

func Closer

func Closer(a, b peer.ID, key string) bool

Closer returns true if a is closer to key than b is

func CommonPrefixLen added in v0.1.0

func CommonPrefixLen(a, b ID) int

func SortClosestPeers

func SortClosestPeers(peers []peer.ID, target ID) []peer.ID

Sort the given peers by their ascending distance from the target. A new slice is returned.

Types

type Bucket

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

Bucket holds a list of peers.

func (*Bucket) Has

func (b *Bucket) Has(id peer.ID) bool

func (*Bucket) Len

func (b *Bucket) Len() int

func (*Bucket) MoveToFront

func (b *Bucket) MoveToFront(id peer.ID)

func (*Bucket) Peers

func (b *Bucket) Peers() []peer.ID

func (*Bucket) PopBack

func (b *Bucket) PopBack() peer.ID

func (*Bucket) PushFront

func (b *Bucket) PushFront(p peer.ID)

func (*Bucket) Remove

func (b *Bucket) Remove(id peer.ID) bool

func (*Bucket) Split

func (b *Bucket) Split(cpl int, target ID) *Bucket

Split splits a buckets peers into two buckets, the methods receiver will have peers with CPL equal to cpl, the returned bucket will have peers with CPL greater than cpl (returned bucket has closer peers)

type CplRefresh added in v0.2.3

type CplRefresh struct {
	Cpl           uint
	LastRefreshAt time.Time
}

CplRefresh contains a CPL(common prefix length) with the host & the last time we refreshed that cpl/searched for an ID which has that cpl with the host.

type ID

type ID []byte

ID for IpfsDHT is in the XORKeySpace

The type dht.ID signifies that its contents have been hashed from either a peer.ID or a util.Key. This unifies the keyspace

func ConvertKey

func ConvertKey(id string) ID

ConvertKey creates a DHT ID by hashing a local key (String)

func ConvertPeerID

func ConvertPeerID(id peer.ID) ID

ConvertPeerID creates a DHT ID by hashing a Peer ID (Multihash)

type RoutingTable

type RoutingTable struct {

	// kBuckets define all the fingers to other nodes.
	Buckets []*Bucket

	// notification functions
	PeerRemoved func(peer.ID)
	PeerAdded   func(peer.ID)
	// contains filtered or unexported fields
}

RoutingTable defines the routing table.

func NewRoutingTable

func NewRoutingTable(bucketsize int, localID ID, latency time.Duration, m peerstore.Metrics) *RoutingTable

NewRoutingTable creates a new routing table with a given bucketsize, local ID, and latency tolerance.

func (*RoutingTable) Find

func (rt *RoutingTable) Find(id peer.ID) peer.ID

Find a specific peer by ID or return nil

func (*RoutingTable) GenRandPeerID added in v0.2.1

func (rt *RoutingTable) GenRandPeerID(targetCpl uint) (peer.ID, error)

GenRandPeerID generates a random peerID for a given Cpl

func (*RoutingTable) GetTrackedCplsForRefresh added in v0.2.3

func (rt *RoutingTable) GetTrackedCplsForRefresh() []CplRefresh

GetTrackedCplsForRefresh returns the Cpl's we are tracking for refresh. Caller is free to modify the returned slice as it is a defensive copy.

func (*RoutingTable) ListPeers

func (rt *RoutingTable) ListPeers() []peer.ID

ListPeers takes a RoutingTable and returns a list of all peers from all buckets in the table.

func (*RoutingTable) NearestPeer

func (rt *RoutingTable) NearestPeer(id ID) peer.ID

NearestPeer returns a single peer that is nearest to the given ID

func (*RoutingTable) NearestPeers

func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID

NearestPeers returns a list of the 'count' closest peers to the given ID

func (*RoutingTable) Print

func (rt *RoutingTable) Print()

Print prints a descriptive statement about the provided RoutingTable

func (*RoutingTable) Remove

func (rt *RoutingTable) Remove(p peer.ID)

Remove deletes a peer from the routing table. This is to be used when we are sure a node has disconnected completely.

func (*RoutingTable) ResetCplRefreshedAtForID added in v0.2.3

func (rt *RoutingTable) ResetCplRefreshedAtForID(id ID, newTime time.Time)

ResetCplRefreshedAtForID resets the refresh time for the Cpl of the given ID.

func (*RoutingTable) Size

func (rt *RoutingTable) Size() int

Size returns the total number of peers in the routing table

func (*RoutingTable) Update

func (rt *RoutingTable) Update(p peer.ID) (evicted peer.ID, err error)

Update adds or moves the given peer to the front of its respective bucket

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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