{
 "item": "stock-calendar",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge Version: 1.9.0 (--use solar, solc 0.8.38)",
 "verdict": "findings",
 "findings": [
  {
   "id": "F1",
   "severity": "low",
   "title": "openTime double-counts the overlap when the offset table steps 5 h -> 4 h between two adjacent trading days: openTime(a,b) > b - a",
   "property": "P3",
   "claim": "P3 openTime never exceeds b - a",
   "files": [
    "src/stocks/MarketCalendar2.sol:136",
    "src/stocks/MarketCalendar2.sol:142-151",
    "src/stocks/MarketCalendar2.sol:266-275",
    "src/stocks/MarketCalendar2.sol:96-98"
   ],
   "sequence": "Winter (computed 5 h). Safe proposes SET_OFFSETS [(Tue 2026-12-08 << 32) | 14400] (any fromDay is accepted, a mid-week one included); 48 h later a stranger applies it. Monday's window (5 h) is [Tue 01:00 UTC - 24h, Tue 01:00 UTC); Tuesday's (4 h) starts Tue 00:00 UTC. openTime(Tue 00:00 UTC, Tue 01:00 UTC) = 7200 for a 3600 s span. session() resolves the overlap (later day first, comment at :96-98) but openTime sums both windows.",
   "expected": "openTime(a,b) <= b - a (each second counted once)",
   "actual": "7200 > 3600",
   "reachableAtDeploy": "no: needs a SET_OFFSETS (Safe, 48 h) whose fromDay is a Tue-Fri at a 5 h -> 4 h step. Money effect bounded: StockOracle.price Stale check (:214) fails closed 1 h sooner; StockOracle.sync STEP_SETTLE (:253) advances baseMultiplier up to 1 h of open time early per step boundary. No price moves.",
   "fix": "In _validate(SET_OFFSETS) refuse an entry whose fromDay is not a Saturday/Sunday/Monday-after-weekend boundary (require (fromDay+4)%7 in {0,1,6}, i.e. the step falls inside a weekend), or in openTime clip each day's window start to the previous trading day's window end.",
   "test": {
    "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {StockOracleBase} from \"../utils/StockOracleBase.sol\";\nimport {MarketCalendar2} from \"../../src/stocks/MarketCalendar2.sol\";\nimport {StockOracle} from \"../../src/stocks/StockOracle.sol\";\nimport {Governed} from \"../../src/Governed.sol\";\n\ncontract CalAudit is StockOracleBase {\n    uint8 constant SET_DAYS = 1;\n    uint8 constant SET_OFFSETS = 2;\n    // NYSE Rule 7.2 holidays / 13:00 closes derived by rule + IANA America/New_York offsets, 2026-11-26..2028-12-31:\n    // byte = status (0 open, 1 closed, 2 early) | offsetHours << 4\n    bytes constant TBL = hex\"...\"; // 767 bytes, generated (see P1 holds)\n    uint256 constant D0 = 20783;\n\n    function setUp() public { _deploy(); }\n\n    // P3 FINDING: an offset table step from 5 h to 4 h between two adjacent trading days (any weekday governance picks)\n    // overlaps the two windows by 1 h; openTime counts that hour twice\n    function test_P3_offsetStepDoubleCounts() public {\n        uint256 mon = cal.daysFromCivil(2026, 12, 7); // Monday, EST (computed 5 h)\n        uint256 tue = mon + 1;\n        _at(mon * 1 days - 3 days);\n        uint64[] memory e = new uint64[](1);\n        e[0] = uint64(tue << 32 | 4 hours); // Tuesday on: 4 h\n        vm.prank(governor); cal.propose(abi.encode(SET_OFFSETS, e));\n        _at(mon * 1 days - 1 days + 1);\n        cal.applyPending(); // a stranger\n        uint256 a = tue * 1 days; // Mon 19:00 New York (EST) = Tue 00:00 UTC = Tue 20:00 the evening before at 4 h\n        uint256 b = a + 1 hours;\n        assertLe(cal.openTime(a, b), b - a, \"openTime(a, b) <= b - a\");\n    }\n",
    "ran": "forge 1.9.0 --use solar, test/scratch/Cal.t.sol (StockOracleBase), --fuzz-runs 5000",
    "result": "[FAIL: openTime(a, b) <= b - a: 7200 > 3600] test_P3_offsetStepDoubleCounts() (gas: 296625)"
   }
  },
  {
   "id": "F2",
   "severity": "info",
   "title": "Sheet P3 does not describe the code: openTime returns type(uint256).max for a span over MAX_SPAN (14 days), which exceeds b - a",
   "property": "P3",
   "claim": "P3 openTime never exceeds b - a",
   "files": [
    "src/stocks/MarketCalendar2.sol:54-55",
    "src/stocks/MarketCalendar2.sol:138"
   ],
   "sequence": "openTime(a, a + 14 days + 1) for any a.",
   "expected": "<= 1209601 per the sheet",
   "actual": "type(uint256).max (documented at :54 'a longer one reads as type(uint256).max')",
   "reachableAtDeploy": "yes, by design. Effect fail-closed: StockOracle.price treats it as Stale (:214) and sync rolls the step base (:253) only after >14 days wall time, which is >= STEP_SETTLE open time anyway unless a >14-day closure. No money effect found; the property text should read 'never exceeds b - a, or is max when b - a > MAX_SPAN'.",
   "fix": "Amend P3 (or cap at b - a instead of max; callers compare against MAX_AGE/STEP_SETTLE which are below 14 days, so returning b - a would be equivalent for them).",
   "test": {
    "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {StockOracleBase} from \"../utils/StockOracleBase.sol\";\nimport {MarketCalendar2} from \"../../src/stocks/MarketCalendar2.sol\";\nimport {StockOracle} from \"../../src/stocks/StockOracle.sol\";\nimport {Governed} from \"../../src/Governed.sol\";\n\ncontract CalAudit is StockOracleBase {\n    uint8 constant SET_DAYS = 1;\n    uint8 constant SET_OFFSETS = 2;\n    // NYSE Rule 7.2 holidays / 13:00 closes derived by rule + IANA America/New_York offsets, 2026-11-26..2028-12-31:\n    // byte = status (0 open, 1 closed, 2 early) | offsetHours << 4\n    bytes constant TBL = hex\"...\"; // 767 bytes, generated (see P1 holds)\n    uint256 constant D0 = 20783;\n\n    function setUp() public { _deploy(); }\n\n    // P3: the MAX_SPAN cliff returns type(uint256).max for a span over 14 days\n    function test_P3_maxSpanCliff() public view {\n        uint256 a = monOpen;\n        assertLe(cal.openTime(a, a + 14 days + 1), 14 days + 1, \"openTime(a, b) <= b - a\");\n    }\n",
    "ran": "forge 1.9.0 --use solar, test/scratch/Cal.t.sol (StockOracleBase), --fuzz-runs 5000",
    "result": "[FAIL: openTime(a, b) <= b - a: 115792089237316195423570985008687907853269984665640564039457584007913129639935 > 1209601] test_P3_maxSpanCliff() (gas: 10486)"
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Known (README.md:824 '- **Calendar**: seeded to 2028, then fails open.', describes current code: dayStatus default OPEN, _trades :303-306). test_C1_failsOpenAfter2028 PASS: 2029-01-01 (Mon, NYSE New Year holiday, unseeded) 10:00 New York -> session REGULAR; a SESSION_ROUNDS-off StockOracle prices min(Fri 2028-12-29 last two rounds) (Stale does not trip: openTime since Friday 15:30 = 18.5 h < MAX_AGE 49 h); SESSION_ROUNDS-on reverts NotYet (no round in the session). So after 2028 every unseeded holiday prices the last trading day's rounds on SESSION_ROUNDS-off markets; borrows and liquidations run against a price the market is not making. Not worse than written; mitigation is SET_DAYS (DeployConfig.sol:633 'The Safe extends the calendar each year'), max 64 days per proposal, one status per proposal, 48 h each, one pending at a time: 2029 needs 2 proposals (~10 closures, ~2-3 early closes), feasible."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "closeDay :230-232 refuses when block.timestamp >= day*1d + offsetOf(day) + 9h30 or day > UTC today + 3. test_C2_closeDayEdge PASS (EDT Mon 2026-10-19 and EST Mon 2026-12-07: open-1 accepted, open BadDay). testFuzz_P2_closeDay PASS 5000 runs (any ts 2026-2028, day yesterday..today+5, acceptance == weekday && ts < open && day <= today+3; matured SET_DAYS of any status applied in front in the same block or at any later instant up to 20 days never re-opens the day). testFuzz_P2_timelockNeverCutsBegun PASS 5000 runs: a matured SET_DAYS (any status, 3 next weekdays) or SET_OFFSETS (1-4 entries, 0/4 h/5 h, any spacing) applied by anyone at any instant leaves the kind and open of every instant in the prior 25 h unchanged and the REGULAR instants of the rest of the UTC day unchanged. No route cuts a session under way short. Only effect on a begun window: closeDay before the open removes the already elapsed EXTENDED part of the day's window from openTime retroactively (not priced under S; makes the Stale/STEP_SETTLE clocks count that day as a holiday, consistent with feeds that do not print on one)."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "Known as written (README.md:824-827: 'a closed day unprices every stock market at once, so repeated `closeDay` before each open holds borrows and liquidations off (an accepted Safe power)'). Code matches: one calendar per chain (StageChain.sol:44), every StockOracle reads it (StockOracle.sol:215 kind != REGULAR -> Closed), MorphoLender binds o.calendar() == calendar (MorphoLender.sol:290). Reach is today..today+3 (:232), so holding liquidations off needs a Safe tx at least every 4 days; not worse than written."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "[data] RAN offline (no RPC needed): NYSE holidays derived from rule (New Year not observed on a Saturday, MLK, Presidents, Good Friday from computus, Memorial, Juneteenth/Jul 4/Christmas Sat->Fri Sun->Mon, Labor, Thanksgiving) and 13:00 closes (Jul 3 if a weekday trading day, day after Thanksgiving, Dec 24 if a weekday trading day), UTC offsets from IANA America/New_York (python zoneinfo) for every day 2026-11-26..2028-12-31. Seed decodes to 21 closed (2026-11-26, 2026-12-25, 2027-01-01, 01-18, 02-15, 03-26, 05-31, 06-18, 07-05, 09-06, 11-25, 12-24, 2028-01-17, 02-21, 04-14, 05-29, 06-19, 07-04, 09-04, 11-23, 12-25) and 5 early (2026-11-27, 2026-12-24, 2027-11-26, 2028-07-03, 2028-11-24): identical to the rule set. test_P1_seedMatchesNyse PASS: for every weekday, open-1 EXTENDED, open REGULAR with open ts exact, close-1 REGULAR, close EXTENDED, openTime(open,close)=close-open; holidays CLOSED; covers DST 2027-03-14, 2027-11-07, 2028-03-12, 2028-11-05. No wrong session found. Not checked against nyse.com's PDF itself (rules only)."
  },
  {
   "id": "C5",
   "result": "holds",
   "evidence": "Invariant covering every route that writes dayStatus (constructor _set :84-85, closeDay :249, _apply SET_DAYS :285) or _offsets (_apply SET_OFFSETS :288-290): INV(t) for every instant x <= t and every x in the window under way at t with x < close: (kind(x), open(x)) after any tx at t equals before, EXCEPT a REGULAR->CLOSED change of a day d with t < open(d) and d <= today+3 by the governor. Equivalently per day: status(d) and offsetOf(d) are frozen once UTC today >= d, except CLOSED_DAY writes before open(d). closeDay also drops a pending SET_DAYS listing d with status != CLOSED (:235-248). Tested as testFuzz_P2_timelockNeverCutsBegun + testFuzz_P2_closeDay. The one gap the invariant does not cover is cross-day: an offset step makes two days' windows overlap (F1); add to the invariant 'windows of distinct days are disjoint' and enforce it in _validate(SET_OFFSETS) (steps only across a weekend)."
  }
 ],
 "holds": [
  {
   "property": "P1",
   "tried": "test_P1_seedMatchesNyse over all 767 days 2026-11-26..2028-12-31 against rule-derived NYSE holidays/early closes and IANA offsets (4 session edges per trading day + openTime); C4 evidence."
  },
  {
   "property": "P2",
   "tried": "test_C2_closeDayEdge (EDT/EST second edges); testFuzz_P2_closeDay 5000 runs (time, day offset -1..+5, pending SET_DAYS status 0-2, applied before closeDay in the same block or after at any time to +20 d); testFuzz_P2_timelockNeverCutsBegun 5000 runs (SET_DAYS / SET_OFFSETS proposed 48 h-16 d before, applied by a stranger at any instant 2026-2028, 200 instants checked around it). Timelock is 48 h (Governed.sol:13,48) with re-validation at apply (Governed.sol:77), so a proposal for 'tomorrow' that matures after tomorrow is refused. Writers listed: dayStatus <- constructor, closeDay, _apply(SET_DAYS); _offsets <- _apply(SET_OFFSETS); pending <- propose, cancel, applyPending, closeDay. Readers: StockOracle.price (openTime, session :214-215), StockOracle.sync (openTime :253), MorphoLender (address equality :290); nextOpen/lastClose/windowOf/isTradingDay/dayStatus have no caller in src/ or script/ (grep). No staged resume adopts an old MarketCalendar2 (StageChain.sol:44 always deploys fresh)."
  },
  {
   "property": "P3",
   "tried": "testFuzz_P3_openTimeComputed 5000 runs (computed DST, 2026-2030, spans <= 14 d): <= b - a, monotone in b, additive. Fails only with an offset step (F1) or a span > 14 d (F2)."
  }
 ],
 "notRun": [
  "No [fork] claim in this sheet; no RPC used. C4 compared against NYSE rules + IANA tz, not the nyse.com PDF."
 ]
}