SHA-256 Miner (From Scratch, Python + C)
C Rewrite (Modular)
This repository now includes a modular C implementation that mirrors the Python module split:
config.c/.h+miner.confrpc.c/.hblock_builder.c/.hminer.c/.hmining_loop.c/.hlauncher.cmain.c
Build:
make
Run single worker:
./miner --config miner.conf
Run multiprocess launcher:
./launcher --config miner.conf -n 4
A clean, production-minded starting point for building SHA-256 miners from scratch.
This repository provides a complete, readable Python implementation of a Bitcoin-style mining pipeline using getblocktemplate and JSON-RPC. It is intentionally written in Python to keep the architecture simple, inspectable, and easy to evolve.
Positioning
This project is designed for engineers who want to:
- understand mining internals end-to-end
- customize nonce-search and block-building logic
- use a practical base before moving performance-critical parts to lower-level languages
This project is not an ASIC-grade miner and is not intended for profitable mainnet mining.
What You Get
- End-to-end mining flow (
template -> coinbase -> merkle -> header -> PoW -> submit) - Multiprocess worker orchestration
- Worker-specific
extranonce2generation - SegWit-aware template processing
- Coinbase construction with custom message support
- Block serialization and submission to Bitcoin Core
- Watchdog-based restart on chain tip updates
- Live aggregated dashboard (attempts/hashrate by worker)
Repository Layout
launcher.py: supervisor, process lifecycle, dashboardmain.py: single-worker mining loopminer.py: PoW nonce search engineblock_builder.py: coinbase/header/block assemblyrpc.py: Bitcoin Core RPC client helpersutils.py: hashing, target conversion, watchdog logicconfig.py: runtime configurationconfig.py.example: full configuration template
Architecture Overview
launcher.pystarts N worker processes.- Each worker requests a fresh block template from Bitcoin Core.
- Coinbase is built using block height, optional message,
EXTRANONCE1, and worker-specificEXTRANONCE2. - Merkle root and block header are assembled.
miner.pysearches for a valid nonce according to the selected strategy.- If a valid header is found, the block is serialized and submitted.
- Workers restart with a new template after successful submission or tip change.
Requirements
- Python 3.10+
- Bitcoin Core node with RPC enabled
- Recommended network for development:
regtest
Install:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Bitcoin Core Setup
Example bitcoin.conf values:
server=1
rpcuser=user
rpcpassword=password
rpcallowip=127.0.0.1
rpcport=18443
regtest=1
Use values consistent with your local node and wallet setup.
Configuration (config.py)
All runtime settings are defined in config.py.
Use config.py.example as the canonical template.
Minimal example:
import multiprocessing as mp
# RPC
RPC_USER = "user"
RPC_PASSWORD = "password"
RPC_HOST = "127.0.0.1"
RPC_PORT = 8332
# Wallet
WALLET_ADDRESS = ""
# Mining
DIFFICULTY_FACTOR = 1.0
NONCE_MODE = "incremental" # incremental | random | mixed
TIMESTAMP_UPDATE_INTERVAL = 30
BATCH = 10_000
COINBASE_MESSAGE = "/py-miner/"
EXTRANONCE1 = "1234567890abcdef"
EXTRANONCE2 = "12341234"
# Workers
_NUM_PROCESSORS = 0
NUM_PROCESSORS = _NUM_PROCESSORS if _NUM_PROCESSORS > 0 else mp.cpu_count()
# Watchdog
CHECK_INTERVAL = 20
Supported Configuration Behavior
NONCE_MODE: must beincremental,random, ormixedTIMESTAMP_UPDATE_INTERVAL: seconds,0disables periodic timestamp updatesBATCH: number of nonces per compute batchNUM_PROCESSORS:> 0: exact number of workers<= 0: auto CPU count
DIFFICULTY_FACTOR:- on
regtest:0: keep original template target> 0: compute target asmax_target / factor< 0: clamped to0.1
- on non-
regtest: forced to1.0
- on
Run
Default:
python launcher.py
CLI options:
-n,--num-procs: number of workers--base-extranonce2: base hex value for per-worker extranonce2 derivation
Example:
python launcher.py -n 4 --base-extranonce2 12341234
Operational Notes
EXTRANONCE1andEXTRANONCE2must be valid hex strings.- Coinbase
scriptSigmust remain within protocol constraints (the implementation enforces a 100-byte limit). - Invalid RPC credentials or invalid wallet address will fail at runtime.
Tested Capacity
Reference test result for this miner implementation:
- Platform: Raspberry Pi 5
- CPU usage: 4 cores
- Observed throughput: ~1.8 MH/s
Performance depends on clock profile, thermal conditions, operating system, and background load.
Current Limits (By Design)
This is a Python starter implementation. It intentionally does not include:
- ASIC/GPU kernels
- Stratum server/client stack
- advanced failover/job persistence
- extensive benchmark/profiling harness
Recommended Evolution Path
- Add strict schema validation for
config.py. - Add deterministic unit tests for block assembly and hashing paths.
- Introduce microbenchmarks for nonce-loop and hashing batches.
- Add integration tests against disposable
bitcoindregtestnodes. - Extract core components into reusable packages (RPC, builder, PoW engine).
- Move performance-critical sections to C/Rust/CUDA while preserving Python orchestration.
License
MIT. See LICENSE.