A learning P2P network in Go: PoW-gossip of messages. A node takes an arbitrary string, mines a nonce (Proof of Work), and floods it to neighbours over TCP. Neighbours verify the PoW, store it in a mempool, and re-flood. There are no blocks or chain — this is PoW-validated gossip, not a blockchain.
Full design and original requirements: docs/spec.md.
One binary, split into packages, stdlib only. The dependency graph is acyclic; p2p
knows nothing about orchestration (it talks back through the Handler interface).
| Package | Responsibility |
|---|---|
cmd/node |
entrypoint: flags, validation, signals, graceful shutdown |
internal/message |
protocol frames (Frame/Hello/Peers/Message), hashing (ID, PoWHash) — leaf of the graph |
internal/consensus |
Proof of Work: Mine / Verify |
internal/mempool |
thread-safe store + dedup + FIFO eviction |
internal/p2p |
TCP transport: connections, handshake, peer exchange, flooding, degree limits |
internal/gossip |
dedup + verify + the decide-whether-to-reflood |
internal/node |
orchestration: wires it together, implements p2p.Handler, HTTP control |
message <- consensus <- gossip <- node -> p2p -> (Handler) node
mempool <-/ \-> mempool
Run: go run ./cmd/node (or make build → bin/node).
Node-to-node is raw TCP, one JSON frame per line (\n-delimited).
| Type | When | Purpose |
|---|---|---|
hello |
on connect | network_id (rejects foreign networks) + listen_addr |
peers |
reply to hello | known addresses — the network builds itself out |
message |
broadcast | a Message with PoW |
Dedup is by the locally computed id = sha256(text|timestamp). Mempool membership is the
"seen" set; it breaks flood loops.
make cluster # 3 nodes, chain node1<-node2<-node3
make cluster N=5 # 5 nodes
make cluster N=4 DIFF=6 # 4 nodes, heavier PoW (mining becomes visible)In another terminal:
curl -X POST localhost:10001/broadcast -d '{"text":"hello"}'
curl localhost:10003/mempool # message arrived via gossip
curl localhost:10003/peers # topology (node3 learned node1 through exchange)Ctrl-C brings the whole cluster down.
Default port is tcp-port + 1000 (flag -http).
| Method | Path | Does |
|---|---|---|
POST /broadcast |
{"text":"..."} |
mines PoW, stores, floods. Returns {id, hash, nonce} |
GET /mempool |
— | []Message — what arrived |
GET /peers |
— | []string of peer addresses |
| Flag | Default | Meaning |
|---|---|---|
-port |
9000 |
gossip TCP port |
-http |
port+1000 |
HTTP control port |
-peers |
"" |
comma-separated initial peer TCP addresses |
-difficulty |
4 |
leading hex zeros in PoW (0..8) |
-max-out |
0 |
outbound dial cap; 0 = unbounded |
-max-in |
0 |
inbound connection cap; 0 = unbounded |
-mempool-max |
100000 |
mempool size cap (FIFO eviction); 0 = unbounded |
-network |
pow-gossip-go |
network_id; rejects foreign networks |
A single node by hand:
go run ./cmd/node -port 9001 -http 10001
go run ./cmd/node -port 9002 -http 10002 -peers 127.0.0.1:9001A node's degree is its number of peers (graph edges), visible via GET /peers. With no
limits, peer exchange builds a full mesh: every node connects to every other (degree
N−1). That does not scale — with 1000 nodes each would hold 999 sockets.
-max-out / -max-in bound the degree (the Bitcoin model: outbound hard, inbound up to a
ceiling):
-max-out— how many connections a node initiates. Reserved atomically, so the cap actually bites even under the concurrent flood of peer exchange. Bootstrap dials (-peers) go first → the backbone stays intact → the graph stays connected.-max-in— how many inbound connections it accepts; extras are refused (inbound rejected).- A node's degree ≤
max-out + max-in.
./scripts/cluster.sh 16 4 0 0 # no limits -> full mesh, degree 15
./scripts/cluster.sh 16 4 3 8 # cap: degree <= 11, network stays connected and convergesNote: limits that are too low, with an unlucky topology, can split the network into
disconnected components (a partition) — gossip cannot jump the gap. max-in must be
comfortably above 1 so backbone edges are not refused. For the localhost demo out=3 in=8
keeps it connected.
make test # all packages, fast
make race # the same under the race detector
make lint # golangci-lintUnit — per package: PoW and tamper detection (text/ts/nonce/hash), difficulty=0,
difficulty threshold, empty text; msgID distinctness; mempool dedup and FIFO eviction;
gossip accept/reject/dup; atomic outbound reserve.
Integration (internal/node, in-process, also run under -race):
- convergence across a chain — a broadcast on one node reaches all others multi-hop;
- bounded fanout — 16 nodes,
max-out 3: the message reaches everyone and the degree does not blow up into a full mesh (the live review question: does it converge under a limit); - concurrent broadcast — N nodes broadcast at once, each ends with exactly N messages
(duplicates over paths are deduped), no races under
-race; - peer drop — the middle node leaves, delivery continues over a direct edge.
Deliberately out of scope: message signatures, on-disk persistence, blocks/chain/consensus,
Sybil/spam/replay protection, dial retries. Assumptions: localhost, identical flags across
nodes. Decisions and rejected alternatives are in docs/adr.md; protocol and
behavior in docs/spec.md.