Skip to content

Bittermun/TheFoundationProtocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

168 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

TFP v3.1 Foundation Protocol

A decentralized content & compute protocol for global information access — uncensorable, efficient, and built for everyone.

Tests Python Files License Security

Quick Start

30-Second Demo

cd TheFoundationProtocol
python demo_30sec.py

This script automatically:

  • Starts a demo server

  • Enrolls a device

  • Publishes sample content

  • Retrieves and displays it

  • Shows timing metrics


Documentation Map

Document Purpose
Integration Guide API reference, task execution, credit economics, deployment runbook, testing, extension guide — start here
Security Model & Checklist Verified security properties, known limitations, per-release checklist, maintenance policy
Deploy & Bootstrap Guide Run locally, Docker, cloud (Render/Railway/Fly.io), Nostr relay setup, compute pool bootstrap
Architecture Component interactions, design decisions, testbed setup, runtime modes
Contributing Setup, PR workflow, high-impact areas, security disclosure
Code of Conduct Contributor expectations
Governance Manifest Maintainer transparency, contribution model, sustainability, accountability
Governance Charter Stewardship model, decision tiers, founder safeguards, amendment rules
Definition of Done North star, end goals, hard acceptance criteria, release DoD, and release scorecard
Porting Guide C/Rust porting to Cortex-M4 / RISC-V32
Plugin Tutorial Build a plugin in 30 minutes
Hackathon Kit Event materials, starter templates
Integrations Playbook IPFS, Nostr, and Kiwix/Wikipedia bridge implementation details
Roadmap v3.2 planned milestones and open implementation priorities
Extension Modules Optional Redis rate limiter, RAGgraph semantic search, OpenTelemetry tracing
Archive Historical planning docs and legacy integration guides (v2.x) — read-only

Vision

Create a Global Information Commons that works for pennies: anyone can publish, discover, and share media reliably — even in low-connectivity or censored environments. It combines peer-to-peer networking, smart erasure coding, strong privacy/security, and a mutualistic internal economy so the system improves the more people use it.

What Makes TFP Different

  • Uncensorable & discoverable — Hash-based NDN routing + tag-overlay index (no central server or registry). Nostr relay bridge for cross-network peer discovery.

  • Bandwidth & compute efficient — RaptorQ erasure coding + hierarchical lexicon tree designed for efficient content distribution in low-bandwidth environments.

  • Secure by design — PUF/TEE identity (Sybil-resistant), HMAC-per-request device auth, ZKPs, post-quantum crypto agility, WASM sandboxing, behavioral heuristics for anomaly detection.

  • Privacy-first — Metadata shielding, zero PII logging, device-bound identity.

  • Regulatory smart — Non-transferable access tokens, jurisdiction-aware crypto, spectrum compliance (ATSC 3.0, 5G MBSFN).

  • Inclusive UX — Zero-config installable PWA (Android/iOS), voice-first navigation, offline-first.

  • Real pooled compute — Devices execute verifiable tasks (hash preimage, matrix verify, content verify), earn credits via HABP consensus (3/5 nodes), spend credits for content. 21M supply cap.

Current Status (v3.1.x)

  • ✅ Production-ready core (42k+ LOC, 189 Python files, 142 with Apache-2.0 headers).

  • 780+ tests passing — Protocol tests + root-level integration tests. TFP_DB_PATH=:memory: PYTHONPATH=. python -m pytest tests/ -q

  • Real compute tasks — 3 task types (HASH_PREIMAGE, MATRIX_VERIFY, CONTENT_VERIFY) with cryptographic proof-of-work.

  • HABP consensus — Credits only mint when 3/5 devices agree on identical output hash. Proofs survive server restart (rebuilt from SQLite on boot).

  • 21M credit supply cap — Hard-coded MAX_SUPPLY enforced at every mint via SupplyCapError.

  • Task dispatch APIPOST /api/task, GET /api/tasks, POST /api/task/{id}/result.

  • Prometheus metricsGET /metrics with 12 counters (tasks, credits, content, devices). Seeded from DB on startup so counters survive restarts.

  • Admin dashboardGET /admin live HTML dashboard (auto-refresh, supply bar, device leaderboard).

  • tfp join — Single command to join the compute pool, earn credits, spend on content.

  • tfp tasks / tfp leaderboard — CLI commands to inspect the live pool.

  • Content paginationGET /api/content?limit=N&offset=N with total in response.

  • Device leaderboardGET /api/devices (sorted by credits) + GET /api/device/{id}.

  • Background maintenance thread — periodic reap + replenishment every 30s (pool never runs dry).

  • SQLite WAL mode — concurrent reads during writes; "database is locked" errors eliminated.

  • SQLite persistence — content, device enrollment, credit ledgers, supply ledger survive restarts.

  • Device auth — HMAC-SHA-256 per-request signing (constant-time compare); identity persisted at ~/.tfp/identity.json.

  • Rate limiting — sliding-window per device on /api/earn and /api/task/{id}/result. Per-device (1000 chunks/min) and per-upload (100 chunks/sec) rate limiting on chunk uploads.

  • Nostr subscriber + bridge — remote peer content discovery & publishing via relay (offline-safe).

  • Parallel chunk upload/api/upload/chunk and /api/upload/complete with 8-16 concurrent uploads, RaptorQ encoding, and retry logic.

  • Upload idle timeout — 5-minute cleanup of abandoned uploads to prevent memory leaks.

  • Chunk checksum validation — Optional SHA-256 validation via X-Chunk-Hash header to detect corruption.

  • Retry queue — Failed background uploads queued with exponential backoff and Prometheus metrics.

  • Parallel RaptorQ encoding — ProcessPoolExecutor for files >= 5MB with thread-safe initialization and graceful shutdown.

  • IPFS bridge — content pinning with hash↔CID mapping; offline-safe fallback.

  • Multipart upload/api/publish supports both application/json and multipart/form-data for large binary payloads.

  • Streaming download/api/get/{hash}?stream=true for chunked 64KB responses.

  • Content discovery/api/discovery?domain=X returns Nostr-announced content hashes.

  • PWA — installable on Android/iOS, offline-first service worker.

  • 10-node testbed — Docker Compose with 10 nodes (ports 9001–9010). Run: docker compose -f docker-compose.testbed.yml up

  • 100-node benchmark — Docker Compose with 100 nodes + OpenTelemetry, Tempo, Prometheus, Grafana. Run: docker compose -f tests/benchmarks/docker-compose.100.yml up (resource-intensive)

  • CI/CD — 9 workflows: tests, security, license, release, OpenSSF Scorecard.

  • Cloud deployment — Docker local verified. Render/Railway/Fly.io need community testing (see docs/deploy_demo.md).


v3.2 Planning

See ROADMAP.md for planned v3.2 milestones and GitHub Milestones for issue-level tracking.

Full Setup

cd tfp-foundation-protocol
pip install -r requirements.txt
TFP_DB_PATH=:memory: PYTHONPATH=. python -m pytest tests/ -q   # 755 tests
uvicorn tfp_demo.server:app --reload                           # Demo node on :8000

Open http://localhost:8000 — the PWA is installable directly from the browser. Open http://localhost:8000/admin — live admin dashboard (tasks + device leaderboard). Open http://localhost:8000/metrics — Prometheus metrics. Open http://localhost:8000/health — health check (used by Docker + load balancers).

Live Demo: https://tfp-icy-cherry-2504.fly.dev/

Quick Benchmark

cd TheFoundationProtocol
python benchmark_simple.py

Measures publish/retrieve latency and throughput. Results (in-memory, single-node):

  • Publish: ~0.1 ops/sec (~7s per operation)

  • Retrieve: ~0.5 ops/sec (~2s per operation)

  • Note: Production with disk persistence will be 2-5x slower

Parallel Chunk Upload Benchmark

cd TheFoundationProtocol
python benchmark_parallel_chunk_upload.py

Measures parallel chunk upload performance with comprehensive metrics. See BENCHMARKS.md for details.

Download/Retrieval Benchmark

cd TheFoundationProtocol
python benchmark_download_retrieval.py

Measures download/retrieval performance for video/audio content with streaming, HTTP Range requests, and concurrent downloads. See BENCHMARKS.md for details.

RaptorQ Encoding Benchmark

cd TheFoundationProtocol
python benchmark_raptorq.py

Benchmarks server-side RealRaptorQAdapter encoding efficiency:

  • Encoding speed: ~1.9 MB/s

  • Overhead: ~12% for realistic file sizes (1MB+)

  • Fault tolerance: Can reconstruct from any k source shards

  • Note: Client-side retrieval requires real NDN adapter (currently mock)

Implementation Status

Working (Production-Ready)

  • Server-side chunking: RealRaptorQAdapter (XOR-based erasure coding)

  • Client-side retrieval: RealNDNAdapter with blob_store fallback (single-node) or python-ndn (multi-node)

  • Lexicon adapter: RealLexiconAdapter with HierarchicalLexiconTree integration

  • Credit ledger: SQLite-backed, non-transferable credits

  • Nostr integration: Real relay connectivity for discovery

  • IPFS bridge: Content pinning and retrieval

  • 755+ tests: Comprehensive test coverage

  • End-to-end real adapters: Working with TFP_REAL_ADAPTERS=1

What's Implemented Now

  • Real NDN adapter: Supports local blob_store for single-node, python-ndn for multi-node

  • Real Lexicon adapter: Uses HierarchicalLexiconTree for domain-aware reconstruction

  • Real RaptorQ adapter: XOR-based erasure coding with fault tolerance

  • End-to-end flow: Publish → chunk → retrieve → reconstruct all working with real adapters

Efficiency Claims

  • RaptorQ encoding: ~12% overhead for realistic file sizes (1MB+)

  • Fault tolerance: Can reconstruct from any k source shards

  • Semantic search: HierarchicalLexiconTree structure ready (domain-aware reconstruction implemented)

What Still Needs Multi-Node Deployment

  • P2P shard exchange: Requires multi-node deployment to measure bandwidth savings

  • Semantic search efficiency: Requires multi-node deployment to benchmark

  • Partial reconstruction benefits: Visible in multi-node scenarios with network latency

Join the compute pool from CLI

# Start the server first, then from another terminal:
python -m tfp_cli.main join --device-id my-laptop --interval 5
# [join] Enrolled. Polling for tasks …
# [join] Executing task a1b2c3d4 (type=hash_preimage, diff=2) …
# [join]   ✓ executed in 0.14s — output_hash=3f8a12…
# [join]   ⏳ pending consensus (2 more proofs needed)
# (run on 2 more devices to reach consensus and earn credits)

# Inspect the pool without joining:
python -m tfp_cli.main tasks
python -m tfp_cli.main leaderboard

With Docker

cd TheFoundationProtocol
docker compose up --build
# Data is persisted in the 'tfp_data' volume
# Open http://localhost:8000
# Open http://localhost:8000/admin (live dashboard)
# Open http://localhost:8000/docs (interactive API docs)

CLI

cd tfp-foundation-protocol
pip install -e .
tfp tasks                              # list open compute tasks

tfp leaderboard                        # top devices by credits earned

tfp join --device-id my-laptop         # join the compute pool

tfp earn --task-id demo-task-1         # legacy earn

tfp publish --title "Hello" --text "From CLI" --tags demo,cli

tfp search --tag demo

tfp status                             # node status + supply info

Architecture

tfp-foundation-protocol/
├── tfp_client/lib/
│   ├── bridges/       # NostrBridge (pub) + NostrSubscriber (sub) + IPFSBridge
│   ├── credit/        # CreditLedger, DWCCCalculator, HybridWallet
│   ├── metadata/      # TagOverlayIndex, BloomFilter (Merkle DAG)
│   ├── publish/       # MeshAggregator
│   ├── identity/      # PUFEnclave
│   ├── zkp/           # ZKP (Schnorr/Fiat-Shamir)
│   ├── fountain/      # RaptorQ (per-shard HMAC)
│   ├── ndn/           # NDN adapter
│   └── core/          # TFPClient orchestrator
├── tfp_broadcaster/src/gateway/
│   └── scheduler.py   # GatewayScheduler + schedule_from_aggregator
├── tfp_demo/
│   └── server.py      # FastAPI (SQLite + auth + Nostr)
├── demo/
│   ├── index.html     # SPA (SubtleCrypto signing + SW registration)
│   ├── manifest.json  # PWA manifest
│   └── service-worker.js
├── docs/
│   ├── v3.0-integration-guide.md  ← canonical API reference, runbook, extension guide
│   ├── SECURITY.md                ← security model, verification checklist, maintenance policy
│   ├── porting_guide.md
│   └── archive/                   ← historical guides (v2.x, read-only)
└── tests/             # 755 pytest tests

API Endpoints (Demo Node)

Method Path Auth Description
GET /health None Liveness check
GET /api/status None Node status + Nostr subscriber info
POST /api/enroll None Register device PUF entropy
POST /api/task None Create a compute task
GET /api/tasks None List open tasks
GET /api/task/{id} None Get task spec + status
POST /api/task/{id}/result X-Device-Sig Submit compute result → HABP → mint credits
POST /api/earn X-Device-Sig Legacy earn path
POST /api/publish X-Device-Sig Publish content
GET /api/content None List/search content by tag
GET /api/get/{hash} None (credits) Retrieve content (spends 1 credit)
GET /api/devices None Device leaderboard
GET /api/device/{id} None Per-device stats
GET /api/discovery None Nostr-discovered remote content
GET /metrics None Prometheus counters
GET /admin None Live HTML admin dashboard
GET / None Demo PWA

Full request/response schemas: docs/v3.0-integration-guide.md.

Key Components

Module Technology Status
ContentStore SQLite + in-memory tag index ✅ v3.0
DeviceRegistry SQLite device enrollment ✅ v3.0
TaskStore SQLite task lifecycle (open→verifying→completed) + HABP ✅ v3.0
CreditStore SQLite credit ledger persistence ✅ v3.0
EarnLog SQLite replay-prevention (UNIQUE device_id+task_id) ✅ v3.0
NostrBridge NIP-01 publisher (pure-Python BIP-340 Schnorr) ✅ v2.12
NostrSubscriber NIP-01 subscriber, daemon thread, auto-reconnect ✅ v2.12
GatewayScheduler Credit-based bidding + schedule_from_aggregator ✅ v2.12
MeshAggregator Demand signal aggregation ✅ v2.5
TagOverlayIndex Merkle DAG + Bloom filters ✅ v2.5
CreditLedger SHA3-256 hash-chain, spend(), Merkle root, 21M cap ✅ v3.0
PUFEnclave HMAC-SHA3 + entropy + nonce, Sybil gate ✅ v2.3
RaptorQAdapter GF(2) systematic erasure code, per-shard HMAC ✅ v2.3
ZKPAdapter Schnorr proof (Fiat-Shamir) ✅ v2.3
IPFSBridge kubo HTTP client, offline stub ✅ v2.12
HierarchicalLexiconTree Delta apply + atomic rollback ✅ v2.5
Attack simulator Shard poisoning, Sybil, congestion ✅ v2.3

Simulation

python tfp-foundation-protocol/tfp_simulator/attack_inject.py --seed 42 --requests 500
bash tfp-foundation-protocol/tfp_simulator/run_sim.sh   # uses ns-3 if installed

Benchmark Results

Caliper Synthetic Benchmarks (In-Memory)

Benchmark Ops/Sec p99 Latency Throughput Status
RaptorQ Encode/Decode 9,704 0.16ms 19.9 MB/sec ✅ Pass
Credit Ledger Ops 164,204 0.007ms N/A ✅ Pass
End-to-End Request 229,885 0.01ms 117.7 MB/sec ✅ Pass

Run with: cd tfp-foundation-protocol && python -c "from tfp_client.lib.caliper.adapter import BenchmarkSuite; suite = BenchmarkSuite(iterations=10); print(suite.summary(suite.run_all()))"

10-Node Testbed Real-World Performance

Network I/O Analysis (1.5 MB content published):

  • IPFS processed: 33.6 MB total (20.6 MB in + 13.6 MB out)

  • Bandwidth overhead: 22.4x (due to RaptorQ erasure coding + IPFS replication)

  • Upload latency: 86s for 1 MB video, 21s for 512 KB audio

  • Per-node coordination overhead: ~5-6 KB

Known Issues:

  • Content retrieval fails (Nostr relay reports "client sent an invalid event")
  • Sequential streaming upload is a bottleneck

Run with: docker compose -f docker-compose.testbed.yml up && python tests/operate_testbed.py

100-Node Benchmark Infrastructure

Components:

  • 100 TFP nodes (ports 8001-8100)

  • OpenTelemetry Collector (traces/metrics)

  • Tempo (distributed tracing)

  • Prometheus (metrics aggregation)

  • Grafana (visualization dashboard)

Status: Infrastructure verified and operational. Full 100-node deployment is resource-intensive and recommended for production benchmarking only.

Run with: docker compose -f tests/benchmarks/docker-compose.100.yml up

Performance Improvement Opportunities

Status: Many improvements have been implemented but accurate performance measurements are not yet available.

Implemented (Performance Impact Unknown):

  • Parallel chunk upload (ChunkUploader with 8-16 concurrent uploads)

  • Larger chunk sizes (256KB default vs 4KB old)

  • HTTP/2 multiplexing and connection pooling

  • RaptorQ erasure coding (ChunkEncoder with configurable redundancy)

  • Exponential backoff retry logic (RetryHandler)

Note: Previous benchmark attempts were invalid. Accurate performance measurement requires a real benchmark comparing old /api/publish streaming upload vs new chunk upload system with full TFP workflow (enrollment, credits, IPFS, Nostr relay).

See BENCHMARKS.md for detailed analysis and implementation status.

Embedded Porting

See docs/porting_guide.md for C/Rust porting to Cortex-M4 / RISC-V32. Memory budget: 122 KB Flash / 130 KB RAM out of a 1 MB / 256 KB envelope.

Security

See docs/SECURITY.md for the full verified security model, known limitations, and validation checklist.

Implemented security controls (all verified against source):

  • All mutating endpoints require X-Device-Sig: HMAC-SHA-256(puf_entropy, message).

  • hmac.compare_digest (constant-time) prevents timing oracles.

  • Sliding-window rate limiting per device on earn and result-submission endpoints.

  • EarnLog SQLite UNIQUE constraint prevents credit replay.

  • 21M hard supply cap enforced at every mint.

  • HABP UNIQUE(task_id, device_id) prevents self-mint.

Known single-node limitations (not vulnerabilities in demo, relevant at scale):

  • Rate limiters are in-memory — reset on restart, not shared across replicas.

  • Hardware-bound Sybil resistance requires PUF/TEE wiring (not wired in demo).

See the historical hardening notes in docs/archive/v2.2-hardening.md.

Who It's For

Audience Use Case Getting Started
Rural communities & NGOs Offline, low-cost delivery of education, health, and emergency information See docs/deploy_demo.md for deployment guide
Developers Building censorship-resistant apps, plugins, browser extensions See docs/hackathon_kit.md + docs/plugin_tutorial_30_min.md
Organizations Compliant, low-cost compute/content distribution See docs/archive/TFP_FINAL_STATUS.md (regulatory positioning)
Researchers Studying decentralized protocols, mesh networks, P2P economics See docs/integrations_playbook.md
Everyone Publishing/sharing without big-tech gatekeepers Run demo node: docker compose up

Get Involved

Immediate Actions You Can Take Today

# 1. Run the demo node (Docker)
cd TheFoundationProtocol && docker compose up --build

# 2. Run all tests locally
cd tfp-foundation-protocol && pip install -r requirements.txt
TFP_DB_PATH=:memory: PYTHONPATH=. python -m pytest tests/ -q

# 3. Join the compute pool from CLI
python -m tfp_cli.main join --device-id my-laptop --interval 5

# 4. Build a plugin in 30 minutes
# Follow: docs/plugin_tutorial_30_min.md

# 5. Configure a community pilot
python tfp_pilots/community_bootstrap.py --community-id "my-region"

Contribution Paths

Role What You'll Do Start Here
Core Contributor Fix bugs, add features, review PRs Pick a good first issue on GitHub
Plugin Developer Build audio galleries, offline packs, browser tools docs/plugin_tutorial_30_min.md
Community Organizer Deploy pilots, onboard NGOs, host hackathons Contact: governance@tfp-protocol.org
Researcher Study protocol economics, mesh behavior, security docs/archive/TFP_VISION_AND_CURRENT_STATE.md
Donor/Partner Fund audits, sponsor pilots, provide infrastructure Contact: governance@tfp-protocol.org

License

Apache-2.0 — see LICENSE.

Fork Rights Guaranteed: This protocol is designed to be forked, adapted, and improved by the community. The Apache-2.0 license ensures perpetual freedom to build upon this work.


Repository Health Metrics

Metric Value Target Status
Python Files 189
Total LOC ~42,000 <50k
Tests Passing 755 >400
Test Warnings 1 0 ⚠️
PII Logged 0 0
Critical Vulnerabilities 0 0
Documentation Pages 16 >5
Plugin SDK Modules 2 >1
Governance Transparency 100% 100%

Creator note: I'm a highschooler. I think bandwidth on decentralized networks could be much better. I used AI, the code is raw. But I think this could be pretty cool. Take a look and let me know where I messed up. "A digital commons for humanity."

About

A decentralized content & compute protocol for global information access - uncensorable, efficient, and built for everyone.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages