paint-brush
How Does Bitcoin Work?by@wasteofserver
122 reads

How Does Bitcoin Work?

by FrankieDecember 25th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The current trend is to dumb down the mechanics of Bitcoin behind multiple levels of abstraction. Here we explore how it actually works without generalizing too much. How are the blocks tied together? A hashing function maps data of variable size to a fixed-size value. And it's generally at this stage that you'll hear that cryptography glues it together.

Company Mentioned

Mention Thumbnail
featured image - How Does Bitcoin Work?
Frankie HackerNoon profile picture

The current trend is to dumb down the mechanics of Bitcoin behind multiple levels of abstraction. Here we explore how it actually works without generalizing too much.

What exactly is the blockchain?

You've heard it countless times; blockchain! Not only is the term ubiquitous, so is the general misunderstanding behind it. And that leaves the door open towards spurious imagination.


Blockchain is a technology that, as the name says, "blocks" the "chain" of fake news.


It most definitely is not that! But this was actually said on national television by a prominent Portuguese intellectual.


Blockchain is a unique kind of database. It's a continuously growing list of records that is open, distributed and impervious to the change of data.


Let's let that sink in.


  1. Open - anyone in the world can query it.
  2. Distributed - approximately 100.000 nodes participate in its quorum.
  3. Impervious - once data is written, no single party can change it.


So we've established that it's a growing list of records. And it's generally at this stage that you'll hear that cryptography glues it together.


Stairway as a chain of steps. NYC, 2018.

How are the blocks tied together?

For that, we must explore the concept of hashing. A hashing function maps data of variable size to a fixed-size value. The one Bitcoin uses is called sha256. You can explore how it works by playing with the following text boxes.


Unfortunately, I can’t insert JavaScript on Hacker News, so this bit is only available on Waste of Server original article.


Just change the string "abc" to whatever you want, and you'll see that given tiny variations, the hash changes dramatically. Bitcoin established, by design, that we would be trying to find hashes with a pre-determined number of zeros in the beginning.


Use the above and try with abc26, you'll get a hash that starts with a 0. Now try it with abc93803 you'll have 0000 zeros at start. You can browse the first block ever mined and see that the initial difficulty was set to 8 zeros.


Hashing


As of now, the current difficulty is set to 19(!) zeros, but that changes depending on the amount of hashing power currently available. Bitcoin sets the difficulty (starting number of zeros) to have blocks being mined approximately every 10 minutes. But it's a game of chance, a block may take from just a few seconds to much longer. From the image below, you can see that it took 21 minutes, to mine block 673088.


Time to mine block


On the early stages of the network, when things were starting and hashing power was scarce, it took almost a full day to mine a single block (block 168 to 169). But since Bitcoin took off, the longest it took, was just a bit over 1 hour and 39 minutes from block 152218 to 152219. Mind you, the more hashing power there is, the less likely it is for events like these to spur.

Exactly what is hashed, and what's in a block?

A block header carries the following information:


  1. miner software version
  2. timestamp of the block
  3. sha256 of the merkle tree root
  4. sha256 of the previous block
  5. a nounce (random data to try and get x number of zeros)
  6. a target (number of zeros to find)


We'll get to the merkle tree root in an instance but, as you can see, given these 6 items the miner starts iterating the nounce to try and find a hash that is less (has more zeros) than the target. As soon as it finds one, the nouce that solves the problem is saved to the block and the client publishes it. The miner that publishes the block first is rewarded with both the transaction fees and new Bitcoin that's minted out of thin air.

What is a Merkle Tree?

It's a way to verify the integrity of large data structures. In the case of Bitcoin just get all the transactions you are trying to validate and hash them two by two. Then get those hashes and also hash them two by two. Proceed like this until you have a single final hash. That is called the root hash and uniquely represents all the transactions that are below it.


Say we have 4 transactions represented by letters. We join them two by two and hash them in sequential order. Then we hash the hashes.


Transactions (leaves): A + B + C + D

Branches Level 1: sha256(A+B) = BrnchAB
Branches Level 1: sha256(C+D) = BrnchCD

Merkle Root: sha256(BrnchAB + BrnchCD) = BrnchABCD

As there are only 4 transactions, there's no need to get to Level 2 Branches

How do you get BTCs from mining?

Before starting to try finding a solution to the block, the miner simply adds a new transaction to their block that gives them X amount of BTC. This is called a generation transaction and its value depends on the block being mined.


The block reward started at 50 BTC and halves every 210.000 blocks. As blocks are mined more or less every 10 minutes, you can pretty much guess when the next halving will happen. Though it has happened to be offset by months!


On 2021 we're on halving3 which gives 6.25 BTC per block.


HALVING

DATE

BLOCK

BLOCK REWARD

MINED IN PERIOD

% MINED

launch

3-Jan-2009

0

50

10,500,000

50

halving 1

28-Nov-2012

210,000

25

5,250,000

75

halving 2

9-Jul-2016

420,000

12.5

2,625,000

87.5

halving 3

11-May-2020

630,000

6.25

1,312,500

93.75

halving 4

On 2024 ?

840,000

3.125

656,250

96.88

halving 5

On 2028 ?

1,050,000

1.5625

328,125

98.44

halving 6

On 2032 ?

1,260,000

0.78125

164,062

99.22

halving 7

On 2036 ?

1,470,000

0.390625

82,031

99.61


As you can grasp from this post, the network works on the basis of Proof of Work (PoS) done by the miners. But, being de-centralized and open, anyone can implement a miner.


Remember above when we said that the Block Reward paid to the miner is added by the miner itself? Well, there is at least one documented case where the miner software wasn't working as expected and successfully mined a block without transactions (no fees) and without adding it's own address(!) no block reward!


You can check this half a million bug right here, block 501727.


If you liked this article, feel free to drop by wasteofserver.com where you will find similar stuff. You can try Is Bitcoin a store of value?, or maybe the Hitchhiker’s guide to NFTs?


Also published here.