What exactly bitcoin miner does ?
Kamal Mustafa
Dec 23 '17 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.
How we can make a difference as software engineers
As the holiday season rolls around again the idea of doing good in the world ...
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 ;)