Reentrancy is not somebody breaking into your contract. It is your own function being called a second time before it finished writing the number that would have stopped the call.
require(balance >= amount)passescall{value:}hands over controlbalance -= amountnever ran yet
I have a habit of re-deriving famous bugs on paper, because the press coverage of a nine-figure exploit and the actual defect are usually two different stories. The coverage says hacker drains protocol. The defect says two statements are in the wrong order.
In June 2016 I sat down with the code that had just emptied roughly 3.6 million ether out of The DAO and tried to reduce it to the smallest thing that could still be called the bug. It took about ten minutes. What I ended up with was four lines of Solidity and one arrow pointing the wrong way.
The DAO had raised about 12.7 million ETH — roughly $150 million at the time — from more than eleven thousand participants in a four-week sale that opened on 30 April 2016. On 17 June 2016 an attacker moved about 3.6 million ETH into a child DAO he controlled. The funds were then locked behind the DAO’s own withdrawal delay, which turned a theft into a 28-day public negotiation, which turned into a hard fork at block 1,920,000 on 20 July 2016, which turned into Ethereum Classic. The bug was three lines. The response was a constitutional crisis. Those two facts get filed under the same headline and they should not.

The Two Lines That Decide Everything
Here is the shape of the vulnerable function. It is not the exact DAO source, but it preserves the order of operations that mattered, which is the only part I care about here.
function withdraw(uint256 amount) public {
require(balance[msg.sender] >= amount); // 1. CHECK
payable(msg.sender).call{value: amount}(""); // 2. INTERACTION
balance[msg.sender] -= amount; // 3. EFFECT
}
Read step 2 carefully. call is not a payment — it is a handover of control. It forwards the value, and it also hands the callee the remaining gas and the right to execute arbitrary code, including code that calls back into this same function, in the same transaction, before line 3 ever runs.
So the second call re-enters at the same require with the balance still holding its original value — the first frame has not written its decrement yet. The check passes again. And again. Each frame sends ether out; only when the recursion finally unwinds does each frame write its decrement, and by then the ether is already gone.
The fix is one reordering, plus a check on the return value that the low-level call does not give you for free:
function withdraw(uint256 amount) public {
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount; // EFFECT first
(bool ok, ) = payable(msg.sender).call{value: amount}("");
require(ok, "send failed");
}
This is what people mean by checks-effects-interactions. It sounds like a library or a design pattern, and it is neither. It is a sentence about the order of three kinds of statement, and if you get the order right you do not need the sentence.
What the Attacker’s Contract Actually Did
The interesting artifact is not The DAO. It is the two-kilobyte contract on the other side, deployed on 15 June 2016 at block 1,706,608 — two days before the attack.
It had a trigger function that set a counter and called splitDAO. It had a fallback function — the default handler that runs whenever the contract receives plain ether with no matching function — and the recursion lived there. When the DAO paid out, the payment itself invoked the fallback. If the caller was the DAO’s reward account, the fallback compared the counter against a limit set to 29, incremented it, and called splitDAO again.
That is the whole exploit. The attacker did not pick a lock. He wrote a contract that answers the phone, and every time the DAO called to pay him, he asked to be paid again. The counter of 29 is just his recursion depth: run 29 nested withdrawals, unwinding them later, then move the DAO tokens out and reset the counter to 1 so the cycle could repeat in a new transaction.
There is even a fingerprint: the contract emits an event carrying the string constuctor fail. The misspelling is present in the deployed bytecode, and it is how researchers later identified the artifact. Nobody was reading the code for spelling, which is roughly the point.
Why the Delay Was the Most Consequential Line in the Code
The DAO’s split mechanism had a waiting period before a child DAO could move its funds. That delay is normally described as the reason the community had time to respond, and that is true — a white-hat group used the same exploit to move the remaining vulnerable ether out of reach, and the eventual recovery ran through a contract that let original holders redeem.
But look at what the delay actually did to the incident. Without it, the story ends in one block and the only possible responses are “accept it” or “change the past”. With it, there was a window in which the theft was legally reversible by protocol surgery. The window is what converted a bug into a fork, and the fork is what converted a theft into a permanent split of the ecosystem.
So the honest lesson is not “audits would have caught it” — the reentrancy class was known and the bug had been flagged in review. The lesson is that a withdrawal delay is a governance mechanism wearing an accounting costume. If you build one, you are deciding in advance how much social coordination your protocol can survive.

The 2020 Remake: When Ordinary Token Transfers Got a Callback
For a few years after 2016 the industry’s practical defence was accidental. Solidity’s transfer() and send() forwarded a fixed 2,300-gas stipend to the recipient, which was rarely enough to write storage, so a receiving contract could not easily re-enter. Then EIP-1884 repriced SLOAD from 200 to 800 gas in Istanbul, and a pile of contracts whose fallbacks wrote storage broke. The stipend was never a security feature; it was a coincidence that happened to be load-bearing.
The real remake came from a different direction: token standards that added callbacks on purpose. ERC-777 notifies the sender through a tokensToSend hook, looked up in the ERC-1820 registry, before balances change.
On 18 April 2020, Uniswap’s imBTC pool was drained through exactly that hook — around 1,278 ETH. About twenty-four hours later, the same trick hit Lendf.Me, a lending market that was a near-verbatim fork of Compound v1, and took roughly $25,236,849 across a dozen markets.
The Lendf.Me version is the one worth studying, because it is not the textbook bug. Its supply() function calculated the user’s new collateral balance into a local variable, then called doTransferIn() — a token transferFrom — and only afterwards wrote the balance to storage. For a plain ERC-20, that ordering is harmless: transferFrom is a leaf call that cannot call you back.
imBTC was not a plain ERC-20. The transfer fired the attacker’s tokensToSend hook, and from inside that hook the attacker called withdraw(). The withdraw correctly decremented the stored balance. Then control returned to supply(), which overwrote storage with the stale value it had computed before the transfer, erasing the record of the withdrawal. The attacker’s collateral was now counted twice. In the proof of concept, 21,595 imBTC went in and 43,189 came out — almost exactly double.
The fix was never “don’t list weird tokens”. The fix is that a token transfer is an external call, and treating it as a leaf was the bug.
Three Flavours, and Only the First One Is Famous
Reentrancy is a family, not a bug. The distinctions matter because the defences differ.
| Flavour | What re-enters | The DAO (2016) | Lendf.Me (2020) | Read-only |
|---|---|---|---|---|
| Entry point | the same function | same splitDAO | a different function, mid-execution | a view another contract trusts |
| What breaks | your balance ledger | drained in one tx | collateral double-counted | a price read in a half-finished state |
| Guard that works | nonReentrant on that function | yes | must cover the whole entry surface | guards do not help; you need a check on the consumer side |
The third column is the one nobody budgets for. In a read-only reentrancy, nothing in the victim’s own accounting goes wrong. An attacker takes a flash loan, enters a legitimate callback, and during that callback calls a view function on a pool — get_virtual_price, say — that reports a value mid-update. A third protocol that trusts that number as an oracle then misprices a loan. The victim contract is fine. The victim is the protocol that asked it a question at a bad moment.
If you want the mechanics of how these get financed and executed inside a single block, the flash-loan piece at what is a flash loan is the prerequisite.

What the Guard Actually Costs
The standard defence is a mutex: a boolean that is set on entry and cleared on exit.
modifier nonReentrant() {
require(!locked, "reentrant");
locked = true;
_;
locked = false;
}
Writing that boolean is a storage write — tens of thousands of gas cold, thousands warm — which is why the guard used to be left off the cheap paths and added only to withdraw. Two things changed that.
First, EIP-1153 introduced transient storage in Cancun (13 March 2024): TSTORE and TLOAD cost 100 gas each and are discarded at the end of the transaction, which is exactly the lifetime a reentrancy lock needs. OpenZeppelin shipped ReentrancyGuardTransient shortly after. The excuse that a guard is too expensive is now mostly historical.
Second, cross-function attacks made per-function guards insufficient. A guard on withdraw does nothing for the supply → withdraw path that cost Lendf.Me $25 million. If you are guarding, guard the surface.
And accept the limit: a guard protects your state. It cannot stop somebody else from reading your state at a moment you consider intermediate. That is a protocol-integration problem, and it is not solvable by a modifier — only by making the intermediate state either unreachable or harmless to read.
The Part I Keep Coming Back To
The DAO’s attacker wrote about two kilobytes of code, most of which was bookkeeping for a recursion counter. There was no cryptography broken, no key stolen, no consensus failure. A function asked an untrusted party to do something and then trusted it to come back.
Every time I read an audit report and see the phrase “external call before state update”, I think about the odd asymmetry of this failure mode: it is the cheapest bug to fix and the most expensive to have shipped. One line moved up. That is the entire patch.
- Write the number before you talk to anybody. Every external call — including a token transfer, a callback hook, or a
.callyou are sure is safe — is a re-entry point. - Guard the whole entry surface, not the one function you are worried about. Cross-function reentrancy is what the modern attacks use, because the single-function version has been understood for a decade.
- Assume someone else is reading your contract mid-update. If a view function can return a half-finished number that another protocol treats as a price, you have exported your reentrancy to a party who never audited you.
The honest summary: reentrancy is not an attack on your contract. It is an attack on your ordering, and ordering is the one thing a compiler will never check for you.
(The End)


