go-ethereum: github.com/ethereum/go-ethereum/p2p/enode Index | Files

package enode

import "github.com/ethereum/go-ethereum/p2p/enode"


Package Files

idscheme.go iter.go localnode.go node.go nodedb.go urlv4.go


var ValidSchemes = enr.SchemeMap{
    "v4": V4ID{},

List of known secure identity schemes.

var ValidSchemesForTesting = enr.SchemeMap{
    "v4":   V4ID{},
    "null": NullID{},

func DistCmp Uses

func DistCmp(target, a, b ID) 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 LogDist Uses

func LogDist(a, b ID) int

LogDist returns the logarithmic distance between a and b, log2(a ^ b).

func SignV4 Uses

func SignV4(r *enr.Record, privkey *ecdsa.PrivateKey) error

SignV4 signs a record using the v4 scheme.

type DB Uses

type DB struct {
    // contains filtered or unexported fields

DB is the node database, storing previously seen nodes and any collected metadata about them for QoS purposes.

func OpenDB Uses

func OpenDB(path string) (*DB, error)

OpenDB opens a 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 (*DB) Close Uses

func (db *DB) Close()

close flushes and closes the database files.

func (*DB) DeleteNode Uses

func (db *DB) DeleteNode(id ID)

DeleteNode deletes all information associated with a node.

func (*DB) FindFails Uses

func (db *DB) FindFails(id ID, ip net.IP) int

FindFails retrieves the number of findnode failures since bonding.

func (*DB) FindFailsV5 Uses

func (db *DB) FindFailsV5(id ID, ip net.IP) int

FindFailsV5 retrieves the discv5 findnode failure counter.

func (*DB) LastPingReceived Uses

func (db *DB) LastPingReceived(id ID, ip net.IP) time.Time

LastPingReceived retrieves the time of the last ping packet received from a remote node.

func (*DB) LastPongReceived Uses

func (db *DB) LastPongReceived(id ID, ip net.IP) time.Time

LastPongReceived retrieves the time of the last successful pong from remote node.

func (*DB) Node Uses

func (db *DB) Node(id ID) *Node

Node retrieves a node with a given id from the database.

func (*DB) NodeSeq Uses

func (db *DB) NodeSeq(id ID) uint64

NodeSeq returns the stored record sequence number of the given node.

func (*DB) QuerySeeds Uses

func (db *DB) QuerySeeds(n int, maxAge time.Duration) []*Node

QuerySeeds retrieves random nodes to be used as potential seed nodes for bootstrapping.

func (*DB) Resolve Uses

func (db *DB) Resolve(n *Node) *Node

Resolve returns the stored record of the node if it has a larger sequence number than n.

func (*DB) UpdateFindFails Uses

func (db *DB) UpdateFindFails(id ID, ip net.IP, fails int) error

UpdateFindFails updates the number of findnode failures since bonding.

func (*DB) UpdateFindFailsV5 Uses

func (db *DB) UpdateFindFailsV5(id ID, ip net.IP, fails int) error

UpdateFindFailsV5 stores the discv5 findnode failure counter.

func (*DB) UpdateLastPingReceived Uses

func (db *DB) UpdateLastPingReceived(id ID, ip net.IP, instance time.Time) error

UpdateLastPingReceived updates the last time we tried contacting a remote node.

func (*DB) UpdateLastPongReceived Uses

func (db *DB) UpdateLastPongReceived(id ID, ip net.IP, instance time.Time) error

UpdateLastPongReceived updates the last pong time of a node.

func (*DB) UpdateNode Uses

func (db *DB) UpdateNode(node *Node) error

UpdateNode inserts - potentially overwriting - a node into the peer database.

type FairMix Uses

type FairMix struct {
    // contains filtered or unexported fields

FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends only when Close is called. Source iterators added via AddSource are removed from the mix when they end.

The distribution of nodes returned by Next is approximately fair, i.e. FairMix attempts to draw from all sources equally often. However, if a certain source is slow and doesn't return a node within the configured timeout, a node from any other source will be returned.

It's safe to call AddSource and Close concurrently with Next.

func NewFairMix Uses

func NewFairMix(timeout time.Duration) *FairMix

NewFairMix creates a mixer.

The timeout specifies how long the mixer will wait for the next fairly-chosen source before giving up and taking a node from any other source. A good way to set the timeout is deciding how long you'd want to wait for a node on average. Passing a negative timeout makes the mixer completely fair.

func (*FairMix) AddSource Uses

func (m *FairMix) AddSource(it Iterator)

AddSource adds a source of nodes.

func (*FairMix) Close Uses

func (m *FairMix) Close()

Close shuts down the mixer and all current sources. Calling this is required to release resources associated with the mixer.

func (*FairMix) Next Uses

func (m *FairMix) Next() bool

Next returns a node from a random source.

func (*FairMix) Node Uses

func (m *FairMix) Node() *Node

Node returns the current node.

type ID Uses

type ID [32]byte

ID is a unique identifier for each node.

func HexID Uses

func HexID(in string) ID

HexID converts a hex string to an ID. The string may be prefixed with 0x. It panics if the string is not a valid ID.

func ParseID Uses

func ParseID(in string) (ID, error)

func PubkeyToIDV4 Uses

func PubkeyToIDV4(key *ecdsa.PublicKey) ID

PubkeyToIDV4 derives the v4 node address from the given public key.

func (ID) Bytes Uses

func (n ID) Bytes() []byte

Bytes returns a byte slice representation of the ID

func (ID) GoString Uses

func (n ID) GoString() string

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

func (ID) MarshalText Uses

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

MarshalText implements the encoding.TextMarshaler interface.

func (ID) String Uses

func (n ID) String() string

ID prints as a long hexadecimal number.

func (ID) TerminalString Uses

func (n ID) TerminalString() string

TerminalString returns a shortened hex string for terminal logging.

func (*ID) UnmarshalText Uses

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

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Iterator Uses

type Iterator interface {
    Next() bool  // moves to next node
    Node() *Node // returns current node
    Close()      // ends the iterator

Iterator represents a sequence of nodes. The Next method moves to the next node in the sequence. It returns false when the sequence has ended or the iterator is closed. Close may be called concurrently with Next and Node, and interrupts Next if it is blocked.

func CycleNodes Uses

func CycleNodes(nodes []*Node) Iterator

CycleNodes makes an iterator which cycles through the given nodes indefinitely.

func Filter Uses

func Filter(it Iterator, check func(*Node) bool) Iterator

Filter wraps an iterator such that Next only returns nodes for which the 'check' function returns true.

func IterNodes Uses

func IterNodes(nodes []*Node) Iterator

IterNodes makes an iterator which runs through the given nodes once.

type LocalNode Uses

type LocalNode struct {
    // contains filtered or unexported fields

LocalNode produces the signed node record of a local node, i.e. a node run in the current process. Setting ENR entries via the Set method updates the record. A new version of the record is signed on demand when the Node method is called.

func NewLocalNode Uses

func NewLocalNode(db *DB, key *ecdsa.PrivateKey) *LocalNode

NewLocalNode creates a local node.

func (*LocalNode) Database Uses

func (ln *LocalNode) Database() *DB

Database returns the node database associated with the local node.

func (*LocalNode) Delete Uses

func (ln *LocalNode) Delete(e enr.Entry)

Delete removes the given entry from the local record.

func (*LocalNode) ID Uses

func (ln *LocalNode) ID() ID

ID returns the local node ID.

func (*LocalNode) Node Uses

func (ln *LocalNode) Node() *Node

Node returns the current version of the local node record.

func (*LocalNode) Seq Uses

func (ln *LocalNode) Seq() uint64

Seq returns the current sequence number of the local node record.

func (*LocalNode) Set Uses

func (ln *LocalNode) Set(e enr.Entry)

Set puts the given entry into the local record, overwriting any existing value. Use Set*IP and SetFallbackUDP to set IP addresses and UDP port, otherwise they'll be overwritten by the endpoint predictor.

func (*LocalNode) SetFallbackIP Uses

func (ln *LocalNode) SetFallbackIP(ip net.IP)

SetFallbackIP sets the last-resort IP address. This address is used if no endpoint prediction can be made and no static IP is set.

func (*LocalNode) SetFallbackUDP Uses

func (ln *LocalNode) SetFallbackUDP(port int)

SetFallbackUDP sets the last-resort UDP-on-IPv4 port. This port is used if no endpoint prediction can be made.

func (*LocalNode) SetStaticIP Uses

func (ln *LocalNode) SetStaticIP(ip net.IP)

SetStaticIP sets the local IP to the given one unconditionally. This disables endpoint prediction.

func (*LocalNode) UDPContact Uses

func (ln *LocalNode) UDPContact(toaddr *net.UDPAddr)

UDPContact should be called whenever the local node has announced itself to another node via UDP. It feeds the local endpoint predictor.

func (*LocalNode) UDPEndpointStatement Uses

func (ln *LocalNode) UDPEndpointStatement(fromaddr, endpoint *net.UDPAddr)

UDPEndpointStatement should be called whenever a statement about the local node's UDP endpoint is received. It feeds the local endpoint predictor.

type Node Uses

type Node struct {
    // contains filtered or unexported fields

Node represents a host on the network.

func MustParse Uses

func MustParse(rawurl string) *Node

MustParse parses a node record or enode:// URL. It panics if the input is invalid.

func MustParseV4 Uses

func MustParseV4(rawurl string) *Node

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

func New Uses

func New(validSchemes enr.IdentityScheme, r *enr.Record) (*Node, error)

New wraps a node record. The record must be valid according to the given identity scheme.

func NewV4 Uses

func NewV4(pubkey *ecdsa.PublicKey, ip net.IP, tcp, udp int) *Node

NewV4 creates a node from discovery v4 node information. The record contained in the node has a zero-length signature.

func Parse Uses

func Parse(validSchemes enr.IdentityScheme, input string) (*Node, error)

Parse decodes and verifies a base64-encoded node record.

func ParseV4 Uses

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

ParseV4 parses a node URL.

There are two basic forms of node URLs:

- 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 or using DNS domain name. 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, TCP listening port 30303 and UDP discovery port 30301.

enode://<hex node id>@

func ReadNodes Uses

func ReadNodes(it Iterator, n int) []*Node

ReadNodes reads at most n nodes from the given iterator. The return value contains no duplicates and no nil values. To prevent looping indefinitely for small repeating node sequences, this function calls Next at most n times.

func SignNull Uses

func SignNull(r *enr.Record, id ID) *Node

func (*Node) ID Uses

func (n *Node) ID() ID

ID returns the node identifier.

func (*Node) IP Uses

func (n *Node) IP() net.IP

IP returns the IP address of the node. This prefers IPv4 addresses.

func (*Node) Incomplete Uses

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) Load Uses

func (n *Node) Load(k enr.Entry) error

Load retrieves an entry from the underlying record.

func (*Node) MarshalText Uses

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

MarshalText implements encoding.TextMarshaler.

func (*Node) Pubkey Uses

func (n *Node) Pubkey() *ecdsa.PublicKey

Pubkey returns the secp256k1 public key of the node, if present.

func (*Node) Record Uses

func (n *Node) Record() *enr.Record

Record returns the node's record. The return value is a copy and may be modified by the caller.

func (*Node) Seq Uses

func (n *Node) Seq() uint64

Seq returns the sequence number of the underlying record.

func (*Node) String Uses

func (n *Node) String() string

String returns the text representation of the record.

func (*Node) TCP Uses

func (n *Node) TCP() int

UDP returns the TCP port of the node.

func (*Node) UDP Uses

func (n *Node) UDP() int

UDP returns the UDP port of the node.

func (*Node) URLv4 Uses

func (n *Node) URLv4() string

func (*Node) UnmarshalText Uses

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

UnmarshalText implements encoding.TextUnmarshaler.

func (*Node) ValidateComplete Uses

func (n *Node) ValidateComplete() error

ValidateComplete checks whether n has a valid IP and UDP port. Deprecated: don't use this method.

type NullID Uses

type NullID struct{}

NullID is the "null" ENR identity scheme. This scheme stores the node ID in the record without any signature.

func (NullID) NodeAddr Uses

func (NullID) NodeAddr(r *enr.Record) []byte

func (NullID) Verify Uses

func (NullID) Verify(r *enr.Record, sig []byte) error

type Secp256k1 Uses

type Secp256k1 ecdsa.PublicKey

Secp256k1 is the "secp256k1" key, which holds a public key.

func (*Secp256k1) DecodeRLP Uses

func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error

DecodeRLP implements rlp.Decoder.

func (Secp256k1) ENRKey Uses

func (v Secp256k1) ENRKey() string

func (Secp256k1) EncodeRLP Uses

func (v Secp256k1) EncodeRLP(w io.Writer) error

EncodeRLP implements rlp.Encoder.

type V4ID Uses

type V4ID struct{}

v4ID is the "v4" identity scheme.

func (V4ID) NodeAddr Uses

func (V4ID) NodeAddr(r *enr.Record) []byte

func (V4ID) Verify Uses

func (V4ID) Verify(r *enr.Record, sig []byte) error

Package enode imports 33 packages (graph) and is imported by 755 packages. Updated 2020-12-09. Refresh now. Tools for package owners.