• Hash256
  • Hash160
  • Reverse Bytes
  • Hexadecimal
  • Satoshis

Longest Chain

The chain of blocks that nodes adopt as their blockchain.

The longest chain is what individual nodes accept as the valid version of the blockchain.

The rule that nodes adopt the longest chain of blocks allows every node on the network to agree on what the blockchain looks like, and therefore agree on the same transaction history.

In other words it means that computers acting independently over a network can maintain a globally shared view of a file.

The proof-of-work chain is the solution to the synchronisation problem, and to knowing what the globally shared view is without having to trust anyone. – Satoshi Nakamoto

What is the longest chain?

The longest chain is the chain of blocks that took the most effort to build.

In short, to add a new block to the blockchain you need to use processing power, which means that every block on the blockchain used up energy to get there.

Adding a new block requires energy.

Therefore, a blockchain with more blocks in it will have taken more energy to build than a chain with fewer blocks in it, and as a rule nodes will always adopt this chain over a “shorter” one.

Longer chains take more work to build.

As a result, nodes will always adopt the chain that took the most energy to build, which is what we mean when we refer to the “longest chain”.

The majority decision is represented by the longest chain, which has the greatest proof-of-work effort invested in it. – Satoshi Nakamoto

Is the longest chain the one with the most blocks?

Sounds about right, but actually the chain that required the most energy to build is not necessarily the one with the most blocks in it. You see, the difficulty changes mean that some blocks are going to require more energy to mine than others.

For example, within the same difficulty period every new block requires the same amount of effort to mine, and therefore adds the same amount of “work” to the chain:

The target is what blocks must get under to be added on to the chain.

However, if the difficulty increases (because blocks were mined more quickly than every 10 minutes on average), the blocks in the new difficulty period are going to take more effort to mine on to the blockchain.

Now, seeing as nodes adopt the chain with the most work, they wouldn’t actually adopt a chain with more blocks in it if it didn’t require as much work to build. For example, if two versions of the blockchain span multiple difficulty periods, nodes will adopt the one that has the most cumulative “chainwork”, and not simply the one with the most blocks in it:

So in summary, the phrase “longest chain” refers to the blockchain that has taken the most energy to build. For the most part this is usually the chain with the most blocks in it, but to be more precise it’s the chain with the most amount of work in it.

Note: In the first version of Bitcoin Satoshi actually used the number of blocks as the metric for determining the longest chain, believing this to be the chain that would have taken the most work to build. However this is vulnerable to manipulation, so it was later changed to using chainwork as the metric for the longest chain instead.1

How do you calculate the longest chain?

The longest chain is measured by a metric called “chainwork”.

[Chainwork] is the total number of hashes that are expected to have been necessary to produce the current chain.
Pieter Wuille

To work out chainwork, you just need to work out how many hashes you would have needed to perform to mine each block in the chain, then add them up.

The average expected number of hashes for each block depends on what the target was at the time.

Chainwork Calculation Explained

The process of mining involves hashing a block header.

Every time you perform a hash, the hash function spits out a 256-bit number, which could be anywhere between 0 and:

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

To successfully mine this block on to the blockchain, this hash result needs to be below the target value for that particular height in the chain. The target for the first ever block was set at:

0x00000000ffff0000000000000000000000000000000000000000000000000000

So to work out how many hashes you’d need to perform (on average) to get below this value, you divide the maximum range of numbers by the number you want to get below.

0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff /
0x00000000ffff0000000000000000000000000000000000000000000000000000
= 0x0100010001

This means you would need to perform 0x0100010001 (4295032833) hashes on average to get a result below this target value. Therefore, this was actually the chainwork for that first block.

So to work out the total chainwork in a chain, you just work out the expected hashes for each block and add them all up.

You can find out what the target was for each block by looking at the bits field in the block header.

Simpler Example

Let’s say you’re randomly generating numbers between 1 and 100, and you’re hoping to randomly generate a number of 5 or below. On average, how many numbers would you need to generate before you get one below your target?

100 / 5 = 20

So on average you’ll need to generate 20 numbers to get one that is below 5.

This is exactly the same kind of calculation that takes place in bitcoin, but just with bigger numbers (and usually represented in hexadecimal).

Why do nodes adopt the longest chain?

Having nodes adopt the longest available chain allows computers across a network to be able to share the same view of the blockchain.

Here are two examples of where this rule comes in handy:

1. Resolving disagreements when two blocks are mined at the same time.

Due to the fact that bitcoin operates on a network, it’s possible that two independent computers will mine a block at the same time. In this situation, nodes across the network will end up being in disagreement about which of these two blocks should be at the top of the blockchain.

Each nodes put the first block they receive at the top of their blockchain.

However, this situation can be resolved by having nodes adopt the longest chain of blocks. This is because the next block to be mined will build upon one of these two blocks, creating a new longest chain that all nodes on the network will be happy to adopt.

Nodes are happy to abandon the shorter chain in favour of the new longer one. This is known as a chain reorganisation.

So even though nodes can be in disagreement at any given time (due to the unpredictability of mining and the speed of broadcasting data across a network), adopting the longest available chain means that nodes will always eventually agree on the same view of the blockchain.

2. Protecting blocks already mined on to the blockchain.

The fact that nodes always adopt the longest chain as the valid version of the blockchain means that it is very difficult to replace blocks (and therefore transactions) in the chain.

If anyone wanted to replace a transaction in the blockchain, they would need to work to build a new longest chain to replace the current one. However, if the majority of miners are continually working to extend the same current longest known chain, an individual miner won’t be able to compete to outwork all of the other miners.

You would need the majority of mining power to be able to out-run all other miners and build a new longest chain (known as a 51% Attack).

In effect, the combined effort of miners coordinating to extend the same chain protects existing blocks and transactions from being replaced by a single miner.

Think of it as a cooperative effort to make a chain. – Satoshi Nakamoto

Why do miners build on the longest chain?

Because a miner can claim a block reward if they are able to mine a block.

Furthermore, the bitcoins from this block reward can only be spent if the block becomes 100 blocks deep in the longest chain. Therefore, this block reward incentivises miners to always try and mine new blocks that will become part of the longest chain (by always trying to build on to the current longest one).

A block reward can only be spent if the block is part of the longest chain.

Miners claim the block reward through the coinbase transaction.

What about transactions that are not part of the longest chain?

A transaction inside a block that is not part of the longest chain is invalid.

If you tried to spend the bitcoins from a transaction that is not in the longest chain, nodes would not accept it nor try to mine it in to a block. This is because nodes only consider the longest chain the valid history history of transactions, and anything outside of that is not a valid transaction.

The bitcoins in a transaction that is not part of the longest chain are unspendable.

So only the transactions inside the longest chain are considered to be part of the valid transaction history, and any transactions outside of it effectively never took place.

Tip: This is why it’s recommended that you wait for a transaction to make it to 2 or more blocks deep in to the blockchain before you consider bitcoins to be “yours”. There is always a chance that the topmost blocks in the blockchain could change due to a reorganisation, making previously valid blocks and transactions invalid.

Summary

The adoption of the longest chain of blocks allows nodes on a network of computers to be able to share a globally accepted view of the blockchain. Furthermore, the fact that it requires energy to add new blocks to the chain makes it is very difficult for any individual to replace blocks that have already been mined in to the chain.

The “longest chain” usually refers to the chain with the greatest number of consecutive blocks, but in actuality it refers to the chain that has the most work in it based on how difficulty it was to mine each block.

Commands

You can see chainwork values for yourself using these bitcoin-cli commands:

bitcoin-cli getblockchaininfo

See the total chainwork for the current longest chain.

$ bitcoin-cli getblockchaininfo                                                         
{
  "chain": "main",
  "blocks": 599501,
  "headers": 599767,
  "bestblockhash": "0000000000000000000cb6141c8076e24f3a1799eef37201634ef392197668f3",
  "difficulty": 13008091666971.9,
  ...
  "chainwork": "0000000000000000000000000000000000000000094b1874d991d4e1fc51005a",
  ...
}

bitcoin-cli getblock [blockhash]

See the chainwork for any given block in the chain.

$ bitcoin-cli getblock 00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699
{
  "hash": "00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
  ...
  "height": 12345,
  ...
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000303a303a303a",
  ...
}

  1. https://bitcoin.stackexchange.com/a/29744/24926↩︎

By Greg Walker,

Last Updated: 22 Nov 2020
  • 22 Nov 2020: fixed typo
  • 21 Jul 2020: renamed /guide/ to /technical/
Back to Top

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.