node

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package node implements the Node representation for discv4.

Unlike discv5 nodes which track sessions and ENR sequences, discv4 nodes track bond status and last seen timestamps for the bond mechanism.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Distance

func Distance(a, b ID) []byte

Distance calculates the XOR distance between two node IDs.

This is used for Kademlia-style routing (if needed by higher layers).

func DistanceCmp

func DistanceCmp(target, a, b ID) int

DistanceCmp compares the distance from target to a vs b.

Returns:

  • negative if distance(target, a) < distance(target, b)
  • zero if distance(target, a) == distance(target, b)
  • positive if distance(target, a) > distance(target, b)

Types

type BondStatus

type BondStatus int

BondStatus represents the bonding state of a node.

const (
	// BondStatusUnknown means we have never interacted with this node
	BondStatusUnknown BondStatus = iota

	// BondStatusPingSent means we sent a PING and are waiting for PONG
	BondStatusPingSent

	// BondStatusBonded means we have completed a successful PING/PONG exchange
	BondStatusBonded

	// BondStatusExpired means the bond has expired (24 hours since last PONG)
	BondStatusExpired
)

func (BondStatus) String

func (s BondStatus) String() string

String returns the string representation of bond status.

type ID

type ID [32]byte

ID represents a node identifier (32-byte Keccak256 hash of public key).

func PubkeyToID

func PubkeyToID(pubKey *ecdsa.PublicKey) ID

PubkeyToID converts a public key to a node ID.

The node ID is the Keccak256 hash of the uncompressed public key (without the 0x04 prefix).

type Node

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

Node represents a discv4 node with bond tracking.

The bond mechanism in discv4 requires nodes to complete a PING/PONG exchange before they can send FINDNODE requests. This prevents amplification attacks.

func FromENR

func FromENR(record *enr.Record, addr *net.UDPAddr) (*Node, error)

FromENR creates a new node from an ENR record.

func FromEnode

func FromEnode(en *enode.Enode) (*Node, error)

FromEnode creates a new node from an enode:// URL.

func New

func New(pubKey *ecdsa.PublicKey, addr *net.UDPAddr) *Node

New creates a new discv4 node from a public key and UDP address.

func ParseEnode

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

ParseEnode creates a node from an enode:// URL string.

func (*Node) Addr

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

Addr returns the node's UDP address.

func (*Node) BondExpiration

func (n *Node) BondExpiration() time.Time

BondExpiration returns when the current bond expires.

func (*Node) BondStatus

func (n *Node) BondStatus() BondStatus

BondStatus returns the current bond status.

func (*Node) ConsecutiveTimeouts

func (n *Node) ConsecutiveTimeouts() uint32

ConsecutiveTimeouts returns the number of consecutive timeouts.

func (*Node) ENR

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

ENR returns the node's ENR record, if available.

func (*Node) Enode

func (n *Node) Enode() *enode.Enode

Enode returns the node's enode:// representation.

func (*Node) FailedPings

func (n *Node) FailedPings() uint32

FailedPings returns the number of failed ping attempts.

func (*Node) ID

func (n *Node) ID() ID

ID returns the node identifier.

func (*Node) IDBytes

func (n *Node) IDBytes() []byte

IDBytes returns the node ID as a byte slice.

func (*Node) IncrementPacketsReceived

func (n *Node) IncrementPacketsReceived()

IncrementPacketsReceived increments the received packet counter.

func (*Node) IncrementPacketsSent

func (n *Node) IncrementPacketsSent()

IncrementPacketsSent increments the sent packet counter.

func (*Node) IsBonded

func (n *Node) IsBonded() bool

IsBonded returns true if the node has a valid bond.

func (*Node) LastPingSent

func (n *Node) LastPingSent() time.Time

LastPingSent returns when we last sent a PING.

func (*Node) LastPongReceived

func (n *Node) LastPongReceived() time.Time

LastPongReceived returns when we last received a PONG.

func (*Node) LastSeen

func (n *Node) LastSeen() time.Time

LastSeen returns when we last saw a packet from this node.

func (*Node) MarkPingReceived

func (n *Node) MarkPingReceived()

MarkPingReceived records that we received a PING from this node.

func (*Node) MarkPingSent

func (n *Node) MarkPingSent()

MarkPingSent records that we sent a PING to this node.

func (*Node) MarkPongReceived

func (n *Node) MarkPongReceived(bondDuration time.Duration)

MarkPongReceived records that we received a PONG from this node.

This establishes or renews the bond.

func (*Node) MarkPongSent

func (n *Node) MarkPongSent()

MarkPongSent records that we sent a PONG to this node.

func (*Node) MarkTimeout

func (n *Node) MarkTimeout()

MarkTimeout records a timeout (failed to receive expected response).

func (*Node) PublicKey

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

PublicKey returns the node's public key.

func (*Node) SetAddr

func (n *Node) SetAddr(addr *net.UDPAddr)

SetAddr updates the node's address.

This is used when we receive packets from a different address than expected.

func (*Node) SetENR

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

SetENR updates the node's ENR record.

func (*Node) SetStats

func (n *Node) SetStats(sharedStats *stats.SharedStats)

SetStats replaces the node's stats with a shared stats pointer. This allows the node to update stats owned by a parent node.

func (*Node) String

func (n *Node) String() string

String returns a human-readable representation of the node.

func (*Node) TotalPacketsReceived

func (n *Node) TotalPacketsReceived() uint64

TotalPacketsReceived returns the total packets received from this node.

func (*Node) TotalPacketsSent

func (n *Node) TotalPacketsSent() uint64

TotalPacketsSent returns the total packets sent to this node.

func (*Node) UpdateLastSeen

func (n *Node) UpdateLastSeen()

UpdateLastSeen updates the last seen timestamp.

Jump to

Keyboard shortcuts

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