# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://davidjosearaujo.gitbook.io/online-courses/blockchain-security/consensus-algorithm-security/introduction/the-longest-chain-rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
