Breaking Down Blockchain Bit by Bit

How the technology behind Bitcoin actually works.

Image for post
Image for post

There was loads of buzz about cryptocurrencies, headlines everyday about people becoming overnight millionaires through this mysterious digital gold. But few people understood how the underlying technology, blockchain actually works.

You’ve heard enough buzzwords like decentralization, so let’s break down blockchain bit by bit. But first let’s understand the difference between crypto and blockchain.

Bitcoin is a ledger of transactions, about who sends money to who. Everyone on the network has a copy of this ledger.

Image for post
Image for post

When a Mickey sends Bitcoin to Donald, everyone updates their copy of the ledger, to show this transaction.

To make sure the only Mickey can move the Bitcoin in his account, he signs his transaction with a digital signature, just like a real signature it authenticates that he’s the one making the transaction.

Digital signatures are used in combination with your public key(everyone can see) and your private key(only you can see).

A digital signature is created with your private key and the transaction message. A public key is used by others to verify that you own the private key.

  1. You take the transaction message, and your private key apply a function to generate the digital signature.

Signature = f(private key, transaction message)

2. Other nodes will put that signature, the transaction message and your public key into a different function to verify that you own your private key.

Verify= f(transaction message, signature, public key)

Image for post
Image for post
A digital signature is created with your private key and the transaction message. A public key is used by others to verify that you own the private key.

Because the signature is created using the transaction message, which is different for every transaction, the resulting digital signature is unique. It can’t be duplicated to sign off on other transactions.

To figure out how much Bitcoin you own, you add up times where you received Bitcoin previously.

Every transaction is visible on the network. But instead of explicitly saying “Mickey” and “Donald,” the ledger shows their public key associated with their account. Their identity is anonymous, but their account activity isn’t. Anyone can audit the network; this is how we verify transactions.

Image for post
Image for post

So Bitcoin is a chain of transactions, but what does this have to do with blockchain?

To make sure the transactions are in chronological order and are secure, we put them on a blockchain.

Now, what is “tHe bLOckChAiN?”

A block contains some type of data. We mark the block with a unique identifier called a hash.

The block also contains the hash of the block before it, a timestamp and variable called a nonce (which we can play around with).

The hashes of the blocks point to each other; in this sense, the blocks are linked like a chain.

BOOM “THE BLOCKCHAIN”

Image for post
Image for post
The first block is called the Genesis

If someone tries to change the data inside one of these blocks, the hash of the block changes. When this happens, the following blocks are broken from the chain because their hashes don’t point to that block.

Image for post
Image for post
Data tampered with on block 3, it’s hash changes, block 4 doesn’t have a block to reference anymore. this is a red flag for everyone on network that something fishy is happening

The blocks are immutable; no one can tamper with them once they’ve been added because they break the chain, and quickly warn the rest of the network something that something is up.

Blockchains are distributed; everyone on the network gets a copy of the chain.

Image for post
Image for post

If one node on the network goes down, the network is still alive. To mess with a network like Bitcoin, you would need to take down at least 51% of nodes.

Most networks are set up in centralized architecture, all the nodes branch of one main node. A blockchain network, on the other hand, is decentralized; all the nodes are linked together, and there’s no one power in control.

Image for post
Image for post

Why is this important:

  1. Centralized powers can control and censor data vs decentralization means no censorship
  2. Centralized systems only need one point of failure to for things to go wrong vs decentralized systems require multiple points of failure
  3. It’s peer to peer we don’t need intermediaries

A hash is a unique set of numbers and letters used to “mark” a block.

To get the hash of a block, we use a function called SHA- 256 (Securing hashing algorithm). We take all the contents inside the block, stick them together into one string and apply the SHA-256 function to it.

The function outputs always outputs a 64 character string, what’s crazy is there are ²²⁵⁶ possible outputs. The output is always the same for it’s for its respective input. So if I input the word “hat” it will ALWAYS output this scary looking:

18E1DD392168F601D0E568612A2BF40E6555C033E033B580AE6627D8AEEE1150

But if I change a single letter or add a space, the output will completely change, “hat” → “ha”:

8693873CD8F8A2D9C7C596477180F851E525F4EAF55A4F637B445CB442A5E340

Notice how it looks like someone coughed up random numbers and letters, it’s impossible to derive any meaning of output from the input; it’s completely random.

Any changes will distort the output

Image for post
Image for post
a single letter was removed from the input

If you touch the data inside the block, the SHA- 256 function will act the same way as above; the hash will completely change. As a result, the chain would break because the hashes won’t point to each other.

These hashes are why the blockchain is immutable; you can’t change or tamper with the data inside them or else the chain brakes.

The SHA- 256 function is also computationally efficient and has collision resistance, meaning it’s fast to apply the function, and it’s EXTREMELY rare to find two inputs that have the same output.

You’re not trusting the people, you’re trusting the cryptography. Trust in the network comes from its consensus mechanism.

Proof of work was the first-ever blockchain consensus mechanism.

Proof of work proves that there has been a computational work done to add the block to the chain. It’s very hard for nodes to randomly add fraudulent blocks because they would have to do a lot of computational work which costs time and money.

The concept of proof of work dates back to 1993. Its first application was Hashcash as a way to prevent email spam and Denial of service attacks in 1997 by Adam Beck. Still, it didn’t gain much traction until Satoshi Nakamoto’s Bitcoin whitepaper.

To add a block to the chain, nodes called “miners” compete to solve a complex mathematical puzzle that is computationally expensive. This is known as mining, the miner that finds the correct answer will broadcast it to the rest of the network to check that their solution is valid.

Image for post
Image for post
continuous trial and error, once the answer is found, it’s broadcasted to the network to be verified.

Once verified, the new block is added. When this happens, the block automatically mints new bitcoin and awards it to the winning miner. This is how we introduce new bitcoin to the network and incentivize mining.

It’s crucial that the puzzle is Asymmetrical, meaning it’s hard to solve but easy to verify. Similar to factoring numbers, it’s really tough to find the factors of a big number, but it’s easy to do the reverse find the number given the factors.

Image for post
Image for post

The miners guess the answer by changing the nonce variable(a number that we add to the block).

The nonce is concatenated with the puzzle string. Then the hash function is applied to the resulting string. The puzzle is solved if the output of that hash function is under a certain threshold of zeros.

Image for post
Image for post
f: hash function n: nonce p: puzzle string #: given amount of zeros

The puzzle is so complex that the only way to solve it is by pure guesswork, trial and error. The faster you can guess, the higher your chances of solving it are.

Okay, sounds easy. Just guess a bunch of times? I mean, you would have to guess on average 2 ⁴⁰ strings to solve it. That’s over a trillion times. If you made a million guesses every day since Jesus was born, you still wouldn’t be close to solving it. But there are so many nodes competing on the bitcoin network, that it’s solved every 10 minutes.

The problem with the proof of work mechanism is the puzzle gets harder(more zeros are added to the threshold) as more miners compete, the required computation power increases.

The more computing power nodes have, the higher their chances are of solving the puzzle are.

Some miners take advantage of this by creating mining pools, where they crowdsource their computing power, increasing their likelihood of winning and divvy up the rewards.

Image for post
Image for post
Bitcoins top mining pools.

But these mining pools take away from blockchains decentralized nature. If the top 5 mining pools in the bitcoin network were to combine, they would own more than half of the network.🤯

Owning 51% of the network means you can approve any transaction, this is known as a 51% attack, and it’s bitcoins biggest flaw.

The mining industry is huge, worth around 6 billion dollars. Almost anyone with a computer can mine bitcoin. But regular CPU’s are too slow for practical mining. Many miners now use ASIC computers. They’re specialized for mining bitcoin. They calculate hashes 100,000 times quicker than the best CPUs. There are buildings filled with these computers called mining farms.

Image result for mining farms
A mining farm: series of ASIC computers for one purpose only, mining Bitcoin.

With this competitive method, there is a lot of wasted computing power; proof of work mining is very energy inefficient. In 2018 it used 0.5% of the world’s power; the bitcoin network uses more energy than all of Iceland🤯

An alternative consensus mechanism is proof of stake.

The key difference is instead of having nodes compete to solve the puzzle; the network elects specific nodes. We call them validators, not miners.

To be elected, you must “stake” bitcoin. You hand over a sum of your crypto to the network, they hold onto this sum, in exchange for your right to validate blocks. If you validate fraudulent blocks the network punishes you by taking away your staked crypto, this disincentivizes bad actors.

The validators profit by charging a fee for every transaction.

The election process is fairly random, but factors in things like how much you’re staking and how long you’ve been on the network.

To take over a proof of stake network, you would need to own 51% of the crypto on that chain, which in major chains is very difficult, expensive and unrewarding.

This is a lot less likely than owning 51% of computing power, like in proof of work due to mining pools.

Proof of work is more environmentally friendly because there are fewer computers guessing → less energy being used.

Say you’re playing a game with thousands of people, a couple of you want to change the rules, but not everyone there wants to.

The group that wants to play with new rules decides to go off and play their own game while the others continue to play their game. Anyone is free to join either of the games.

Likewise, when the blockchain community disagrees on protocols, users branch off into different chains with alterations from the source code. All the previous transactions are still recorded on both chains, but the new branches blocks follow its own rules.

We call these new chains forks.

A hard fork happened when the bitcoin network had a size limit of 1mb. There could only be 4.4 transactions/sec in comparison visa does over 1700 transactions/sec. The community was debating whether this was good for scalability, they couldn’t come to an agreement, and thus, bitcoin cash was created with a block size of 8mb, some of the community used the new fork while others kept to the original.

Wait, but what happens to the bitcoin on the network? Users have the same amount of crypto on both chains because they share the same previous block history. So if you had two bitcoin on bitcoin, you would also have two on bitcoin cash.

There are TONS of forks, to name a few in bitcoin alone:

Image for post
Image for post

Despite all these chains, one usually prevails over the other due to network effects. The chain has the most blocks being added to it, dominates because more users are using it, and crypto-only has value based on how much others value it. Going back to the game analogy, the game with the most people playing will, over time, become the best game.

There are two types of forks: hard and soft.

Soft forks are like you’re changing lanes on a highway; they have changes in their code that are flexible and can still be used with the main chain.

Hard forks are like you’re exiting the highway; they follow very different rules and aren’t compatible with the previous chain; they require a whole new chain.

Image for post
Image for post
A hard fork; both chains have the same previous block history. They separate when one changes the protocols of their code.
  • Bitcoin is a record of transactions, that’s stored on a blockchain
  • Blockchains are immutable (can’t be changed) and decentralized (everyone owns a copy of the chain, and there’s no “middle man”)
  • They use the SHA-256 algorithm to create their hashes. Hashing functions are why it’s immutable
  • Proof of work is a consensus method that’s competitive and energy-intensive. To get around this miners are creating mining pools. Taking over, you would need 51% computing power.
  • Proof of stake consensus method elects validators and is more energy-efficient. To hack this, you would need 51% crypto supply
  • Forks are branches of software that follow different protocols. There are hard forks that need a new blockchain and soft forks that are compatible with the original.

These small layers when combined, create the superpowered technology that drives bitcoin. Blockchains aren’t just limited to crypto; they can disrupt dozen of industries! It’s only 10 years young and has lots in store for the future.

Look out for my next article on applications of blockchain! Feel free to reach out on LinkedIn or subscribe to my newsletter!😊

Please enjoy this video for making it to the end.

Playing around with blockchain and machine learning. 17. Trying to understand my self and the world.

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