discover

package
v0.0.0-...-1df7544 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: GPL-3.0 Imports: 35 Imported by: 0

Documentation

Overview

Package discover implements the Node Discovery Protocol.

The Node Discovery protocol provides a way to find RLPx nodes that can be connected to. It uses a Kademlia-like protocol to maintain a distributed database of the IDs and endpoints of all listening nodes.

Index

Examples

Constants

View Source
const (
	UnknownNode = 0
	AlienNode   = 1
	UncleNode   = 2 // old nodes that does not support STORE and FINDVALUE udp message
	BrotherNode = 3 // new nodes that does support STORE and FINDVALUE udp message
)

enum for node type, order number means more information learned about the node

View Source
const (
	BrotherOnly           = 0 // send only to brother node
	EitherUncleAndBrother = 1 // send to brother and uncle node
	AllExceptAlien        = 2 // send to brother, uncle and unknown node
)
View Source
const (
	PINGPACKET = iota + 1 // zero is 'reserved'
	PONGPACKET
	FINDNODEPACKET
	NEIGHBORSPACKET
	STOREPACKET
	STOREREPLYPACKET
	FINDVALUEPACKET
	FINDVALUEREPLYPACKET
)

RPC packet types

View Source
const BootNodeLimits = 5
View Source
const (

	// exported
	KvstoreCacheUpdateInterval = 3 * time.Minute
)
View Source
const NodeIDBits = 512
View Source
const Version = 4

Variables

View Source
var ShowToPublic bool
View Source
var VnodeBeneficialAddress *common.Address
View Source
var VnodeServiceCfg *string

Functions

This section is empty.

Types

type BootNodeCacheItem

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

type Node

type Node struct {
	IP       net.IP // len 4 for IPv4 or 16 for IPv6
	UDP, TCP uint16 // port numbers
	ID       NodeID // the node's public key
	// contains filtered or unexported fields
}

Node represents a host on the network. The fields of Node may not be modified.

func MustParseNode

func MustParseNode(rawurl string) *Node

MustParseNode parses a node URL. It panics if the URL is not valid.

func NewNode

func NewNode(id NodeID, ip net.IP, udpPort, tcpPort uint16, benefialAddress *common.Address, serviceCfg *string, showToPublic bool, ipString *string) *Node

NewNode creates a new node. It is mostly meant to be used for testing purposes.

Example
id := MustHexID("1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439")

// Complete nodes contain UDP and TCP endpoints:
n1 := NewNode(id, net.ParseIP("2001:db8:3c4d:15::abcd:ef12"), 52150, 30303, nil, nil, false)
fmt.Println("n1:", n1)
fmt.Println("n1.Incomplete() ->", n1.Incomplete())

// An incomplete node can be created by passing zero values
// for all parameters except id.
n2 := NewNode(id, nil, 0, 0, nil, nil, false)
fmt.Println("n2:", n2)
fmt.Println("n2.Incomplete() ->", n2.Incomplete())
Output:

n1: enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439@[2001:db8:3c4d:15::abcd:ef12]:30303?discport=52150
n1.Incomplete() -> false
n2: enode://1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439
n2.Incomplete() -> true

func ParseNode

func ParseNode(rawurl string) (*Node, error)

ParseNode parses a node designator.

There are two basic forms of node designators

  • incomplete nodes, which only have the public key (node ID)
  • complete nodes, which contain the public key and IP/Port information

For incomplete nodes, the designator must look like one of these

enode://<hex node id>
<hex node id>

For complete nodes, the node ID is encoded in the username portion of the URL, separated from the host by an @ sign. The hostname can only be given as an IP address, DNS domain names are not allowed. The port in the host name section is the TCP listening port. If the TCP and UDP (discovery) ports differ, the UDP port is specified as query parameter "discport".

In the following example, the node URL describes a node with IP address 10.3.58.6, TCP listening port 30303 and UDP discovery port 30301.

enode://<hex node id>@10.3.58.6:30303?discport=30301

func (*Node) Addr

func (n *Node) Addr() *net.UDPAddr

func (*Node) GetBeneficialAddress

func (n *Node) GetBeneficialAddress() *common.Address

func (*Node) GetIp

func (n *Node) GetIp() string

func (*Node) GetServiceCfg

func (n *Node) GetServiceCfg() *string

func (*Node) Incomplete

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) IsShowToPublic

func (n *Node) IsShowToPublic() bool

func (*Node) MarshalText

func (n *Node) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Node) SetBeneficialAddress

func (n *Node) SetBeneficialAddress(address *common.Address)

func (*Node) SetIp

func (n *Node) SetIp(ip *string)

func (*Node) SetServiceCfg

func (n *Node) SetServiceCfg(cfg *string)

func (*Node) SetSha

func (n *Node) SetSha()

func (*Node) SetShowToPublic

func (n *Node) SetShowToPublic(showToPublic bool)

func (*Node) String

func (n *Node) String() string

The string representation of a Node is a URL. Please see ParseNode for a description of the format.

func (*Node) UnmarshalText

func (n *Node) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type NodeID

type NodeID [NodeIDBits / 8]byte

NodeID is a unique identifier for each node. The node identifier is a marshaled elliptic curve public key.

func HexID

func HexID(in string) (NodeID, error)

HexID converts a hex string to a NodeID. The string may be prefixed with 0x.

func MustHexID

func MustHexID(in string) NodeID

MustHexID converts a hex string to a NodeID. It panics if the string is not a valid NodeID.

func PubkeyID

func PubkeyID(pub *ecdsa.PublicKey) NodeID

PubkeyID returns a marshaled representation of the given public key.

func (NodeID) GoString

func (n NodeID) GoString() string

The Go syntax representation of a NodeID is a call to HexID.

func (NodeID) Pubkey

func (id NodeID) Pubkey() (*ecdsa.PublicKey, error)

Pubkey returns the public key represented by the node ID. It returns an error if the ID is not a point on the curve.

func (NodeID) String

func (n NodeID) String() string

NodeID prints as a long hexadecimal number.

func (NodeID) TerminalString

func (n NodeID) TerminalString() string

TerminalString returns a shortened hex string for terminal logging.

type NodesByDistance

type NodesByDistance struct {
	Target common.Hash
	// contains filtered or unexported fields
}

NodesByDistance is a list of nodes, ordered by distance to target.

func (*NodesByDistance) GetEntries

func (h *NodesByDistance) GetEntries() []*Node

push adds the given node to the list, keeping the total size below maxElems.

func (*NodesByDistance) Push

func (h *NodesByDistance) Push(n *Node, maxElems int)

push adds the given node to the list, keeping the total size below maxElems.

type Table

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

func ListenUDP

func ListenUDP(
	priv *ecdsa.PrivateKey,
	laddr string,
	natm nat.Interface,
	nodeDBPath string,
	netrestrict *netutil.Netlist,
	networkid uint64,
	strictNodeCheck bool,
) (*Table, error)

ListenUDP returns a new table that listens for UDP packets on laddr.

func (*Table) Bond

func (tab *Table) Bond(pinged bool, id NodeID, addr *net.UDPAddr, tcpPort uint16) (*Node, error)

Public version of the bond() below

func (*Table) Close

func (tab *Table) Close()

Close terminates the network listener and flushes the node database.

func (*Table) DeleteWithNodeId

func (tab *Table) DeleteWithNodeId(id NodeID)

func (*Table) DumpNodeTypes

func (tab *Table) DumpNodeTypes() *gocache.Cache

GetNodeType return if a node is a moac node

func (*Table) FindBootNodes

func (tab *Table) FindBootNodes(subnetID NodeID, toNodes []*Node)

func (*Table) GetAllNodes

func (tab *Table) GetAllNodes() []*Node

func (*Table) GetKey

func (tab *Table) GetKey(key []byte) (map[string]string, error)

GetKey() get the value from the kvstore using the provided key

func (*Table) GetNodeType

func (tab *Table) GetNodeType(id NodeID) int

GetNodeType return if a node is a moac node

func (*Table) GetOurEndpoint

func (tab *Table) GetOurEndpoint() string

return endpoint from udp class in the format of ip:udpport

func (*Table) GetSubnetBootnodeKey

func (tab *Table) GetSubnetBootnodeKey(subnet string) string

GetSubnetBootnodeKey generates the key for the subnet used in tab.kvstore

func (*Table) Lookup

func (tab *Table) Lookup(
	targetID NodeID, lookupID int, strictNodeCheck bool,
) []*Node

Lookup performs a network search for nodes close to the given target. It approaches the target by querying nodes that are closer to it on each iteration. The given target does not need to be an actual node identifier.

func (*Table) NodeTypeKey

func (tab *Table) NodeTypeKey(id NodeID) string

NodeTypeKey() generates the key used in the nodeTypes cache

func (*Table) NodeTypeSize

func (tab *Table) NodeTypeSize() int

SetNodeType set if a node is a moac node

func (*Table) ReadRandomNodes

func (tab *Table) ReadRandomNodes(buf []*Node) (n int)

ReadRandomNodes fills the given slice with random nodes from the table. It will not write the same node more than once. The nodes in the slice are copies and can be modified by the caller.

func (*Table) RefreshSubnetBootNode

func (tab *Table) RefreshSubnetBootNode(subnetID NodeID, nodesToRefresh []*Node)

func (*Table) Resolve

func (tab *Table) Resolve(targetID NodeID) *Node

Resolve searches for a specific node with the given ID. It returns nil if the node could not be found.

func (*Table) Self

func (tab *Table) Self() *Node

Self returns the local node. The returned node should not be modified by the caller.

func (*Table) SetFallbackNodes

func (tab *Table) SetFallbackNodes(nodes []*Node) error

SetFallbackNodes sets the initial points of contact. These nodes are used to connect to the network if the table is empty and there are no known nodes in the database.

func (*Table) SetKey

func (tab *Table) SetKey(key []byte, value []byte, fromID NodeID) bool

SetKey() set the key/value to the kvstore fromID is used to double check node ID and print out debug information

func (*Table) SetNodeType

func (tab *Table) SetNodeType(id NodeID, flag int) error

SetNodeType set if a node is a moac node

func (*Table) SetNodeTypeStr

func (tab *Table) SetNodeTypeStr(id string, flag int) error

Jump to

Keyboard shortcuts

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