{
 "item": "bridge-instant-allowance",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge 1.10.1 (7c15a1d) with --use solar (Solar 0.3.1, reports Solidity 0.8.38/0.8.39). The machine's forge was 1.8.3; 1.10.1 was downloaded into the ignored .cache directory.",
 "verdict": "holds",
 "findings": [],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "src/bridge/ImdBridge.sol:1242-1245: _setDelay checkpoints the meter, then calls _meter(eid, capSD, capSD > instantIn[eid].capSD). A raise sets used = max(used, cap) (:1268): room 0, refilling at the new cap. A lower passes empty=false: used decays at the old cap up to now and is kept, and room = max(0, newCap - used) (:833-839). First settings: _meter forces empty=true when lastUpdated == 0 and the token has accepted (:1261-1266). If the token has not accepted, it stores only capSD and the meter starts empty at the first message after acceptance (lzReceive :581). An UNPAUSE of an unset path (paused only by the Safe's pause) starts the meter at cap 0, so the later first SET_DELAY/SET_PATH is a raise from 0 and empties it (wave-6 J4). Tests: testFuzz_C1_raiseEmptiesLowerKeeps (cap 0..30,000, any fill, applied 0-2 days after maturity: raise -> availableInstant 0; lower -> min-room formula), testFuzz_C1_firstSettingsStartEmpty (pause of an unset eid, UNPAUSE or not, SET_BRIDGE before or after, gaps 0-3 days: 0 at the act, <= cap x t / day after), testFuzz_batchLowerThenRaise (BATCH[lower, raise(, UNPAUSE)] applied by a stranger: 0 room, a forged 10 imdUSD in the same block queues). All PASS at 3,000 runs. tighten cannot raise: it clamps the cap to the standing one (:661-662)."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "Per path, within D (12 h) a forged stream mints at once at most C + C x D / 1 day = 15,000 imdUSD (the meter, P1). Forged claims queued in D are not claimable before queuedAt + D (claim :601-602). Pause or the bulk cutoff stops them, and D can be shortened only on a paused path (_check :1035). testFuzz_P2_ruleA: 16 forged messages (10 to 10,000 each) at fuzzed times in [s, s+D), starting from a full allowance; the Safe pauses and sets tighten(cutoff s) at s+D-1; every claim is tried then and after 30 days. Minted <= 15,000 in 3,000 runs (PASS). Two paths into Ethereum each have their own meter: 30,000, which equals README:784 ('can still mint up to 30,000 imdUSD into Ethereum (15,000 per spoke path)') and test/unit/LzAggregate.t.sol ETHEREUM_WITHIN_DELAY. That is not worse than written. Over face value: within this mechanism the only value routes are mint (face value), claim (face value) and send, which burns `amount` and re-mints the fee to the Treasury (supply-neutral). I found no route here that pays more than face value. Redeemer, StableModule and other money-movers that forged imdUSD could reach were not audited in this item (see notRun)."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "Code: every reader of the instant meter uses EMPTY = no room (used = cap). This covers _instantUsed :836-837 (a meter that has not started counts as used = cap), availableInstant :635-639, _meter :1268, lzReceive :577-588 and script/launch/BridgeBatch.s.sol:235, which requires availableInstant == 0 at the reopening. Every reader of the Limit uses FULL = no room (inFlight = limit, _setLimit :1271-1290). No code path mixes them, and no money moves on a misreading. Wording that does mix them, a documentation note only and not a finding (no failing test is possible): script/launch/BridgeBatch.s.sol:128 says 'its full windows after apply' to mean all room on a path's first Limit (availableIn == limit), the opposite of README:752 ('a Limit that restarts FULL has no room'). Also stale: README:1563 'UNPAUSE is refused on an open path (`PathOpen`).' The code accepts an UNPAUSE of an open path and applies it as a no-op (ImdBridge.sol:1042-1044 _check, :1226 `if (!paused[eid]) return;`), so it does not empty the allowance there. NatSpec misplaced: availableInstant's @notice sits above tripBreaker (:627 vs :635), and _instantUsed's @dev sits above _accepted (:821-823 vs :833)."
  }
 ],
 "holds": [
  {
   "property": "P1 Instant mints in any t <= C + C x t / 1 day, and <= C x t / 1 day after a cap raise, a reopening, any SET_PATH on a set path or a path's first settings (UNPAUSE of an unset path too).",
   "tried": "testFuzz_P1_instantWithinRate (3,000 runs of 24 random ops). The ops: forged messages (10 to 20,000) with several in one block; warps of 0-30 h; the Safe's tighten of the cap; SET_DELAY with cap 0-30,000; pause+UNPAUSE; SET_PATH on the set path. Each matured proposal is applied by a stranger 0-2 days after maturity, with a forged message just before and just after it in the same block. Checked: for every pair of mints, the sum <= Cmax + Cmax x dt / day; after every reset, the mints since it <= Cmax x dt / day. PASS. Also testFuzz_C1_firstSettingsStartEmpty and testFuzz_batchLowerThenRaise. Writers of instantIn: _meter (from _setDelay via first SET_PATH, SET_DELAY and tighten; SET_PATH on a set path :1149; UNPAUSE :1231) and lzReceive (meter start :581, mint :590). Readers: _instantUsed, availableInstant, tighten :661, BridgeBatch.s.sol:148,235. All are covered. Unblocks at the payload level (hold expiry, RELEASE, UNVOID) queue (:584) and are covered by the repo's test/unit/AuditW5BridgeUnblockClass.t.sol. Route the claims missed: _accepted() (:824-831) counts a failed staticcall to bridge() as accepted. testFuzz_acceptProbeUnderGasGrief runs lzReceive or UNPAUSE apply with a caller-chosen gas of 30k-600k before SET_BRIDGE; the meter never starts (lastUpdated stays 0), 3,000 runs PASS. By the 63/64 rule, a starved probe leaves too little gas for the storage writes after it. Sources: forge 1.10.1 (installed alongside the machine forge 1.8.3), `forge test --use solar --match-path test/scratch/Instant*.t.sol`, FOUNDRY_FUZZ_RUNS=3000: all 6 PASS. Mutants (in the local clone, then reverted): _setDelay passing empty=false -> P1, C1 raise, BATCH tests FAIL; UNPAUSE passing empty=false -> P1 FAIL."
  },
  {
   "property": "P2 Within D a path mints at most C(1 + D / 1 day) (15,000; 30,000 into Ethereum).",
   "tried": "testFuzz_P2_ruleA (16 forged messages at fuzzed times and sizes over D, start offset 0-3 days, from a full allowance; the Safe acts at s+D-1; claims tried at once and 30 days later): <= 15,000, PASS at 3,000 runs. D can be shortened only on a paused path (_check :1035; earlier finding C14 fixed). The 2-path sum into Ethereum comes from script/LzPaths.sol and test/unit/LzAggregate.t.sol (30,000), unchanged. Test sources:\n--- InstantAllowance.t.sol ---\n// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {W5Bridge, GatedTok} from \"../utils/W5Bridge.sol\";\n\n/// bridge-instant-allowance: P1 (instant mints within C + C t / 1 day, and C t / 1 day after a reset), C1, P2/C2 (rule A).\ncontract InstantAllowanceScratch is W5Bridge {\n    struct Ev { uint256 t; uint256 amt; }\n    Ev[] mints;\n    uint256[] resetAt;     // timestamps of a cap raise / UNPAUSE / SET_PATH on a set path\n    uint256[] resetIdx;    // mints.length at the reset\n    uint256[] capAfter;    // cap in force at each mint (for the bound, the max is used)\n    uint256 cmax;\n\n    function setUp() public {\n        _setUpW5();\n        cmax = C;\n    }\n\n    function _deliver(uint256 amt) internal {\n        uint256 b0 = rhTok.balanceOf(attacker);\n        homeEp.deliver(_forge(amt));\n        uint256 d = rhTok.balanceOf(attacker) - b0;\n        if (d != 0) mints.push(Ev(vm.getBlockTimestamp(), d));\n    }\n\n    function _noteCap() internal {\n        uint256 c = _cap();\n        if (c > cmax) cmax = c;\n    }\n\n    /// A matured proposal applied by a stranger at a chosen moment, with forgeries in the same block before and after it.\n    function _applyInFront(bytes memory p, uint256 pre, uint256 post, uint256 wait) internal {\n        _proposeRh(p);\n        _warp(48 hours + wait);\n        _deliver(pre);\n        uint256 capBefore = _cap();\n        vm.prank(makeAddr(\"stranger\"));\n        rh.applyPending();\n        uint256 capNow = _cap();\n        if (capNow > capBefore || abi.decode(p, (uint8)) != 15) { resetAt.push(vm.getBlockTimestamp()); resetIdx.push(mints.length); }\n        _noteCap();\n        _deliver(post); // same block as the reset: must queue\n    }\n\n    function testFuzz_P1_instantWithinRate(uint256[24] memory ops) public {\n        for (uint256 i; i < ops.length; ++i) {\n            uint256 o = ops[i] % 8;\n            uint256 x = ops[i] >> 8;\n            uint256 amt = 10e18 + (x % (2 * C)) / 1e12 * 1e12;\n            if (o <= 1) {\n                _deliver(amt); // several per block\n            } else if (o == 2) {\n                _warp(x % 30 hours);\n                _deliver(amt);\n            } else if (o == 3) {\n                vm.prank(safe);\n                rh.tighten(HOME, (x % (C + 1)) / 1e12 * 1e12, 0, type(uint256).max, 0);\n            } else if (o == 4) {\n                (, uint256 d,,,,) = _brk();\n                uint256 cap = (x % (3 * C)) / 1e12 * 1e12;\n                _applyInFront(_setDelayP(cap, d, B * 100, 0), amt, amt, (x >> 64) % 2 days);\n            } else if (o == 5) {\n                vm.prank(safe);\n                rh.pause(HOME);\n                _applyInFront(abi.encode(uint8(5), HOME), amt, amt, (x >> 64) % 2 days);\n            } else {\n                _applyInFront(_path(HOME, _b32(address(home)), LIM, LIM), amt, amt, (x >> 64) % 2 days);\n            }\n        }\n        // any interval [ti, tj]: <= Cmax + Cmax (tj - ti) / 1 day\n        for (uint256 i; i < mints.length; ++i) {\n            uint256 s;\n            for (uint256 j = i; j < mints.length; ++j) {\n                s += mints[j].amt;\n                assertLe(s, cmax + cmax * (mints[j].t - mints[i].t) / 1 days, \"P1: more than C + C t / day at once\");\n            }\n        }\n        // after any reset r: <= Cmax (t - r) / 1 day\n        for (uint256 r; r < resetAt.length; ++r) {\n            uint256 s;\n            for (uint256 j = resetIdx[r]; j < mints.length; ++j) {\n                s += mints[j].amt;\n                assertLe(s, cmax * (mints[j].t - resetAt[r]) / 1 days, \"P1: more than C t / day after a reset\");\n            }\n        }\n    }\n\n    /// C1: a raise (any size, any fill) empties; a lowering keeps min(room, new cap) and does not empty.\n    function testFuzz_C1_raiseEmptiesLowerKeeps(uint256 used, uint256 newCap, uint256 wait) public {\n        used = bound(used, 10, 10_000) * 1e18;\n        newCap = bound(newCap, 0, 30_000) * 1e18;\n        wait = bound(wait, 0, 2 days);\n        homeEp.deliver(_forge(used));\n        _proposeRh(_setDelayP(newCap, D, B, 0));\n        _warp(48 hours + wait);\n        uint256 room = rh.availableInstant(HOME);\n        rh.applyPending();\n        if (newCap > C) assertEq(rh.availableInstant(HOME), 0, \"raise not emptied\");\n        else assertEq(rh.availableInstant(HOME), room < newCap ? (newCap - (C - room) > newCap ? 0 : (C - room) >= newCap ? 0 : newCap - (C - room)) : (C - room >= newCap ? 0 : newCap - (C - room)));\n    }\n\n    /// First settings: a path set while the token refuses the bridge, any order of SET_BRIDGE, UNPAUSE of the unset path and\n    /// the first SET_PATH / SET_DELAY: availableInstant is 0 right after the act that lets it mint, and a message there queues.\n    function testFuzz_C1_firstSettingsStartEmpty(uint8 order, uint256 gap) public {\n        gap = bound(gap, 0, 3 days);\n        GatedTok g = new GatedTok();\n        vm.etch(address(rhTok), address(g).code); // rh's token now gates on bridge()\n        uint32 E = 30184;\n        vm.prank(safe);\n        rh.pause(E); // pause an unset path\n        if (order % 2 == 0) _govern(rh, abi.encode(uint8(5), E)); // UNPAUSE of the unset path\n        _warp(gap);\n        bool early = order % 3 == 0;\n        if (early) GatedTok(address(rhTok)).setBridge(address(rh));\n        _warp(gap);\n        _govern(rh, abi.encode(uint8(15), E, 20_000e18, uint256(12 hours), B, uint40(0), uint256(0)));\n        assertEq(rh.availableInstant(E), 0, \"first settings not empty\");\n        uint256 tAct = vm.getBlockTimestamp();\n        _warp(gap);\n        GatedTok(address(rhTok)).setBridge(address(rh));\n        // accepted before the settings: refilled since them; accepted after: not started until a message (0)\n        assertLe(rh.availableInstant(E), early ? 20_000e18 * (vm.getBlockTimestamp() - tAct) / 1 days : 0, \"first settings: room before the rate\");\n    }\n\n    /// P2 / C2 (rule A): a forged stream, the Safe acts (pause + bulk cutoff) at s + D - 1. Within D at most C (1 + D / day).\n    function testFuzz_P2_ruleA(uint256[16] memory split, uint256 start) public {\n        _warp(start % 3 days);\n        uint256 s = vm.getBlockTimestamp();\n        uint256 b0 = rhTok.balanceOf(attacker);\n        for (uint256 i; i < split.length; ++i) {\n            uint256 at = s + (split[i] % D);\n            if (at > vm.getBlockTimestamp()) vm.warp(at);\n            homeEp.deliver(_forge(10e18 + (split[i] >> 32) % (C + 1) / 1e12 * 1e12));\n        }\n        vm.warp(s + D - 1);\n        vm.startPrank(safe);\n        rh.pause(HOME);\n        rh.tighten(HOME, 0, 0, 0, uint40(s));\n        vm.stopPrank();\n        uint256 n = homeEp.packetCount();\n        for (uint256 i; i < n; ++i) _tryClaim(i);\n        _warp(30 days);\n        for (uint256 i; i < n; ++i) _tryClaim(i);\n        assertLe(rhTok.balanceOf(attacker) - b0, C + C * D / 1 days, \"rule A: > C (1 + D / day) within D\");\n    }\n}\n\n--- InstantRoutes.t.sol ---\n// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {W5Bridge, GatedTok} from \"../utils/W5Bridge.sol\";\nimport {Origin} from \"layerzero-v2/protocol/interfaces/ILayerZeroEndpointV2.sol\";\n\n/// Routes the sheet's claims miss: the token-acceptance probe (_accepted: a failed staticcall counts as accepted) under a\n/// caller-chosen gas limit, and a BATCH that lowers then raises C (or raises inside BATCH[SET_DELAY, UNPAUSE]).\ncontract InstantRoutesScratch is W5Bridge {\n    function setUp() public {\n        _setUpW5();\n    }\n\n    /// Before SET_BRIDGE, a stranger executes a packet (or applies UNPAUSE) with any gas: the meter never starts early.\n    function testFuzz_acceptProbeUnderGasGrief(uint256 g, bool viaUnpause) public {\n        g = bound(g, 30_000, 600_000);\n        GatedTok t = new GatedTok();\n        vm.etch(address(rhTok), address(t).code); // token refuses the bridge (bridge() == 0)\n        // restart: a fresh path on a new eid, never started\n        uint32 E = 30184;\n        vm.prank(safe);\n        rh.pause(E);\n        _govern(rh, abi.encode(uint8(15), E, C, D, B, uint40(0), uint256(0)));\n        (, uint64 lu,) = rh.instantIn(E);\n        assertEq(lu, 0, \"meter started before acceptance\");\n        if (viaUnpause) {\n            vm.prank(safe);\n            rh.propose(abi.encode(uint8(5), E));\n            _warp(48 hours);\n            (bool ok,) = address(rh).call{gas: g}(abi.encodeWithSignature(\"applyPending()\"));\n            ok;\n        } else {\n            // the home path (set, open) re-created unstarted is not possible; probe lzReceive on HOME after wiping its meter slot\n            bytes32 slot = _meterSlot(HOME);\n            vm.store(address(rh), slot, bytes32(uint256(C / 1e12) << 192)); // used 0, lastUpdated 0, cap C\n            uint256 i = _forge(20e18);\n            (,, bytes32 sender, uint64 n, bytes32 guid, bytes memory m,,) = homeEp.packets(i);\n            vm.prank(address(rhEp));\n            (bool ok,) = address(rh).call{gas: g}(\n                abi.encodeWithSignature(\"lzReceive((uint32,bytes32,uint64),bytes32,bytes,address,bytes)\", Origin(HOME, sender, n), guid, m, address(0), bytes(\"\"))\n            );\n            ok;\n            (, lu,) = rh.instantIn(HOME);\n            assertEq(lu, 0, \"lzReceive started the meter before acceptance\");\n        }\n        (, lu,) = rh.instantIn(E);\n        assertEq(lu, 0, \"UNPAUSE started the meter before acceptance\");\n    }\n\n    function _meterSlot(uint32 eid) internal view returns (bytes32) {\n        // find instantIn's mapping slot by probing: the slot whose value matches instantIn(HOME)\n        (uint128 u, uint64 l, uint64 c) = rh.instantIn(eid);\n        bytes32 want = bytes32(uint256(u) | uint256(l) << 128 | uint256(c) << 192);\n        for (uint256 s; s < 80; ++s) {\n            bytes32 k = keccak256(abi.encode(uint256(eid), s));\n            if (vm.load(address(rh), k) == want) return k;\n        }\n        revert(\"slot not found\");\n    }\n\n    /// BATCH[SET_DELAY lower, SET_DELAY raise back] and BATCH[SET_DELAY raise, UNPAUSE]: applied by anyone, the allowance is empty.\n    function testFuzz_batchLowerThenRaise(uint256 lo, uint256 hi, uint256 wait, bool withPause) public {\n        lo = bound(lo, 0, 10_000) * 1e18;\n        hi = bound(hi, 10_001, 100_000) * 1e18;\n        wait = bound(wait, 0, 14 days);\n        if (withPause) {\n            vm.prank(safe);\n            rh.pause(HOME);\n        }\n        bytes[] memory items = new bytes[](withPause ? 3 : 2);\n        items[0] = _setDelayP(lo, D, B, 0);\n        items[1] = _setDelayP(hi, D, B, 0);\n        if (withPause) items[2] = abi.encode(uint8(5), HOME);\n        _proposeRh(_batch(items));\n        _warp(48 hours + wait);\n        vm.prank(attacker);\n        rh.applyPending();\n        assertEq(rh.availableInstant(HOME), 0, \"a raise inside a BATCH left room\");\n        uint256 b0 = rhTok.balanceOf(attacker);\n        homeEp.deliver(_forge(10e18));\n        assertEq(rhTok.balanceOf(attacker), b0, \"minted at once in the raise's block\");\n    }\n}\n"
  }
 ],
 "notRun": [
  "[fork]/[data]: none in this sheet; NOT RUN.",
  "Value routes for forged imdUSD outside the bridge (Redeemer, StableModule, Morpho lender) were not tested here: they belong to other items.",
  "Gas-grief test: whether the bridge() probe actually ran out of gas at some gas values was not instrumented. The property (the meter does not start before acceptance) was checked directly."
 ]
}