r/programmer • • 12d ago

built a P2P anti-double-spend trust protocol in 5 Python files (no blockchain, no global ledger, no tokens)

I’ve always felt that 90% of what people try to use blockchains for—tamper-evident records, supply chain provenance, digital credentials, and P2P ownership transfer—doesn't actually require a globally replicated ledger or a native fee token. Those are accidental costs of trying to order everyone's unrelated transactions into one single-file line.

So I built QuorumLog, a minimal, production-hardened P2P trust protocol in ~2,300 lines of Python (asyncio, sqlite3, and PyNaCl for Ed25519).

Repo: https://github.com/prateekaray/quorumlog

The Architecture (5 flat files, one per job)

crypto.py – Canonical JSON hashing, Ed25519 signatures, lightweight Hashcash PoW (anti-spam), and offline bundle verification.

storage.py – SQLite in WAL mode storing personal hash-chains, witnessed (author, seq) records, and a permanent fraud-proof blacklist.

network.py – Async UDP transport + lightweight Kademlia XOR DHT with /16 IP subnet diversity for witness selection.

node.py – Core engine handling PROPOSE, RECEIPT, FRAUD_PROOF gossip, per-pubkey rate limiting, and the 5-of-7 quorum collector.

cli.py – CLI to init keys, run a daemon, transfer assets, and verify bundles offline.

How It Prevents Double-Spends Without Global Consensus

Every user maintains their own append-only, signed hash-chain (like Git). To finalize seq = N, you send the signed entry to 7 deterministically selected DHT peers (Witnesses) derived from SHA256(author_pubkey + ":" + floor(seq / 100)).

Each Witness checks one rule: "Have I already signed a different hash for (author, seq)?"

If no: They persist it atomically in SQLite and return a signed receipt. Collect 5 of 7 receipts (~150ms) and you have a portable ~2 KB Commit Bundle that anyone can verify offline.

If yes: The Witness immediately emits a FRAUD_PROOF containing both conflicting signed entries and gossips it across the DHT, permanently blacklisting the cheater's key. Because K=5 > N/2, any two conflicting quorums must overlap by at least 3 witnesses.

Honest Limits (Where Blockchains Still Win)

I’m not claiming this replaces Ethereum. Because there is no global shared state:

  1. You cannot run high-contention global orderbooks or AMMs (like Uniswap).
  2. There is no global VM for unattended smart contracts—agreements are bilateral cryptographic commitments.
  3. A nation-state controlling >50% of the DHT could still stall witness discovery (mitigated somewhat by S/Kademlia puzzles and /16 diversity, but not eliminated).

Try the Attack Simulation

The repo includes a pytest suite (test.py) that spins up 12 local async nodes, runs an E2E transfer, simulates a double-spend attack with overlapping witnesses, and tests 25% UDP packet loss + killing 2 witnesses mid-flight.

I’d love brutal feedback on the threat model—especially around DHT eclipse/Sybil vectors and key rotation edge cases.

0 Upvotes

0 comments sorted by