enode

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: Apache-2.0 Imports: 35 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

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

List of known secure identity schemes.

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

Functions

func DistCmp

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.

used to determine which node is closer to the target node based on the node id

func LogDist

func LogDist(a, b ID) int

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

func SignV4

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

SignV4 signs a record using the v4 scheme. Node Record: set up public key, create signature and stored into node record

Types

type DB

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

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.

NOTE: if the db existed, it will open the db only, otherwise, it will create and then open the db. For the leveldb, the key is byte slice, and the blob is byte slice as well

func (*DB) Close

func (db *DB) Close()

close flushes and closes the database files.

func (*DB) DeleteNode

func (db *DB) DeleteNode(id ID) error

DeleteNode deletes all information/keys associated with a node. any key has n:nodeID as prefix

func (*DB) FindFails

func (db *DB) FindFails(id ID) int

FindFails retrieves the number of findnode failures since bonding.

func (*DB) LastPingReceived

func (db *DB) LastPingReceived(id ID) time.Time

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

func (*DB) LastPongReceived

func (db *DB) LastPongReceived(id ID) time.Time

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

func (*DB) Node

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

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

func (*DB) NodeSeq

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

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

func (*DB) QuerySeeds

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

QuerySeeds retrieves random nodes to be used as potential seed nodes for bootstrapping. n defines how many bootstrap nodes want to retrieve 5 * n defines the max number of loops can run, after this time, even if I did not get the desired number of nodes, still exit the for loop maxAge is used to determine if the node is active based on the last pong message received

func (*DB) Resolve

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

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

if the node sequence is greater than the node sequence stored in DB, return the original node otherwise, return the node from the db Resolve returns the node with newest node record sequence

func (*DB) UpdateFindFails

func (db *DB) UpdateFindFails(id ID, fails int) error

UpdateFindFails updates the number of findnode failures since bonding.

func (*DB) UpdateLastPingReceived

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

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

func (*DB) UpdateLastPongReceived

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

UpdateLastPongReceived updates the last pong time of a node.

func (*DB) UpdateNode

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

UpdateNode inserts - potentially overwriting - a node into the peer database. update both node record and node sequence number (node record is encoded by RLP) node sequence number is used to indicate if the record is the newest record

type ID

type ID [32]byte

ID is a unique identifier for each node.

func HexID

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 PubkeyToIDV4

func PubkeyToIDV4(key *ecdsa.PublicKey) ID

PubkeyToIDV4 derives the v4 node address from the given public key. based on the public key, get the node address, which is also the node id

func RandomID

func RandomID(a ID, n int) (b ID)

RandomID returns a random ID b such that logdist(a, b) == n.

func (ID) Bytes

func (n ID) Bytes() []byte

Bytes returns a byte slice representation of the ID

func (ID) GoString

func (n ID) GoString() string

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

func (ID) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface

Hex Encode the ID. hex.EncodeToString is equivalent to String() function

func (ID) String

func (n ID) String() string

ID prints as a long hexadecimal number. convert the ID from byte slice to hexadecimal string

func (ID) TerminalString

func (n ID) TerminalString() string

TerminalString returns a shortened hex string for terminal logging. encode and return the first 7 bytes to hexadecimal string

func (*ID) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface.

Parse the text passed in, update the ID with the one passed in

type LocalNode

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

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

NewLocalNode creates a local node. set the id = public key cur = nil Node key = private key set up updTracker and allocates space for entries

func (*LocalNode) Database

func (ln *LocalNode) Database() *DB

Database returns the node database associated with the local node.

func (*LocalNode) Delete

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

Delete removes the given entry from the local record.

func (*LocalNode) ID

func (ln *LocalNode) ID() ID

ID returns the local node ID.

func (*LocalNode) Node

func (ln *LocalNode) Node() *Node

Node returns current Node Object If the cur is pointed to a nil node then sign() function will be called to create new Node based on the information stored in the LocalNode object and the Node pointer will be stored in the cur object

func (*LocalNode) Set

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

Set puts the given entry into the local record, overwriting any existing value.

func (*LocalNode) SetFallbackIP

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

func (ln *LocalNode) SetFallbackUDP(port int)

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

func (*LocalNode) SetStaticIP

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

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

func (*LocalNode) UDPContact

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

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

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

Node represents a host on the network.

func MustParseV4

func MustParseV4(rawurl string) *Node

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

func New

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 (contains functions on verifying node signatures stored in node record, and deriving node addresses --> Creating and initializing new Node object (node record and node address)

func NewV4

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.

create node record and stores information inside create Node based on the information retrieved from the URL

func ParseV4

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, 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

node after parsing will have empty signature

func SignNull

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

set everything to null, including node ID, node record entries, node record signature

func (*Node) ID

func (n *Node) ID() ID

ID returns the node identifier.

func (*Node) IP

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

IP returns the IP address of the node.

func (*Node) Incomplete

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) Load

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

Load retrieves an entry from the underlying record. entry is a key value pair getting from Record.pair value is RLP encoded

func (*Node) MarshalText

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

MarshalText implements encoding.TextMarshaler.

Convert the node URL to byte slice

func (*Node) Pubkey

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

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

func (*Node) Record

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

func (n *Node) Seq() uint64

Seq returns the sequence number of the underlying record.

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.

Get the node's URL

func (*Node) TCP

func (n *Node) TCP() int

UDP returns the TCP port of the node.

func (*Node) UDP

func (n *Node) UDP() int

UDP returns the UDP port of the node.

func (*Node) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

Update the node to the new node created using Node URL

func (*Node) ValidateComplete

func (n *Node) ValidateComplete() error

checks whether n is a valid complete node. Complete Node Criteria: 1. IP address 2. UDP Port 3. IP cannot be multicast or unspecified 4. Public Key

type NullID

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

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

func (NullID) Verify

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

type Secp256k1

type Secp256k1 ecdsa.PublicKey

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

func (*Secp256k1) DecodeRLP

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

DecodeRLP implements rlp.Decoder.

func (Secp256k1) ENRKey

func (v Secp256k1) ENRKey() string

func (Secp256k1) EncodeRLP

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

EncodeRLP implements rlp.Encoder.

type V4ID

type V4ID struct{}

v4ID is the "v4" identity scheme.

func (V4ID) NodeAddr

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

Node address is derived from its' public key using Keccak256 encoding rule

func (V4ID) Verify

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

Jump to

Keyboard shortcuts

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