|
| 1 | +# Fork-choice: LMD GHOST |
| 2 | + |
| 3 | +Let's separate the two parts of the name. |
| 4 | + |
| 5 | +- GHOST: **G**reediest, **H**eaviest-**O**bserved **S**ub-**T**ree. The algorithm provides a strategy to choose between two forks/branches. Each branch points to a block, and each block can be thought of as the root of a subtree containing all of its child nodes. The weight of the subtree is the sum of the weights of all blocks in it. The weight of each individual block is obtained from the attestations on them. |
| 6 | +- LMD: each validator gives attestations/votes to the block they think is the current head of the chain (Message Driven). "Latest" means that only the last attestation for each validator will be taken into account. |
| 7 | + |
| 8 | +By choosing a fork, each node has a single, linear chain of blocks that it considers canonical. The last child of that chain is called the chain's "head". |
| 9 | + |
| 10 | +## Reacting to an attestation |
| 11 | + |
| 12 | +When an attestation arrives, the `on_attestation` callback must: |
| 13 | + |
| 14 | +1. Perform the [validity checks](https://eth2book.info/capella/part3/forkchoice/phase0/#validate_on_attestation). tl;dr: the slot and epoch need to be right, the vote must be for a block we have, validate the signature and check it doesn't conflict with a different attestation by the same validator. |
| 15 | +2. [Save the attestation](https://eth2book.info/capella/part3/forkchoice/phase0/#update_latest_messages) as that validator's latest message. If there's one already, update the value. |
| 16 | + |
| 17 | +## Choosing forks |
| 18 | + |
| 19 | +We now have a store of each validator's latest vote, which allows LMD GHOST to work as a `get_head(store) -> Block` function. |
| 20 | + |
| 21 | +We first need to calculate each block's weight: |
| 22 | + |
| 23 | +- For leaf blocks, we calculate their weight by checking how many votes they have. |
| 24 | +- For each branch block we calculate its weight as the sum of the weight of every child, plus its own votes. We repeat this until we reach the root, which will be the last finalized block (there won't be any branches before, so there won't be any more fork-choice to perform). |
| 25 | + |
| 26 | +This way we calculate the weight not only for each block, but for the subtree where that block is the root. |
| 27 | + |
| 28 | +Afterwards, when we want to determine which is the head of the chain, we traverse the tree, starting from the root, and greedily (without looking further ahead) we go block by block chosing the sub-tree with the highest weight. |
| 29 | + |
| 30 | +Let's look at an example: |
| 31 | + |
| 32 | +```mermaid |
| 33 | +graph LR |
| 34 | +
|
| 35 | + Genesis --> A[A\nb=10\nw=50] |
| 36 | + Genesis --> B[B\nb=20\nw=20] |
| 37 | + A --> C[C\nb=15\nw=15] |
| 38 | + A --> D[D\nb=25\nw=25] |
| 39 | +
|
| 40 | + classDef chosen fill: #666666 |
| 41 | + class Genesis chosen |
| 42 | + class A chosen |
| 43 | + class D chosen |
| 44 | +``` |
| 45 | + |
| 46 | +Here, individual block weights are represented by "b", while subtree weights are represented by "w". Some observations: |
| 47 | + |
| 48 | +- $W = B$ for all leaf blocks, as leafs are their own whole subtree. |
| 49 | +- $W_A=W_C+W_B +B_A= B_B + B_C + B_A$ |
| 50 | +- While the individual weight of $A$ is smaller than $B$, its children make the $A$ subtree heavier than the $B$ subtree, so its chosen by LMD GHOST over $B$. |
| 51 | + |
| 52 | +In general: |
| 53 | + |
| 54 | +$$W_N = B_N + \sum_i^{i \in \text{children}[N]}W_i$$ |
| 55 | + |
| 56 | +## Slashing |
| 57 | + |
| 58 | +In the previous scheme, there are two rewards: |
| 59 | + |
| 60 | +- Proposer rewards, given to a proposer when their block is included in the chain. This also adds an incentive for them to try to predict the most-likely branch to be the canonical one. |
| 61 | +- Attester rewards, which are smaller. These are given if the blocks they attest to are included. |
| 62 | + |
| 63 | +These incentives, however, are not enough. To maximize their likelihood of getting rewards, they may misbehave: |
| 64 | + |
| 65 | +- Proposers may propose a block for every current fork. |
| 66 | +- Attesters may attest to every current head in their local chains. |
| 67 | + |
| 68 | +These misbehaviors debilitate the protocol (they give weight to all forks) and no honest node running fork-choice would take part on them. To prevent them, nodes that are detected while doing them are slashed (punished), which means that they are excluded from the validator set and a portion of their stake is burned. |
| 69 | + |
| 70 | +Nodes provide proofs of the offenses, and proposers including them in blocks get whistleblower rewards. Proofs are: |
| 71 | + |
| 72 | +- For proposer slashing: two block headers in the same slot signed by the same signature. |
| 73 | +- For attester slashing: two attestations signed in the same slot by the same signature. |
| 74 | + |
| 75 | +## Guarantees |
| 76 | + |
| 77 | +- Majority honest progress: if the network has over 50% nodes running this algorithm honestly, the chain progresses and each older block is exponentially more unlikely to be reverted. |
| 78 | +- Stability: fork-choice is self-reinforcing and acts as a good predictor of the next block. |
| 79 | +- Manipulation resistence. Not only is it hard to build a secret chain and propose it, but it prevents getting attestations for it, so the current canonical one is always more likely to be heavier. This holds even if the length of the secret chain is higher. |
0 commit comments