โ ๏ธ Storage Collisions: Don't Break State
Learn how to avoid catastrophic storage layout errors
Learn upgradeable smart contract patterns
Your Progress
0 / 5 completed๐ฅ The Storage Layout Trap
Storage collisions are the #1 cause of catastrophic upgrade failures. When Logic V2 uses different storage layout than V1, variables overwrite each other. Example: V1 has address owner at slot 0. V2 adds uint256 fee at slot 0. After upgrade, fee overwrites owner. Contract bricked. Storage layout must be append-only.
๐ฎ Interactive: Storage Collision Simulator
Toggle between safe and unsafe upgrades to see how storage layout changes cause collisions. Watch variables overwrite each other when layout rules are violated.
V2 keeps all V1 variables in same slots (0-2). New variable added at slot 3. No overwrites. All existing data preserved. Append-only is safe.
๐ Storage Layout Rules
๐ก๏ธ Storage Collision Prevention
Add uint256[50] __gap at end of contracts. Reserves 50 slots for future variables. Can shrink gap when adding new variables. OpenZeppelin standard.
Use OpenZeppelin Upgrades Plugin or Hardhat Upgrades. They compare storage layouts before upgrade. Detect collisions automatically. Fail deployment if unsafe.
Maintain STORAGE_LAYOUT.md showing slot assignments. Update on every version. Reviewers can spot collisions before deployment.
Proxy's own variables (implementation address, admin) use random storage slots calculated from keccak256(string). This prevents Logic from accidentally overwriting Proxy state.
โ ๏ธ Real Collision Disasters
Not exactly storage collision, but related: library contract (shared logic) was made ownable by accident. Attacker became owner, self-destructed library. All wallets using it became unusable. 300+ wallets frozen permanently.
Upgrade added new storage variables without gap. Overwrote critical governance parameters. Attacker exploited collision to pass malicious proposal. Funds drained. Storage validation would have prevented this.
๐ก Key Insight
Storage collisions are silent killers. Your upgrade transaction succeeds. No error messages. But data is corrupted. owner variable now contains a random number. totalSupply overwritten with false. Contract appears to work but behaves unpredictably. Prevention is mandatory, not optional. Use storage gaps, validate with tools, document layouts, and review every upgrade. One collision can brick a billion-dollar protocol.