dht

package
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2021 License: MIT Imports: 33 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Network         = "udp4"
	DefaultPort     = 4444
	DefaultPeerPort = 3333

	DefaultAnnounceRate   = 10               // send at most this many announces per second
	DefaultReannounceTime = 50 * time.Minute // should be a bit less than hash expiration time

)

Variables

This section is empty.

Functions

func NodeFinderUseLogger

func NodeFinderUseLogger(l *logrus.Logger)

func RoutingTableRefresh

func RoutingTableRefresh(n *Node, refreshInterval time.Duration, parentGrp *stop.Group)

RoutingTableRefresh refreshes any buckets that need to be refreshed

func TestingCreateNetwork

func TestingCreateNetwork(t *testing.T, numNodes int, bootstrap, concurrent bool) (*BootstrapNode, []*DHT)

TestingCreateNetwork initializes a testable DHT network with a specific number of nodes, with bootstrap and concurrent options.

func UseLogger

func UseLogger(l *logrus.Logger)

Types

type BootstrapNode

type BootstrapNode struct {
	Node
	// contains filtered or unexported fields
}

BootstrapNode is a configured node setup for testing.

func NewBootstrapNode

func NewBootstrapNode(id bits.Bitmap, initialPingInterval, rePingInterval time.Duration) *BootstrapNode

NewBootstrapNode returns a BootstrapNode pointer.

func (*BootstrapNode) Add

func (b *BootstrapNode) Add(c Contact)

Add manually adds a contact

func (*BootstrapNode) Connect

func (b *BootstrapNode) Connect(conn UDPConn) error

Connect connects to the given connection and starts any background threads necessary

type Config

type Config struct {
	// this node's address. format is `ip:port`
	Address string
	// the seed nodes through which we can join in dht network
	SeedNodes []string
	// the hex-encoded node id for this node. if string is empty, a random id will be generated
	NodeID string
	// print the state of the dht every X time
	PrintState time.Duration
	// the port that clients can use to download blobs using the LBRY peer protocol
	PeerProtocolPort int
	// if nonzero, an RPC server will listen to requests on this port and respond to them
	RPCPort int
	// the time after which the original publisher must reannounce a key/value pair
	ReannounceTime time.Duration
	// send at most this many announces per second
	AnnounceRate int
	// channel that will receive notifications about announcements
	AnnounceNotificationCh chan announceNotification
}

Config represents the configure of dht.

func NewStandardConfig

func NewStandardConfig() *Config

NewStandardConfig returns a Config pointer with default values.

type Contact

type Contact struct {
	ID       bits.Bitmap
	IP       net.IP
	Port     int // the udp port used for the dht
	PeerPort int // the tcp port a peer can be contacted on for blob requests
}

Contact contains information for contacting another node on the network

func FindContacts

func FindContacts(node *Node, target bits.Bitmap, findValue bool, parentGrp *stop.Group) ([]Contact, bool, error)

func (Contact) Addr

func (c Contact) Addr() *net.UDPAddr

Addr returns the address of the contact.

func (Contact) Equals

func (c Contact) Equals(other Contact, checkID bool) bool

Equals returns true if two contacts are the same.

func (Contact) MarshalBencode

func (c Contact) MarshalBencode() ([]byte, error)

MarshalBencode returns the serialized byte slice representation of a contact.

func (Contact) MarshalCompact

func (c Contact) MarshalCompact() ([]byte, error)

MarshalCompact returns a compact byteslice representation of the contact NOTE: The compact representation always uses the tcp PeerPort, not the udp Port. This is dumb, but that's how the python daemon does it

func (Contact) String

func (c Contact) String() string

String returns a short string representation of the contact

func (*Contact) UnmarshalBencode

func (c *Contact) UnmarshalBencode(b []byte) error

UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the contact.

func (*Contact) UnmarshalCompact

func (c *Contact) UnmarshalCompact(b []byte) error

UnmarshalCompact unmarshals the compact byteslice representation of a contact. NOTE: The compact representation always uses the tcp PeerPort, not the udp Port. This is dumb, but that's how the python daemon does it

type DHT

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

DHT represents a DHT node.

func New

func New(config *Config) *DHT

New returns a DHT pointer. If config is nil, then config will be set to the default config.

func (*DHT) Add

func (dht *DHT) Add(hash bits.Bitmap)

Add adds the hash to the list of hashes this node is announcing

func (*DHT) Get

func (dht *DHT) Get(hash bits.Bitmap) ([]Contact, error)

Get returns the list of nodes that have the blob for the given hash

func (DHT) ID

func (dht DHT) ID() bits.Bitmap

func (*DHT) Ping

func (dht *DHT) Ping(addr string) error

Ping pings a given address, creates a temporary contact for sending a message, and returns an error if communication fails.

func (*DHT) PrintState

func (dht *DHT) PrintState()

PrintState prints the current state of the DHT including address, nr outstanding transactions, stored hashes as well as current bucket information.

func (*DHT) Remove

func (dht *DHT) Remove(hash bits.Bitmap)

Remove removes the hash from the list of hashes this node is announcing

func (*DHT) Shutdown

func (dht *DHT) Shutdown()

Shutdown shuts down the dht

func (*DHT) Start

func (dht *DHT) Start() error

Start starts the dht

func (*DHT) WaitUntilJoined

func (dht *DHT) WaitUntilJoined()

WaitUntilJoined blocks until the node joins the network.

type Error

type Error struct {
	ID            messageID
	NodeID        bits.Bitmap
	ExceptionType string
	Response      []string
}

Error represents a DHT error response

func (Error) MarshalBencode

func (e Error) MarshalBencode() ([]byte, error)

MarshalBencode returns the serialized byte slice representation of an error message.

func (*Error) UnmarshalBencode

func (e *Error) UnmarshalBencode(b []byte) error

UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the error message.

type Message

type Message interface {
	bencode.Marshaler
}

Message is a DHT message

type Node

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

Node is a type representation of a node on the network.

func NewNode

func NewNode(id bits.Bitmap) *Node

NewNode returns an initialized Node's pointer.

func (*Node) AddKnownNode

func (n *Node) AddKnownNode(c Contact)

AddKnownNode adds a known-good node to the routing table

func (*Node) Connect

func (n *Node) Connect(conn UDPConn) error

Connect connects to the given connection and starts any background threads necessary

func (*Node) CountActiveTransactions

func (n *Node) CountActiveTransactions() int

CountActiveTransactions returns the number of transactions in the manager

func (*Node) Send

func (n *Node) Send(contact Contact, req Request, options ...SendOptions) *Response

Send sends a transaction and blocks until the response is available. It returns a response, or nil if the transaction timed out.

func (*Node) SendAsync

func (n *Node) SendAsync(contact Contact, req Request, options ...SendOptions) <-chan *Response

SendAsync sends a transaction and returns a channel that will eventually contain the transaction response The response channel is closed when the transaction is completed or times out.

func (*Node) Shutdown

func (n *Node) Shutdown()

Shutdown shuts down the node

func (*Node) Store

func (n *Node) Store(hash bits.Bitmap, c Contact)

Store stores a node contact in the node's contact store.

type Request

type Request struct {
	ID              messageID
	NodeID          bits.Bitmap
	Method          string
	Arg             *bits.Bitmap
	StoreArgs       *storeArgs
	ProtocolVersion int
}

Request represents a DHT request message

func (Request) MarshalBencode

func (r Request) MarshalBencode() ([]byte, error)

MarshalBencode returns the serialized byte slice representation of the request

func (*Request) UnmarshalBencode

func (r *Request) UnmarshalBencode(b []byte) error

UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the request.

type RequestHandlerFunc

type RequestHandlerFunc func(addr *net.UDPAddr, request Request)

RequestHandlerFunc is exported handler for requests.

type Response

type Response struct {
	ID              messageID
	NodeID          bits.Bitmap
	Data            string
	Contacts        []Contact
	FindValueKey    string
	Token           string
	ProtocolVersion int
}

Response represents a DHT response message

func (Response) MarshalBencode

func (r Response) MarshalBencode() ([]byte, error)

MarshalBencode returns the serialized byte slice representation of the response.

func (*Response) UnmarshalBencode

func (r *Response) UnmarshalBencode(b []byte) error

UnmarshalBencode unmarshals the serialized byte slice into the appropriate fields of the store arguments.

type RpcBucketResponse

type RpcBucketResponse struct {
	Start       string
	End         string
	NumContacts int
	Contacts    []Contact
}

type RpcFindArgs

type RpcFindArgs struct {
	Key    string
	NodeID string
	IP     string
	Port   int
}

type RpcFindValueResult

type RpcFindValueResult struct {
	Contacts []Contact
	Value    string
}

type RpcIterativeFindValueArgs

type RpcIterativeFindValueArgs struct {
	Key string
}

type RpcIterativeFindValueResult

type RpcIterativeFindValueResult struct {
	Contacts   []Contact
	FoundValue bool
}

type RpcPingArgs

type RpcPingArgs struct {
	Address string
}

type RpcRoutingTableResponse

type RpcRoutingTableResponse struct {
	NodeID     string
	NumBuckets int
	Buckets    []RpcBucketResponse
}

type SendOptions

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

SendOptions controls the behavior of send calls

type UDPConn

type UDPConn interface {
	ReadFromUDP([]byte) (int, *net.UDPAddr, error)
	WriteToUDP([]byte, *net.UDPAddr) (int, error)
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
	Close() error
}

UDPConn allows using a mocked connection to test sending/receiving data TODO: stop mocking this and use the real thing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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