The stratum protocol is currently the most commonly used TCP communication protocol between miners and mining pools. Let's briefly explain the network architecture of mining.
1. Network architecture of mining
Bitcoin is a decentralized network architecture that forwards new transactions and blocks by installing nodes with Bitcoin daemons. Mining machines and mining pools also form another network, which we call the miner network.
The miner network is divided into several main parts, such as mining machines, mining pools, wallets, etc., and sometimes the mining pool software is installed together with the wallet, which can be collectively called a mining pool.
The communication protocol between the mining machine and the mining pool software is stratum, while the communication between the mining pool software and the wallet is the bitcoinrpc interface.
stratum is JSON data format, and the specific protocol is as follows:
2. Stratum agreement
1. Task subscription
When the mining machine starts, it first connects to the mining pool using the mining.subscribe method to subscribe to the work.
The pool returns the subscription number, ExtraNonce1, and ExtraNonce2_size in mining.notify.
Client:{"id":1,"method":"mining.subscribe","params":[]}
Server:{"id":1,"result":[["mining.notify","ae6812eb4cd7735a302a8a9dd95cf71f"],"08000002",4],"error":null} Among them:
ae6812eb4cd7735a302a8a9dd95cf71f is the subscription number;
08000002 is ExtraNonce1, which is used to build coibase transactions;
ExtraNonce2_size is 4, the number of bytes of the miner ExtraNonce2 counter.
2. Task allocation
This command is periodically sent to the miner by the mining pool, and when the mining machine registers with the mining.subscribe method, the mining pool should immediately return the task as mining.notify.
Server:{"params":["bf","4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000", "010000000100000000000000000000000000000000000000000000000000000000000000 00ffffffff20020862062f503253482f04b8864e5008", "072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000",["c5bd77249e27c2d3a3602dd35c3364a7983900b64a34644d03b930bfdb19c0e5", "049b4e78e2d0b24f7c6a2856aa7b41811ed961ee52ae75527df9e80043fd2f12"], "00000002","1c2ac4af","504e86b9",false],"id":null,"method":"mining.notify"} Among them:
Task number: "bf";
Previous block HASH:
"4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000"; Coinbase Part I:
"010000000100000000000000000000000000000000000000000000000 00000000000000000ffffffff20020862062f503253482f04b8864e5008"; Coinbase Part 2:
"072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7 e756a64a7a9688ef9903327048ed988ac00000000"; Transaction ID List:
["c5bd77249e27c2d3a3602dd35c3364a7983900b64a34644d03b930bfdb19c0e5", "049b4e78e2d0b24f7c6a2856aa7b41811ed961ee52ae75527df9e80043fd2f12"]; Block version number: "00000002";
nBit:”1c2ac4af”;
Current time: "504e86b9";
Cleaning tasks: If true, the miner will abort all tasks and start a new task immediately. If false, wait until the current task ends before starting a new task.
3. Mining machine login
The miner uses the mining.authorize method to log in to the mining pool with a certain account and password, the password can be empty, and the mining pool returns true to log in successfully. This method must be done immediately after initializing the connection, otherwise the miner will not get the pool task.
Client:{"params":["miner1","password"],"id":2,"method":"mining.authorize"}
Server:{"error":null,"id":2,"result":true} 4. Submit the results
When a miner finds a legitimate share, it submits a task to the pool using the "mining.submit" method. If the pool returns true, the commit is successful, and if it fails, there is a specific reason for the error.
Client:{"params":["miner1","bf","00000001","504e86ed","b2957c02"],"id":4,"method":"mining.submit"}
Server:{"error":null,"id":4,"result":true} Among them:
Username: "miner1";
Task number: "bf";
ExtraNonce2:”00000001″;
Current time: "504e86ed";
nonce:”b2957c02″;
5. Difficulty adjustment
The difficulty adjustment is issued by the mining pool to the mining machine to adjust the difficulty in mining.set_difficulty way, and the difficulty value is in the params.
Server:{"id":null,"method":"mining.set_difficulty","params":[2]} The miner will apply the new difficulty to the next task, and the pool will sometimes issue a new task immediately and set the cleanup task to true so that the miner can work on the new difficulty immediately.
3. General communication process
The general communication process between the mining machine and the mining pool is as follows:
|