{
 "item": "bridge-limiter",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge 1.8.3 (older than 1.9; --use solar did not resolve a compiler on this install). Tests compiled with the installed solc 0.8.38 per foundry.toml (via_ir), only --match-path test/scratch/BridgeLimiter.t.sol, harness test/unit/ThreePaths.t.sol (ThreeChains: Ethereum bridge with two live inbound paths 4663 and Base on MockLzEndpoint, real LzPaths launch batches).",
 "verdict": "holds",
 "findings": [],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Every Limit write in src/: outbound/inbound are written only by _consume (src/bridge/ImdBridge.sol:890-895, called from send src/bridge/ImdBridge.sol:518 and lzReceive src/bridge/ImdBridge.sol:572) and _setLimit (src/bridge/ImdBridge.sol:1282-1290, called only by SET_PATH apply src/bridge/ImdBridge.sol:1137-1138). script/ writes none (script/LzPaths.sol only declares LIMIT 169, LIMIT_WINDOW 170, BREAKER_SPAN 172, QUEUE_BREAKER 175). Semantics at src/bridge/ImdBridge.sol:1285: lower scales inFlight by new/old, raise (also from 0, since l.limit==0 != limit) sets inFlight=limit (full), same limit keeps the decayed amount (new window), close (limit 0) keeps it unscaled and frozen (decay limit*dt/window = 0), first set (window 0) keeps 0 = all room. Checked by an exact model assertion at every stranger-applied SET_PATH in testFuzz_P1P2 (256 runs, ~6 SET_PATHs/run) and test_C1_closeThenReopen (PASS). DuplicateInBatch src/bridge/ImdBridge.sol:906-913: test_C1_duplicateInBatch PASS. PathOpen for an inbound lower on an open path src/bridge/ImdBridge.sol:936, checked at propose and again at applyPending (src/Governed.sol:46,77): test_C1_lowerOnOpenPathRefused PASS."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "Known README:749-751 as written: a raise sets inFlight = new limit (src/bridge/ImdBridge.sol:1285), availableIn = 0 right after (test_C1_closeThenReopen asserts 0 after a raise from 0); it refills at the new rate. Not worse than written: the raise never lowers available below 0 nor extends past one new window."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "Fuzzed: testFuzz_P1P2_inLimitNeverTripsAcrossSetPath (256 runs) drives greedy and half-greedy in-limit traffic on BOTH live paths (RH and BASE into Ethereum) with the instant cap C=0 (Safe tighten, so every transfer queues: strongest case), several transfers per block, at UTC-day edges (00:00 and 23:59:59), across random SET_PATHs whose pairs keep L x (W + 4 d) <= B x W (W in [1 h, 7 d]); lowers go pause -> SET_PATH -> UNPAUSE, all applied by a stranger at eta or later, with transfers in front of and behind the apply in its block. testFuzz_countLowers (256 runs) asserts every run contained at least one lower. testFuzz_P1_sameLimitNewWindow (256 runs). All PASS; no trip."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "One invariant: for every path p and every UTC day d, breakerSum_p(d-3..d) <= sum of amounts _consume(inbound[p]) admitted in [00:00 of d-3, now] <= B_p. The first inequality holds structurally: the only route that adds to breakerBuckets is _queue (src/bridge/ImdBridge.sol:845-862), reached only from lzReceive after _consume (src/bridge/ImdBridge.sol:572), including the hold/unvoided routes; instant mints only reduce it. The second holds iff the limiter admits at most B in any span < 4 days: L + max rate x span for a raise / same-limit window change / first set (each bounded by one of the two pair rules), and for a lower or close because PathOpen (src/bridge/ImdBridge.sol:936) + UnpauseWithChange (src/bridge/ImdBridge.sol:1046) + the single proposal slot (src/Governed.sol:45, TIMELOCK 48 h) keep the path paused >= 96 h, so old and new traffic never share a 4-UTC-day window (span < 96 h). Smallest replacement design: enforce the rule L x (1 + 4 days / W) <= breaker in _check for SET_PATH, SET_DELAY and tighten (README:1100 keeps it off-chain), so the invariant no longer depends on the 96 h pause coupling."
  },
  {
   "id": "P1-bound-text",
   "result": "refuted",
   "evidence": "The sheet's P1 and the _setLimit comment (src/bridge/ImdBridge.sol:1276-1281: 'under B whenever both pairs keep L x (1 + 4 days / W) <= B') state a bound L_old + max(L_old/W_old, L_new/W_new) x 4 days that is NOT <= B for a lower that raises the rate: 63,636/7 d -> 1,030/1 h both keep the rule yet 63,636 + 1,030 x 96 = 162,516 > 100,000 (test_statedBoundForALowerExceedsB PASS). No trip results only because a lower needs the >= 96 h pause (above): test_P1_tightestLowerDoesNotTrip spends the full 63,636 at 00:00 UTC, pauses in that block, lowers and unpauses at the earliest etas, then sends greedily every 15 min for 5 days: no trip (PASS). Info only, no failing test: the guarantee rests on the pause coupling, not on the stated bound."
  }
 ],
 "holds": [
  {
   "property": "P1 no in-limit traffic trips a path, across any rule-keeping SET_PATH applied by anyone at any moment",
   "tried": "testFuzz_P1P2_inLimitNeverTripsAcrossSetPath (256 runs), testFuzz_countLowers (256 runs, each with >= 1 lower), testFuzz_P1_sameLimitNewWindow (256 runs), test_P1_tightestLowerDoesNotTrip (worst lower: full old window spent at 00:00 UTC, earliest etas), test_lowerCannotReopenInItsOwnBatch (BATCH[SET_PATH lower, UNPAUSE] refused: UnpauseWithChange). C = 0 throughout (all traffic queues). Same-block transfers before and after a stranger's applyPending, consecutive blocks, 00:00 and 23:59:59 UTC edges. Sanity: test_sanity_driverTripsWhenRuleBroken shows the same greedy driver trips at a 90,000 breaker, so the passes are not vacuous. Writers/readers reviewed: breakerBuckets written at src/bridge/ImdBridge.sol:859 (_queue), 1144 (first SET_PATH), 1216 (RESET_BREAKER), 1222 (SET_DELAY), read by _window 866-875; tripped written by _trip 877-881 (from _queue 861, _setDelay 1250, tripBreaker 631) and RESET_BREAKER 1215; bucket width 63 bits vs MAX_LIMIT 2.5e14 SD/day x 25 (W 1 h): no carry. _window clears exactly days (b.day, day]; trip compares strictly (> B). All PASS."
  },
  {
   "property": "P2 holds on two live paths at once",
   "tried": "Every Limit, Breaker, breakerBuckets and Meter is keyed by eid; the only shared governance state is the one proposal slot (src/Governed.sol:45), which only lengthens pauses. testFuzz_P1P2 runs greedy traffic on RH and BASE simultaneously, with one path paused for a lower while the other keeps sending and changing: no trip on either (256 runs PASS)."
  }
 ],
 "notRun": [
  "No [fork]/[data] claims in this sheet: none RUN. Real EndpointV2 not used (MockLzEndpoint harness). test/unit/LzPaths.t.sol (the LzPaths rule) not rerun; the rule was read at script/LzPaths.sol:161-175.",
  "Earlier findings (jobs 74838ad4..., dc9419af..., 439d66ba...) fetched only as summaries from api.imd.fun; they target earlier commits and were not re-verified here."
 ],
 "tests": {
  "path": "test/scratch/BridgeLimiter.t.sol",
  "command": "forge test --offline --match-path test/scratch/BridgeLimiter.t.sol",
  "ran": true,
  "result": "10 passed, 0 failed (fuzz runs 256 each): testFuzz_P1P2_inLimitNeverTripsAcrossSetPath, testFuzz_P1_sameLimitNewWindow, testFuzz_countLowers, test_C1_closeThenReopen, test_C1_duplicateInBatch, test_C1_lowerOnOpenPathRefused, test_P1_tightestLowerDoesNotTrip, test_lowerCannotReopenInItsOwnBatch, test_statedBoundForALowerExceedsB, test_sanity_driverTripsWhenRuleBroken",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {ThreeChains} from \"../unit/ThreePaths.t.sol\";\nimport {ImdBridge} from \"../../src/bridge/ImdBridge.sol\";\nimport {LzPaths} from \"../../script/LzPaths.sol\";\n\ncontract BridgeLimiterScratch is ThreeChains {\n    uint256 constant B = 100_000e18;\n    uint256 constant SPAN = 4 days;\n    address stranger = makeAddr(\"stranger\");\n    uint256 public sets;\n    uint256 public lowers;\n\n    function setUp() public {\n        _setUpThree();\n        _openAll();\n    }\n\n    function _now() internal view returns (uint256) {\n        return vm.getBlockTimestamp();\n    }\n\n    function _eid(uint256 i) internal pure returns (uint32) {\n        return i == 1 ? RH : BS;\n    }\n\n    function _tripped(uint32 eid) internal view returns (bool t) {\n        (,, t,,,,) = br[0].breakers(eid);\n    }\n\n    function _sp(uint256 i, uint256 L, uint256 W) internal view returns (bytes memory) {\n        return abi.encode(uint8(1), _eid(i), _b32(address(br[i])), LzPaths.LIMIT, LzPaths.LIMIT_WINDOW, L, W, bytes(\"\"));\n    }\n\n    /// a pair (L, W) inside the rule L x (W + 4 days) <= B x W, W in [1 h, 7 d]\n    function _pair(uint256 r) internal pure returns (uint256 L, uint256 W) {\n        W = 1 hours + (r % (7 days - 1 hours + 1));\n        uint256 lmax = B * W / (W + SPAN);\n        L = 1_000e18 + ((r >> 64) % (lmax - 1_000e18 + 1));\n        L = L / 1e18 * 1e18;\n        assertLe(L * (W + SPAN), B * W);\n    }\n\n    function _traffic(uint256 r) internal {\n        for (uint256 i = 1; i < 3; ++i) {\n            uint32 eid = _eid(i);\n            if (!br[0].paused(eid)) {\n                uint256 a = br[0].availableIn(eid) / 1e12 * 1e12;\n                if ((r >> (8 * i)) % 4 == 0) a = a / 2 / 1e12 * 1e12;\n                if (a >= 10e18) {\n                    (, bool ok) = _inbound(i, alice, a);\n                    assertTrue(ok, \"in-limit transfer refused\");\n                }\n            }\n            assertFalse(_tripped(eid), \"in-limit traffic tripped the path\");\n        }\n    }\n\n    function _step(uint256 r) internal {\n        _traffic(r);\n        uint256 m = r % 5;\n        uint256 t = _now();\n        if (m == 1) vm.warp(t + 1);\n        else if (m == 2) vm.warp(t + bound(r >> 100, 1, 12 hours));\n        else if (m == 3) vm.warp((t / 1 days + 1) * 1 days); // UTC day edge\n        else if (m == 4) vm.warp((t / 1 days + 1) * 1 days - 1);\n        // m == 0: the next transfers land in the same block\n        vm.roll(block.number + 1);\n    }\n\n    /// the expected inbound Limit after a SET_PATH, per claim C1\n    function _expect(uint32 eid, uint256 L) internal view returns (uint256 f) {\n        (uint128 l, uint32 w, uint128 inF, uint64 u) = br[0].inbound(eid);\n        uint256 dec = w == 0 ? 0 : uint256(l) * (_now() - u) / w;\n        f = inF > dec ? inF - dec : 0;\n        if (L != 0 && L != l && w != 0) f = L < l ? f * L / l : L;\n    }\n\n    function _gov(bytes memory p, uint256 r, uint32 eid, uint256 L, bool isPath) internal {\n        vm.prank(safe);\n        br[0].propose(p);\n        uint256 eta = _now() + 48 hours;\n        uint256 k;\n        while (_now() < eta) _step(uint256(keccak256(abi.encode(r, k++))));\n        uint256 extra = r % 3;\n        for (uint256 j; j < extra; ++j) _step(uint256(keccak256(abi.encode(r, \"x\", j))));\n        if (r & 1 == 0) _traffic(r >> 3); // transfers in the apply's block, in front\n        uint256 want = isPath ? _expect(eid, L) : 0;\n        vm.prank(stranger); // a matured proposal applied by anyone\n        br[0].applyPending();\n        if (isPath) {\n            (uint128 l,, uint128 f, uint64 u) = br[0].inbound(eid);\n            assertEq(l, L);\n            assertEq(f, want, \"C1 limit semantics\");\n            assertEq(u, _now());\n        }\n        _traffic(r >> 5); // and behind it\n    }\n\n    function _setPathRandom(uint256 i, uint256 r) internal {\n        (uint256 L, uint256 W) = _pair(r);\n        (uint128 cur,,,) = br[0].inbound(_eid(i));\n        ++sets;\n        if (L < cur) {\n            ++lowers;\n            vm.prank(safe);\n            br[0].pause(_eid(i));\n            _gov(_sp(i, L, W), r, _eid(i), L, true);\n            _gov(abi.encode(uint8(5), _eid(i)), r >> 1, 0, 0, false);\n        } else {\n            _gov(_sp(i, L, W), r, _eid(i), L, true);\n        }\n    }\n\n    /// P1 + P2: greedy (and half-greedy) in-limit traffic on BOTH live paths, several per block, at day edges, with C = 0 (the\n    /// Safe's tighten: every transfer queues), across random SET_PATHs (raises, lowers via pause, same limit new window) whose\n    /// old and new pairs keep the rule, each applied by a stranger at a random moment after its eta. Never trips.\n    function testFuzz_P1P2_inLimitNeverTripsAcrossSetPath(uint256 seed) public {\n        vm.startPrank(safe);\n        br[0].tighten(RH, 0, 0, type(uint256).max, 0);\n        br[0].tighten(BS, 0, 0, type(uint256).max, 0);\n        vm.stopPrank();\n        _setPathRandom(1, uint256(keccak256(abi.encode(seed, \"a\"))));\n        _setPathRandom(2, uint256(keccak256(abi.encode(seed, \"b\"))));\n        for (uint256 k; k < 24; ++k) {\n            uint256 r = uint256(keccak256(abi.encode(seed, k)));\n            if (r % 5 == 0) _setPathRandom(1 + (r >> 200) % 2, r >> 8);\n            else _step(r);\n        }\n    }\n\n    /// same-limit, new-window route only (no pause needed): L fixed at 50,000, W in [4 d, 7 d]\n    function testFuzz_P1_sameLimitNewWindow(uint256 seed) public {\n        vm.prank(safe);\n        br[0].tighten(BS, 0, 0, type(uint256).max, 0);\n        for (uint256 k; k < 20; ++k) {\n            uint256 r = uint256(keccak256(abi.encode(seed, k)));\n            if (r % 4 == 0) {\n                uint256 W = 4 days + (r >> 9) % (3 days + 1);\n                _gov(_sp(2, 50_000e18, W), r >> 8, BS, 50_000e18, true);\n            } else {\n                _step(r);\n            }\n        }\n    }\n\n    /// The tightest lower: spend all of L_old = 63,636 (W 7 d) at 00:00 UTC, pause in that block, lower to 1,030 / 1 h at once\n    /// (eta), UNPAUSE at its eta: 96 h after the spend, outside the 4 UTC-day window. Then greedy traffic for 5 days.\n    function test_P1_tightestLowerDoesNotTrip() public {\n        vm.prank(safe);\n        br[0].tighten(BS, 0, 0, type(uint256).max, 0);\n        _gov(_sp(2, 63_636e18, 7 days), 1, BS, 63_636e18, true); // a raise (restarts full)\n        vm.warp((_now() / 1 days + 8) * 1 days); // refilled, at 00:00 UTC\n        uint256 a = br[0].availableIn(BS) / 1e12 * 1e12;\n        assertEq(a, 63_636e18);\n        (, bool ok) = _inbound(2, alice, a);\n        assertTrue(ok);\n        vm.startPrank(safe);\n        br[0].pause(BS);\n        br[0].propose(_sp(2, 1_030e18, 1 hours));\n        vm.stopPrank();\n        vm.warp(_now() + 48 hours);\n        br[0].applyPending();\n        vm.prank(safe);\n        br[0].propose(abi.encode(uint8(5), BS));\n        vm.warp(_now() + 48 hours);\n        br[0].applyPending();\n        for (uint256 k; k < 5 * 24 * 4; ++k) {\n            a = br[0].availableIn(BS) / 1e12 * 1e12;\n            if (a >= 10e18) _inbound(2, alice, a);\n            assertFalse(_tripped(BS));\n            vm.warp(_now() + 15 minutes);\n        }\n    }\n\n    /// The stated bound L_old + max(rates) x 4 d exceeds B for a lower inside the rule (63,636 / 7 d -> 1,030 / 1 h): the\n    /// no-trip property for lowers rests on the >= 96 h pause, not on that bound.\n    function test_statedBoundForALowerExceedsB() public pure {\n        uint256 lOld = 63_636e18;\n        uint256 lNew = 1_030e18;\n        assertLe(lOld * (7 days + SPAN), B * 7 days);\n        assertLe(lNew * (1 hours + SPAN), B * 1 hours);\n        assertGt(lOld + lNew * SPAN / 1 hours, B);\n    }\n\n    function test_C1_lowerOnOpenPathRefused() public {\n        vm.prank(safe);\n        vm.expectRevert(abi.encodeWithSelector(ImdBridge.PathOpen.selector, BS));\n        br[0].propose(_sp(2, 40_000e18, 4 days));\n        // and a pause lifted before application: refused at apply\n        vm.startPrank(safe);\n        br[0].pause(BS);\n        br[0].propose(_sp(2, 40_000e18, 4 days));\n        vm.stopPrank();\n        assertTrue(br[0].paused(BS));\n    }\n\n    function test_C1_duplicateInBatch() public {\n        bytes[] memory items = new bytes[](2);\n        items[0] = _sp(2, 60_000e18, 6 days);\n        items[1] = _sp(2, 50_000e18, 4 days);\n        vm.prank(safe);\n        vm.expectRevert(abi.encodeWithSelector(ImdBridge.DuplicateInBatch.selector, uint8(1), BS));\n        br[0].propose(abi.encode(uint8(6), items));\n    }\n\n    function test_lowerCannotReopenInItsOwnBatch() public {\n        vm.prank(safe);\n        br[0].pause(BS);\n        bytes[] memory items = new bytes[](2);\n        items[0] = _sp(2, 40_000e18, 4 days);\n        items[1] = abi.encode(uint8(5), BS);\n        vm.prank(safe);\n        vm.expectRevert(abi.encodeWithSelector(ImdBridge.UnpauseWithChange.selector, BS));\n        br[0].propose(abi.encode(uint8(6), items));\n    }\n\n    /// close keeps in-flight unscaled and frozen; a reopening (raise from 0) restarts full\n    function test_C1_closeThenReopen() public {\n        (, bool ok) = _inbound(2, alice, 30_000e18);\n        assertTrue(ok);\n        vm.prank(safe);\n        br[0].pause(BS);\n        _gov(_sp(2, 0, 4 days), 7, BS, 0, true);\n        (,, uint128 f,) = br[0].inbound(BS);\n        vm.warp(_now() + 10 days);\n        assertEq(br[0].availableIn(BS), 0);\n        (,, uint128 f2,) = br[0].inbound(BS);\n        assertEq(f, f2);\n        _gov(_sp(2, 20_000e18, 2 days), 9, BS, 20_000e18, true);\n        assertEq(br[0].availableIn(BS), 0, \"raise from 0 restarts full\");\n    }\n\n    /// sanity: the same greedy driver DOES trip a path once the rule is broken (breaker 90,000 < 100,000), so a pass above means something\n    function test_sanity_driverTripsWhenRuleBroken() public {\n        vm.prank(safe);\n        br[0].tighten(BS, 0, 0, 90_000e18, 0);\n        bool t;\n        for (uint256 k; k < 4 * 24 && !t; ++k) {\n            uint256 a = br[0].availableIn(BS) / 1e12 * 1e12;\n            if (a >= 10e18) _inbound(2, alice, a);\n            t = _tripped(BS);\n            vm.warp(_now() + 1 hours);\n        }\n        assertTrue(t, \"driver reaches the breaker\");\n    }\n\n    function testFuzz_countLowers(uint256 seed) public {\n        testFuzz_P1P2_inLimitNeverTripsAcrossSetPath(seed);\n        assertGt(lowers, 0, \"no lower in this run\");\n    }\n}\n"
 }
}