{
  "group": "fix-buy (buy side: onBuy, carried-value and rate rounding, minNetBuyRate and BuyAtLoss, duration and absolute cap checks, transient pre-buy state read by realAssets, token approvals)",
  "commit": "bbeda7b27ae80abcf070e0158cb8a31e87bb2286",
  "blocked": true,
  "blockedWhy": "Could not clone or build on this machine. The volume holding the workspace and TMPDIR (/dev/sda1, 38G) was 100% full, with 2.1 MB free at the start and 1.2 MB later. 'git clone https://github.com/morpho-org/vault-v2' failed with 'fatal: write error: No space left on device' / 'fetch-pack: invalid index-pack output'. So no Foundry build or test ran, and no PoC could be executed. As a fallback I fetched the source files at the pinned commit (bbeda7b) and at the audited commit (6476e9d) from raw.githubusercontent.com straight to stdout, and compared them by reading. Everything below is static analysis. Nothing was tested, and every statement about behaviour is UNVERIFIED by execution.",
  "fixes": [
    {
      "priorId": "Cantina 3.2.3",
      "change": "onBuy now requires absoluteCap(durationId) > 0 (DurationZeroAbsoluteCap) for every affected duration, before the absolute and relative cap comparisons. The duration id preimage also changed from (\"duration\", adapter, d) to (\"this/duration\", adapter, d). Old code: src/adapters/MidnightAdapter.sol@6476e9d:412-420, which had only the allocations[i] <= absoluteCap check.",
      "status": "fixed",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:586-603", "src/adapters/midnight/MidnightAdapter.sol:64-71"],
      "tested": false,
      "note": "A zero-exposure buy (units = 0) whose TTM reaches a duration with an unset cap now reverts. Side effect introduced by the change: once durations are configured, every buy with ttm >= durations[0] requires a positive absolute cap on each covered duration id, including zero-unit or reducing-exposure buys. This is a deliberate configuration requirement, not a fault. I found no new fault by reading. The durationAllocations loop changed from while to for+break with the same semantics (817-834)."
    },
    {
      "priorId": "Cantina 3.2.5",
      "change": "Register marks this present (not changed). Duration allocations still sum stored net credit (durationAllocations 817-834), and the NatSpec now documents this explicitly as an upper bound (line 69).",
      "status": "unchanged",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:69", "src/adapters/midnight/MidnightAdapter.sol:816-834", "src/adapters/midnight/MidnightAdapter.sol:591-603"],
      "tested": false,
      "note": "The prior issue is out of scope. Only the market being bought gets its stored netCredit refreshed (line 542) before durationAllocations. Other markets hit by bad debt still count their stale stored credit. This is the documented behaviour."
    },
    {
      "priorId": "Cantina 3.2.8",
      "change": "Carried value of existing credit is now floored: (newNetCredit - boughtNetCredit).mulDivDown(WAD, WAD + oldNetRate*ttm) + buyerAssets. Old code used mulDivUp (6476e9d:377-379). The rate is still floored with mulDivDown and cast with toUint64.",
      "status": "fixed",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:543-547", "src/adapters/midnight/MidnightAdapter.sol:19-24", "src/adapters/midnight/MidnightAdapter.sol:836-863"],
      "tested": false,
      "note": "Static argument, UNVERIFIED. For a zero-unit, zero-asset buy, av = floor(C/(1+r*t)) <= exact value, so the exact rate for av is >= r and r' = floor(...) >= r. The rate can no longer be ratcheted down by repeated empty buys. The rate does not ratchet up without bound either: realAssets uses the same floor, so floor(C/(1+r'*t)) >= av = floor(C/(1+r*t)). The market's realAssets term is therefore unchanged (non-decreasing, and it cannot exceed the exact value at r' because r' >= r), and av becomes a fixed point after at most one step. Remaining effect per non-empty buy: up to about netCredit*ttm/1e18 wei of value recognised early, because the rate is floored (documented at line 22). Example: 1e13 net credit (10M 6-dec) and ttm 3e7 s gives about 300 wei per buy. The expensive-smallest-unit token caveat is documented at line 24."
    },
    {
      "priorId": "Cantina 3.2.11",
      "change": "Carried value is floored instead of ceiled (545). The blended-rate BuyRateTooLow check against minRate was removed. It is replaced by a per-trade check: (boughtNetCredit - buyerAssets)*WAD >= minNetBuyRate*ttm*buyerAssets (NetBuyRateTooLow, line 537), which runs before any state change.",
      "status": "fixed",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:535-547"],
      "tested": false,
      "note": "The reported revert path is gone: the stored blended rate is no longer compared with the floor. With floored carry, the exact blended rate is >= the weighted average of oldNetRate and the trade rate, so floor(blend) >= floor(min(oldNetRate, tradeRate)). The stored rate can no longer fall below both inputs by more than the final 1-wei floor. The uint64 cast remains, so extreme-rate buys revert (documented, line 509). Division by zero remains when pre-buy carried value and buyerAssets are both 0 (documented, line 546). It is reachable only by a buy that pays 0 assets into a market with no carried value, and it reverts only that buy."
    },
    {
      "priorId": "Cantina 3.6.6",
      "change": "Same change as 3.2.8/3.2.11: carried value floored, rate floored. The rounding is documented in the VALUATION block.",
      "status": "partial",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:19-24", "src/adapters/midnight/MidnightAdapter.sol:544-547", "src/adapters/midnight/MidnightAdapter.sol:858-860"],
      "tested": false,
      "note": "The bias direction no longer inflates present value through the carry term. The rate floor still advances a bounded amount of interest per buy (line 22). This is now a documented, bounded rounding drift, not an unbounded cohort shift. I found no new fault by reading."
    },
    {
      "priorId": "TrustSec TRST-L-5",
      "change": "minRate (uint64, settable immediately) was replaced by minNetBuyRate (uint128). Increases are curator-only and immediate. Decreases go through timelocked() (291-302). The check now applies to each buy's own implied net rate (537), not to the blended rate.",
      "status": "partial",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:139-140", "src/adapters/midnight/MidnightAdapter.sol:291-302", "src/adapters/midnight/MidnightAdapter.sol:535-537"],
      "tested": false,
      "note": "Lowering is now timelocked. The default is still 0, and BuyAtLoss allows boughtNetCredit == buyerAssets, so par (zero-rate) buys pass until the curator raises the floor. The default timelock for decreaseMinNetBuyRate is 0 until set (line 80 requires timelocks to be set before the adapter is added). Possible overflow: minNetBuyRate*ttm*buyerAssets is checked arithmetic, so a curator-set extreme minNetBuyRate makes buys revert. That is trusted-role configuration, not a finding."
    },
    {
      "priorId": "TrustSec TRST-PR-1",
      "change": "Constructor max approvals were removed. deallocate calls SafeERC20Lib.forceApproveMax(asset, parentVault) on entry (426), and onBuy calls forceApproveMax(asset, midnight) before returning (613). forceApproveMax skips when allowance >= type(uint96).max/2 and otherwise approves 0, then type(uint256).max (src/libraries/SafeERC20Lib.sol:35-39).",
      "status": "fixed",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:426", "src/adapters/midnight/MidnightAdapter.sol:613", "src/libraries/SafeERC20Lib.sol:35-39"],
      "tested": false,
      "note": "Allowance is refreshed before every vault pull and after every buy callback, which happens before Midnight pulls the loan token from the buyer adapter. A token that caps allowances below uint96.max/2 triggers the approve(0)+approve(max) pair on every call, which costs more gas but works. The onBuy approval also runs on the forceDeallocate branch, where the adapter is the seller and the approval is unneeded. That is harmless. No new fault found by reading."
    },
    {
      "priorId": "TrustSec TRST-R-2",
      "change": "MarketData.maturity narrowed from uint48 to uint40 (market.maturity.toUint40() at 550). netRate stays uint64. The struct is now uint128 netCredit, uint64 netRate, uint40 maturity, uint8 index, 2 bools.",
      "status": "partial",
      "refs": ["src/adapters/midnight/interfaces/IMidnightAdapter.sol:10-23", "src/adapters/midnight/MidnightAdapter.sol:509", "src/adapters/midnight/MidnightAdapter.sol:547-550"],
      "tested": false,
      "note": "The freed bits were not given to the rate, so the toUint64 revert on very high implied rates remains (documented, line 509). Such a revert only blocks the specific buy. Maturities above 2^40 revert on the first buy via toUint40, which requires a curator-set maxTtm of about 34,000 years. Not a finding."
    },
    {
      "priorId": "TrustSec TRST-R-7",
      "change": "Per-trade minNetBuyRate (buy) and maxNetSellRate (sell) checks were introduced. They are adapter-wide, not a TTM-keyed curve.",
      "status": "partial",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:139-148", "src/adapters/midnight/MidnightAdapter.sol:535-537"],
      "tested": false,
      "note": "The per-trade check closes the 'low-rate buy passes on existing headroom' part. A per-duration curve is not implemented, though sub-ratifiers can constrain offers (line 32). It is a recommendation, so there is nothing to test."
    },
    {
      "priorId": "TrustSec TRST-R-13",
      "change": "Old flow: the overridden net credit was set, accrueInterest ran, and only then was state written. New flow: netCredit/netRate/market list are written first (539-557). Transient inProgressBuyMarketId / inProgressBuyNetCredit (= newNetCredit - boughtNetCredit, the live pre-buy credit) / inProgressBuyNetRate (= old rate) are then set, and realAssets reads them for that market (846-847) during vault.accrueInterest (575). They are cleared at 576, before funding (578-584), the duration caps (586-603) and allocate (606-611). The old idle-balance check was removed: a non-zero funding adapter is now always asked for the full buyerAssets, and data must always decode as (address, bytes).",
      "status": "partial",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:118-121", "src/adapters/midnight/MidnightAdapter.sol:508", "src/adapters/midnight/MidnightAdapter.sol:539-611", "src/adapters/midnight/MidnightAdapter.sol:836-863"],
      "tested": false,
      "note": "Documentation was added (118, 508). I checked the following edge cases of the new transient state by reading. (a) First buy into a market: the market is pushed with the new maturity, and realAssets reads inProgressBuyNetCredit = 0, so it contributes 0. Correct. (b) A buy where newNetCredit == 0 and oldNetCredit > 0: the market is removed, realAssets skips it, and the pre-buy live credit was 0. Correct. (c) Bought market with lossFactor != 0: the override uses live pre-buy credit, the same as the non-buy path. (d) removeMarket and forceRemove zero netRate, so a re-entered market's override uses rate 0 with 0 credit. (e) The override is cleared before vault.deallocate/allocate. Their accrueInterest is a no-op in the same block, so a stale override is not read. (f) Self-funding from the bought market itself: withdrawToVault diffs against the already-updated stored credit, so the two reported changes sum correctly. I found no exploitable fault, but this could not be executed. UNVERIFIED."
    },
    {
      "priorId": "TrustSec TRST-R-14",
      "change": "Rounding is now documented (19-24). Buys floor carried value, and sales ceil per-market targets when there is a shortfall.",
      "status": "fixed",
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:19-24", "src/adapters/midnight/MidnightAdapter.sol:544-547"],
      "tested": false,
      "note": "The documentation request is met. Buy-side drift is bounded as described under 3.2.8. The sell side is outside this group."
    }
  ],
  "hasCandidates": false,
  "candidates": [],
  "lowInfo": [
    {
      "title": "Zero-asset buy into a market with zero carried value reverts with a raw division by zero, not a named error",
      "class": "Informational",
      "verified": false,
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:535-547"],
      "detail": "With buyerAssets = 0 and units > 0, BuyAtLoss (536) and NetBuyRateTooLow (537: 0 >= minNetBuyRate*ttm*0) both pass. If pre-buy carried value floors to 0, amortizedValue = 0 and mulDivDown(WAD, 0) reverts with a panic. The inline comment at 546 acknowledges this. Only the offending take reverts, so the impact is limited to a confusing error. A named error, or requiring buyerAssets > 0 when units > 0, would be clearer. UNVERIFIED: not executed (disk full)."
    },
    {
      "title": "Per-buy early interest recognition from the floored rate scales with netCredit*ttm",
      "class": "Informational",
      "verified": false,
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:22", "src/adapters/midnight/MidnightAdapter.sol:547", "src/adapters/midnight/MidnightAdapter.sol:859"],
      "detail": "After the 3.2.8/3.2.11 fix, empty buys are a fixed point of the carry/rate computation. Each non-empty buy can still raise the market's realAssets term above floor(pre) + buyerAssets by up to about netCredit*ttm/1e18 wei, because netRate is floored to 1 wei per second. Examples: 1e13 (10M units of a 6-dec token) at ttm 3e7 gives about 3e2 wei; 1e25 (10M of an 18-dec token) gives about 3e14 wei (0.0003 tokens). That is far below gas cost, so it is not profitable for a dust seller. It is documented at line 22. Listed only to quantify the remaining bound. UNVERIFIED by execution."
    },
    {
      "title": "Funding semantics changed: a named funding adapter is always asked for the full buyerAssets, and empty callbackData now reverts",
      "class": "Informational",
      "verified": false,
      "refs": ["src/adapters/midnight/MidnightAdapter.sol:36-38", "src/adapters/midnight/MidnightAdapter.sol:578-584"],
      "detail": "At 6476e9d (lines 394-403), funding was pulled only for paidAssets - idleAssets and only when callbackData was non-empty. Now abi.decode(data, (address, bytes)) always runs, so empty data reverts. Any non-zero funding adapter is deallocated for the whole buyerAssets even when the vault holds idle assets. This is documented at line 36 and controlled by the offer's maker/allocator and sub-ratifiers (line 38), so it is a trusted-role choice. An integrator upgrading from the audited version must change its offer encoding."
    }
  ],
  "covered": [
    "Diff of onBuy, realAssets, durationAllocations, the MarketData struct and the approval paths between 6476e9d (src/adapters/MidnightAdapter.sol) and bbeda7b (src/adapters/midnight/MidnightAdapter.sol), read from raw.githubusercontent.com",
    "Status of each prior id in the group: Cantina 3.2.3, 3.2.5, 3.2.8, 3.2.11, 3.6.6; TrustSec TRST-L-5, TRST-PR-1, TRST-R-2, TRST-R-7, TRST-R-13, TRST-R-14",
    "Algebraic check of the new carried-value floor and rate floor for zero-unit and non-empty buys (monotonicity and fixed point)",
    "Per-trade minNetBuyRate / BuyAtLoss check: overflow, zero-asset and zero-unit inputs",
    "Duration cap loop: new absoluteCap > 0 requirement and the changed id preimage",
    "Transient pre-buy override: first buy, full removal, lossFactor != 0, cleared before funding/allocate, self-funding from the bought market",
    "forceApproveMax threshold behaviour for the vault and Midnight approvals"
  ],
  "notCovered": [
    "No Foundry build, test suite run or PoC execution: the clone failed because the disk was full ('No space left on device', 2.1 MB free). All conclusions are UNVERIFIED by execution.",
    "Midnight internals (take ordering, how buyerAssets/units/pendingFeeIncrease are computed, whether credit can reach the adapter outside callbacks, the pre-maturity withdraw semantics used by self-funding) were not read",
    "VaultV2 accrueInterest/firstTotalAssets behaviour within a transaction was taken from memory of its design, not re-read at this commit",
    "Sell side, gates, ratifiers and factory: outside this group"
  ]
}
