{
 "item": "core-backstop-sleeve",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge 1.8.3 (no --use solar; solc 0.8.38 not installed). Tests ran with forge 1.8.3 + solc 0.8.30 (via_ir, cancun, isolate) on copies of src/backstop/Backstop.sol, src/backstop/IBackstop.sol, src/TransientReentrancyGuard.sol, src/Treasury.sol, src/Governed.sol at ff1c192 with only the pragma changed to 0.8.30; Ledger/Liquidator/PricerOracle replaced by mocks (the Ledger mock's cover/retire copy Ledger.sol:354-369; the Liquidator mock runs openBid, burn+collateral in, closeBid in one call like Liquidator.standingBid:158-170).",
 "verdict": "findings",
 "findings": [
  {
   "id": "CBS-1",
   "severity": "low",
   "title": "Treasury's timelocked payout spends the bad-debt cover and the notes' face: no guard, applied by anyone at a moment of their choosing",
   "property": "P2 / C5 (cover keeps first call on the Treasury's imdUSD; notes' face is kept)",
   "claim": "C5 (one invariant over every route that spends Treasury imdUSD) and the Treasury NatSpec 'nothing here is earmarked for notes, so bad-debt cover keeps first call on the balance' (Treasury.sol:44-45)",
   "files": [
    "src/Treasury.sol:187",
    "src/Treasury.sol:192",
    "src/Treasury.sol:119",
    "src/Treasury.sol:170",
    "src/Governed.sol:69",
    "src/Governed.sol:14"
   ],
   "sequence": "1) Safe proposes the routine payout (imdUSD, to, X) with no bad debt booked. 2) 48h pass (the proposal stays applicable 14 days). 3) A liquidation books core bad debt (its in-tx cover found too little, or the builder orders the apply in front of it in the same block). 4) Any account calls Treasury.applyPending(): _apply sends X imdUSD (Treasury.sol:192) though totalBadDebt != 0 and though it leaves the balance below FeeNotes.outstanding(). retireReserve and payNote both revert BadDebtOutstanding in the same state; coverBadDebt then finds nothing and payNote reverts for lack of balance.",
   "expected": "applyPending of an imdUSD payout reverts while ledger.totalBadDebt() != 0 (and keeps outstanding notes' face), like retireReserve (:120-123) and payNote (:172)",
   "actual": "payout succeeds: [FAIL: next call did not revert as expected; counterexample: args=[315673, 658232834895533319519108640]] — the whole imdUSD balance left with bad debt booked and all of it owed as notes' face",
   "reachableAtDeploy": "yes: from the first imdUSD payout proposal after Treasury.wire; not a new Safe power but a stranger-chosen moment (14-day window) that bypasses the guards the other two routes enforce; the Safe can only cancel ahead of it",
   "fix": "In Treasury._apply, when asset == address(token): revert BadDebtOutstanding if ledger.totalBadDebt() != 0, and cap/revert so the balance after stays >= INoteBook(notes).outstanding(). Better: route retireReserve, payNote and the imdUSD payout through one internal _spend(amount, isNote) that enforces both (see C5).",
   "test": {
    "source": "contract TreasurySpendTest is Test {\n    Tok usd;\n    LedgerT ledger;\n    Notes notes;\n    Treasury t;\n    address gov = address(0x5AFE);\n    address stranger = address(0xBAD);\n\n    function setUp() public {\n        vm.warp(1_800_000_000);\n        usd = new Tok(\"imdUSD\");\n        ledger = new LedgerT(usd);\n        notes = new Notes();\n        t = new Treasury(gov, IERC20(address(usd)), address(this));\n        t.wire(ICoverable(address(ledger)));\n        t.wireNotes(address(notes));\n    }\n    /// C5 / P2 route missed by the sheet: the timelocked payout (_apply, Treasury.sol:187-193) spends the same imdUSD with\n    /// neither guard. A matured payout applied by ANY account after a liquidation booked bad debt spends the cover\n    /// (and the notes' face), where retireReserve and payNote would both revert BadDebtOutstanding.\n    function testFuzz_C5_payoutSpendsCoverWhileBadDebtBooked(uint96 bal, uint96 bad) public {\n        bal = uint96(bound(bal, 2, type(uint96).max));\n        bad = uint96(bound(bad, 1, bal));\n        usd.mint(address(t), bal);\n        notes.set(bal); // all of it is notes' face\n        vm.prank(gov);\n        t.propose(abi.encode(address(usd), gov, uint256(bal))); // Safe's routine payout, no bad debt at propose\n        vm.warp(block.timestamp + 48 hours);\n        ledger.book(bad); // a liquidation books bad debt (its own-tx cover found nothing or was starved)\n        // property: Treasury imdUSD never leaves (other than to cover) while core bad debt is booked\n        vm.prank(stranger);\n        vm.expectRevert(Treasury.BadDebtOutstanding.selector);\n        t.applyPending();\n    }\n}\n// mocks (test/scratch/p/test/Mocks.sol)\ncontract Tok is ERC20 {\n    mapping(address => bool) public blocked;\n    constructor(string memory n) ERC20(n, n) {}\n    function mint(address to, uint256 a) external { _mint(to, a); }\n    function burn(address from, uint256 a) external { _burn(from, a); }\n    function setBlocked(address a, bool b) external { blocked[a] = b; }\n    function _update(address f, address t, uint256 v) internal override {\n        require(!blocked[f] && !blocked[t], \"blocked\");\n        super._update(f, t, v);\n    }\n}\n/// Core Ledger stand-in for the Treasury: cover/retire burn the caller's imdUSD exactly like Ledger.sol:354-369.\ncontract LedgerT {\n    Tok public token;\n    uint256 public totalBadDebt;\n    uint128 public bad;\n    uint256 public reserveBacked;\n    constructor(Tok t) { token = t; }\n    function book(uint128 b) external { bad += b; totalBadDebt += b; }\n    function setReserve(uint256 r) external { reserveBacked = r; }\n    function ilkState(bytes32) external view returns (uint128, uint128, uint256, uint64, uint128) { return (0, 0, 0, 0, bad); }\n    function cover(bytes32, uint256 amount) external {\n        if (amount > bad) amount = bad;\n        bad -= uint128(amount); totalBadDebt -= amount; token.burn(msg.sender, amount);\n    }\n    function retire(uint256 amount) external {\n        if (amount > reserveBacked) amount = reserveBacked;\n        reserveBacked -= amount; token.burn(msg.sender, amount);\n    }\n}\ncontract Notes {\n    uint256 public outstanding;\n    function set(uint256 o) external { outstanding = o; }\n}",
    "ran": "forge test --match-path test/TreasurySpend.t.sol (forge 1.8.3, solc 0.8.30 copy; 512 fuzz runs)",
    "result": "testFuzz_C5_payoutSpendsCoverWhileBadDebtBooked: FAIL: next call did not revert as expected; counterexample args=[315673, 658232834895533319519108640]. Same suite: testFuzz_P2_retireReserveBound PASS (512 runs), testFuzz_C2_strangerCannotRetire PASS (512 runs)."
   }
  },
  {
   "id": "CBS-2",
   "severity": "info",
   "title": "Dust rule reads a stale lastHigh: a re-priced holding whose feed dies counts as 0 and a converting bucket takes part of it",
   "property": "P1 (no conversion moves nav in its own favour)",
   "claim": "Backstop NatSpec: 'a holding worth under DUST_VALUE at its last good high price counts as 0' (Backstop.sol:24); lastHigh is refreshed only in closeBid (:493) and in _convert after a bucket has matured (:575-580), so it can be arbitrarily old when the feed dies",
   "files": [
    "src/backstop/Backstop.sol:301",
    "src/backstop/Backstop.sol:575",
    "src/backstop/Backstop.sol:493"
   ],
   "sequence": "Holder H 1000 converted; a bid buys 9 WETH-like at 1 (lastHigh=1, 9 < 10 dust). Token re-prices to 3 (27 held) while no bucket matures; A deposits 1000; feed stalls (ok=false, high=0); A's bucket converts at nav that counts the 27 as 0; A exits in kind.",
   "expected": "A leaves with <= 1000 of value at market",
   "actual": "A leaves with 999.99 imdUSD + 4.52 tokens = 1013.56 at price 3 (13.56 taken from H). Bound: about DUST_VALUE x (price now / lastHigh) x the converter's share; small for blue tokens.",
   "reachableAtDeploy": "yes (any sleeve holding)",
   "fix": "Refresh lastHigh on every good read reached by a conversion step (e.g. in _clearRetired's loop or before _convert's early return), or apply the dust rule only to a lastHigh younger than a bound.",
   "test": {
    "source": "/// Liquidator stand-in: open, the Ledger's burn + join.exit, close — in ONE call like Liquidator.standingBid (isolate=true).\ncontract Liq {\n    function bid(Backstop s, Tok usd, Tok c, bytes32 ilk, uint256 spent, uint256 recv) external returns (uint256) {\n        s.openBid(ilk);\n        if (s.bidRoom(ilk) < spent) { s.closeBid(ilk, 0); return 0; }\n        usd.burn(address(s), spent);\n        c.mint(address(s), recv);\n        s.closeBid(ilk, spent);\n        return spent;\n    }\n}\n\ncontract Base is Test {\n    Tok usd; Tok weth; Orc orc; Backstop s; Liq liq;\n    bytes32 constant ILK = \"ETH\";\n    function _setUp() internal {\n        vm.warp(1_800_000_000);\n        usd = new Tok(\"imdUSD\"); weth = new Tok(\"WETH\"); orc = new Orc(); liq = new Liq();\n        s = new Backstop(IERC20(address(usd)), ILedgerCover(address(0x1ed)), IBackstopOracle(address(orc)), \"b\", \"b\");\n        s.wire(address(liq), address(this));\n        s.addIlk(ILK, address(weth));\n        s.seal();\n        orc.set(address(weth), 2000e18, 2010e18, true, block.timestamp);\n    }\n    function _dep(address who, uint256 a) internal {\n        usd.mint(who, a);\n        vm.startPrank(who); usd.approve(address(s), a); s.deposit(a, who); vm.stopPrank();\n    }\n}\n// in contract SleeveTest is Base { function setUp() public { _setUp(); } ...\n    /// Dust rule with a stale lastHigh (Backstop.sol:301): a holding bought as \"dust\" that the market has since\n    /// re-priced is valued 0 when its feed dies; the converting bucket takes part of it from the holders.\n    function test_dustRuleStaleLastHigh() public {\n        _dep(address(0x11), 1000e18); vm.warp(1_800_000_000 + 2 days); s.realize(address(0x11));\n        orc.set(address(weth), 1e18, 1e18, true, 1_800_000_000 + 2 days);\n        liq.bid(s, usd, weth, ILK, 9e18, 9e18); // 9 tokens at 1: 9 < DUST_VALUE, lastHigh = 1\n        // the market re-prices the token 3x (27 imdUSD held); no bucket matures, so nothing refreshes lastHigh\n        orc.set(address(weth), 3e18, 3e18, true, 1_800_000_000 + 2 days + 1);\n        _dep(address(0xA), 1000e18);\n        orc.set(address(weth), 0, 0, false, 1_800_000_000 + 2 days + 1); // then its feed stalls\n        vm.warp(1_800_000_000 + 4 days);\n        s.realize(address(0xA));\n        uint256 sh = s.balanceOf(address(0xA));\n        vm.prank(address(0xA)); s.requestWithdraw(sh);\n        vm.warp(1_800_000_000 + 11 days);\n        vm.prank(address(0xA)); s.withdraw(address(0xA));\n        uint256 aw = weth.balanceOf(address(0xA));\n        emit log_named_uint(\"A weth out (1e18)\", aw);\n        emit log_named_uint(\"A usd out\", usd.balanceOf(address(0xA)));\n        // property P1: a conversion never moves value to the converter: A paid 1000 and must not leave with more\n        assertLe(usd.balanceOf(address(0xA)) + aw * 3, 1000e18, \"converter took value from the holders\");\n    }\n// Orc: settable (low,high,ok,pricedAt) per token; Tok: mintable/burnable ERC20 (Mocks.sol)",
    "ran": "forge test --match-test test_dustRuleStaleLastHigh -vv (forge 1.8.3, solc 0.8.30 copy)",
    "result": "FAIL: converter took value from the holders: 1013561024610748367652 > 1000000000000000000000; logs: A weth out 4520341536916122551, A usd out 999999999999999999999"
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Direct oracle reads in src/backstop/Backstop.sol: retire priceOf :239; nav priceOf :298; closeBid priceOf :491; _clearRetired priceOf :560; _convert priceOf :578; _stillRetired pricedAt :551. No other function calls the oracle; bidRoomNow (:280) reads only through nav. By grep of 'oracle.' in the file."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "retireReserve is onlyGovernor (Treasury.sol:119; testFuzz_C2_strangerCannotRetire PASS 512 runs) and burns exactly min(amount, bal-outstanding, reserveBacked), reverting with bad debt booked (testFuzz_P2_retireReserveBound PASS 512 runs over bal/outstanding/reserveBacked/amount/bad). A builder can place the Safe's signed retireReserve ahead of a same-block liquidation, but that equals the Safe's tx landing a block earlier: no stranger-chosen amount and no notes' face burned. coverBadDebt may burn below outstanding by design (Treasury.sol:42-45). Backstop.retire(token,true) refuses whenever high != 0 (Backstop.sol:239-240; testFuzz_P3_unpricedFailsClosed asserts StillPriced for every fuzzed high != 0 with ok=false, 512 runs). The stated cover protection is however bypassed by the payout route: CBS-1."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "testFuzz_C3_timedDepositGetsOnlyItsShare PASS (512 runs): A deposits so its bucket matures exactly at the bid and converts inside openBid (:455) before the bid; over fuzzed holder size, deposit, spend and 0-20% discount, A's exit value <= a + gain*a/(h+a) + rounding. Deposits cannot be JIT (bucket converts day+2, :575); exits wait 7 days. Exits of matured requests before a loss are README:1192 (known)."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "test_C4_blocklistOnlyThatLeg PASS: with the token blocklisting the sleeve the bid reverts whole (Liquidator.bite's try/catch Liquidator.sol:138-146 then runs the caller path), the exit pays imdUSD in full and credits unsent (Backstop.sol:430-432), claim succeeds after unblock. Deposits/conversions read only prices, not token transfers."
  },
  {
   "id": "C5",
   "result": "refuted",
   "evidence": "Invariant: while ledger.totalBadDebt() != 0, Treasury imdUSD leaves only through Ledger.cover; otherwise any non-cover outflow leaves balance >= notes.outstanding() (payNote may draw that face). retireReserve and payNote satisfy it; the timelocked payout Treasury._apply (:187-193, asset == token) does not (CBS-1). Smallest design: one internal _spend(amount, isNote) gate used by retireReserve, payNote and _apply's imdUSD branch."
  }
 ],
 "holds": [
  {
   "property": "P1",
   "tried": "invariant_P1_noActionMovesPpsInItsFavour PASS (64 runs x 60 calls, 3 actors: deposit/cancel/realize/requestWithdraw/withdraw/warp up to 3 days/donate+sync/profitable bids; checks nav/supply of the others never falls across a deposit, cancel, realize, request or withdraw beyond 1 wei/share). First-depositor/donation: DEAD's 1e6 shares (:591) and the 7-day stream (:504-520) make inflation unprofitable (donor loses half to DEAD); unvested is excluded from conversion value and from exits, which only favours remaining holders. Exception CBS-2 (info). Known: README:1153, :1195, :1267."
  },
  {
   "property": "P2",
   "tried": "testFuzz_P2_retireReserveBound (512 runs) and Ledger.retire's cap (Ledger.sol:365); writers of reserveBacked: LedgerBase.sol:400 (+), Ledger.sol:366 (-). Ledger.retire is also callable by anyone with their own imdUSD (burns the caller's money only). Holds for retireReserve; the payout route is CBS-1."
  },
  {
   "property": "P3",
   "tried": "testFuzz_P3_unpricedFailsClosed (512 runs, any low/high with ok=false): nav ok=false, buckets wait, a bid reverts BadPrice. testFuzz_P3_P4_bidBelowLowReverts (512 runs): received*low < spent always reverts. PricerOracle returns ok=false for a refused/reverting quote (PricerOracle.sol:44-54). Note: IBackstop.pricedAt is documented as the oldest leg's update (IBackstop.sol) but PricerOracle.pricedAt reads the median only (:58-66); since Pricer.priced needs only the median (Pricer.sol:55-59) this changes nothing. Retire at a stale read is README:1205 (known)."
  },
  {
   "property": "P4",
   "tried": "testFuzz_P3_P4_bidBelowLowReverts: a refused close leaves tracked and coll unchanged; the whole open/settle/close runs as one self-call inside try (Liquidator.sol:133-146,158-170), so any revert undoes it; transient bid slots clear at closeBid (:473-481) and at transaction end; while open, deposit/cancel/realize/request/withdraw/claim are notInBid and sync is a no-op (:507). The room-short path keeps only openBid's conversion (the README:1267 moment)."
  }
 ],
 "notRun": [
  "forge 1.9 --use solar with solc 0.8.38: NOT RUN (forge 1.8.3 installed, no solar; solc 0.8.38 not installed). Tests ran on pragma-0.8.30 copies with mocks as stated in 'forge'.",
  "Full-system tests with the real Ledger/LedgerSettlement/Liquidator/PricerOracle: NOT RUN (mocks used).",
  "No [fork]/[data] claims in this sheet: none run."
 ]
}