Bitcoin has become one of the hottest trends in recent years. Last year, it surpassed Donald Trump in Google searches. Everyone is talking about bitcoin. Many people don’t exactly understand the concept.
In laymen's terms, bitcoin is a ledger. A ledger is a set of transactions. Let’s understand this with an example.
Suppose you go to a grocery store. You buy vegetables for $15. The ledger entry will be — You are paying the vegetable owner 15 dollars.
Then, let’s say the vegetable guy goes to a doctor and pays $10 for a medical examination. The ledger entry will be — The vegetable owner is paying 10 dollars to the doctor.
These all are considered as a set of transactions. This is similar to what we see in our bank account statements. A ledger is a set of transactions, and at the end, you have your account balance. This is what bitcoin is in a nutshell.
In this article, we will first learn the blockchain and how mining works. Then we will learn how to do bitcoin mining in python.
Let’s get started.
Blockchain and Transactions
In the bitcoin ledger, all transactions are stored in blocks. It contains millions of transactions from the time when bitcoin was invented. It contains several blocks for these transactions that grows every day.
These blocks are linked as a linked list. Just like linked lists have multiple nodes where all nodes are connected to another, blockchain has multiple blocks.
Let’s say, there is a blockchain consists five blocks — Block1, Block2, Block3, Block4, and Block5. Then Block1 will point to Block2, Block2 will point to Block3, and so on.
The size of one bitcoin block is one megabyte. So, In one megabyte, we store some transactions, then we move to the next block for further transactions. All these blocks are linked together.
Bitcoin Cryptography and Mining
Bitcoin protocol has some security mechanism to detect fraud. It uses cryptography to ensure secure transactions. It uses a cryptographic function called SHA256 to implement it.
It takes an input string and generates a hash that’s 256 bit long. It’s next to impossible to crack this value. It’s a deterministic value but impossible to guess.
In bitcoin, a block not only consists of a set of transactions. It has a previous hash as well as the nonce (number once).
I’ll explain what a nonce is in a bit, but first, understand this. We convert everything in a block into a string and generate a hash for that block. At any given point in time, there’s a specific requirement that the generated hash should have x number of zeros in the beginning.
Let’s say a block generated hash is 03a5x4bh34bh2jkiig243gh. As per the requirement, we need the first four digits as zero. This is where nonce comes into the picture. The number of zeros we require in our hash is known as difficulty.
Bitcoin mining is the process of guessing a nonce that generates a hash with the first X number of zeros. It consists of complex calculations where we try to find the required nonce value.
What’s the Benefit of Mining a Bitcoin?
Bitcoin miners receive bitcoin for mining a block. In 2009, for mining one block, you’d receive 50 BTC. In 2012, it was reduced to 25 BTC.
Every four years, the reward is halved for mining a block. In 2020, the reward was reduced to 6.25 BTC. But bitcoin appreciated a lot in the last few years. Even 6.25 BTC per block means 190,000 dollars. That’s quite a lot of money for doing such work.
Many people around the world are doing bitcoin mining. It’s not very difficult, but it is a time-consuming task. It takes a lot of computation power to get the right value. If 10 people are doing the guessing work, then whoever gets the result first wins the reward.
Therefore, it takes both time and luck to win the reward in bitcoin mining.
Let’s Do Bitcoin Mining With Python
That’s it. We can mine the bitcoin with these 12 lines of code in python.
Walkthrough of the code
Line 1: Import the sha256 library in our project.
Line 2: Declare a variable with the maximum value of nonce up to which you want to guess. It can be increased or decreased based on your system’s computation power.
Line 3–4: We define a function SHA256 to generate a hash value.
Line 5: Define another function for mining where we take input parameters of the block number, transactions, previous hash value, and difficulty (number of zeros needs to be added as a prefix in the generated hash).
Line 6: We create another prefix_str variable to store the Hexa value after adding the required prefix. This string will be later on used for comparison with the generated hash.
Line 7–9: A for loop is iterated for nonce values to generate a new hash by calling the SHA256 function that we generated on Line 3.
Line 10–12: We compare the newly generated hash value’s prefix with the desired prefix. If it’s a match, then we print the nonce value for which bitcoin is mined and returned this generated hash.
I’ve taken the difficulty of eight for my testing. The application had to iterate around 1.2 million times before it guessed the correct nonce.
The current difficulty level going for blocks is 20. It means you have to guess a nonce that generates a hash with a prefix of 20 zeros. This type of computation can take up to a year on normal systems.
Bitcoin mining requires special hardware. Some of the popular ones are the DragonMint T1, Antminer T9+, Antminer R4, Avalon6, and Antminer S9. You can check other hardware here.
This is the website where you can get the block information for mining. If you are really willing to do it, then I’ll suggest investing a little in hardware for a better chance of mining a bitcoin.
- Get latest and updated exam material from mockdumps with passing guarantee in first try. We provide 24/7 customer support to our honorable students