Chain Reorganization
Adopting a new longest chain
Height | Length | Date |
---|---|---|
877,770 | 1 | 04 January 2025 |
863,888 | 1 | 03 October 2024 |
856,689 | 1 | 14 August 2024 |
853,051 | 1 | 20 July 2024 |
851,248 | 1 | 08 July 2024 |
849,233 | 1 | 24 June 2024 |
848,477 | 1 | 18 June 2024 |
847,849 | 1 | 14 June 2024 |
829,613 | 1 | 09 February 2024 |
827,853 | 1 | 28 January 2024 |
819,343 | 1 | 01 December 2023 |
818,038 | 1 | 23 November 2023 |
815,202 | 1 | 04 November 2023 |
813,210 | 1 | 21 October 2023 |
803,389 | 1 | 16 August 2023 |
800,786 | 1 | 29 July 2023 |
792,379 | 1 | 01 June 2023 |
789,147 | 1 | 10 May 2023 |
788,837 | 1 | 08 May 2023 |
788,805 | 1 | 08 May 2023 |
781,487 | 1 | 19 March 2023 |
781,277 | 1 | 18 March 2023 |
730,848 | 1 | 07 April 2022 |
685,135 | 1 | 27 May 2021 |
675,407 | 1 | 20 March 2021 |
675,392 | 1 | 20 March 2021 |
A chain reorganization (or "reorg") takes place when your node receives blocks that are part of a new longest chain. Your node will deactivate blocks in its old longest chain in favor of the blocks that build the new longest chain.
This process allows individual nodes across the network to agree on the same version of the blockchain, because the globally accepted view of the blockchain will always be the one with the longest* chain of blocks.
*Technically it's the chain with the most amount of work, but the most number of blocks is usually the same thing.
Scenario
When does a chain reorganization take place?
A chain reorganization most commonly takes place after two blocks have been mined at the same time.
Due to the propagation speed of blocks across the network, some nodes will receive the one block first, and some nodes will receive the other block first. Therefore, there will be a temporary disagreement about which of these blocks was actually "first" and belongs at the top of everyone's blockchain.
So how can we resolve this dispute and make sure everyone agrees upon the same version of the blockchain?
Well, it's resolved when the next block is mined. The next block to be mined will build on top of one of these blocks, creating a new longest chain. When nodes receive this newest block, they will see that it creates a new longest chain, and they will perform a chain reorganization to adopt it.
So thanks to chain reorganizations, each node eventually agrees on the same version of the blockchain as everyone else.
Stale Blocks
What happens to the transactions in the old longest chain?
If a block is deactivated due to a chain reorganization ("stale block"), the transactions inside it are no longer part of the blockchain.
So if you try to spend the outputs from a transaction inside a stale block, nodes would reject your transaction because you are trying to spend bitcoins that do not exist in the valid chain.
Practically speaking though, if two blocks are mined at the same time, they're probably going to include the same (or similar) transactions in them, so a reorg isn't usually going to cause a problem.
However, if there are transactions in the stale block that are not in the competing block, they will get sent back into your node's memory pool and propagated around the network again for the chance to be mined into a future block.
But this is not a guarantee, and if a transaction does not exist in the active chain, it may as well have never happened.
It's worth waiting for a transaction to make it 2+ blocks into the blockchain before considering it as final. There is always the chance that it will get reorg'd out, and you will have to wait/hope for it to be re-mined back into the longest chain.
A "stale block" is sometimes referred to as an "orphan block", but "stale block" is the more accurate term. An orphan block is when your node receives a block before receiving the parent block that it builds upon, and stale blocks aren't orphans because they have parents.
Example
Here we can see an example of an actual chain reorganization that took place in the blockchain at block height 578,141.
Size
How big can chain reorganizations be?
A chain reorganization can be any number of blocks in length.
If your node receives a new chain of blocks that's longer than your current active chain, your node will do a chain reorganization to adopt the new chain, no matter how many blocks will be replaced.
This is why it's possible for a miner with a majority of hashing power to replace blocks and transactions in your current longest chain via a 51% attack. The longest chain always wins.
However, "natural" chain reorganizations (ones that take place due to two blocks being mined at the same time) rarely involve more than the top block in your chain.
Frequency
How often do chain reorganizations happen?
Not very often. For your node to experience an honest chain reorganization, the following needs to take place:
- Two blocks are mined at the same time.
- Your node receives one of the blocks first, but the other block gets built upon and becomes the new longest chain.
I don't know what the mathematical probability of this is, so here's the frequency of chain reorganizations based on the data from my bitcoin node (which has been running continuously since 17 December 2016):
- Actual Reorganizations: 26 (1 every 16,755 blocks / 113.5 days)
- We received a new longest chain and updated to it, deactivating blocks in our old longest chain.
- Avoided Reorganizations: 210 (1 every 2,074 blocks / 14.1 days)
- We heard about a chain that could become the new longest chain, but our active chain at the time continued as the longest.
Commands
How can you find chain reorganizations?
You can see the chain reorganizations your node has observed with the bitcoin-cli getchaintips
command.
For example:
[
{
"height": 589919,
"hash": "000000000000000000149b18e74316248d106e42ca410f509305ae58ccda6b13",
"branchlen": 0,
"status": "active"
},
{
"height": 578141,
"hash": "0000000000000000001253a5f37d3763dbe928d21f7d72a708f05268c044179c",
"branchlen": 1,
"status": "valid-fork"
},
{
"height": 575695,
"hash": "0000000000000000002409ed07fdbb1d0359a0c516014115c5451aea724baec8",
"branchlen": 1,
"status": "valid-headers"
},
...
brachlen
– tells you how many blocks are in the competing chain of blocks.status
– indicates the following:active
– This is our current active chain (the longest chain).valid-fork
– Our node performed a chain reorganization. We downloaded and validated these blocks and had them as part of our active chain, but we later deactivated them after receiving a new longer chain of blocks.valid-headers
– Our node observed a possible chain reorganization. We downloaded these blocks, but did not validate them as our active chain was equivalent and became longer.headers-only
– Our node observed a possible chain reorganization. We received the block headers for a competing chain but did not download the full blocks.invalid
– A branch that contains invalid blocks.
The branches with the status of valid-fork
are the ones with blocks that we originally considered part of our active blockchain, but we later deactivated them after receiving a new longer chain of blocks.
The branches with the status of valid-headers
are competing chains that we received after already having an equivalent active chain. These could have resulted in a reorganization, but our chain continued as the longest chain, so no reorganization took place.
You're unlikely to see any chain reorganizations if you have not been running your node continuously for a few weeks or months. When your node downloads the blockchain it will only download the blocks in the current longest chain (and not blocks from any branches or old chain reorganizations). Your node needs to experience chain reorganizations as they happen for them to show up in bitcoin-cli getchaintips
.
Summary
Chain reorganizations are a perfectly normal part of a Bitcoin node's function. Adopting the longest known chain allows nodes across a network to agree on the same blockchain, and chain reorganizations are just a part of this process.
Transactions inside blocks that are deactivated due to a chain reorganization will become invalid, but they will be recycled into the memory pool for the opportunity to be mined into a block on the new longest chain.
So basically, if your transaction gets mined into a block, there's still a chance that it could get thrown back into the mempool due to a chain reorganization. However, these natural reorgs typically only happen to the top block on the chain, so you should wait for your transaction to make it 2 blocks deep to help avoid this scenario.