discover

package
v0.0.0-...-88de051 Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: Apache-2.0 Imports: 31 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

Constants

View Source
const DELIMITER = "\n\r"
View Source
const MAX_BUF_LEN = 1024 * 16
View Source
const NodeIDBits = 512
View Source
const Version = 4

Variables

This section is empty.

Functions

func DistCmp

func DistCmp(target, a, b common.Hash) int

DistCmp compares the distances a->target and b->target. Returns -1 if a is closer to target, 1 if b is closer to target and 0 if they are equal.

func MakeEndpoint

func MakeEndpoint(addr *net.UDPAddr) rpcEndpoint

func NewNodeDB

func NewNodeDB(path string, version int, self NodeID) *nodeDB

newNodeDB creates a new node database for storing and retrieving infos about known peers in the network. If no path is given, an in-memory, temporary database is constructed.

func UUID

func UUID() []byte

Types

type Broadcast

type Broadcast struct {
	*Packet
}

广播针对所有节点的通知,把消息传递给每一个节点,最终所有节点信息一致

type IPacket

type IPacket interface {
	OnHandle(t *KRpc, tx *Tx) error
	Type() byte
	String() string
	Encode() ([]byte, error)
	Decode([]byte) error
	Expiration() int64
	Sign(*ecdsa.PrivateKey) ([]byte, error)
	Verify([]byte) error
}

type KBuffer

type KBuffer struct {
	Delim []byte

	sync.RWMutex
	// contains filtered or unexported fields
}

func (*KBuffer) Add

func (t *KBuffer) Add(key string, b []byte)

func (*KBuffer) Next

func (t *KBuffer) Next(key string) [][]byte

type KRpc

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

udp implements the RPC protocol.

func NewKRpc

func NewKRpc(priV *ecdsa.PrivateKey, lAddr string, nodeDBPath string, netRestrict *netutil.Netlist) (*KRpc, error)

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

func (*KRpc) Broadcast

func (t *KRpc) Broadcast(tx *Tx) error

func (*KRpc) FindNode

func (t *KRpc) FindNode(toId NodeID, toAddr *net.UDPAddr, target NodeID) ([]*Node, error)

findnode sends a findnode request to the given node and waits until the node has sent up to k neighbors.

func (*KRpc) Multicast

func (t *KRpc) Multicast(tx *Tx) error

func (*KRpc) Ping

func (t *KRpc) Ping(toId NodeID, toAddr *net.UDPAddr) error

ping sends a ping message to the given node and waits for a reply.

func (*KRpc) Unicast

func (t *KRpc) Unicast(tx *Tx) error

type Multicast

type Multicast struct {
	*Packet
	To []rpcEndpoint
}

多播用于服务之间的通信 中继用于信息的传递,把消息传递给指定的节点,最终目标节点收到消息 当本网络能够访问目标网络的时候,那么直接发送给目标网络,当本网络无法直接访问的时候,或者访问比较慢的时候 那么就把信息发送给其他的节点,然后中继传播

type NetType

type NetType byte

网络传播类型

const (
	UNICAST NetType = iota
	MULTICAST
	BROADCAST
)

type Node

type Node struct {
	IP  net.IP // len 4 for IPv4 or 16 for IPv6
	UDP 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 uint16) *Node

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

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) Incomplete

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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 BytesID

func BytesID(b []byte) (NodeID, error)

BytesID converts a byte slice to a NodeID

func HexID

func HexID(in string) (NodeID, error)

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

func MustBytesID

func MustBytesID(b []byte) NodeID

MustBytesID converts a byte slice to a NodeID. It panics if the byte slice is not a valid NodeID.

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 RecoverNodeID

func RecoverNodeID(hash, sig []byte) (id NodeID, err error)

recoverNodeID computes the public key used to sign the given hash from the signature.

func (NodeID) Bytes

func (n NodeID) Bytes() []byte

Bytes returns a byte slice representation of the NodeID

func (NodeID) GoString

func (n NodeID) GoString() string

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

func (NodeID) MarshalText

func (n NodeID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

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.

func (*NodeID) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Packet

type Packet struct {
	NID     NodeID
	Payload []byte
	From    rpcEndpoint
	N       uint8 //衰减次数,每传播一次,次数减少1
	// contains filtered or unexported fields
}

func (*Packet) Decode

func (p *Packet) Decode(data []byte) error

func (*Packet) Encode

func (p *Packet) Encode() ([]byte, error)

func (*Packet) Expiration

func (p *Packet) Expiration() int64

func (*Packet) OnHandle

func (p *Packet) OnHandle(t *KRpc, tx *Tx) error

func (*Packet) Sign

func (p *Packet) Sign(priV *ecdsa.PrivateKey) ([]byte, error)

func (*Packet) String

func (p *Packet) String() string

func (*Packet) Type

func (p *Packet) Type() byte

func (*Packet) Verify

func (p *Packet) Verify(sig []byte) error

type Packets

type Packets struct {
	// contains filtered or unexported fields
}
var PacketManager *Packets

func (*Packets) Add

func (t *Packets) Add(p IPacket) error

func (*Packets) Packet

func (t *Packets) Packet(id byte) IPacket

type Table

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

func ListenUDP

func ListenUDP(priV *ecdsa.PrivateKey, lAddr string, nodeDBPath string, netRestrict *netutil.Netlist) (*Table, error)

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

func (*Table) Close

func (tab *Table) Close()

Close terminates the network listener and flushes the node database.

func (*Table) LenTab

func (tab *Table) LenTab() (n int)

func (*Table) Lookup

func (tab *Table) Lookup(targetID NodeID) []*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) 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) 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.

type Tx

type Tx struct {
	Type    byte
	SignMsg []byte
	ID      []byte
	// contains filtered or unexported fields
}

func DecodeTx

func DecodeTx(data []byte) (*Tx, error)

func (*Tx) Encode

func (tx *Tx) Encode() ([]byte, error)

func (*Tx) String

func (tx *Tx) String() string

type Unicast

type Unicast struct {
	*Packet
	To rpcEndpoint
}

单播用于一个节点的通信

Jump to

Keyboard shortcuts

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