routing

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const K = 16

Variables

This section is empty.

Functions

func BucketIndex

func BucketIndex(self, peer a2al.NodeID) int

BucketIndex is the K-bucket slot for peer relative to self: shared MSB prefix length in [0,255]. Returns -1 if peer equals self (spec §4.1, Step 5).

func LessXORDistance

func LessXORDistance(a, b, target a2al.NodeID) bool

LessXORDistance reports whether a is strictly closer to target than b under XOR metric (lexicographic compare of Distance(a,target) vs Distance(b,target)).

Types

type PeerDebugRow

type PeerDebugRow struct {
	Bucket        int    `json:"bucket"`
	XORDistToSelf string `json:"xor_distance_to_self_hex"`
	AddressHex    string `json:"address_hex"`
	NodeIDHex     string `json:"node_id_hex"`
	IP            string `json:"ip"`
	Port          uint16 `json:"port"`
}

PeerDebugRow is a JSON-friendly routing row (spec §3.6).

type PingFunc

type PingFunc func(protocol.NodeInfo) bool

PingFunc probes whether a candidate entry is still alive (spec Step 5).

type Table

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

Table is a 256-bucket Kademlia routing table (Phase 1: single LRU zone per bucket).

func NewTable

func NewTable(self a2al.NodeID, ping PingFunc) *Table

NewTable constructs an empty table for the local node. ping may be nil (treated as always true on eviction probe).

func (*Table) Add

func (t *Table) Add(n protocol.NodeInfo, trusted bool) bool

Add inserts or refreshes a peer. Returns false if the peer is self, invalid, or rejected when the bucket is full and the LRU peer answers PING. trusted must be true when the NodeInfo originates from direct communication (see bucket.addOrTouch for semantics).

func (*Table) AllPeers

func (t *Table) AllPeers() []protocol.NodeInfo

AllPeers returns every distinct peer in the table (unordered). Excludes self.

func (*Table) BucketIndexOf

func (t *Table) BucketIndexOf(peer a2al.NodeID) int

BucketIndexOf returns BucketIndex(t.self, peer).

func (*Table) Contains

func (t *Table) Contains(id a2al.NodeID) bool

Contains reports whether id is present in any bucket.

func (*Table) DebugPeerRows

func (t *Table) DebugPeerRows() []PeerDebugRow

DebugPeerRows returns a flat list of peers with bucket index and XOR distance to local self (read-only snapshot; caller must serialize table access if concurrent).

func (*Table) EstimatedNetworkSize added in v0.1.3

func (t *Table) EstimatedNetworkSize() int

EstimatedNetworkSize estimates the total number of nodes in the network using bucket density. For a bucket at CPL c with m nodes the estimate is m×2^(c+1). Returns the estimate from the highest non-empty bucket (least noisy); 0 if the table is empty.

func (*Table) Len

func (t *Table) Len() int

Len returns the total number of stored peers.

func (*Table) NearestN

func (t *Table) NearestN(target a2al.NodeID, n int) []protocol.NodeInfo

NearestN returns up to n peers with smallest XOR distance to target (excluding self). Results sorted closest-first.

func (*Table) OldestInBucket

func (t *Table) OldestInBucket(peer a2al.NodeID) (protocol.NodeInfo, bool)

OldestInBucket returns the LRU entry in the bucket that would hold peer (may be nil slice if empty).

func (*Table) PeerBucketLen

func (t *Table) PeerBucketLen(peer a2al.NodeID) int

PeerBucketLen is for tests / debug: number of entries in the bucket for peer's CPL slot.

func (*Table) Remove

func (t *Table) Remove(id a2al.NodeID)

Remove deletes a peer by NodeID if present.

func (*Table) Self

func (t *Table) Self() a2al.NodeID

Self returns the local NodeID.

Jump to

Keyboard shortcuts

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