📜 ⬆️ ⬇️

Bitcoin in a nutshell - Mining

Even people infinitely distant from the topic of cryptocurrency, most likely heard about mining. Probably you, dear reader, thought about switching on your gaming Pentium 4 at night, and in the morning wake up already rich.


But, as it often happens in the blockchain world, there are a lot of those who have heard, but only those who really understand the process from beginning to end. Therefore, in the last chapter, I tried to cover all the details in as much detail as possible, starting from the technical implementation of PoW, ending with the profitability of mining on video cards.


mining_meme


Book



Table of content


  1. Explain me like I'm five
  2. Sky is the limit?
  3. Reward
  4. Hard challenge
  5. Technical side
  6. 2 Blocks 1 Chain
  7. Hardware
  8. Conclusion
  9. Links

Explain me like I'm five


Mining , as well as mining (from English mining - mining) is the activity of maintaining a distributed platform and creating new blocks with the opportunity to receive remuneration in the form of emitted currency and commission fees in various cryptocurrencies , particularly in Bitcoin . Computations are required to ensure protection against the re-use of the same currency units, and the link between mining and emissions encourages people to spend their computing power and support network operation - Wikipedia

If on the fingers, then mining is a critical process for Bitcoin, consisting in the creation of new blocks and pursuing two goals at once. The first is the production of money supply. Each time a miner creates a new block, he is rewarded with an award for the Nth number of coins, which he then spends somewhere, thus launching new funds into the network.


The second and much more important goal is to ensure the operation of the entire network. Surely, reading the previous articles, you already asked yourself the questions "Who is the person who checks the transaction scripts?" or "If, as the input, I indicate the output already used, at what point will it be noticed?" .


So, all these actions are performed first of all by the miners. Well, in fact, each member of the network to one degree or another ensures its security. To synchronize Bitcoin for so long not because you have to download 100 GB, but because you need to check every byte, count every hash, run every script, and so on.


But if you draw the whole process, starting from pressing the "Send" button in your wallet and ending with viewing the block with your transaction somewhere on blockchain.info , then the miners will decide whether your transaction will be in the block or not.


Sky is the limit?


nope


To begin with, let’s go through the first point again and discuss the concept of the money supply.


One of the fundamental pieces that cryptocurrency supporters often flaunt is deflation that was originally pledged . This is due to the fact that even at the design stage of the system, a total limit of 21 million coins (approximately) was indicated, and even if you really want to, you will not be able to raise this threshold. Unlike the ruble or the dollar, which at the request of the treasury can be printed in any quantity, which sometimes leads to dire consequences, as in Zimbabwe .


BTW, not everyone thinks deflation is such a definite plus .


Reward


The next good question is where did the figure of 21 million come from?


I think you understand that the amount of coins issued at a particular point in time is equal to the amount of rewards for the blocks created by that moment . Quite an obvious fact, given that there is only one way in which new coins fall into the network.


But the remuneration is not fixed, and moreover, every 210,000 units (about once every 4 years), it is halved.


consensus.nSubsidyHalvingInterval = 210000; // https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L73 

So, for example, when it all started in January 2009, the unit reward was 50 BTC. After 210,000 units, in November 2012 it fell to 25 BTC, and more recently, on July 9, 2016, it dropped to 12.5 BTC .


It is easy to calculate the exact number of Satoshi to be produced, assuming that Bitcoin does not stall in the next 200 years:


 start_block_reward = 50 reward_interval = 210000 def max_money(): # 50 BTC = 50 0000 0000 Satoshis current_reward = 50 * 10**8 total = 0 while current_reward > 0: total += reward_interval * current_reward current_reward /= 2 return total print "Total BTC to ever be created:", max_money(), "Satoshis" # Total BTC to ever be created: 2099999997690000 Satoshis 

The picture below shows the production curve, which will be more and more smoothly approaching the mark of 21 million BTC, reaching a peak around 2140. At this time, the block reward will be 0 BTC.


btc_curve


One can only guess what will happen with Bitcoin, but one thing we can know for sure is that the miners will not be left without money at all. At least they still have a transaction fee , another thing is that this commission can increase by an order of magnitude.


Take for clarity any fresh block, for example # 447119 . The amount of commissions from all transactions in the block is approximately 0.78 BTC, while the reward for it is 12.5 BTC. That is, if tomorrow the reward disappears, then in our case the commission should increase by more than 16 times in order to level this unpleasant event. It is clear that no micropayments here already and does not smell.


Mining for dummies


Let's try again to present the process of mining at our, so far primitive level.


There is a network with a bunch of participants. Some of the participants call themselves miners - they are ready to collect new transactions on their PC, check them for validity, then somehow mine the new unit from them, scatter this unit over the network and get money for it. The logical question is - if everything is so simple, then why isn't every member of the network doing this?

It is clear that if everything would be as I described it now, then the blocks would go out a hundred times a second, there would be so much currency that no one would give a cent for it, and so on.


Therefore, Satoshi was forced to come up with an algorithm with the following properties:



Proof-of-Work (PoW)


Most likely you are now arriving in complete prostration and do not really understand how this is even possible. But Satoshi did not lose his head and was able to come up with a solution for all these problems - the algorithm was called Proof-of-Work , that's what it looks like (I advise you first to read Bitcoin in a nutshell - Blockchain ):


Let you - the miner. You have 10 transactions that you want to merge into a block. You check these transactions for validity, form a block out of them, indicate 0 in the nonce field and count the hash of the block. Then change the nonce to 1, again consider the hash. And so on to infinity.

Your task is to find a nonce in which the hash of the block (256 bit number) is less than a predetermined number N. The search for such a hash is possible only with a blunt search of nonce, there are no beautiful algorithms here. Therefore, the faster you want to find a nonce , the more power you will need.

The number N is the parameter (it is also called the target ) that the network adjusts depending on the total power of the miner. If tomorrow blocks start to go out, conditionally speaking, once every three minutes, then N will somehow be reduced, the time required to search for nonce will take more and the block time will again grow to 10 minutes. And vice versa.

Technical side


General view of the algorithm


It's time to move from words to deeds and demonstrate how Proof-of-Work and mining work as a whole. And in my humble opinion, there is nothing better than to show the whole process at all right in combat conditions. To do this, we will immediately write our mining node and even try to make a new block before anyone else, although the chances of success are slim.


Receive transactions


In an amicable way, you need to dive into the protocol specification again, establish contact with other nodes and wait for fresh transactions to be sent to us. In this case, we will have a real real-time miner, no worse than ready-made solutions (but this is not certain).


I suggest to go in a simplified way. Open blockchain.info and select several transactions from the "Recent Transactions" list. They just got into the network and so far do not belong to any of the blocks. Next, open another block explorer - chainquery.com . He is able to issue transactions in the raw format and using hashes we get transactions in the form already familiar to us. I limited myself to two ( one , two ):


 txn_pool = [] txn_pool.append("0100000001440d795fa6267cbae00ae18e921a7b287eaa37d7f41b96ccbc61ef9a323a003d010000006a47304402204137ef9ca79bcd8a953c0def89578838bbe882fe7814d6a7144eaa25ed156f66022043a4ab91a7ee3bf58155d08e5f3f221a783f645daf9ac54fed519e18ca434aea012102965a03e05b2e2983c031b870c9f4afef1141bf30dc5bb993197ee4a52f1443e0feffffff0200a3e111000000001976a914f1cfa585d096ea3c759940d7bacd8c7259bbd4d488ac4e513208000000001976a9146701f2540186d4135eec14dad6cb25bf757fc43088accbd50600") txn_pool.append("0100000001517063b3d932693635999b8daaed9ebf020c66c43abf504f3043850bca5a936d010000006a47304402207473cda71b68a414a53e01dc340615958d0d79dd67196c4193a0ebcf0d9f70530220387934e7317b60297f5c6e0ca4bf527faaad830aff45f1f5522e842595939e460121031d53a2c228aedcde79b6ccd2e8f5bcfb56e2046b4681c4ea2173e3c3d7ffc686ffffffff0220bcbe00000000001976a9148cc3704cbb6af566598fea13a3352b46f859581188acba2cfb09000000001976a914b59b9df3700adae0ea819738c89db3c2af4e47d188ac00000000") 

Check


The next step is to check the received transactions. I will not do this, just list the main points:



Some miners reject transactions with a zero or too small commission, but this everyone decides for himself.


Sort


Just in case, I will explain that nothing prevents you from including transactions in the block in any order, as long as they pass all the checks. In my case there are only two transactions, so there is no point in sorting them all the more. But do not forget that the block size is limited to 1 MB, so if you have 10,000 transactions in the pool, it will be wise to sort them out by commission and write only the most expensive ones to the block.


BTW Often come across articles / books that say that before mining a new block, Bitcoin Core sorts transactions by the special parameter priority , which is counted as


 Priority = Sum (Value of input * Input Age) / Transaction Size 

This was true up to version 0.12.0, then the sorting by priority was turned off .


Get reward


block


If you look at the structure of any block, then the very first coinbase transaction always goes - it sends the reward to the address of the miner. Unlike regular transactions, coinbase transaction does not waste exits from the UTXO pool as inputs. Instead, it has only one entry, called coinbase , which "creates" coins from nothing. There is only one way out of such a transaction. He sends to the miner's address a reward for the block plus the amount of commissions from all transactions in the block. In my case, this is 12.5 + 0.00018939 + 0.0001469 = 12.50033629 .


Let's take a closer look at the coinbase structure of the transaction, and more specifically, its input. Just in case, let me remind you what the input for a “normal” transaction looks like:



Here are three differences in the coinbase transaction entry:



Often the so-called extra nonce is inserted into coinbase data , more details here . The bottom line is that the desired nonce may not be found, in which the hash of the block is less than the target (in fact, this will happen in most cases). Then it remains to change something in the transaction to get other hashes, for example - UNIX timestamp . But if you read Bitcoin in a nutshell - Blockchain , then you know that the timestamp won't change much either, otherwise the other nodes will reject your block. The solution turned out to be quite simple: it is enough to add any number to the coinbase data and change it if the required nonce was not found for the current header .


The process of creating a new transaction is described in detail in the chapter Bitcoin in a nutshell - Protocol , so here I just give you the received coinbase transaction , all the code, as usual, is available on [Github] ():


 coinbase_txn = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff8a47304402207e8495986ec27ed4556fee9dcd897ea028d4eb2023959c2299eb573e0771dee702201489e40115ccc45d4c23f1109cb56b513543517f3efc0031965ad94d94d3d2d901410497e922cac2c9065a0cac998c0735d9995ff42fb6641d29300e8c0071277eb5b4e770fcc086f322339bdefef4d5b51a23d88755969d28e965dacaaa5d0d2a0e09ffffffff01ddff814a000000001976a91478e10cf8e4bd38266d8fd4ed5c8b430d30a3cde888ac00000000" 

It remains only to count for these three transactions merkle root . To do this, use the code snippet from Bitcoin in a nutshell - Blockchain :


 txn_pool.insert(0, coinbase_txn) txn_hashes = map(getTxnHash, txn_pool) print "Merkle root: ", merkle(txn_hashes) # Merkle root: 4b9ff9ab901df82050f858accde99b9169067acafaeade25598ea5505fb53836 

Target


As I wrote above, all mining comes down to finding a hash of a block less than the number called target . In the block structure, this number is written in the bits field, for example, for block # 277,316, target was equal to 1903a30c .


 $ bitcoin-cli getblock 0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4 { "hash" : "0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4", "confirmations" : 35561, "size" : 218629, "height" : 277316, "version" : 2, "merkleroot" : "c91c008c26e50763e9f548bb8b2fc323735f73577effbc55502c51eb4cc7cf2e", "tx" : ["d5ada064c6417ca25c4308bd158c34b77e1c0eca2a73cda16c737e7424afba2f", 418 more transactions], "time" : 1388185914, "nonce" : 924591752, "bits" : "1903a30c", // Here it's "difficulty" : 1180923195.25802612, "chainwork" : "000000000000000000000000000000000000000000000934695e92aaf53afa1a", "previousblockhash" : "0000000000000002a7bbd25a417c0374cc55261021e8a9ca74442b01284f0569", "nextblockhash" : "000000000000000010236c269dd6ed714dd5db39d36b33959079d78dfd431ba7" } 

In fact, two numbers are written to bits at once: the first byte 0x19 is the exponent, the remaining three bytes 0x03a30c are the mantissa. In order to get the target from bits, you need to use the following formula: target = mantissa * 2^(8 * exponent - 3)) . In the case of block # 277.316 it turns out:


 >>> bits = 0x1903a30c >>> exp = bits >> 24 >>> mant = bits & 0xffffff >>> target_hexstr = '%064x' % (mant * (1 << (8 * (exp - 3)))) >>> target_hexstr '0000000000000003a30c00000000000000000000000000000000000000000000' 

Another term reflecting the complexity of mining is difficulty . For example, for block # 449.584 it was equal to 392,963,262,344.37 . This parameter is the max_target / current_target ratio, where max_target is the maximum possible target, namely 0x00000000FFFF0000000000000000000000000000000000000000000000000000 ( 0x1d00ffff in bits format). It is bits that are typically specified in all block explorer.


BTW the smaller the target, the more difficulty and vice versa.


Pow


Now that you have understood all the nuances, you can launch a miner:


 import hashlib import struct import sys # ======= Header ======= ver = 2 prev_block = "000000000000000000e5fb3654e0ae9a2b7d7390e37ee0a7c818ca09fde435f0" merkle_root = "6f3ef687979a1f4866cd8842dcbcebd2e47171e54d1cc76c540faecafe133c39" bits = 0x10004379 # Not the actual bits, I don't have synced blockchain timestamp = 0x58777e25 # Calculate current time with this code: # hex(int(time.mktime(time.strptime('2017-01-12 13:01:25', '%Y-%m-%d %H:%M:%S'))) - time.timezone) exp = bits >> 24 mant = bits & 0xffffff target_hexstr = '%064x' % (mant * (1 << (8 * (exp - 3)))) # '0000000000000000000000000000000000437900000000000000000000000000' target_str = target_hexstr.decode('hex') # ======== Header ========= nonce = 0 while nonce < 0x100000000: # 2**32 header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] + merkle_root.decode('hex')[::-1] + struct.pack("<LLL", timestamp, bits, nonce)) hash = hashlib.sha256(hashlib.sha256(header).digest()).digest() sys.stdout.write("\rNonce: {}, hash: {}".format(nonce, hash[::-1].encode('hex'))) sys.stdout.flush() if hash[::-1] < target_str: print 'Success!' break nonce += 1 

Hash rate


If you have waited for the cherished line of Success! you either have an Intel Core i7 or have a lot of free time. I have no idea when this code will finish its work and whether it will find nonce at all, because the current complexity is simply monstrously great. Even if we assume that our program is capable of shorting 100,000 hashes per second (and this is not so), then it is still a million times slower than any real miner, so it can take several days to search for nonce .


So that you understand the scale of the problem: there is a hashrate metric. It reflects the total power of miners in the Bitcoin network, the unit of measurement is SHA256 hashes per second. Here is her schedule :


hashrate


We assume that the hashrate is 2.000 PH / s = 2.000.000 TH / s = 2.000.000.000 GH / s = 2.000.000.000.000 MH / s = 2.000.000.000.000.000 KH / s. And our program cannot even master 100 KH / s, so there is no point in competing with the entire network.


2 Blocks 1 Chain


Fork


Let's imagine for a moment that the miners are looking for block # 123456. And at about the same time, he was found by two independent miners, one of whom lives in Australia and the other in the United States. Each of them begins to scatter its version of the block over the network, and as a result it turns out that one half of the world has one blockchain, and the other has the other.


Is it possible and what will happen in this case?


fork1


Yes, it is possible. Moreover, this happens quite often. In this case, each node continues to adhere to its version of the blockchain until someone finds the next block. Suppose that the new block continues the “green” branch, as in the picture below.


fork2


In this case, those nodes that stick to the “red” version automatically synchronize the green one, because the rule in the Bitcoin world is the “true” longest version of the blockchain . The "red" version of the blockchain will be simply forgotten, along with awards for those who found it.


You may ask: what if the fork goes further? That is, simultaneously with the “purple” block, will they find another one that will continue the “red” version of the blockchain?


Most likely, this book will be read not only by people with a good mathematical education, therefore I will give the most general answer - this is absolutely possible. But the probability of even the first fork is rather small, the second one is even less and so on. For you to understand, the longest fork in the entire history of Bitcoin was only 4 blocks . So at some point one of the branches will still break out ahead, and the entire network will switch to it.


If you are interested in this problem from the perspective of probability theory, then you can read "What is the probability of forking in blockchain?" This question is also well described in the famous "Bitcoin: A Peer-to-Peer Electronic Cash System" by Satoshi Nakamoto .


51% attack


On the simple fact that the longest chain in the blockchain is dominant, the whole attack is based:


Imagine that you are a scammer and buy goods for 1000 BTC in any store. You negotiate with the seller and send him money. The seller checks the blockchain, sees that such a transaction really was, has passed all the checks and even got into some block, for example # 123. After that, the seller goes to the post office and sends you the goods.
')
At this time, you turn on your mining farm and start mining, starting with block # 122 . If you have enough power, you can outrun the rest of the network and quickly count to block # 124, after which the whole world will switch to your version of the blockchain. In this case, your transaction at 1000 BTC, you will not include in any of the blocks, which means it will be forgotten forever, as if it never existed. As a result, the seller will lose the goods and not receive their money.

I will not go into the theory of probability, but to carry out such an attack is impossible, unless you have at least half the hashrate of the entire network. Read more in bitcoin.pdf .


Nevertheless, some mining pools have very significant capacities. For example, BTC Guild in 2013 almost overcame the threshold of 51% of hashrate. At some point they immediately mined 6 blocks in a row, so that if they wanted to, they could carry out this attack. Therefore, it is recommended to consider the transaction confirmed only after it was created 6 blocks on top.


Hardware


You can immediately forget about mining on a CPU or GPU. To make you understand, below is a hashrate for the beginning of 2017. We assume that it averages 2.300.000 TH / S, that is, 2.300.000.000.000 MH / s. For comparison, the most brutal video cards, such as the ATI Radeon HD 5870 Eyefinity or the AMD Radeon HD 7970 (x3) , produce at best 2000 MH / S. Among the processors, the first place is occupied by the Xeon Phi 5100 with a ridiculous 140 MH / s.


hashrate


So even on the basis of the rate of $ 1000 / BTC and having on hand 10,000 MH / s, you will earn an average of 20 cents per month .


gpu


CPU mining has ceased to be cost-effective in 2011, the GPU lasted until about 2013, but also went bankrupt when the so-called application-specific integrated circuit - ASIC became widespread. These are special chips sharpened for mining at the iron level. The simplest ones cost around $ 100, which is much cheaper than a top-end video card, but at the same time they can produce from 1 TH / s.


That is, other things being equal, having two Antminer S9 for $ 3.000 each, you will earn almost $ 700 per month (excluding electricity bills)



But this is not all. You can team up with other miners in the mining pool and start mining together, and share the earned money in proportion to the invested capacity. This is obviously much more profitable than trying to earn at least something alone, which is why pools are the main driving force in the mining world today. At the beginning of 2017, the main players in the pool market are AntPool , F2Pool and Bitfury , providing more than 40% of the hashrate of the entire network.


Pools


Conclusion


On this high note, I finish my story about the technical device Bitcoin. Sources of text plus code examples here , there is also a pdf version. Pull requests welcome, ask your questions in Issues or in the comments.


Obama out



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


All Articles