libp2p Host

The libp2p networking host

The XE host is a standard libp2p node configured for TCP transport with persistent identity, connection management, and multiple discovery mechanisms.

#Host Creation

go
func NewHost(ctx context.Context, port int, dataDir string, version string, enableRelay bool) (host.Host, error)
ParameterDescription
portTCP listen port (default: 9000)
dataDirDirectory for persistent data. If non-empty, the identity key is persisted here.
versionNode version string, used in the User-Agent header
enableRelayEnable relay and hole-punching support

The host listens on all interfaces:

text
/ip4/0.0.0.0/tcp/{port}

#Connection Manager

The connection manager keeps the number of active peer connections within bounds:

SettingValue
Low watermark100 peers
High watermark400 peers
Grace period1 minute

When the number of connections exceeds the high watermark, the connection manager begins pruning connections down toward the low watermark. New connections are not pruned within the grace period.

go
cm, err := connmgr.NewConnManager(100, 400, connmgr.WithGracePeriod(time.Minute))

#Persistent Identity

When dataDir is provided, the host generates an Ed25519 keypair on first run and persists it to {dataDir}/host.key. On subsequent starts, the key is loaded from disk, giving the node a stable peer ID across restarts.

text
{dataDir}/host.key    # Ed25519 private key (libp2p marshaled format, mode 0600)

If dataDir is empty (e.g., in tests), a new ephemeral identity is generated each time.

#User-Agent

The host identifies itself with:

text
xe/{version}

This appears in the libp2p identify protocol and can be used for version-aware peer selection.

#Relay Support

When enableRelay is true, the host enables three capabilities:

FeaturePurpose
EnableRelay()Accept relayed connections through other peers
EnableRelayService()Act as a relay for other peers
EnableHolePunching()NAT traversal via hole-punching

This allows nodes behind NAT or firewalls to participate in the network by routing connections through relay-capable peers.

#Discovery

#mDNS (Local Network)

mDNS discovery is started automatically via SetupDiscovery(). It uses the service tag xe-poc and connects to any discovered peer on the local network.

go
func SetupDiscovery(ctx context.Context, h host.Host) error

When a peer is found via mDNS, the node connects automatically. Peers with the same peer ID (self) are ignored.

#Bootstrap Peers

Bootstrap peers are specified via the -dial CLI flag as comma-separated multiaddrs:

shell
xe-node -dial "/ip4/1.2.3.4/tcp/9000/p2p/12D3KooW...,/ip4/5.6.7.8/tcp/9000/p2p/12D3KooW..."

The node dials each address on startup with a 10-second timeout per peer. If any connections fail, a background goroutine retries every 10 seconds until all bootstrap peers are connected.

diagram
Startup  │  ├─ Dial peer A ─── success  ├─ Dial peer B ─── fail  │  └─ Start retry loop (every 10s)       ├─ Check: A connected? yes, skip       ├─ Check: B connected? no, dial again       └─ All connected? → stop loop

#Kademlia DHT

The DHT provides network-wide peer discovery beyond the local network and bootstrap list. See DHT below.

#DHT Setup

go
func SetupDHT(ctx context.Context, h host.Host) (*dht.IpfsDHT, error)

The DHT is configured with:

SettingValue
ModeServer (participates in routing)
Protocol prefix/xe

The /xe protocol prefix ensures XE nodes form their own DHT, isolated from the public IPFS DHT. Server mode means the node stores and serves routing records, not just queries them.

After creation, Bootstrap() is called to populate the routing table from the existing peerstore.

The DHT is used by the Messenger to resolve peer IDs to addresses via FindPeer() when a target peer is not already known.

#Peer Dialing

The DialPeer function connects to a peer given a multiaddr string:

go
func DialPeer(ctx context.Context, h host.Host, addr string) error

The ParsePeerAddr helper extracts peer address info from a multiaddr:

go
func ParsePeerAddr(addr string) (*peer.AddrInfo, error)

#Configuration Summary

CLI FlagDefaultDescription
-port9000TCP listen port for libp2p
-data./dataData directory (identity key stored here)
-dial(none)Comma-separated bootstrap multiaddrs

#Lifecycle

  1. Create host -- NewHost() sets up TCP transport, connection manager, and identity
  2. Start mDNS -- SetupDiscovery() enables local network discovery
  3. Bootstrap DHT -- SetupDHT() creates and bootstraps the Kademlia DHT
  4. Dial bootstrap peers -- bootstrapWithRetry() connects to known peers with retry
  5. Register protocols -- Sync, messaging, and gossip handlers are registered on the host
  6. Shutdown -- Host is closed, all streams and connections are terminated