A blockchain-based Pokemon trading game. Be the ultimate Pokemon master and catch all 151 Gen-1 Pokemon: mine blocks to capture Pokemon, then trade them with other trainers over a shared distributed ledger. Gotta catch 'em all.
Requirements: Python 3.10+.
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python demo.pyThen open http://127.0.0.1:5010 in your browser (and :5011 for a second
trainer). demo.py starts a tracker plus two trainers; Ctrl-C stops
everything. Options: --tracker-port, --trainers, --base-port.
To run processes by hand instead:
python nodetracker.py 5009
python app.py 5009 5010 # tracker port, then this trainer's port
python app.py 5009 5011- Enter a trainer name on each trainer's page.
- Press Mine to forge a block and capture its reward Pokemon.
- Press Trade, pick one of your Pokemon, then pick a Pokemon another trainer owns. They accept (or reject) from Trade Offers; once a block carrying the trade is mined, the request shows Verified.
Mining incentives: empty blocks only yield common (rarity-1) Pokemon, while blocks carrying more trades unlock rarer tiers — rarity-5 elites at 20+ trades, and the rarity-6 mythics (Mewtwo, Mew) at 40+ trades.
One tracker process introduces trainers to each other; each trainer runs its own Flask app holding a full copy of the chain.
Each block carries an index, a timestamp, the trades it seals, a proof, the previous block's hash, the miner, and a reward Pokemon. A block's fingerprint is the SHA-256 hash of its canonical JSON encoding, so any edit to a sealed block changes its hash and breaks every link after it.
Mining is a hashcash-style search: starting from proof zero, the miner counts up until this check passes:
sha256(str(last_proof + proof) + last_hash).hexdigest()[:N] == "0" * NN is the difficulty (leading zeros, default 4, up to 6 in the simulator).
Each extra zero makes mining roughly sixteen times slower, and every node on
a network must use the same value or their chains fork permanently.
flowchart TD
Q[Pending trades<br/>own queue plus peers] --> V{Each trader owns<br/>what they offer?}
V -- Yes --> W[Brute force a proof until the block hash starts with N zeros]
V -- No --> X[Drop trade, mark Invalid]
W --> R[Grant reward by trade count]
R --> B[Seal block, append, broadcast]
flowchart LR
O[Trainer A offers a mon for one of B's mons] --> P{B accepts?}
P -- Yes --> Q[Trade queued on B's node]
P -- No --> R[Cancelled]
Q --> S[Miner pulls it, validates, seals it in a block]
S --> T[Verified on every synced chain]
Trainers follow the longest valid chain: a longer chain is adopted only if every block links correctly, every proof meets the difficulty, every reward fits its trade count, and every trade spends Pokemon the traders actually owned at that point (replayed in order). Anything else is rejected, however long it is. Pending trades are validated without mutating the queue, and a re-entrant lock guards chain state instead of a spin-wait.
source .venv/bin/activate
python -m pytest tests/ -qsimulator.py runs a bot network on its own ports (tracker :5109, trainers
:5110+, separate from demo.py) to exercise trading and mining at scale:
python simulator.py --trainers 20 --minutes 20
python simulator.py --trainers 4 --miner-weights 50,25,12.5,12.5 \
--eagerness 3 --accept-rate 0.8 --fair-only --seed 7 --serve-afterKnobs: --trainers (bot count), --miner-weights (per-trainer mining
share, default uniform), --eagerness (mean trades/sec/bot),
--accept-rate (offer acceptance probability), --fair-only (bots reject
rarity-losing trades), --difficulty (proof-of-work leading zeros, 0-6;
every node on a network must share it), plus --minutes, --target-pending,
--funding-rounds, --seed, --results, --log, and --serve-after to
keep browsing when the run ends. Results land in sim_results.json
(ignored by git).
Pacing knobs keep big runs inside what localhost can sustain (the dev
server closes every connection, so request volume is the limit):
--startup-stagger, --funding-settle, --chain-cache (shared chain
snapshot TTL), and --min-round-interval.
monitor.py watches a running network and shows every trainer's current
collection on one auto-refreshing page, with rarity-4/5 catches called out:
python monitor.py --tracker http://127.0.0.1:5109
python monitor.py --trainers http://127.0.0.1:5010/node,http://127.0.0.1:5011/node --port 5301Then open the monitor port in a browser. It discovers trainers from the
tracker (or takes an explicit list), reads one status snapshot per trainer
plus the longest chain once, and refreshes every --refresh seconds
(default 15).
app.py— trainer node (browser UI + peer API)nodetracker.py— trainer registryblockchain.py— blocks, trades, proof-of-work, consensuspokedex.py— Gen-1 dex lookups and reward pools (Kanto.csv)demo.py— one-command local gamesimulator.py— configurable bot network for experimentsmonitor.py— live dashboard of every trainer's collectiontemplates/+static/— pages, sprites, styles
Chains live in memory: restarting a trainer resets it to a fresh genesis block. Peer endpoints have no authentication, so only play on localhost.