How does blockchain work?
  • Hash256
  • Hash160
  • Reverse Bytes
  • Hexadecimal
Youtube

Blockchain The file of bitcoin transactions.

The blockchain is a file of transactions. It’s the most significant file that a bitcoin node maintains.

It is called the “blockchain” because new transactions are added to the file in blocks, and these blocks are built on top of one another to create a chain of blocks. Hence, blockchain.

But ultimately, the blockchain is permanent storage for bitcoin transactions.

How do you get a copy of the blockchain?

When you run bitcoin, your node will download blocks from other nodes on the network until you have an up-to-date copy of the blockchain.

When nodes connect to each other, they tell each other the height of their chain (how many blocks they have) during the initial handshake. If another node has more blocks than you, your node will request these blocks from the other nodes until you have a full copy of the blockchain.

As a result, nodes are constantly communicating with each other to replicate the blockchain across every computer on the network.

Tip: There is no single or definitive version of “the blockchain”. Every node keeps their own local copy of the blockchain, and the these can vary from computer to computer at any given time.

Note: My node’s blockchain is currently 243.43 GB, so it can take a while to download when you run bitcoin for the first time. This is referred to as the Initial Block Download (or IBD).

How are new blocks added to the blockchain?

New blocks of transactions must be mined before they can be added on to the blockchain.

In short, the process of mining involves collecting transactions from the memory pool in to a candidate block, and then using processing power to produce a block hash that is below a specific target value. This means that any node on the network can mine a new block, but you need to expend energy to be able to do so.

When a node (or “miner”) successfully mines a new block, they will share it with the other nodes on the network. When other nodes receive this new block, they will add it to their blockchain, and miners will start trying to mine a new block on top of it.

As a result, miners are constantly working to extend the blockchain with brand new blocks of transactions.

Note: Due to the difficulty and processing power required to mine a block, new blocks are added to the blockchain once every 10 minutes (on average).

Note: A node doesn’t have to try and mine new blocks. Instead it can just keep a copy of the blockchain and relay new blocks to other nodes when it receives them.

Can two blocks be mined at the same time?

Yes, and this is perfectly normal.

In this situation, nodes will consider the first block they receive as part of their blockchain, but also keep the second block they receive just in case. However, the second block to arrive (and the transactions inside it) will not be considered as part of their active blockchain.

Consequently, nodes on the network will actually be in disagreement about which of these two blocks belongs at the top of the chain.

This disagreement is resolved when the next block is mined. The next block will build on top of one of these blocks, creating a new longest chain of blocks, and as a rule nodes will always adopt the longest known chain of blocks as their active blockchain.

As a result, some nodes will perform a chain reorganisation to move out blocks from an old active chain in favour of blocks that make up a new longer chain.

Therefore, although there may be disagreements at any given time about which block(s) belong at the top of the blockchain, the mining of new blocks and the adoption of the longest chain means that nodes will always end up in agreement.

Nodes always adopt the longest chain of blocks as the accepted version of the blockchain.

Does this mean that blocks in the blockchain can be replaced?

Yes.

Due to the fact that nodes always adopt the longest chain, you could always try and build a new longer chain of blocks to replace an existing one, and every node on the network will adopt it. In effect, this would allow you to “undo” or reverse a bitcoin transaction from the blockchain.

If you build a new longest chain of blocks other nodes will adopt it.

However, the problem is that all miners are incentivised to always be building on to the longest known chain. This means that the combined processing power of miners on the network will be focused on building one single chain, which will be built faster than any chain you could build on your own.

Every other miner is working to extend the current longest chain.

In other words, the combined processing power of the network working to build the blockchain helps to protect blocks (and transactions) that have already been mined on to the blockchain.

So the only way you could perform an intentional chain reorganisation (to “undo” a transaction in an existing block) would be to have more processing power than every other miner combined so that you could out-mine the network and build a longer chain for everyone to adopt.

This is referred to as a “51% Attack”.

Where is the blockchain?

The blockchain files can usually be found in the following locations:

  • Linux: ~/.bitcoin/blocks/
  • Mac: ~/Library/Application Support/Bitcoin/
  • Windows: C:\Users\[username]\AppData\Roaming\Bitcoin\

Note: The blockchain is split in to multiple files named blk00000.dat, blk00001.dat, blk00002.dat, and so on. This is because it’s generally easier to work with smaller files than it is to work with one giant file.1

Summary

The blockchain is permanent storage for bitcoin transactions. New transactions are added to the file in blocks, and these blocks are built on top of each other to create a chain.

New blocks are added to the blockchain through mining, which involves the use of computer processing power. This means that it takes energy to mine a block, but any node on the network can work to try and add the next block on to the chain.

When a new block is mined, it will be relayed across the network, which nodes will verify and add on to their chain. In this way the blockchain is a constantly growing ledger of transactions, which is distributed across multiple computers on the network.

Nodes always adopt the longest chain of blocks as the active version of the blockchain, which resolves disagreements about which blocks belong at the top of the chain. This also protects blocks that are already in the blockchain, as it would require large amounts of energy to build a chain that replaces existing blocks in the chain.

As a result, the blockchain is a regularly updated file of transactions. The system of mining blocks and adopting the longest chain allows multiple computers over a network to agree on the same set of blocks and transactions that are considered active, whilst also making it difficult for anyone to make historic changes to the blocks and transactions in the file.

Click the image to see a gif animation of a blockchain being built.
Click image to see a gif animation of a blockchain being built.

  1. https://bitcoin.stackexchange.com/questions/50693/why-are-blk-dat-files-134200000-bytes↩︎

By Greg Walker,

Last Updated: 25 Oct 2019
  • 25 Oct 2019: 51% attack (fourth draft - edits, links)
  • 21 Oct 2019: edits
  • 17 Oct 2019: longest chain (third draft, links)
  • 17 Oct 2019: longest chain (second draft, diagrams), resources page, router now includes config when running pages through PHP
  • 15 Oct 2019: removed some old hard-code div class='figure'
  • 15 Oct 2019: updated CSS and router to use pandoc 2.7 (from 1.19)
  • 14 Oct 2019: changed php code
  • 14 Oct 2019: blockchain page
  • 24 May 2019: Added Hashover comment system (basic functionality to test it out)
  • 27 Nov 2018: Typo and blockchain size update.
  • 26 Nov 2018: Added sub titles to every page.
  • 27 Mar 2018: Added more descriptive titles to be shown in search engines.
  • 23 Mar 2018: Made diagrams in the guide bigger.
  • 20 Mar 2018: CSS - Separated mobile CSS in to its own file, and changed all em to px
  • 13 Mar 2018: guide/ - renamed all files from .text to .md and removed numerical prefix. Makes it cleaner and makes for easier working with search box.

Hey there, it's Greg.

I'll let you know about cool website updates, or if something seriously interesting happens in bitcoin.


Don't worry, it doesn't happen very often.