kademlia

package module
v0.0.0-...-b7ff38c Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: MIT Imports: 19 Imported by: 11

README

kademlia

GoDoc Go Report Card Build Status MIT licensed

This is a Go implementation of a vanilla Kademlia DHT. The implementation is based off of a combination of the original Kademlia whitepaper and the xlattice design specification. It does not attempt to conform to BEP-5, or any other BitTorrent-specific design.

This project has not been heavily battle-tested, and I would not recommend using it in any production environment at this time.

Implementation characteristics

  • uses uTP for all network communication
  • supports IPv4/IPv6
  • uses a well-defined Store interface for extensibility
  • supports STUN for public address discovery

TODO

  • Implement STUN for public address discovery
  • Load testing/Benchmarks
  • More testing around message validation
  • More testing of bad/malicious message handling
  • Banning/throttling of malicious messages/nodes
  • Implement UDP hole punching for NAT traversal
  • Use loose parallelism for iterative lookups
  • Consider breaking store into two messages and transfer bulk of data over TCP
  • Implement republishing according to the xlattice design document
  • Better cleanup of unanswered expected messages
  • Logging support

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DHT

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

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

func NewDHT

func NewDHT(store Store, options *Options) (*DHT, error)

NewDHT initializes a new DHT node. A store and options struct must be provided.

func (*DHT) Bootstrap

func (dht *DHT) Bootstrap() error

Bootstrap attempts to bootstrap the network using the BootstrapNodes provided to the Options struct. This will trigger an iterativeFindNode to the provided BootstrapNodes.

func (*DHT) CreateSocket

func (dht *DHT) CreateSocket() error

CreateSocket attempts to open a UDP socket on the port provided to options

func (*DHT) Disconnect

func (dht *DHT) Disconnect() error

Disconnect will trigger a disconnect from the network. All underlying sockets will be closed.

func (*DHT) Get

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

Get retrieves data from the networking using key. Key is the base58 encoded identifier of the data.

func (*DHT) GetNetworkAddr

func (dht *DHT) GetNetworkAddr() string

GetNetworkAddr returns the publicly accessible IP and Port of the local node

func (*DHT) GetSelfID

func (dht *DHT) GetSelfID() string

GetSelfID returns the base58 encoded identifier of the local node

func (*DHT) Listen

func (dht *DHT) Listen() error

Listen begins listening on the socket for incoming messages

func (*DHT) NumNodes

func (dht *DHT) NumNodes() int

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

func (*DHT) Store

func (dht *DHT) Store(data []byte) (id string, err error)

Store stores data on the network. This will trigger an iterateStore message. The base58 encoded identifier will be returned if the store is successful.

type MemoryStore

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

MemoryStore is a simple in-memory key/value store used for unit testing, and the CLI example

func (*MemoryStore) Delete

func (ms *MemoryStore) Delete(key []byte)

Delete deletes a key/value pair from the MemoryStore

func (*MemoryStore) ExpireKeys

func (ms *MemoryStore) ExpireKeys()

ExpireKeys should expire all key/values due for expiration.

func (*MemoryStore) GetAllKeysForReplication

func (ms *MemoryStore) GetAllKeysForReplication() [][]byte

GetAllKeysForReplication should return the keys of all data to be replicated across the network. Typically all data should be replicated every tReplicate seconds.

func (*MemoryStore) GetKey

func (ms *MemoryStore) GetKey(data []byte) []byte

GetKey returns the key for data

func (*MemoryStore) Init

func (ms *MemoryStore) Init()

Init initializes the Store

func (*MemoryStore) Retrieve

func (ms *MemoryStore) Retrieve(key []byte) (data []byte, found bool)

Retrieve will return the local key/value if it exists

func (*MemoryStore) Store

func (ms *MemoryStore) Store(key []byte, data []byte, replication time.Time, expiration time.Time, publisher bool) error

Store will store a key/value pair for the local node with the given replication and expiration times.

type NetworkNode

type NetworkNode struct {
	// ID is a 20 byte unique identifier
	ID []byte

	// IP is the IPv4 address of the node
	IP net.IP

	// Port is the port of the node
	Port int
}

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

func NewNetworkNode

func NewNetworkNode(ip string, port string) *NetworkNode

NewNetworkNode creates a new NetworkNode for bootstrapping

type Options

type Options struct {
	ID []byte

	// The local IPv4 or IPv6 address
	IP string

	// The local port to listen for connections on
	Port string

	// Whether or not to use the STUN protocol to determine public IP and Port
	// May be necessary if the node is behind a NAT
	UseStun bool

	// Specifies the the host of the STUN server. If left empty will use the
	// default specified in go-stun.
	StunAddr string

	// A logger interface
	Logger log.Logger

	// The nodes being used to bootstrap the network. Without a bootstrap
	// node there is no way to connect to the network. NetworkNodes can be
	// initialized via dht.NewNetworkNode()
	BootstrapNodes []*NetworkNode

	// The time after which a key/value pair expires;
	// this is a time-to-live (TTL) from the original publication date
	TExpire time.Duration

	// Seconds after which an otherwise unaccessed bucket must be refreshed
	TRefresh time.Duration

	// The interval between Kademlia replication events, when a node is
	// required to publish its entire database
	TReplicate time.Duration

	// The time after which the original publisher must
	// republish a key/value pair. Currently not implemented.
	TRepublish time.Duration

	// The maximum time to wait for a response from a node before discarding
	// it from the bucket
	TPingMax time.Duration

	// The maximum time to wait for a response to any message
	TMsgTimeout time.Duration
}

Options contains configuration options for the local node

type Store

type Store interface {
	// Store should store a key/value pair for the local node with the
	// given replication and expiration times.
	Store(key []byte, data []byte, replication time.Time, expiration time.Time, publisher bool) error

	// Retrieve should return the local key/value if it exists.
	Retrieve(key []byte) (data []byte, found bool)

	// Delete should delete a key/value pair from the Store
	Delete(key []byte)

	// Init initializes the Store
	Init()

	// GetAllKeysForReplication should return the keys of all data to be
	// replicated across the network. Typically all data should be
	// replicated every tReplicate seconds.
	GetAllKeysForReplication() [][]byte

	// ExpireKeys should expire all key/values due for expiration.
	ExpireKeys()

	// GetKey returns the key for data
	GetKey(data []byte) []byte
}

Store is the interface for implementing the storage mechanism for the DHT.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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