env-02-gossip-max
1.000
Challenge · difficulty 4/5
# Gossip convergence — global maximum Three peer nodes (`peer0`, `peer1`, `peer2`) are on a private network, each running the **same** program `peer.py` (launched as `python peer.py`). There is no coordinator. - Each peer starts with an integer in `value.txt` in its working directory. - Each peer listens on the port given by the `PORT` environment variable. - Each peer is told its two peers' addresses via environment variables `PEER_<NAME>_HOST` / `PEER_<NAME>_PORT` (e.g. `PEER_PEER1_HOST`, `PEER_PEER1_PORT`). Write `peer.py` so that, by exchanging information with its peers, **every** peer determines the **global maximum** of all three starting values and writes it (as a decimal string) to `result.txt` in its working directory. The peers must converge even though they start at different times. Only the Python standard library is available (no internet, no pip). The same `peer.py` runs on all three nodes.
Proposed solution
[turn 1] list_nodes({}) -> {"nodes": [{"name": "peer0", "provides": "python peer.py", "ports": [9000], "needs": ["peer1", "peer2"]}, {"name": "peer1", "provides": "python peer.py", "ports": [9000], "needs": ["peer0", "peer2"]},
[turn 2] write_file({"node": "peer0", "path": "peer.py", "content": "#!/usr/bin/env python3\nimport os\nimport socket\nimport time\nimport threading\n\ndef get_peer_addresses():\n \"\"\"Get peer addresses from environ) -> {"ok": true, "node": "peer0", "path": "peer.py"}
[turn 3] write_file({"node": "peer1", "path": "peer.py", "content": "#!/usr/bin/env python3\nimport os\nimport socket\nimport time\nimport threading\n\ndef get_peer_addresses():\n \"\"\"Get peer addresses from environ) -> {"ok": true, "node": "peer1", "path": "peer.py"}
[turn 4] write_file({"node": "peer2", "path": "peer.py", "content": "#!/usr/bin/env python3\nimport os\nimport socket\nimport time\nimport threading\n\ndef get_peer_addresses():\n \"\"\"Get peer addresses from environ) -> {"ok": true, "node": "peer2", "path": "peer.py"}
[turn 5] verify({}) -> {"passed": true, "checks": [{"name": "peer0 converged to 12", "ok": true, "detail": "seed 5"}, {"name": "peer1 converged to 12", "ok": true, "detail": "seed 12"}, {"name": "peer2 converged to 12", "ok