|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This repo is `libp2p_node`, the Go implementation of a node for the Agent Communication Network (ACN). ACN lets agents (see `open-aea`) discover each other and exchange messages addressed solely by wallet address. The binary is a single entry point (`libp2p_node.go`, `package main`, module `libp2p_node`) that wires together the AEA-side pipe and the libp2p-based DHT. |
| 8 | + |
| 9 | +## Common commands |
| 10 | + |
| 11 | +```bash |
| 12 | +make build # go build |
| 13 | +make test # full test suite with coverage (serial: -p 1, no inlining, no test cache) |
| 14 | +make race_test # same, with -race |
| 15 | +make lint # golines . -w && golangci-lint run |
| 16 | +make install # go get -v -t -d ./... |
| 17 | +make clean # remove binary + coverage.txt |
| 18 | +``` |
| 19 | + |
| 20 | +Run a single test: |
| 21 | +```bash |
| 22 | +go test -gcflags=-l -count=1 -v ./dht/dhtpeer -run TestRoutingAllToAllConnectClientsSamePeer |
| 23 | +``` |
| 24 | +`-gcflags=-l` disables inlining (required by `bou.ke/monkey`), and `-p 1` in `make test` forces serial package execution because tests bind fixed ports — keep those flags when running tests that touch networking. |
| 25 | + |
| 26 | +`run_acn_node_standalone.py` launches the built binary standalone (outside an AEA), reading env-file config; `--config-from-env` on the binary reads config from environment variables instead of an AEA pipe. See README for bootstrap/entry node env-file examples. |
| 27 | + |
| 28 | +## Architecture |
| 29 | + |
| 30 | +The node can run in one of two modes, selected in `libp2p_node.go:main` by whether a public URI is set: |
| 31 | + |
| 32 | +- **Peer mode** (`dht/dhtpeer`) — full libp2p host. Runs the DHT, optionally a relay service, a delegate service (TCP endpoint for `p2p_libp2p_client` connections), an optional mailbox service, and optional Prometheus monitoring. Constructed via functional options (`dht/dhtpeer/options.go`). |
| 33 | +- **Client mode** (`dht/dhtclient`) — lightweight, bootstraps from entry peers, no public address. Used when `AEA_P2P_URI_PUBLIC` is unset. |
| 34 | + |
| 35 | +Both modes implement the `dhtnode.DHTNode` interface (`dht/dhtnode/dhtnode.go`): `RouteEnvelope`, `ProcessEnvelope`, `MultiAddr`, `PeerID`, `Close`. `main` connects the AEA pipe to the node by (a) forwarding envelopes from `agent.Queue()` into `node.RouteEnvelope`, and (b) registering `agent.Put` as the node's inbound envelope handler. |
| 36 | + |
| 37 | +Key packages: |
| 38 | + |
| 39 | +- `aea/` — the AEA-facing API. `api.go` handles config (env or pipe-based), `envelope.proto`/`envelope.pb.go` is the wire format, `pipe.go` is the Unix-pipe transport to a co-located Python AEA. |
| 40 | +- `dht/dhtnode/` — shared stream protocol handlers and the `DHTNode` interface. The ACN libp2p protocol IDs (`/aea/0.1.0`, `/aea-address/0.1.0`, `/aea-register/0.1.0`) are defined here. |
| 41 | +- `dht/dhtpeer/` — peer implementation; `mailbox.go` is the store-and-forward mailbox service, `notifee.go` hooks libp2p connection events, `benchmarks_test.go` contains throughput benchmarks. |
| 42 | +- `dht/dhtclient/` — client implementation. |
| 43 | +- `dht/dhttests/` — shared test fixtures/harness (imported by other `dht/*` tests). |
| 44 | +- `dht/common/`, `dht/monitoring/` — shared helpers and the Prometheus monitoring abstraction. |
| 45 | +- `acn/`, `protocols/`, `utils/`, `common/` — ACN-level helpers, generated protocol messages, logging and crypto utilities. |
| 46 | +- `mocks/` — gomock-generated mocks (see https://github.com/golang/mock for regeneration). |
| 47 | + |
| 48 | +### Messaging patterns |
| 49 | + |
| 50 | +ACN supports several delivery paths between an AEA `Connection` and a `Peer`, via optional `Delegate Client` and `Relay Peer` hops — see README.md "Messaging patterns" for the full matrix. ACN must guarantee total ordering of messages between any pair of agents regardless of which path is used. |
| 51 | + |
| 52 | +## Go / tooling notes |
| 53 | + |
| 54 | +- Go module: `libp2p_node` (go 1.17). Internal imports use the module path, e.g. `libp2p_node/dht/dhtpeer`. |
| 55 | +- Dependencies are pinned to older libp2p (`go-libp2p v0.8.3`, `go-libp2p-core v0.5.3`, `go-libp2p-kad-dht v0.7.11`) — do not casually bump these; the DHT protocol and stream APIs differ substantially in newer versions. |
| 56 | +- `golines` reformats long lines as part of `make lint`; run it before committing Go changes. |
0 commit comments