A Deep Dive Into How Bitcoin Mining Works

A Deep Dive Into How Bitcoin Works

With the massive rise in cryptocurrency popularity today, many people have heard of Bitcoin. Here we will take a deep dive into the inner mechanics of Bitcoin and mining.

What is Bitcoin Mining?

Bitcoin is a cryptocurrency, meaning it can be used by computers to send money. It runs on computers around the world. These computers all run the same computer program–the Bitcoin program. The Bitcoin program contains some code for Bitcoin mining. Anybody can read the Bitcoin program online. If you have a computer, you are able to download the Bitcoin program to your computer. After you download it, you can run it. This means that your computer is now part of the Bitcoin network, communicating with similar computers around the world. These computers also have the Bitcoin program running and they compete with you to win Bitcoin. However, in order to have a chance at earning money, you can’t just use a normal computer. People buy specialized computers in order to mine Bitcoin–computers that may cost upwards of thousands of dollars.

Bitcoin runs on a computer network. Each computer on the Bitcoin network runs the Bitcoin program.

Q1: Where is the source code (the code which makes up the Bitcoin program) located?

Bitcoin Mining is a Guessing Game

Computers on the Bitcoin network guess numbers in order to win Bitcoin. These numbers are 64-digit hexadecimal numbers, meaning that the letters a, b, c, d, e, and f can also serve as digits, representing 10, 11, 12, 13, 14, and 15 respectively. How does one know that they have made a right guess? If the number they guess is lower than a target number set by the Bitcoin network. This number is called the hash target. The word hash comes because in order to make a guess, a computer has to send a number through a hash function. This number is called a nonce, which stands for number used once. The nonce is passed through the hash function to generate the 64-digit number we referred to above, known as a hash value.

Q2: What do the terms, nonce, hash function, and hash value mean? Which one is compared with the hash target to determine if a miner has won Bitcoin?

Hash Functions: The Heart of Bitcoin

No, not this kind of hash.

A hash function is needed because without it, some people could cheat by just coming up with a number below the hash target. A hash function prevents cheating in a way similar to a one-way door. Given a hash, it is next to impossible to find a nonce that, when fed through the hash function, generates the hash. Given a nonce, it is very easy to feed that nonce through the hash function to get a hash. However, if you change the nonce number slightly, say by adding one to it, and feed it through the hash function, the hash changes dramatically. It is very hard to predict for a given nonce what the hash function will output without feeding the nonce through the hash function directly. Bitcoin’s hash function is a hash function called SHA-256, which is very secure.

Q3: What happens if the hash target is set to the highest 64-digit hexadecimal number possible, consisting of all e’s?

Given these properties of Bitcoin’s hash function, it is very difficult to cheat. In practice, miners have no choice but to randomly guess nonce values. These nonce values are fed through Bitcoin’s hash function to generate hashes, which are then compared with the hash target. You can look at the total number of guesses the Bitcoin network is performing here. The number of guesses is given in TH/S, which stands for terahashes per second. One terahash is equal to one trillion hashes per second. Currently the total hash rate is around 200 million TH/S. As you can see, the hashing power of the Bitcoin network has grown exponentially in recent years. For reference, a typical CPU on your laptop is capable of computing 20 KH/S (kilohashes / second). This is an infinitesimal amount of computing power compared to the Bitcoin network. If the hashing power of your laptop computer were represented by a single ant, then the hashing power of the Bitcoin network would be represented by all ants alive on Earth today. To improve their chances, Bitcoin miners invest in specialized hardware and participate in Bitcoin mining pools, detailed below.

Q4: If the total hash rate of the Bitcoin network decreased, how would your ability to mine Bitcoin change (assuming you are still using the same computer that has the same hash rate)?

Rewards and Difficulty of Mining

Given that the hashing power of the Bitcoin network has increased rapidly in recent years, you might wonder if there would be more and more correct guesses. Even though all the computers are guessing randomly, a higher rate of guesses would lead to a higher rate of correct guesses for a certain fixed probability of a correct guess. Thus, wouldn’t Bitcoin be produced at a faster and faster rate as more miners joined in? The answer is that Bitcoin is designed to keep coin supply limited by two mechanisms–block time and block reward. When you guess a correct hash value, a new block is added to the end of Bitcoin’s blockchain. However, the block time parameter controls how fast the blocks are added, and the block reward controls how much reward a miner receives per block. Bitcoin’s program is designed so that new blocks are added to the blockchain at a roughly constant rate.

Q5: How does Bitcoin adjust its difficulty so that new blocks are created at a roughly constant rate? Hint: consider hash functions.

Block time is a measure of how fast blocks are created by the Bitcoin network. In Bitcoin, the block time is set to 10 minutes. This means that roughly every 10 minutes, a new block is added to Bitcoin’s blockchain. As we have seen above, mining is the way Bitcoins are added to the blockchain. Bitcoin keeps the block time constant by adjusting the mining difficulty. It does so by adjusting the hash target. Recall that the hash target is the hash value below which a miner has successfully created a new block and won Bitcoin. As the hashing power of the Bitcoin network increases, the hash target decreases so that it becomes harder and harder for individual miners to mine blocks. The net result is that blocks are created roughly every 10 minutes. You can view a snapshot of how fast blocks were created here.

What Rewards Do Miners Get?

After guessing a correct hash value, a miner receives a reward known as a block reward. Initially set to 50 BTC in 2009, this block reward is designed to halve every 210,000 blocks, or approximately every four years. In 2012, the reward halved to 25 BTC. In 2016, the reward halved again to 12.5 BTC, and currently the reward is 6.25 BTC, which is what it is equal to now. The reward will keep on halving every four years, limiting the total supply of Bitcoin to 21 million BTC. As the block reward halves again and again, miners receive less and less block reward per block. However, there is another reward component that Bitcoin miners receive known as the transaction fee. The Bitcoin transaction fee is paid to by people who send transactions on the Bitcoin network in order to make their transactions go through. The transaction fee incentivizes miners to continue mining, contributing to the security of the Bitcoin network. We will talk more about transaction fees below.

Q6: How is the total supply of Bitcoin capped at 21 million BTC?

Mining Pools

Miners contribute to the stability of the Bitcoin system through Proof of Work (the guessing game described in Part 1) and by verifying transactions, as we will discuss below. As blocks are only created every ten minutes, individual miners may go long periods of time without getting any rewards. For example, if a miner controls 0.001% of the network’s hashing power, then it is expected to take them 1 / 0.00001 * 10 = 1,000,000 minutes = 1.9 years to earn a block reward. To combat this, groups of miners have created mining pools. These mining pools divide block rewards between members in a way proportional to their hashing power. Miners in a mining pool all help contribute to the success of the mining pool, pooling their rewards together fairly. The rewards received by each miner are thus more stable and predictable. You can view details about the current Bitcoin mining pools here https://blockchain.info/pools.

Q7: Go to https://www.blockchain.com/explorer and find the info page about a block that has been added. Which mining pool did it come from? What is the block reward and transaction fee reward? What do the other numbers on the page mean?

Is Bitcoin Mining Profitable?

Whether or not Bitcoin mining is profitable depends on several factors: the price of Bitcoin, the price of electricity, what hardware you have, and the hashing power of the network. Generally, the higher the price of Bitcoin, the cheaper the electricity, the faster the hardware, and the lower the hashing power of the network, the more chance one has of making a profit. Without specialized hardware, it is extremely unlikely that you will make any profit. Bitcoin miners today rely on specialized hardware such as Application-Specific Integrated Circuits (ASICs), Field-Programmable Gate Arrays (FPGAs), and Graphics Processing Units (GPUs) to make a profit. However, in recent years Bitcoin mining has come to be dominated by ASICs, which have superior performance to GPUs and ASICs at the cost of being engineered for one specific purpose: Bitcoin mining. There are several websites where one can estimate the profitability of mining: https://www.cryptocompare.com/mining/calculator/btc. However, note that these calculators neglect to include the up-front costs of buying mining hardware, which can easily cost thousands of dollars or more.

Pen and paper calculations–the OG way of mining Bitcoin.

Q8: Suppose you were to try Bitcoin mining on your computer (20 KH/S) without participating in any pools. How long would it take to mine 1 Bitcoin? Is this feasible?

Why is Mining Important?

Bitcoin mining is important because it makes the Bitcoin network safe. It ensures that an opponent (or opponents) with less than 50% control of the hashing power in a network has a very low probability of overpowering the network to create fraudulent transactions. This is because the honest portion of the network will produce blocks at a faster rate than the dishonest portions, and nodes in the Bitcoin network treat the longest chain in the network as the truth. A transaction can only be valid if its hash is valid, and the hash is valid only if the miner has put in the work required to determine that correct hash. Bitcoin mining serves as a mechanism for distributing power through all nodes in the network, preventing malignant nodes from acquiring undue influence. It serves as a mechanism for both verifying transactions and for minting new Bitcoins. Minting new Bitcoins comes through block rewards. In addition to mining, nodes on the blockchain network verify transactions by making sure they are legitimate.

Q9: What would happen if a miner gained control of more than 50% of the hashing power in Bitcoin’s network?

Drawbacks of Bitcoin Mining

Bitcoin mining is incredibly energy-intensive and its energy usage is exponentially increasing. Miners are incentivized to throw as much compute power as possible in order to win Bitcoin. This compute power is needed for hashing as described above. Thus, Bitcoin mining becomes an arms race between miners to compete for the fastest mining hardware and cheapest electricity costs. Bitcoin is estimated to use more than seven times as much electricity as all of Google’s global operations. However, Bitcoin needs all this electricity in order to secure its network. This is a consequence of Bitcoin’s proof of work (PoW) consensus mechanism. It is estimated that a single Bitcoin transaction requires nearly 7 billion joules, or nearly 60 gallons of gasoline, to complete. Given this energy efficiency, alternative consensus mechanisms, such as Proof of Stake and Proof of History, have emerged which enable cryptocurrencies to consume less energy. While Bitcoin itself may have its flaws, the ideas spawned by it have given rise to newer blockchains that make improvements over it.

Don’t be this guy.

Congratulations for making it all the way to the end! Hopefully this guide has given you some intuition for how Bitcoin mining works. Now, armed with this knowledge, you should have a good knowledge for how crypto mining works in general since mining for other cryptocurrencies is very similar to, if not identical to, Bitcoin.

Q10: Given that Bitcoin can only process a constant 7 transactions per second, why does Bitcoin’s energy consumption keep on going up?

Next Steps

Curious to learn more? A fantastic next step would be to read the Bitcoin Whitepaper. It covers many of the topics that we have mentioned in this blog post in greater detail.

References / Further Resources

Investopedia article on Bitcoin mining: https://www.investopedia.com/tech/how-does-bitcoin-mining-work/

Bitcoin whitepaper: https://bitcoin.org/bitcoin.pdf

Investopedia article on target hash: https://www.investopedia.com/terms/t/target-hash.asp

Wikipedia article on Proof of Work: https://en.wikipedia.org/wiki/Proof_of_work

In-depth technical blog post on Bitcoin’s block reward halving mechanism: https://ma.ttias.be/dissecting-code-bitcoin-halving. The block reward is controlled by the GetBlockSubsidy function which is here: https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L1417

Bitcoin whitepaper: https://bitcoin.org/bitcoin.pdf

NYTimes article on Bitcoin mining energy usage: https://www.nytimes.com/interactive/2021/09/03/climate/bitcoin-carbon-footprint-electricity.html#commentsContainer

Solana post on the energy usage per transactions of various blockchains: https://solana.com/news/solana-energy-usage-report-november-2021

--

--

--

A Yale Blockchain Club project, the YBB is run by Yalies in the blockchain club who are passionate about sharing interesting and groundbreaking blockchain stories with the Yale community and beyond.

Recommended from Medium

Free Crypto Portfolio Tracker based on Google Sheets

How Australia To Change The World BlockchainEconomy With Just 305 Bitcoins Per Day

Crypto and Web3: I’ll wait and see

Raise The Stakes: 2018 Brings More Antes To The ICO Table

Vaulty is launching on TrustPad!

InPoker is Launching on TrustPad!

FAQ: How are you different from other gambling sites?

Good day, dear readers, today we will talk about earning money without investments in the…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Zhao

James Zhao

Yale Blockchain Club

More from Medium

The First 5 Cryptocurrencies And Their Worth Now

Weekly Crypto Market Wrap, 4th January 2022

The Original Bitcoin (BTC) White Paper

Bitcoin Mining cosumes a lot of energy.