This is a basic, multi-threaded CPU miner written in C++ for the SHA256d algorithm (used by Bitcoin and others). It connects to mining pools using the Stratum v1 protocol.
The python version does not require any dependencies, and you can mine by simply filling in the wallet address in the configuration file, but it is 4 times slower than C++.
- CPU mining using the SHA256d algorithm.
- Supports the Stratum v1 mining protocol.
- Multi-threaded to utilize multiple CPU cores.
- Optimized SHA256 hashing using SSE2 and SSSE3 instruction sets.
- Configuration via a
config.jsonfile. - Automatic reconnection on pool connection loss.
- Periodic status display (hashrate, pool difficulty, block height, etc.).
- Cross-platform support (Linux, macOS, Windows with MinGW).
- Uses the GMP library for arbitrary-precision arithmetic (for target difficulty calculations).
- A C++11 compliant compiler (e.g., g++).
- CPU with SSE2 and SSSE3 instruction set support (the miner checks this at startup).
- Required dependencies (and their development packages/headers):
libcurl(for fetching initial block height via API)libjson-c(for parsing Stratum protocol JSON messages)libgmp(for large number arithmetic)pthreads(threading library - usually built-in on Linux/macOS, may require setup with MinGW)(Windows only)ws2_32(Winsock library - typically included with MinGW)
Before compiling, ensure you have installed the necessary dependencies. Here are commands for common systems:
Debian / Ubuntu:
sudo apt update
sudo apt install build-essential g++ libcurl4-openssl-dev libjson-c-dev libgmp-dev(build-essential usually includes g++ and make)
Fedora / CentOS / RHEL (using dnf or yum):
sudo dnf install gcc-c++ make libcurl-devel json-c-devel gmp-devel
sudo yum groupinstall "Development Tools"
sudo yum install libcurl-devel json-c-devel gmp-devel
First, ensure you have Xcode Command Line Tools installed:
xcode-select --install
Then, install dependencies using Homebrew:
brew install curl json-c gmp
(macOS usually includes curl, but brew install curl ensures headers/libs are easily found by the linker)
If you are using the MSYS2 environment with a MinGW-w64 toolchain, you can install dependencies using pacman:
Run these in the MSYS2 MinGW 64-bit terminal
pacman -Syu
pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-curl mingw-w64-x86_64-json-c mingw-w64-x86_64-gmp
(Note: pthreads is typically included in the MinGW toolchain, and ws2_32 is a Windows system library)
After installing the dependencies, compile the miner:
g++ miner.cpp sha256.cpp sha256_sse.cpp -o miner -O3 -march=native -lcurl -ljson-c -pthread -lm -lgmp
g++ miner.cpp sha256.cpp sha256_sse.cpp -o miner.exe -O3 -march=native -lcurl -ljson-c -lpthread -lm -lgmp -lws2_32
-std=c++11: Specifies the C++ standard.
-march=native: General detection CPU instruction optimization compilation
-O3: Enable level 3 optimizations. Unsupported computers will automatically downgrade to O2 Normal Optimization.
-msse2 -mssse3: Enable the required CPU instruction sets.
-l...: Links the necessary libraries.
The miner requires a configuration file named config.json in the same directory as the executable. If the file doesn't exist, the miner will attempt to create an example file.
{
"pool_host": "solo.ckpool.org",
"pool_port": 3333,
"wallet_address": "13HQ67Cqb8ZbfQThSAqX2RcXi6mhG69y39.pyworker",
"threads": 1,
"pool_password": "x",
"log_file": "miner.log"
}
pool_host: (Required) The Stratum server address of your mining pool.
pool_port: (Required) The Stratum port number for your mining pool.
wallet_address: (required) Key: Change this to your own wallet address (or wallet_address.worker_name format, usually fill in the address directly without the miner name, it depends on you and the pool).
threads: (Required) The number of CPU mining threads to use. Setting this to the number of your CPU's physical cores or logical threads is recommended.
pool_password: (Optional) The password for your worker on the pool. Often set to "x" or can be left empty, depending on the pool's requirements. Defaults to "x".
log_file: (Optional) The name of the file where logs will be written. Defaults to "miner.log".
Install Dependencies (see "Dependencies Installation" section above).
Compile the miner (see "Compilation" section above).
Create and edit the config.json file in the same directory as the compiled executable, filling in your pool and wallet details.
On Linux/macOS: ./miner
On Windows: miner.exe
The program will connect to the specified pool, retrieve mining jobs, and start hashing using the configured number of threads. Status information will be displayed periodically in the terminal, and detailed logs will be written to the configured log file.
Press Ctrl+C to stop the miner gracefully.
This miner application acts as a "worker" that connects to a "mining pool". The pool coordinates the work of many miners to collectively solve a block and share the reward.
graph TD
subgraph "Mining Pool (e.g., solo.ckpool.org)"
A[Stratum Server] -- "1. Sends Job (Block Header, Difficulty)" --> B;
end
subgraph "Your Miner (This Program)"
B[miner] -- "2. Distributes work to threads" --> T1[Thread 1];
B --> T2[Thread 2];
B --> Tn[Thread N...];
T1 -- "3. Hashes with different Nonces" --> S1{Found a valid hash?};
T2 -- "3. Hashes with different Nonces" --> S2{Found a valid hash?};
Tn -- "3. Hashes with different Nonces" --> Sn{Found a valid hash?};
S1 -- "No" --> T1;
S2 -- "No" --> T2;
Sn -- "No" --> Tn;
S1 -- "Yes! (Share found)" --> B;
end
subgraph "Bitcoin Network"
D[Blockchain]
end
B -- "4. Submits Share to Pool" --> A;
A -- "5. Validates & Submits Block to Network" --> D;
This diagram illustrates the core loop: the miner receives a work template, its threads race to find a special number (a "nonce") that makes the block's hash meet a certain difficulty target, and then submits the successful result back to the pool.
If this project has been helpful to you, please consider buying me a coffee. Your support is greatly appreciated. Thank you!
BTC: bc1qt3nh2e6gjsfkfacnkglt5uqghzvlrr6jahyj2k
ETH: 0xD6503e5994bF46052338a9286Bc43bC1c3811Fa1
Dogecoin: DTszb9cPALbG9ESNJMFJt4ECqWGRCgucky
TRX: TAHUmjyzg7B3Nndv264zWYUhQ9HUmX4Xu4
Mining with CPU (e.g. Bitcoin) is usually not very profitable because high network hashrate is mainly occupied by ASIC miners. This software is mainly used for educational purposes, testing, or mining currencies with very low network difficulty. Unless you have spare electricity, idle computer servers, routers, mobile phones and other devices, you might as well try SOLO mode. What if you win? There are also mini miners for sale here, with power consumption of about 10W and hashrate ranging from 200G to 1.2T. The cheapest 200G hashrate is only 150 RMB. If you are interested, you can contact me.