PeerNet.Registry (PeerNet v0.1.0)

Copy Markdown View Source

Per-instance registry of peer state — pubkey → {connection, last-seen address, status} — plus the auto-connect dispatcher.

Sits between PeerNet.Discovery (which emits "this pubkey is at this address") and PeerNet.Connection.Supervisor (which spins up sockets). When a discovery event arrives for a pubkey in the trust list, the Registry dials the address; the resulting Connection registers itself back here once authenticated.

State per peer

%{
  pubkey: <<...>>,
  status: :unknown | :discovered | :connecting | :connected | :disconnected,
  last_address: %{ip: ..., port: ...} | nil,
  last_seen: DateTime.t() | nil,
  conn_pid: pid() | nil
}

Public API

Discovery integration

Receives {:peer_discovered, pubkey, address} and {:peer_lost, pubkey, _} messages from any PeerNet.Discovery implementation. Trust-checks the pubkey before acting.

Summary

Functions

Returns a specification to start this module under a supervisor.

True iff there's a live connection for pubkey.

Return all known peer entries.

Look up the live connection for pubkey.

Register pid as the live connection for pubkey. Idempotent.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

connected?(server \\ __MODULE__, pubkey)

@spec connected?(GenServer.server(), binary()) :: boolean()

True iff there's a live connection for pubkey.

list(server \\ __MODULE__)

@spec list(GenServer.server()) :: [map()]

Return all known peer entries.

lookup_connection(server \\ __MODULE__, pubkey)

@spec lookup_connection(GenServer.server(), binary()) :: {:ok, pid()} | :not_connected

Look up the live connection for pubkey.

register_connection(server \\ __MODULE__, pubkey, pid)

@spec register_connection(GenServer.server(), binary(), pid()) :: :ok

Register pid as the live connection for pubkey. Idempotent.

start_link(opts)