loading...

What exactly bitcoin miner does ?

k4ml profile image Kamal Mustafa ・5 min read

I wrote an article about the topic. It's in Malay though, but I think Google Translate's translation is acceptable and can be understood by English reader, although some parts sound a bit funny, especially the dice part ;)

The primary reason I wrote this is because after searching all info about bitcoin mining, most of it fall into either 2 groups of articles:-

  • Type 1 - The media type article where they just mention about "things" not describing what exactly the "things" is.
  • Type 2 - The "thesis" type article where it full of algorithm and complex stuff but still failed to explain what the "things" is.

So I need a middle ground. Something like ELI5, just to satisfy the dev soul inside me. Hopefully I done it. At least now I can sit down and ignore all the bitcoin hype, or maybe the next thing I want to understand is the proof-of-stake concept which I think a more novel idea than just burning cpu to find some stupid random number.

UPDATE: I've included the translated version below, copied from Google translate with some minor correction.


The main purpose of bitcoin mining is to confirm the transactions to be included in the blockchain. Because the bitcoin network is decentralized, no single entity is given the responsibility to verify the transaction. Each node in the network can enter a transaction into a blockchain if it meets certain conditions.

These conditions need to be done by bitcoin miners. The idea is, if anyone can enter a transaction into a blockchain, then we need to make sure that the operation performed by the node has a very high degree of difficulty. This is to discourage certain entities from entering counterfeit transactions but rather encouraging them to collaborate in continuing to support the network. If they comply with these terms, besides being able to include the transactions into the blockchain, they are also awarded a number of new bitcoins that indirectly function as a source of new money supply in the bitcoin network.

The concept of doing something to be accepted is called 'proof of work' (POW). POW requires the use of high-power computing that directly requires electrical power source and as we know it, it definitely needs a lot of capital. But what exactly does POW need to be done by bitcoin miner? We always hear that they need to solve difficult math problems. But what is the exact math problem ?

Actually no exact mathematical problems need to be solved. The formula has been ready for decades. What needs to be done is to convert some variables in the hope of obtaining answers that meet the requirements set by the network. Every transaction to be included in the blockchain, should have an identification or ID. This ID can be obtained by running 'hashing' operations on the transaction. This is a common operation and every programmer must have used it for some purpose.

Examples of hashing if using Python are: -

import hashlib print(hashlib.sha256('hello'.encode('utf8')).hexdigest())

The resulting output is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824. The above example uses the sha256 hashing function where the resulting output is a random 256 bits number. In the above example, we display the hash in the form of hexadecimal in which each character represents 4 bits, so the length of the whole character is 64. The main feature of the hash function is for each of the same inputs it will produce the same hash. But at the same time, it is not possible to know exactly what the input is based on the output.

Any hash can be an ID to a blockchain, as it is just a random number generated by the hashing function as shown above. But that would be very easy for a miner. So the bitcoin protocol specifies the number resulting from a hash function, must be less than a target number. This is what makes this hash process difficult.

Simple analogy is when you throw a dice. If the condition is set you must get a number less than 6, then it is very easy. With one throw, you might get the number. The difficulty here is 1, or 6/6. But what if the condition is the number you need to get is 3? It will be 2 times harder. The degree of difficulty (difficulty) becomes 6/3. The same concept is applied in bitcoin mining. Only the numbers used are very large. How big is it? Ok look at the small numbers first. For 8-bit numbers, the biggest number we can get is 256 or 2 ^ 8. For 16 bits, 65536 and for 32 bits 4294967296. Next you can calculate for yourself.

So 256 bits are a very big number. What if the target number to get is very small? It will require a million-times trial for the possibility of getting the number. The bitcoin network however will 'adjust' this target number based on the overall level of hashing capabilities. If many are joining mining with very powerful computers, this target number will be lowered so that it becomes more difficult. If the hashing capability decreases then the target number will be increased so that it gets easier. The main objective is to create a new block always in 10 minutes.

To see how difficult it is to get a number that is less than the target number, we can try it with a simple program in Python as below: -

import hashlib

target = '0000000000000000008e3dff56e95d4b9f954d64674e935e06505e86e02134e8'
target = '0111111111121875ca8e3dff56e95d4b9f954d64674e935e06505e86e02134e8'
target = '5fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
target = '9fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
nonce = 1
while True:
    trans = 'trans 1' + 'trans 2' + 'trans 3' + str(nonce)
    hash = hashlib.sha256(trans.encode('utf8')).hexdigest()
    if int(hash, 16) < int(target, 16):
        print(hash)
        print(nonce)
        print(bin(nonce))
        break
    nonce += 1

In the example above, the first target is drawn from hash block #500735. It's almost impossible to get a lesser hash than that number using the python program above and run it just over the laptop. Although the target is raised slightly, by changing only the first number to 0, it is still impossible. Even with a very big number like the third target it's still tough.

Although difficult, what we see here miners is not solving a math problem. They only look for a number with repeated attempts, with each attempt, they will convert some permitted variables such as nonce or restructure the transactions to be verified so that they can get different hash numbers. The first miner can guess the number that meets the conditions will be declared the winner and the confirmed block will be inserted into the blockchain.

Posted on by:

k4ml profile

Kamal Mustafa

@k4ml

Python/Django Developer at xoxzo.com.

Discussion

markdown guide
 

Miners in a blockchain compete to verify blocks. In the Bitcoin world, a block contains transactions ( from several hundreds to several thousands ). Miners compete to solve a complex mathematical problem based on a cryptographic hash algorithm. This solution to the problem is called the Proof-Of-Work. That proof is then added to the block's identity.

Miners validate new transactions and record them on the global ledger ( blockchain ). Mining is the invention that makes a decentralized security system.

In Bitcoin, miners who solve the mathematical are rewarded with bitcoins, either new bitcoins or taken from the transactions fees.

Hope it helps!

 

Hi Damien. Thanks for chipping in, but that Type 1 explanation ;)