📜 ⬆️ ⬇️

Mining in blockchain networks: how it works

In early January, the Chinese government ordered the country's "bitcoin miner" army, which generates 3/4 of the entire world cryptocurrency, to stop working. One of the main reasons for this decision was the too high amount of electricity consumed by mining farms - now, for security of Bitcoin-blockchain and mining of coins, it takes about 0.2% of the total world energy consumption.

Today let's talk about why mining consumes such amount of energy, why it is needed and how it is implemented. We will talk about the hashing algorithm (SHA-256) and why miners “look for help” to pools.


/ image of Cindy Shebley CC
')

What is mining for?


Mining is often considered a way to create new bitcoins, but this is not entirely true. The main task of mining is to achieve consensus on which transactions to be considered valid, in order not to allow someone from the network participants to spend coins already used in another transaction.

For this, miners solve complex math problems on their equipment and consume electricity, and in return receive bitcoin rewards (and transaction processing fees), which is an incentive to protect the blockchain further (now the network issues 12.5 bitcoins for each block).

What is the work of miners


Miners are committed to selecting a hash value that would fit the transactions in the block and allow you to get the secret key. The desired hash is formed based on the hash of the previous block, the random number (nonce), and the sum of the transaction hashes over the past 10 minutes. At the same time, it must satisfy the conditions of the system: meet the declared complexity of mining (Difficulty) and be less than the target complexity (Target) - it determines the number of zero bits at the beginning of the desired hash.

To calculate the complexity of the hash, you can use an algorithm with decomposition into a modified Taylor series, which is listed on the bitcoinwiki page :

#include <iostream> #include <cmath> inline float fast_log(float val) { int * const exp_ptr = reinterpret_cast <int *>(&val); int x = *exp_ptr; const int log_2 = ((x >> 23) & 255) - 128; x &= ~(255 << 23); x += 127 << 23; *exp_ptr = x; val = ((-1.0f/3) * val + 2) * val - 2.0f/3; return ((val + log_2) * 0.69314718f); } float difficulty(unsigned int bits) { static double max_body = fast_log(0x00ffff), scaland = fast_log(256); return exp(max_body - fast_log(bits & 0x00ffffff) + scaland * (0x1d - ((bits & 0xff000000) >> 24))); } int main() { std::cout << difficulty(0x1b0404cb) << std::endl; return 0; } 

At the time of writing, the complexity of the blockchain network is : 2874674234415.941, however, this parameter is recalculated every 2016 blocks. It is increased or decreased in order to maintain the average speed of creating blocks (approximately 6 pieces per hour).

SHA-256

The SHA-256 algorithm was chosen as a tool for hashing in the bitcoin blockchain. Next, we look at one of his rounds.

/ Round SHA-256 for eight input words / Wikimedia / kockmeyer / CC

Suppose that the algorithm was given eight words as input, denote them as A, B, C ... H. The function Ma performs bitwise operations with the words A, B and C - if most of the obtained values ​​are zero, it also returns zero, otherwise - one.

The Σ0 block shifts the word A three times: by 2, 13, and 22 bits, and the generated values ​​are added bit-wise by the xor operation. Block Σ1 works in a similar way - shifts are performed on 6, 11 and 25 bits.

The Ch block is a function of selecting the result bit based on the bits in E. If the input value is one, then the output will receive the corresponding bit of the word F, otherwise the bit of the word G.

The red squares in the diagram are blocks of 32-bit addition, which generate new values ​​for A and E. The whole cycle repeats 64 times, after which the information is reliably encrypted.

To illustrate the process of calculating the hash, we give the code in Python, which forms the hash of the words "Bl0Ckchain" and "blockchain":

 import itertools from hashlib import sha256 #          to_long = lambda x: sum(ord(b) << (8*i) for i, b in enumerate(x)) #       nonce  ; #   -   .    #  (":") ,  PoW   # . combine = lambda nonce, msg: str(nonce) + ":" + msg def verify_pow(msg, nonce, difficulty): h = sha256(combine(nonce, msg)).digest() return to_long(h) % (1 << difficulty) == 0 def create_pow(msg, difficulty): for nonce in itertools.count(0): if verify_pow(msg, nonce, difficulty): return nonce def print_pow(msg, nonce): print combine(nonce, msg), sha256(combine(nonce, msg)).hexdigest() #---------------------------------------------------- msg = "Bl0Ckchain" nonce = create_pow(msg, 16) print_pow(msg, nonce) # 6571:Bl0Ckchain 0000d087d242930aaf6ac5a790ae8d8ece6b502cdb70ba07c1168738b253d279 assert verify_pow(msg, nonce, 16) msg = "blockchain" nonce = create_pow(msg, 16) print_pow(msg, nonce) # 43952:blockchain 000027b5022f88d2da21bd2802268966050f5a0b031058ce4562939c13727303 assert verify_pow(msg, nonce, 16) #        (1 << difficulty), #      msg = "Bl0Ckchain" nonce = create_pow(msg, 16) print_pow(msg, nonce) # 6571:Bl0Ckchain 0000d087d242930aaf6ac5a790ae8d8ece6b502cdb70ba07c1168738b253d279 assert verify_pow(msg, nonce, 16) 

But checking created hashes is performed very quickly, even if a lot of resources were spent on their creation:

 msg = "blockchain" nonce = 5263268363 print_pow(msg, nonce) # 5263268363:blockchain 000000007cf39dfc8fccae534b39b5f362c16891abca02d0e7b1dbd5a129ee17 assert verify_pow(msg, nonce, 32) 

The result of using the SHA-256 function is considered irreversible, therefore, the selection by the “target” hash miners is performed using brute force search.

Since the bitcoin protocol uses double hashing, the miners' task is to find the second hash type of x '(having the first type of x of the hash of y, where y = H (x)), which would satisfy the condition y = H (x'). However, in order to simplify the task, the miners are looking for only a partial preimage - H (x) / 2 ^ (nk) = 0. Here n is the “size” of the result (n = 256 bits for SHA256), and k is the factor responsible for the number of zeros in the resulting hash. For example, k = 20 will require about one million attempts.

Thus, miners change the nonce parameter in the block header and consider the hash until the complexity conditions are met. When the goal is reached, the block is added to the chain.

Mining - joint efforts of network members


The complexity of the decision block is extremely high. Now miners in the bitcoin network perform about 20 million terahesh per second, and this figure is growing. For comparison, one video card produces about 30 megahesh per second. Also on his blog, engineer Ken Shirriff (Ken Shirriff) tried to solve the block with a pen and paper - his achievement was even more modest (0.67 hashes per day).


/ Number of terahesh per second, according to blockchain.info

Because of the increased (and constantly growing) complexity, miners have a hard time deciding blocks alone. Therefore, they are united in pools, where they divide the work on the decision of the block and reward. Pools issue miners tasks and monitor the amount of work done. The more contribution to the decision of the block was made by the miner, the greater share of the reward he will receive.

To estimate the percentage of work done, the pool asks miners to send information about finding partial solutions. For example, if a bitcoin blockchain requires a block hash of 15 zeros, the pool can ask to send it results with 10 zeros as proof of work. It is millions of times easier and the miner will receive such a decision several times an hour.

Special protocols are responsible for organizing the interaction between the pool and miners. For example, the Stratum protocol (still Getwork , Getblocktemplate, and others), which is used by most pools. In his blog, Ken Shirrif lists the code sent by the pool when issuing the task, which contains all the necessary information to start working on the block.

When a block is resolved, the pool forms the appropriate transaction to issue a reward, approves the header, and verifies the block. If the block is decided by a miner who was outside the pool, the service gives the participants a new task. In this case, the miners do not receive a reward for selecting a hash.

At its core, bitcoin mining resembles a kind of "arms race." At the very beginning, people mine cryptocurrency using a CPU, then the CPU power was not enough, and the industry switched to GPUs and specialized ASIC equipment .

Note that increasing the complexity of computing, in addition to regulating the number of bitcoins produced, acts as a defense against attacks on the blockchain. If mining using personal computers on a large-scale network turns out to be profitable, it will be easier for attackers to find equipment to attack. The need to invest in "iron" acts as a limiting factor and an additional protective mechanism.

But such a binding to technology and the "real world" has its drawbacks. As already noted, mining is a fairly “expensive” activity in terms of energy consumption. To date, Bitcoin blockchain consumes 48 TW / hour per year, with 30–40% of the energy used for cooling the chips.

However, it is believed that the amount of energy consumed will begin to gradually decline after 2020, when the next decrease in the block reward will occur, as mining will become less profitable. In this case, an effective way to motivate miners to continue to protect information in the blockchain remains an increase in revenue from transaction processing fees.

And for this, it is important to continue to increase public interest in the blockchain-based ecosystem. This will allow in the future to use bitcoin-blockchain as a kind of “arbiter” for permissioned-blockchains and sidechains , which will create a new generation financial system.



We offer you a few more materials about the work of blockchains from our blog:

Source: https://habr.com/ru/post/348822/


All Articles