This is the Python-based backend for DecenTrack, migrated from the original Solidity + Sepolia setup. It replaces Ethereum smart contracts with a FastAPI server + BlockSim-style simulator, and integrates a custom ML model to validate and weight validator uptime reports.
- In-memory chain state (websites, validators, ticks, balances).
- Block production loop with reward distribution.
- A trained ML model (ml_engine/model.joblib) is used to score validator ticks.
- Low-quality ticks are rejected automatically.
- Rewards are weighted by ML score.
- /website/create → create a website
- /tx/addTick → submit uptime tick
- /tx/addMultipleTicks → batch tick submission
- /websites, /website/{id} → query websites
- /ticks/{id} → query ticks (with ML weights)
- /validator/register → register validator
- /me/pendingPayout, /me/payouts → validator rewards
- API mirrors the original Solidity contract functions.
- Works with the existing Next.js + Tailwind UI.
.
├── ml_engine/
│ ├── __init__.py
│ ├── features.py # Feature engineering for ML
│ ├── model.py # MLEngine wrapper
│ └── model.joblib # Trained ML model (generated)
├── sim/
│ ├── __init__.py
│ ├── api.py # FastAPI server
│ ├── node.py # Blockchain node logic
│ ├── state.py # Chain state (websites, validators, reports)
│ └── models.py # Pydantic request/response models
├── blocksim/ # Optional BlockSim experiment code
├── train_model.py # Script to train and save ML model
├── requirements.txt # Python dependencies
└── README.md
-
Clone the repo
git clone https://github.com//.git cd
-
Create virtual environment
python -m venv .venv source .venv/bin/activate # Linux/Mac .venv\Scripts\activate # Windows
-
Install dependencies
pip install -r requirements.txt
-
Train ML model
python train_model.py
This generates ml_engine/model.joblib.
-
Run FastAPI server
uvicorn sim.api:app --host 0.0.0.0 --port 8000 --reload
Server will be available at: 👉 http://localhost:8000 👉 API docs: http://localhost:8000/docs
curl -X POST "http://localhost:8000/website/create?owner=0xabc" \
-H "Content-Type: application/json" \
-d '{"url":"https://github.com","contactInfo":"me@example.com"}'
curl -X POST "http://localhost:8000/tx/addTick?validator=0xval1" \
-H "Content-Type: application/json" \
-d '{"websiteId":"1","status":0,"latency":250}'
curl "http://localhost:8000/ticks/1?n=5"
{
"status": "Success",
"data": [
{
"validator": "0xval1",
"createdAt": 1735710732,
"status": 0,
"latency": 250,
"location": "sim-location",
"ml_weight": 0.87
}
]
}
- Replace ethers.js calls in your Next.js frontend with fetch calls to this FastAPI server.
- Example:
- contract.addTick(...) → POST /tx/addTick
- contract.getAllWebsites() → GET /websites
- The API responses are shaped to match the Solidity contract outputs, so minimal frontend changes are needed.
You can run the BlockSim simulation to test consensus + ML integration:
python -m blocksim.run_sim
This prints block counts, reports, and validator balances.
- ML model is pluggable: retrain with new dataset → replace model.joblib.
- Default ML threshold = 0.3 (ticks below this score are rejected).
- Rewards are distributed proportionally to ML weights.
Developed with ❤️ by Abhishek B R CSE Student | Full-stack & Blockchain Developer | Learning AI, Golang, Rust