# The `Market.migrate()` function has no effect and does not migrate `PositionStorageGlobal` to the new storage layout, breaking the migration assumption.

Perennial's Market.migrate() fails to consolidate PositionStorageGlobal from 2-slot to 1-slot layout because read()/store() were never updated, breaking the v2.2→v2.3 migration.

- Payout: Audit contest
- Severity: medium
- Program: Perennial
- Found by: eeyore
- Disclosed: 2024-08-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/55
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-perennial-v2-update-3%3AM-10
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:incomplete-fix technique:source-review vuln:accounting-error

**Target:** Solidity storage library (PositionStorageGlobalLib) in Perennial market contracts
**Root cause:** PositionStorageGlobalLib.read() and store() still encode/decode maker from slot1 using the old 2-slot layout, so migrate() reads and writes the same stale layout and never moves maker into slot0.
**Steps:**
1. Open Position.sol PositionStorageGlobalLib.read()/store() and note maker is still read from self.slot1 and written to slot1.
2. Call Market.migrate() on a market with deprecated maker set in slot1 and new maker unset in slot0.
3. Observe migrate() succeeds but slot0.maker remains zero and slot1.maker unchanged; read() still returns maker from slot1.
4. Confirm the 1-slot layout documented in StoredPositionGlobal is never actually applied.
**How found:** Code review against the v2.3 migration runbook: compared documented 1-slot StoredPositionGlobal layout to the actual read()/store() bit-shift offsets and noticed maker still sourced from slot1.
**Insight:** When auditing storage migrations, diff the documented new struct layout against the actual bit-shift offsets in read()/store(); a migrate() that calls unchanged accessors is a silent no-op.

## Proof of concept (copied exactly from the original report)

**Recommended read() fix for 1-slot layout**

```
function read(PositionStorageGlobal storage self) internal view returns (Position memory) {
        (uint256 slot0, uint256 slot1) = (self.slot0, self.slot1);
        return Position(
            uint256(slot0 << (256 - 32)) >> (256 - 32),
-           UFixed6.wrap(uint256(slot1 << (256 - 64)) >> (256 - 64)),
+           UFixed6.wrap(uint256(slot0 << (256 - 32 - 64)) >> (256 - 64)),
-           UFixed6.wrap(uint256(slot0 << (256 - 32 - 48 - 48 - 64)) >> (256 - 64)),
+           UFixed6.wrap(uint256(slot0 << (256 - 32 - 32 - 64 - 64)) >> (256 - 64)),
-           UFixed6.wrap(uint256(slot0 << (256 - 32 - 48 - 48 - 64 - 64)) >> (256 - 64))
+           UFixed6.wrap(uint256(slot0 << (256 - 32 - 32 - 64 - 64 - 64)) >> (256 - 64))
        );
```

**Recommended store() fix removing slot1 maker**

```
function store(PositionStorageGlobal storage self, Position memory newValue) public {
        PositionStorageLib.validate(newValue);

        if (newValue.maker.gt(UFixed6.wrap(type(uint64).max))) revert PositionStorageLib.PositionStorageInvalidError();
        if (newValue.long.gt(UFixed6.wrap(type(uint64).max))) revert PositionStorageLib.PositionStorageInvalidError();
        if (newValue.short.gt(UFixed6.wrap(type(uint64).max))) revert PositionStorageLib.PositionStorageInvalidError();

        uint256 encoded0 =
            uint256(newValue.timestamp << (256 - 32)) >> (256 - 32) |
+           uint256(UFixed6.unwrap(newValue.maker) << (256 - 64)) >> (256 - 32 - 32 - 64) |
-           uint256(UFixed6.unwrap(newValue.long) << (256 - 64)) >> (256 - 32 - 48 - 48 - 64) |
+           uint256(UFixed6.unwrap(newValue.long) << (256 - 64)) >> (256 - 32 - 32 - 64 - 64) |
-           uint256(UFixed6.unwrap(newValue.short) << (256 - 64)) >> (256 - 32 - 48 - 48 - 64 - 64);
+           uint256(UFixed6.unwrap(newValue.short) << (256 - 64)) >> (256 - 32 - 32 - 64 - 64 - 64);
-       uint256 encoded1 =
-           uint256(UFixed6.unwrap(newValue.maker) << (256 - 64)) >> (256 - 64);

        assembly {
            sstore(self.slot, encoded0)
-           sstore(add(self.slot, 1), encoded1) 
        }
```

