# Networking and peers

ahto nodes can form a closed peer-to-peer network, one per organization, where only nodes the organization signed in may connect.

This page says what that gives you today, how to set a network up, and what is not built yet.
The peer-to-peer node is off by default, and `ahto serve` without it opens no port besides
the gateway's.

## What it does today

A network is a set of ahto nodes that know each other by key. Each node has a key; its peer ID
(`12D3KooW...`) is the public half. Built on libp2p, the node:

- lets in only the peers it is told about: those in its own peer list, or those in the
  organization's signed membership log (below);
- finds the other members with a private DHT that only the network's own members answer, so a
  new node that knows one bootstrap address finds the rest;
- remembers where it last reached each member (`<id>.addrs` in `dir`), so after a restart it
  reaches them even when every bootstrap peer is down;
- trades the membership log with the peers it reaches, so an admit or a revoke made on one
  machine reaches every node;
- closes the connections of a peer the moment its revoke arrives;
- can ask the network's authority group to sign people in (see [identity](/docs/identity)).

A network never merges with another one: two organizations are two networks with two keys.

### Not built yet

ahto does not yet move lake data, repositories or build-cache blobs between peers. The network
carries membership and sign-in, not your code. These parts of the design are planned, and
nothing here depends on them:

- sharing lake snapshots and files between peers
  (RFC 0008, "Lake over P2P");
- fetching a build-cache blob from a peer: the store exists on one machine only
  (RFC 0013;
  see [checks](/docs/checks#a-go-build-cache));
- links between two organizations' networks;
- `ahto p2p status` and a peer-to-peer service on the gateway. Today the `node up` line in
  the log is the only report of the node, and there is no command that lists the peers
  connected right now.

## Set up a network

Two admission modes exist. A **list** network lets in the peers you name on each node. A
**members** network lets in whoever the organization's CA has signed into a membership log;
it is the one to use beyond two or three nodes. The steps below make a members network of a
server and a laptop. Each command was run against a build of this repository.

**1. Name the network on each node.** Put this in the config file of every node (`--config`)
and give each its own `dir`; the default is `~/.local/share/ahto/p2p`:

```yaml
p2p:
  enabled: true
  networks:
    - id: acme
```

**2. Make each node's key.** This prints the peer ID. Run it on every node:

```sh
ahto --config ahto.yaml p2p init
```

```text
network: acme
peer id: 12D3KooWCA7VfKm4oSECWgD5MGrjXMe65XXuZ1Aob76g4b1ts3aR
key: /home/you/.local/share/ahto/p2p/acme.key
...
```

**3. Make the CA, once, on the machine that will admit members.** Keep its key
(`acme.ca.key`) on that machine or offline; it signs every member.

```sh
ahto --config ahto.yaml p2p ca init
```

```text
network: acme
ca: ed25519:4cEDhM12KuTNTD0Hi81HC3G21cklJ88sxj8HmD6KkaE=
...
Put the ca line in every node's config to let in the members of this log:
```

**4. Admit each node, the bootstrap node first.** Use the peer IDs from step 2:

```sh
ahto --config ahto.yaml p2p admit 12D3KooWCA7V...  --name server --role bootstrap
ahto --config ahto.yaml p2p admit 12D3KooWQeoc...  --name laptop
```

```text
entry 2: admit 12D3KooWQeoc9DxL5mM3yamNkEhTB9tmazW1tv1XY6Grddr1xSFj (laptop), roles member, expires never
```

`--role` is `member`, `bootstrap`, `relay` or `client` (repeat it for several), and
`--expires` ends a membership: `720h`, `30d`, `2027-01-31` or an RFC 3339 time.

**5. Point every node at the CA and the bootstrap node.** Add the `ca:` line step 3 printed to
every node. Every node but the bootstrap one also names where to dial it, an address ending in
`/p2p/<peer id>`:

```yaml
p2p:
  enabled: true
  networks:
    - id: acme
      ca: ed25519:4cEDhM12KuTNTD0Hi81HC3G21cklJ88sxj8HmD6KkaE=
      bootstrap: ["/ip4/203.0.113.10/udp/4100/quic-v1/p2p/12D3KooWCA7VfKm4oSECWgD5MGrjXMe65XXuZ1Aob76g4b1ts3aR"]
```

The bootstrap node must be reachable: open UDP and TCP port 4100 to it.

**6. Start the nodes, the bootstrap node first.**

```sh
ahto --config ahto.yaml serve
```

```text
INF p2p: node up dht=true listen=["/p2p-circuit","/ip4/0.0.0.0/udp/4100/quic-v1","/ip4/0.0.0.0/tcp/4100"] mode=server network=acme peer=12D3KooW... peer_list=0
INF p2p: membership log updated entries=5 head=159336200a5d network=acme
```

The second line is the laptop taking the log from the server. Check it on any node:

```sh
ahto --config ahto.yaml p2p network
```

```text
network: acme
ca: ed25519:4cEDhM12KuTNTD0Hi81HC3G21cklJ88sxj8HmD6KkaE=
entries: 5, head 159336200a5dda0c3230acacdf5c45e211b9506bef502339a4fdbe8e67df701e

NAME    PEER                                                  ROLES      EXPIRES  STATE
laptop  12D3KooWQeoc9DxL5mM3yamNkEhTB9tmazW1tv1XY6Grddr1xSFj  member     never    live
server  12D3KooWCA7VfKm4oSECWgD5MGrjXMe65XXuZ1Aob76g4b1ts3aR  bootstrap  never    live
```

If a node logs `bootstrap did not answer ... this node is not on its list`, its peer ID is not
admitted yet, or was revoked: run `ahto p2p network` on the CA machine and look at its STATE.

### Add and remove members later

On the CA machine, `admit` a new peer, or change a peer's name, roles or expiry by admitting it
again, and `revoke` one that must go:

```sh
ahto p2p admit 12D3KooWNew...  --name build-server --expires 90d
ahto p2p revoke 12D3KooWQeoc...
ahto p2p publish           # hand the log to the bootstrap peers; or --to <multiaddress>
```

`publish` needs a `bootstrap` list, or `--to`; without one it says so. A node that reads its log
from a file takes in a change on `SIGHUP`. A revoked peer's connections close as the entry
arrives; the log keeps the revoke, since it is never trimmed.

### A list network, with no CA

For two machines you control, skip the CA. Each node lists the peers allowed to connect:

```yaml
p2p:
  enabled: true
  networks:
    - id: acme
      bootstrap: ["/ip4/203.0.113.10/tcp/4100/p2p/12D3KooWCA7V..."]
      peers:
        - {id: 12D3KooWCA7V..., name: server}
```

A peer with `enabled: false` is refused and its connections closed, also when it is a bootstrap
node or a member. `SIGHUP` reloads the list. A list network logs nothing when a peer connects,
so a wrong list shows only as a node that finds nobody.

### A pre-shared key

`ahto p2p psk` writes a key file. Only nodes that hold it can start a handshake, which hides the
node from anyone who scans for it. Copy it to every node by a safe route and set `psk_file`:

```sh
ahto --config ahto.yaml p2p psk        # writes <network id>.psk in p2p.dir; --out sets the file
```

A pre-shared key works over TCP only. A network with one listens on TCP alone, and its bootstrap
addresses must be `/tcp/` ones.

## Settings

Everything is under `p2p` and is off until `enabled: true`. `ahto config` prints the result.

| Key | Default | What it does |
|---|---|---|
| `enabled` | `false` | start the node with `ahto serve` (`AHTO_P2P_ENABLED`) |
| `mode` | `server` | `server` listens and answers; `client` only dials out |
| `dir` | `~/.local/share/ahto/p2p` | the keys, membership logs, members' last-known addresses and pre-shared keys |
| `listen` | QUIC and TCP on port 4100 | addresses a server listens on |
| `announce` | none | addresses to tell others about, in place of the ones the node finds |
| `dht` | `true` | the network's private DHT; `false` and a node reaches only peers it has addresses for |
| `nat.port_map`, `nat.autonat`, `nat.hole_punching` | `true` | ask the router for a port, ask peers whether an address is reachable, connect through NAT |
| `nat.relay_service` | `false` | relay for other members; set it on a bootstrap node |
| `limits.max_peers`, `limits.max_conns_per_ip` | `200`, `8` | how many connections the node keeps |
| `networks[].id` | | the network's name; its protocols start with `/ahto/<id>` |
| `networks[].ca` | | the CA's public key; with it the network is a members network |
| `networks[].admission` | `list`, or `members` with a `ca` | who is let in |
| `networks[].key_file`, `psk_file` | `<id>.key`, none | the node's key and the pre-shared key, in `dir` unless absolute |
| `networks[].bootstrap` | none | addresses dialed first |
| `networks[].peers` | none | the node's own list: who may connect, or who is disabled |
| `networks[].authority` | off | the node's part in the sign-in group; see [identity](/docs/identity) |
| `networks[].audit` | off | a daily copy of audit events for a log shipper; see [identity](/docs/identity#audit) |

`networks` is a list, so it comes from a YAML file, not from an environment variable. The first
build takes one network. The NAT keys are set but only the DHT and the admission rules are
proved by the design's tests; a node behind NAT across the internet is not yet reported by any
command.

## All the commands

| Command | What it does |
|---|---|
| `p2p init` | make this node's key for a network and print its peer ID |
| `p2p id` | print this node's peer ID |
| `p2p ca init` | make the CA key and the log's first entry; `--wait` waits for recovery to be ready |
| `p2p admit <peer id>` | sign a peer in, or change it (`--name`, `--role`, `--expires`) |
| `p2p revoke <peer id>` | sign a peer out |
| `p2p network` | print the CA, its fingerprint and the members |
| `p2p publish` | hand the log to the bootstrap peers, or `--to` |
| `p2p psk` | make a pre-shared key file |
| `p2p guardian list`, `set`, `rehearse`, `sign`, `cancel` | the guardians that sign the network's recovery ([identity](/docs/identity#recovery)) |

Each takes `--network <id>` when the config holds more than one. `admit`, `revoke` and `ca init`
take `--ca-key` for a CA key kept somewhere else than `<id>.ca.key` in `p2p.dir`.

The design is in RFC 0008.