> For the complete documentation index, see [llms.txt](https://davidjosearaujo.gitbook.io/online-courses/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://davidjosearaujo.gitbook.io/online-courses/blockchain-security/consensus-algorithm-security/introduction/the-longest-chain-rule.md).

# The longest chain rule

Consensus algorithms are intended to **ensure that the entire blockchain network agrees on the current state of the distributed ledger**. This doesn't always work

The longest chain rule states that **the longer of two divergent blockchains should be accepted**. An attacker can take advantage of this rule

## Example

Imagine you and your friends are playing a game of building a tower using blocks. Each time someone adds a block to the tower, it becomes taller. Now, let's say there are two versions of the tower, one built by the majority of players and another built by a smaller group. According to the longest chain rule, **the tower with the most blocks should be considered the official version**. This is because the longer tower is theoretically supported by the majority of players.

However, it's important to note that in practice, **an attacker could create a longer tower to rewrite the history of the game**. This is why attacks against consensus algorithms often focus on taking advantage of the longest chain rule to replace the current version of the blockchain with an attacker-controlled one.
