The Bitcoin Blockchain and Mining

Paavan Bhavsar
Aug 23, 2017 · 10 min read

In my first Bitcoin post, I covered what Bitcoin is at a high level: it’s just a public ledger of people giving “bitcoins” to other people. Bitcoin is the blockchain! I figured it was only natural to dive into how the blockchain actually works, because if you’re going to invest in bitcoins then you might want to know why you can trust it. This is the fun stuff! Like before, I’m going to try to take away all the magic and let you see how surprisingly simple it is.

The Network

Visualization of Bitcoin nodes across the globe (Source: Bitnodes)

The Bitcoin network consists of thousands of computers running “full nodes”. This network is completely open, meaning that you can run a full node on your computer right now. Each full node connects to other nodes and downloads the entire blockchain, which contains every Bitcoin transaction that’s ever been performed. As of August 2017, that’s over 120 GB of data.

After downloading the blockchain, a full node can participate in running Bitcoin, making sure that only valid blocks are accepted into the blockchain.

  1. A valid block contains only valid transactions: those that are signed correctly (to prevent forgeries) and don’t double-spend any bitcoins. Transactions almost always contain a transaction fee. When you create a transaction paying someone else, the amount you give will be a tiny bit greater than the amount the other person receives. That small amount is the transaction fee. For example, this transaction has a fee of 0.00072445 BTC.
  2. A valid block creates a certain number of bitcoins. This is the only way that bitcoins are created.
    All this means is that the creator of the block is allowed to make the first transaction say “Give x BTC to me.” This is called the coinbase transaction. Here’s an example that creates ~12.9 BTC. Notice that unlike other transactions, there’s no “from” wallet address. Those bitcoins were “created” out of thin air, because the network let them do it!
    The amount created is the sum of all the transaction fees in the block, plus 12.5 BTC (this number is halved every 210,000 blocks, or about every four years).
    If the amount created is incorrect, or the block is missing the coinbase transaction, the block is rejected.
  3. A valid block points to another valid block in the blockchain. These pointers can be followed all the way back to Block 0 (the Genesis Block). This is what makes the blockchain a chain.
A simplified valid block, ostensibly created by someone with a wallet address of 261a4

These simple rules make sure that the blockchain is consistent, which makes it trustworthy since thousands of independent nodes have agreed on it. This amount of decentralization means that Bitcoin is stupid resilient and basically impossible to shut down. You could remove thousands of nodes from the network without affecting anything, because as long as good actors make up more than half of the network’s power, Bitcoin will chug along just fine.

Bitcoin Mining

“Mining” is the horrible word used to describe the process of proposing a new block. It’s not some sort of illicit activity, it’s literally the only way that blocks are added to the blockchain. There is no Bitcoin without miners, because without them the blockchain could not grow to include new transactions. That’s why Bitcoin incentivizes mining by allowing miners to give themselves bitcoins via coinbase transactions.

Any full node can propose blocks, but the term “mining” is terribly confusing because it has nothing to do with what’s going on. Proposing a block consists of two steps: creating the block of transactions itself, and then “solving” it by winning a kind of lottery. Let’s mine a block.
If you’re not interested in the details, you can skip to the end where I close out with an explanation of Bitcoin’s recent split.

Choosing Transactions

First, we’re going to pick which transactions are going to go in our new block. We’re being kept up to date on the new transactions entering the network since we’re a full node, and each is begging to be included in a block in the blockchain. These are called unconfirmed transactions, because until they end up in a block they might as well have never happened.

Average number of transactions per block (Source: blockchain.info)

Surprisingly, the number of unconfirmed transactions we include in our block is entirely up to us! The only limit is that our block must be less than one megabyte. That means that we can include anywhere between 0 and ~2,200 transactions. Yes, we can have zero transactions. Check out this block that has nothing and just cashes out on the reward! This actually isn’t a huge problem since blocks are hard to create, but it’s not a nice thing to do.

Once we’ve chosen a set of transactions to include in our block we’re going to add a couple of things to it, the major ones being:

  1. The coinbase transaction giving ourselves the correct number of bitcoins. We’re going to add up all the transaction fees in the block and add 12.5 BTC.
  2. The previous block’s hash (kind of like its ID.) Since every block points to the previous block, we can know what all the blocks are in the blockchain.
  3. A timestamp specifying when the block was created.

Now that we have our block, we’re going to “solve” it. This is the hard part, and it’s why miners can’t just spam the network with zero-transaction blocks. It essentially requires miners to win a lottery.

Hashing the Block

In order to solve the block we need to include its hash. We’re going to take all the data we put together so far and apply a hash function to it. Hash functions let us take any amount of data and quickly crunch it down into a fixed length of characters. Here’s an example using the SHA-256 hash function used in Bitcoin:

“math is dope”
⇒ 4e8bb4a8fc0bd113a6b926f716e27a261a4883d0541f1564b19bacd2f100407

What’s crazy is that you can do this with any amount of data, and you’re practically guaranteed to get a unique hash. Watch what happens if we change a character:

“math is nope”

048981083a27b4707f31ac291997cce5191fc1c0ab1740d65195c47393faac97

It’s completely different. There’s no known way to predict what the hash of some data will look like, and neither is there any known way to figure out what data was used to make a hash (at least for SHA-256). That’s super important, because these two properties of hashes go a long way to underpin the security of the blockchain.

Winning the Lottery

Here’s an example of a block’s hash:

00000000000000002da7989ab1c8a51b44504e6470d7f44be04eaa1853e617b1

Notice anything strange? There’s a ton of zeroes at the beginning. That’s not an accident, it’s required. The Bitcoin network requires all blocks to meet a certain level of “difficulty”. The difficulty of a block is the number of zeroes that a block’s hash must start with. But as I said earlier, you can’t predict what a block’s hash will be so there’s no way that the block we created will meet the difficulty by accident!

In order to get around this we’re going to add a nonce to our block. A nonce is a random number used just for the purpose of making the block’s hash fit the difficulty. And as an added bonus, it sounds silly.

We’re going to start from 0 and increment the nonce until we “win”: when the hash of the nonce with the block data meets the difficulty. Then we propose the block to the network, they verify that everything checks out, and it gets added to the blockchain.

Check out this block for an example. It has 17 zeroes at the start of its hash, and they found it by using a nonce of 657,220,870.

That’s all there is to mining!

Our brand new block sitting at the end of the chain!

Wait what’s the big deal

Well, it takes a long time to find a nonce that works. The nonce can only be as big as 4,294,967,295, and if you hit the limit (which you will) you have to change your block data slightly and start over. Changing the block data can be as simple as changing the timestamp, or adding/removing transactions. Anything really.

It sounds like easy money, but it takes so many attempts to win the hash lottery that you’re not going to get anywhere on your own. Mining today is handled by pools of computers that share the profits of blocks that they create, capable of churning through several quintillion hashes a second (their collective “hashrate”). Your best shot is joining an open pool, but the electricity and hardware costs make it a weak bet. It’s for a good cause though: the reason it’s made to take so long is that all of our work secures the block against attackers.

Network hashrate distribution among mining pools (Source: blockchain.info)

Attack the Block(chain)!

Since Bitcoin is completely open and decentralized, we have to assume that some number of nodes in the network are not good actors and are attempting to manipulate the system.
In order for Bitcoin to be a trustworthy currency we need to assure people that after they are paid, nobody will go back and change it. If that was possible, then I could pay you a bitcoin and then later alter the transaction to say I only paid you 0.01 bitcoins. Since whatever the blockchain says is the end-all, you wouldn’t be able to dispute it.

Hashes to the Rescue

Here’s where all that work we put into mining becomes invaluable. Remember what happened when we changed a single character in “math is dope”? Well, the same thing happens if someone tries to change a transaction in a block: the block’s hash changes completely. Not a chance that it has the right number of zeroes anymore.
To be accepted into the blockchain, the attacker needs to find a new nonce and block data that makes the hash meet the difficulty. But wait! Since it’s a chain, the block after that needs to reference the new hash! So that block also needs its data changed, which means it also needs its hash recalculated!

Altering even one number in a block “breaks” the hash and every hash after it

You get the picture: the deeper a transaction is within the blockchain, the more exponentially unlikely it is for an attacker to ever hope to alter it. They have to win the lottery for that block and every block that comes after it, keeping in mind that a new block is added every ten minutes by massive pools of powerful computers. It’s just not possible. In this way, every transaction is secured by the cumulative work that goes into every block after it!

We use the term “number of confirmations” to describe how trustworthy a transaction is. A transaction sitting in a block at the end of the blockchain is considered to have “one confirmation”, which implies that an attacker only has to redo one block to tamper with it. That number increases as new blocks are added, so the number of confirmations is just how deep in the blockchain a transaction is. At the time of writing, this transaction only had 4 confirmations, but it’s going to be a lot more by the time you get to it. It’s generally agreed that a transaction is tamper-proof after six confirmations (~60 minutes).

A branch in the blockchain.

It’s not just attackers we have to worry about. What happens if two miners find a block at around the same time? They both point to the same previous block, so we have a split in the blockchain! There’s no central authority to ordain one as “the one true blockchain,” so what can we do? At this point, the network tends to choose the one that was created first and building on it more, while abandoning the other block. If your transaction was in the dead-end branch, then it gets re-entered into the system as an unconfirmed transaction for another chance at making it into the blockchain. This is why it’s important to wait for the confirmations to pile up before calling it a done deal.

In Blockchain We Trust

If at this point if you’re surprised that Bitcoin works at all, you probably get it. It’s all based on collective trust that the network is comprised of mostly good actors. As someone gains a larger share of the entire network’s power, their chances of being able to rewrite the blockchain history increases. That chance becomes a certainty once they surpass 50% of the network’s hashrate, so it can’t be overstated how important it is to make sure that nobody gets there. If I can’t trust the blockchain then I’m not going to accept bitcoins, and the currency will collapse.

Bitcoin Cash?

So if Bitcoin is such an incredibly trustworthy, secure, and predictable currency, why did it split? Well, it got too popular. Remember that blocks are added only once every ten minutes, and each block needs to be under one megabyte. That’s a pretty tight bottleneck! As more and more people used Bitcoin, the network started getting saturated with transactions. Blocks were creeping up to the one megabyte limit trying to keep up with demand and transaction fees were increasing to incentivize miners to add them to blocks.

Much like the decentralized network of nodes, no one person owns Bitcoin. The community came together to try to decide the best way to solve this scaling problem by changing the software, and there were two factions that could not resolve their differences. As a result, Bitcoin Cash was created as a “hard fork” with a block size of 8 megabytes, giving it eight times the throughput of Bitcoin.

What’s crazy is that Bitcoin Cash shares the same blockchain history as Bitcoin up until the split. So if you had 5 bitcoins before the split, you now have 5 bitcoins and 5 bitcoin cash. And since Bitcoin Cash also has value (by virtue of someone willing to pay you for it), you might end up with more money than you started with. Value appeared overnight.

Digital currency is weird. ❤

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade