What does the term "Longest chain" mean?

25

12

What does the term "Longest chain" mean, as there is only one 'right' blockchain exists? How another longer chain would make the 'right' chain invalid?

32

Bitcoin's block chain system is really two quite separate systems, and they are easily confused. The first one is the block tree and the second is the active chain.

The block tree consists of all valid blocks whose entire ancestry is known, up to the genesis block. The rules for validness include no double spending, valid signatures, no introduction of more currency than allowed, ... These are the network rules, and every full Bitcoin node verifies them.

The active chain is one path from genesis block at the top to some leaf node at the bottom of the block tree. Every such path is a valid choice, but nodes are expected to pick the one with the most "work" in it they know about (where work is loosely defined as the sum of the difficulties). Relativity and technological constraints prevent us from doing instant communication across the globe, so two nodes can not be expected to pick the same chain as the active one. This is no problem: the mining mechanism makes sure that the chance two nodes disagree about blocks in the past decreases exponentially as they are older.

So no, there is not one "right chain", there are many. Nodes choose for themselves, but the system is designed to make sure consensus arises quickly.

The rules in practice are this: when a new block arrives, and it extends the previous active chain, we just append it to the active chain. If not, it depends on whether the branch it extends now has more work than the currently active branch. If not, we store the block and stop. If it does have more work, we do a so called "reorganisation": deactivating blocks from the old branch, and activating blocks from the new branch.

@DairaHopwood helpful comment, although technically the comment right above yours does define it ("expected number of hashes..") :) . For those wondering where the formula comes from: the expected number of hashes is 1/p where p is the probability of hashing to the target or below, so p = (target + 1) / 2^256. (the "+1" is to count the hash with all 0s). – Chan-Ho Suh – 2020-01-23T21:56:54.733

Sir, how a node selects the most recent block on the longest path; e.g., assume node knows two sub-chains, how it identifies which one is longer? Is it just going compare their lengths (number of blocks)? Thanks. – Nur – 2015-01-25T18:23:10.623

2We look at the amount of work in each chain: the expected number of hashes that were necessary to build them. This loosely corresponds to the sum of the difficulties of the blocks in both chains (which is almost always the same as just comparing the length, unless the fork spans a retarget). – Pieter Wuille – 2015-01-25T19:32:29.643

3This answer doesn't precisely define "work". The work of a block is actually defined as floor(2^256 / (target + 1)), where 'target' is the target threshold converted from the 'nBits' field of the block header. (See the GetBlockProof function in src/pow.cpp.) – Daira Hopwood – 2017-02-04T04:47:37.237

12

Imagine that the blockchain is 210000 blocks long and TWO miners both find valid blocks within a few seconds of each other and broadcast them to the network.

This is perfectly normal as the Bitcoin network is peer to peer and global.

You now have two chains, each of length 210001. Neither of these are longer than each other. Some bitcoind nodes will see the first miner's block and some bitcoind nodes will see the second.

Temporarily you have two forks of the blockchain, each of length 210001 blocks long. They are identical for 210000 blocks, but the 210001st is different on the two forks.

Sometime later another miner finds another valid block, the 210002nd block, and that will be attached to exactly one of the forks.

This chain is now the longest at 210002 blocks and becomes the longest chain. It becomes the "definitive" blockchain.

(The transactions in the alternate fork don't disappear - they simply get put back into the pool of unconfirmed transactions and miners will put them into a subsequent block).

This excellent description cleared things up for me, thanks! and the answer I was looking for is: it may be that at block N two blocks are broadcast at nearly the same time, but it is very unlikely that happens twice in a row, and vanishingly likely it happens three times in a row, thus things converge quickly. My next question is: has it ever happened (or recently happened) that it happened twice in a row, thus there were 3 or four chains each of the same length and miners start working on each? I guess I'll find a blockchain explorer and start looking ... – davidbak – 2017-07-14T04:14:30.937

1But is that one block that was on the 210001 chain awarded? The network chose the 210002 chain, and the transactions from the 210001 chain get recycled for the next block (210003?) and the 210001th goes away? – Colin Dean – 2012-11-29T15:40:48.207

3My understanding is that under most circumstances the transactions in each of the forks are mostly the same. Any fee-paying transactions in the (soon-to-be) orphaned block (eg 210001B) would likely be included in the block immediately following (ie 210002) because those transactions are considered still available by miners working on their own fork (210001A). They wouldn't need to wait for block 210003, as the final paragraph implies. – Highly Irregular – 2012-11-30T09:26:04.080

This is a nice answer! – Jinhua Wang – 2019-04-09T13:50:36.117