Optimism
A bug in Optimism's Geth fork caused the SELFDESTRUCT opcode to set the account balance to zero directly on the state object instead of redirecting to the OVM_ETH contract, allowing an attacker to repeatedly self-destruct a contract and inflate its balance, then cash out the duplicated Ether.
medium.com
Published on medium.com
Optimism
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Optimism is a Layer 2 (L2) scaling solution for Ethereum built on the concept of optimistic rollups. Rather than executing and storing all data on Ethereum, Optimism stores only a summary of the L2 state on Ethereum, while the actual computation and storage of contracts and data happens on L2. This lets rollups inherit Ethereum's security guarantees while acting as an efficient scaling solution.
Optimistic rollups batch off-chain transactions together but do not include additional proof guaranteeing their validity; they "optimistically" assume all transactions are valid. When assertions of the L2 state are posted on-chain, validators can challenge an assertion they believe is incorrect, which is called fraud detection. The security of the system relies on the validator posting a bond along with their state that is forfeited to the challenger if the challenger can prove the state is incorrect.
Optimism depends on fault proofs to detect whether a transaction being verified is incorrect, and it handles fault proofs using OVM 2.0. Executing means advancing the state of a blockchain by running transactions inside the virtual machine; proving is the act of convincing L1 contracts that the state produced by executing some transactions is correct. In Optimism, executing and proving are done together: if there were a dispute between Alice and Bob, the transaction in question would be re-executed (replayed) on the L1 chain. Because certain opcodes such as BLOCKNUMBER cannot be relied on to return the same value on L1 and L2, OVM 2.0 replaces all context-dependent opcodes with OVM counterparts, like ovmBLOCKNUMBER, to retain the context of the disputed transaction on L2 when verifying it on L1.
OVM is equivalent to the Ethereum Virtual Machine (EVM) specification, so Optimism is close to identical with Ethereum and shares the same account and state structure. OVM 1.0 stored ETH as ERC-20 tokens instead, which caused problems for the network, such as broken gas tokens. When OVM 2.0 launched it stopped supporting that feature but still stores all user account balances in the storage state of an ERC-20 contract. Optimism modified Geth, one of the original three implementations of the Ethereum protocol, to apply patches to StateDB to store native balances in an ERC-20 token storage state.
ETH is therefore still represented internally as an ERC-20 token at the address [redacted]. That contract is also called OVM_ETH. As a result, user balances are always zero inside the state trie (where Ethereum would store the balance), and the user's actual balance is stored in that token's storage. The UsingOVM flag is set with a USING_OVM environment variable; operations on the StateDB that affect an account balance are redirected from the underlying stateObject (which represents an individual account) to the storage state in the OVM_ETH contract when that flag is active.
Setup
On February 2, whitehat Jay Freeman (Saurik), known for developing Cydia, reported a critical vulnerability in the Optimism protocol found in OVM 2.0. The bug was found in the Optimism Geth client's handling of account balances in StateDB. The relevant code lives in the Optimism Geth implementation, and the report links to it in the MetisProtocol mvm repository at l2geth/core/state/statedb.go.
Finding it
The starting point is the SELFDESTRUCT opcode. This command causes a contract to self-destruct, deleting its account object; its main advantage is that it permits enormous amounts of obsolete state to be easily removed from the active set of the blockchain.
The selfdestruct(address) method, renamed from suicide(address), destroys all bytecode from the calling contract address and transfers all Ether held to the target address. No functions, including the fallback, are called if the target address is also a contract. The important detail is that the contract object is destroyed at the end of a transaction, so operations on that contract can still be performed after calling selfdestruct, as long as they are in the same transaction. After selfdestruct has been executed, the remaining Ether stored at that address is sent to the designated target, and then the storage and code are removed from the state.
All of this is handled by the Optimism client, a fork of Geth. In the Optimism Geth implementation there is a piece of code that causes the whole vulnerability:

The issue is with setting the balance of an account after selfdestruct to 0:
stateObject.data.Balance = new(big.Int)
The Optimism client sets the balance to zero directly on the stateObject instead of checking UsingOVM and redirecting the balance modification to the OVM_ETH contract.
Exploitation
Because of this bug, when a contract is selfdestructed it gives the balance of the calling contract to the target and still keeps the original balance. The code modifies a stateObject balance and does not update native balances in the ERC-20 token storage state (the OVM_ETH contract).
An attacker could use this to inflate the balance of the target contract by repeatedly selfdestructing a contract that holds Ether. After several iterations the attacker can cash out the inflated Ether balance, creating money out of thin air.
Step 1: build the exploit contract
Create an exploit contract that contains two internal functions. One has selfdestruct, and a second withdraws the balance of the contract to the caller. The function with selfdestruct sends the contract's balance to itself, which inflates the value due to the bug in Optimism's Geth implementation.
Step 2: deploy and loop
The attacker deploys the contract and, in the constructor, makes calls to the selfdestruct function multiple times in a loop.
Step 3: withdraw the inflated funds
After the loop is finished, the constructor calls the second internal function of the exploit contract to transfer the inflated funds to the attacker.
Step 4: the contract disappears
The exploit contract is destroyed at the end of the transaction.
Root cause
The mistake is in the Optimism Geth StateDB handling of selfdestruct: the balance is zeroed on the stateObject directly rather than through the UsingOVM redirection that sends balance changes to the OVM_ETH ERC-20 storage. Since native balances live in the OVM_ETH contract's storage, zeroing the stateObject balance does not remove the real balance, so the transfer to the target adds funds while the original balance remains. The code sets the balance directly on the stateObject instead of checking UsingOVM and redirecting the change to the OVM_ETH contract.
Impact
The bug would have allowed an attacker to replicate money continuously on any chain using the vulnerability found in OVM 2.0, printing an unlimited amount of OETH. That OETH could then be used to drain DEXes and bridge assets from Optimism to Ethereum, allowing a hacker to steal large amounts of money. At the time of submission, Optimism's TVL of $322.5M was potentially at risk.
The bug was quite complex to understand and hard to find, so the likelihood of exploitation was low, but if anybody had found it before Saurik responsibly disclosed it, the impact would have been enormous. The financial impact was clearly critical, hence the full payout from Optimism: the full critical amount listed on Immunefi's bug bounty page for Optimism, $2,000,042.
Fix
Optimism fixed the bug in a GitHub PR (ethereum-optimism/optimism PR 2146), and the team released a fix quickly after receiving the report. The team also responsibly disclosed the bug downstream to all forks of Optimism, including Metis on Immunefi. The report points readers to Saurik's blog post for the reasoning behind the fix and why it was implemented in that particular way.
- Audit every
StateDBbalance mutation in an OVM-style fork for a missingUsingOVMcheck. A positive result is a code path that writesstateObject.data.Balancedirectly while native balances are held in theOVM_ETHstorage. - Test other opcodes that move value (calls, transfers, refunds) for the same redirection gap. A positive result is a balance change visible in the
stateObjectthat is not reflected in theOVM_ETHstorage, or vice versa. - Replay the selfdestruct loop on a local fork: a contract that selfdestructs to itself repeatedly in one transaction and then withdraws. A positive result is a withdrawable balance larger than the Ether originally held.
- Check downstream forks of Optimism for the same unpatched code. A positive result is the same direct balance assignment in their
statedb.go. - Look for other places where the
USING_OVMenvironment flag is read but a balance-affecting branch bypasses it. A positive result is a branch that changes balances without consulting the flag.
What happened, in plain words
On 2 February, a security researcher called Jay Freeman, who goes by the handle Saurik and is known for building Cydia, reported a serious flaw in Optimism. Optimism is a system that sits on top of Ethereum to make it cheaper and faster to use. The flaw would have let an attacker create money out of nothing, over and over, on any chain that used the same code. The project paid him the full critical reward listed on its bug bounty page: $2,000,042.
A bug bounty is a public offer: a company says "if you find a security problem in our software and tell us privately instead of abusing it, we will pay you". A whitehat is a researcher who does exactly that, finding bugs to get them fixed rather than to steal. The report calls Saurik a whitehat because he disclosed the problem responsibly.
This article walks through the same story the technical write-up tells, but from zero. You only need to be able to use a browser. Every term is explained the first time it appears, and there is a glossary at the end.

The background you need first
What Ethereum is, and why it is slow
Ethereum is a blockchain: a shared ledger that many independent computers keep copies of, so nobody can quietly change the past. People run programs on it, called smart contracts. A smart contract is code that lives at an address on the chain and runs automatically when someone sends it a message.
The problem is throughput. Ethereum can only process a limited number of transactions at a time, and when lots of people want to use it, the fee to get a transaction included goes up. The report describes this as low transaction throughput and high latency: transactions are slow and expensive relative to what the network can take.
Layer 1 and Layer 2
A Layer 1 (L1) is the base blockchain itself. Ethereum is the L1 here.
A Layer 2 (L2) is a system built on top of an L1 to make things faster and cheaper. The comparison that helps: think of a busy shop with one cashier. Instead of every customer paying one by one at the till, you let groups of customers settle up among themselves and only hand the cashier a single summary at the end. The cashier (L1) still provides the final record, but most of the work happens off to the side (L2).
The report lists two general kinds of scaling:
- On-chain scaling: changing the blockchain itself, for example splitting data or computation across many machines (sharding).
- Off-chain scaling: doing the work outside the blockchain. The transaction code runs elsewhere, and only a summary goes back to Ethereum. These are the L2 solutions.
Optimistic rollups
Optimism is an L2 built on optimistic rollups. A rollup takes many off-chain transactions, bundles them together (rolls them up), and posts a summary of the resulting state to Ethereum. The word state means the current contents of all accounts and contracts: who owns what, and what each contract's storage holds.
The "optimistic" part: the rollup does not send a mathematical proof that every transaction in the batch was valid. It simply assumes they are valid, and hopes nobody cheats. To keep that safe, validators can challenge an assertion they believe is incorrect. When a summary is posted on-chain, validators can dispute it if they think it is wrong. This is called fraud detection.
To make challenging meaningful, a validator who posts a claim must also put up a bond: money locked up that they lose to the challenger if the challenger can prove the claim was wrong. The everyday comparison is a deposit on a rental: you get it back if you behave, you forfeit it if you don't.
Executing versus proving
Two words come up constantly:
- Executing means actually running the transactions in the virtual machine and moving the state forward. The virtual machine here is the EVM, the Ethereum Virtual Machine: the engine that runs smart contract code.
- Proving means convincing the L1 contracts that the state produced by executing those transactions is correct.
In Optimism, executing and proving are done together. If two parties, say Alice and Bob, disagree, the disputed transaction is replayed on the L1 chain so the L1 can check the result.
The EVM, the stack, and opcodes
The EVM has a stack-based architecture. A stack is like a pile of plates: you put things on top and take them off the top. The EVM manipulates this stack using opcodes, which are the basic instructions of the machine, such as ADD, SSTORE and MLOAD. When a smart contract receives a message, its bytecode (compiled code) is run, and that code can update its own state or send further messages to other contracts.
Why replaying on L1 is tricky, and what OVM 2.0 does about it
Some opcodes depend on context. BLOCKNUMBER, for example, returns the number of the current block. If you replay a transaction on L1, that opcode will return a different number than it did on L2, because the two chains are at different points in time. The report calls these context-dependent opcodes.
The fix Optimism chose: replace every context-dependent opcode with an OVM counterpart, such as ovmBLOCKNUMBER, so the original L2 context is preserved when the transaction is verified on L1. OVM stands for Optimism Virtual Machine, and OVM 2.0 replaces context-dependent opcodes with OVM counterparts. The report notes that OVM is equivalent to the EVM specification, so Optimism is close to identical to Ethereum and shares the same account and state structure.
How Optimism stores ETH: the OVM_ETH contract
Here is the detail that makes the whole bug possible.
In OVM 1.0, Optimism stored ETH as an ERC-20 token instead of as a native balance. ERC-20 is a standard for tokens on Ethereum: a common set of rules that lets any wallet or exchange handle a token. Storing ETH this way caused problems, for example with gas tokens (tokens used to pay for transaction fees).
When OVM 2.0 launched, it stopped supporting that feature, but it still keeps all user balances in the storage of an ERC-20 contract. Optimism modified Geth, one of the original three implementations of the Ethereum protocol, patching its StateDB so that native balances are stored in an ERC-20 token's storage.
StateDB is the part of the client that keeps track of account state: balances, storage, code. A client is the software a node runs to participate in the network.
The result: ETH is still represented internally as an ERC-20 token at this address:
[redacted]
That contract is called OVM_ETH. Because of this design, a user's balance is always zero inside the state trie (the data structure where Ethereum would normally store the balance), and the real balance lives in the OVM_ETH contract's storage.
To keep this straight, the code uses a flag called UsingOVM, set through an environment variable called USING_OVM. An environment variable is a named setting passed to a program when it starts, like a switch on a machine. When the flag is active, any operation on StateDB that changes an account balance is redirected from the underlying stateObject (the object representing one individual account) to the storage of the OVM_ETH contract.
In plain words: normally, if you want to change someone's balance, you have to go through the special token contract. The UsingOVM flag is the signpost that says "go the long way round".
How the bug was found
The SELFDESTRUCT opcode
Before the vulnerability itself, one more concept: SELFDESTRUCT.
This opcode makes a contract destroy itself, deleting its account object. Its main advantage is housekeeping: it lets huge amounts of obsolete state be removed from the active set of the blockchain, like clearing out a warehouse.
The method is selfdestruct(address), renamed from the older suicide(address). It does two things:
- It destroys all bytecode at the calling contract's address.
- It transfers all Ether held by that contract to the target address.
If the target address is also a contract, no functions are called on it, not even the fallback. The fallback is a special function that runs when a contract receives a message that doesn't match any of its named functions.
The crucial detail: the contract object is destroyed at the end of the transaction, not immediately. So within the same transaction, you can still perform operations on that contract after calling selfdestruct. After the transaction ends, the remaining Ether at that address has been sent to the target, and the storage and code are removed from the state.
The line of code at the heart of it
All of this is handled by the Optimism client, a fork of Geth. A fork here means a copy of a codebase that a project modifies for its own use. In the Optimism Geth implementation there is a piece of code that causes the whole vulnerability.

The image above shows the code snippet from the Optimism Geth implementation that the report points to as the source of the vulnerability.
The problem is in how the balance of an account is set to zero after selfdestruct:
stateObject.data.Balance = new(big.Int)
Line by line:
stateObjectis the object representing one account inside the client..datais the record inside that object holding the account's data..Balanceis the balance field of that record.new(big.Int)creates a brand new big integer, which starts at zero.big.Intis a number type that can hold very large values, which matters for money.- The
=assigns that zero value to the balance field.
So the line sets the account's balance to zero. The mistake is where it does it. The Optimism client writes the zero directly onto the stateObject, instead of checking UsingOVM and redirecting the balance change to the OVM_ETH contract. Remember: the real balance lives in the OVM_ETH storage, not in the stateObject. Zeroing the stateObject therefore does not remove the real money.
How it could have been exploited
Because of this bug, when a contract is selfdestructed, it gives the balance of the calling contract to the target and still keeps the original balance. The code modifies a stateObject balance and never updates the native balances in the ERC-20 token storage state, which is the OVM_ETH contract.
An attacker could use this to inflate the balance of the target contract by repeatedly selfdestructing a contract that holds Ether. After several iterations, the attacker can cash out the inflated Ether balance, creating money out of thin air.
The everyday comparison: imagine a bank that, when you close an account, hands you the money but forgets to cross the balance off its books. Close the account, get the money, close it again, get the money again. The books still say you have the money, so you can keep withdrawing.
Here is the step-by-step plan the report describes.
Step 1: build the exploit contract
Create an exploit contract with two internal functions. One has selfdestruct; the second withdraws the contract's balance to the caller. The function with selfdestruct sends the contract's balance to itself, which inflates the value because of the bug in Optimism's Geth implementation.
Why send the balance to itself? Because the bug means the transfer adds money to the target while the original balance is not removed. If the target is the contract itself, the contract's balance grows with every call.
Step 2: deploy and loop
The attacker deploys the contract and, in the constructor, calls the selfdestruct function multiple times in a loop. The constructor is the special function that runs once, automatically, when a contract is first created. Doing the loop there means everything happens inside a single transaction, which is exactly the window in which the contract still exists after selfdestruct.
Step 3: withdraw the inflated funds
After the loop finishes, the constructor calls the second internal function to transfer the inflated funds to the attacker.
Step 4: the contract disappears
At the end of the transaction, the exploit contract is destroyed.
The root cause, restated
The mistake is in how the Optimism Geth StateDB handles selfdestruct. The balance is zeroed on the stateObject directly, rather than through the UsingOVM redirection that sends balance changes to the OVM_ETH ERC-20 storage. Since native balances live in the OVM_ETH contract's storage, zeroing the stateObject balance does not remove the real balance. So the transfer to the target adds funds while the original balance remains. The existing UsingOVM redirection, which normally protects balance operations, was simply not applied on this path.
What the impact would have been
The bug would have allowed an attacker to replicate money continuously on any chain using the vulnerability found in OVM 2.0, printing an unlimited amount of OETH. That OETH could then be used to drain DEXes and bridge assets from Optimism to Ethereum, allowing a hacker to steal large amounts of money.
Two terms there:
- A DEX is a decentralised exchange: a smart contract that lets people swap tokens without a central company in the middle. If you can mint unlimited tokens, you can swap them for everything else in the pool.
- A bridge moves assets between chains. Draining a bridge means taking the assets it holds on the other side.
At the time of submission, Optimism's TVL of $322.5M was potentially at risk. TVL stands for total value locked: the total amount of money sitting in the system.
The bug was complex to understand and hard to find, so the likelihood of exploitation was low. But if anyone had found it before Saurik responsibly disclosed it, the impact would have been enormous. The financial impact was clearly critical, hence the full payout from Optimism: the full critical amount listed on Immunefi's bug bounty page for Optimism, $2,000,042.
The fix
Optimism fixed the bug in a GitHub pull request, ethereum-optimism/optimism PR 2146, and released the fix quickly after receiving the report. The team also responsibly disclosed the bug downstream to all forks of Optimism, including Metis on Immunefi. Downstream here means the projects that copied Optimism's code and would inherit the same flaw.
The report points readers to Saurik's blog post for the reasoning behind the fix and why it was implemented in that particular way.
Things worth testing if you look at similar code
- Audit every
StateDBbalance mutation in an OVM-style fork for a missingUsingOVMcheck. A positive result is a code path that writesstateObject.data.Balancedirectly while native balances are held in theOVM_ETHstorage. - Test other opcodes that move value (calls, transfers, refunds) for the same redirection gap. A positive result is a balance change visible in the
stateObjectthat is not reflected in theOVM_ETHstorage, or vice versa. - Replay the selfdestruct loop on a local fork: a contract that selfdestructs to itself repeatedly in one transaction and then withdraws. A positive result is a withdrawable balance larger than the Ether originally held.
- Check downstream forks of Optimism for the same unpatched code. A positive result is the same direct balance assignment in their
statedb.go. - Look for other places where the
USING_OVMenvironment flag is read but a balance-affecting branch bypasses it. A positive result is a branch that changes balances without consulting the flag.
Why this story matters
Even audited code can have bugs. Bug bounty programs are the last line of defence against blackhat activity: they give researchers a large reward for disclosing a bug responsibly instead of exploiting it. A blackhat is someone who uses a vulnerability for personal gain. Having a security-oriented mindset for a project and protocol is what turns a potential catastrophe into a paid fix.
Glossary
- L1 (Layer 1): the base blockchain itself, here Ethereum.
- L2 (Layer 2): a system built on top of an L1 to make transactions faster and cheaper.
- Blockchain: a shared ledger kept by many independent computers, so its history cannot be quietly changed.
- Smart contract: code that lives at an address on a blockchain and runs automatically when it receives a message.
- Transaction: a message that changes the state of the blockchain, such as moving funds or calling a contract.
- Throughput: how many transactions a system can process in a given time.
- Latency: the delay before a transaction is processed.
- Scaling solution: a design that increases a blockchain's capacity.
- On-chain scaling: changing the blockchain itself, for example by sharding data or computation.
- Off-chain scaling: doing work outside the blockchain and posting only a summary back to it.
- Sharding: splitting data or computation across many machines so each handles a part.
- Rollup: a system that bundles many off-chain transactions and posts a summary of the resulting state to the L1.
- Optimistic rollup: a rollup that assumes transactions are valid unless someone challenges them.
- State: the current contents of all accounts and contracts: balances, storage and code.
- State trie: the data structure where Ethereum normally stores account state, including balances.
- Validator: a participant who checks and can dispute claims about the state of the chain.
- Fraud detection: the process of challenging an incorrect state claim posted on-chain.
- Bond: money a validator locks up and forfeits if their claim is proven wrong.
- Fault proof: a mechanism for detecting whether a transaction being verified is incorrect.
- Executing: running transactions in the virtual machine and advancing the state.
- Proving: convincing the L1 contracts that a given state is correct.
- EVM (Ethereum Virtual Machine): the engine that runs smart contract code on Ethereum.
- OVM (Optimism Virtual Machine): Optimism's version of the EVM, designed to be equivalent to it.
- OVM 2.0: the version of the OVM that introduced EVM equivalence and the OVM-prefixed opcodes.
- Stack-based architecture: a design where values are pushed onto and popped off a pile, the stack.
- Opcode: a basic instruction of the virtual machine, such as
ADDorSSTORE. - Bytecode: compiled code that the virtual machine runs.
- Context-dependent opcode: an opcode whose result depends on the chain's current context, such as
BLOCKNUMBER. - ERC-20: a standard set of rules for tokens on Ethereum, so wallets and exchanges can handle them uniformly.
- Gas token: a token used to pay for transaction fees.
- Geth: one of the original three implementations of the Ethereum protocol.
- Client: the software a node runs to take part in the network.
- Fork: a copy of a codebase that a project modifies for its own use.
- StateDB: the part of the client that tracks account state: balances, storage and code.
- stateObject: the object inside the client that represents one individual account.
- OVM_ETH: the ERC-20 contract at
[redacted]that holds native balances on Optimism. - UsingOVM / USING_OVM: the flag, set through an environment variable, that tells the client to redirect balance changes to the
OVM_ETHcontract. - Environment variable: a named setting passed to a program when it starts.
- SELFDESTRUCT: the opcode that makes a contract destroy itself and send its Ether to a target address.
- Fallback function: a special function that runs when a contract receives a message matching none of its named functions.
- Constructor: the function that runs once, automatically, when a contract is first created.
- Exploit contract: a contract written specifically to take advantage of a vulnerability.
- OETH: the Optimism representation of ETH.
- DEX (decentralised exchange): a smart contract that lets people swap tokens without a central company.
- Bridge: a system that moves assets between blockchains.
- TVL (total value locked): the total amount of money sitting in a system.
- Bug bounty: a public offer to pay researchers for privately reporting security bugs.
- Whitehat: a researcher who finds bugs in order to get them fixed rather than to abuse them.
- Blackhat: someone who uses a vulnerability for personal gain.
- Pull request: a proposed change to a codebase that others can review before it is merged.
- Downstream: projects that copied code and would inherit the same flaw.
- Critical: the highest severity rating, used here for a bug that could cause enormous financial loss.