Files
seismo-relay/tests/test_5a_blastware_walk.py
T
serversdownandClaude Opus 5.5 887e75d1ba fix(minimateplus): MiniMate requests escape 02/03/04/10, and the 5A walk is 32-bit linear -- the 64 KB page bug
A recording of Blastware downloading all 8 of BE9441's events (three crossing
a 64 KB page; tests/fixtures/minimate-bench-10-26-26-blastware/) settles it:
every one of its 748 requests is the standard 16-byte payload with a SUM8
checksum and every 02 / 03 / 04 / 10 byte DLE-escaped -- 748/748 re-encode
byte-identically.  A 5A request is not a special frame: it is that payload with
a 2-byte LENGTH (0x0200 per chunk, the remainder for TERM) and a 4-byte linear
ADDRESS.  The walk is the event's start, +0x200 at a time carrying into the page
byte, then TERM at the next boundary for exactly the remainder.

What the old model misread: the fixed 'offset 0x1002' is length 0x0200 escaped
(10 02 00); the first event's 'metadata pages at 0x1002/0x1004' are its chunks
at 0x0200 and 0x0400; the 16-bit counter could not leave the key's page.

- build_bw_frame escapes the four values (was 0x10 only); new s3_stuff().
- build_5a_request(address, length); parse_strt_end_key() -> the full 32 bits.
- read_bulk_waveform_stream: one linear walk, no event-1 special case; strict
  mode keeps every refusal; an end at or before the start is refused as a
  corrupt STRT (a page crossing is no longer refused -- it downloads).

Tests: all 540 Blastware 5A requests and all 208 others rebuilt byte for byte;
SFM's walks for events 0, 1, 3 and 7 send exactly Blastware's requests and,
fed Blastware's recorded replies, build whole events.  In-page requests are
byte-identical to before.  No codec, store or DB change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-26 14:38:19 -04:00

155 lines
5.5 KiB
Python

"""SFM's MiniMate requests and 5A walk, against Blastware's own (recorded 2026-10-26).
Blastware downloaded all 8 of BE9441's events -- three of which cross a 64 KB page
-- while a bridge recorded both directions (fixture README). Every one of its 748
requests is the standard 16-byte payload with a SUM8 checksum and every 02 / 03 /
04 / 10 byte DLE-escaped; a 5A request carries a length (0x0200, or the remainder
for TERM) and a 4-byte linear address. The walk is the event's start, then
+0x200 at a time carrying into the page byte, then TERM at the next boundary for
exactly the remainder. SFM must send exactly those requests.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import minimateplus.protocol as mp # noqa: E402
from minimate_replay import fast_clock # noqa: E402
from minimateplus.blastware_file import extract_body_bytes # noqa: E402
from minimateplus.framing import S3FrameParser, build_5a_request, build_bw_frame, waveform_key_params # noqa: E402
from minimateplus.waveform_codec import STREAM_END_ID, decode_waveform_v2, walk_records # noqa: E402
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-blastware"
def _unstuff(b: bytes) -> bytes:
out, k = bytearray(), 0
while k < len(b):
if b[k] == 0x10 and k + 1 < len(b):
out.append(b[k + 1]); k += 2
else:
out.append(b[k]); k += 1
return bytes(out)
def bw_requests() -> list:
"""Blastware's requests, each as wire bytes; the SESSION_RESETs dropped."""
raw, out, i = (FIX / "blastware_requests.bin").read_bytes(), [], 0
while i < len(raw):
if raw[i:i + 2] == b"\x41\x03":
i += 2
continue
j = i + 2
while raw[j] != 0x03: # the first ETX not escaped by a DLE
j += 2 if raw[j] == 0x10 else 1
out.append(raw[i:j + 1])
i = j + 1
return out
def logical(frame: bytes) -> bytes:
return _unstuff(frame[2:-1])[:-1] # the 16-byte payload, checksum dropped
def unit_a5_replies() -> list:
raw, out, start, p = (FIX / "unit_replies.bin").read_bytes(), [], 0, S3FrameParser()
for i in range(len(raw)):
for f in p.feed(raw[i:i + 1]):
if f.sub == 0xA5:
out.append(raw[start:i + 1])
start = i + 1
return out
REQS = bw_requests()
FIVE_A = [f for f in REQS if logical(f)[2] == 0x5A]
def addr_len(frame: bytes) -> tuple:
p = logical(frame)
return int.from_bytes(p[6:10], "big"), int.from_bytes(p[4:6], "big")
def test_the_recording_is_what_the_readme_says():
assert (len(REQS), len(FIVE_A)) == (748, 540)
def test_every_blastware_5a_request_is_rebuilt_byte_for_byte():
assert [build_5a_request(*addr_len(f)) for f in FIVE_A] == FIVE_A
def test_every_other_blastware_request_is_rebuilt_byte_for_byte():
others = [f for f in REQS if logical(f)[2] != 0x5A]
assert len(others) == 208
for f in others:
p = logical(f)
assert build_bw_frame(p[2], p[5], p[6:16]) == f
def test_a_standard_request_escapes_02_03_04_in_its_params_and_checksum():
frame = build_bw_frame(0x0A, 0x46, waveform_key_params(bytes.fromhex("01120203")))
assert b"\x10\x02\x10\x03" in frame # the key's 02 and 03
for low in range(256): # a checksum of 0x03
f = build_bw_frame(0x0A, 0x46, waveform_key_params(bytes([0x01, 0x11, 0x50, low])))
if sum(_unstuff(f[2:-1])[:-1]) & 0xFF == 0x03:
assert f.endswith(b"\x10\x03\x03")
break
else:
pytest.fail("no key gave a checksum of 0x03")
# -- the walk ------------------------------------------------------------------
class ByAddress:
"""Answers each 5A request with Blastware's recorded reply for the same
(address, length); records what SFM sent."""
def __init__(self):
self.replies = {addr_len(q): r for q, r in zip(FIVE_A, unit_a5_replies())}
self.sent, self._buf = [], b""
def write(self, data):
data = bytes(data)
self.sent.append(data)
self._buf += self.replies.get(addr_len(data), b"")
def read(self, n):
out, self._buf = self._buf[:n], self._buf[n:]
return out
def blastware_walk(start: int) -> list:
"""Blastware's requests for the event that starts at `start`."""
i = next(k for k, f in enumerate(FIVE_A) if addr_len(f) == (start, 0x200))
walk = [FIVE_A[i]]
while addr_len(walk[-1])[1] == 0x200: # until TERM (a short read)
i += 1
walk.append(FIVE_A[i])
return walk
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
fast_clock(monkeypatch)
@pytest.mark.parametrize("key", ["01110000", "01118a2c", "0112a0b6", "0114b0b2"])
def test_the_walk_sends_exactly_blastwares_requests_and_the_event_is_whole(key):
t = ByAddress()
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True, strict=True)
want = blastware_walk(int(key, 16))
assert t.sent == want
assert len(frames) == len(want)
_s, body, _f = extract_body_bytes(frames)
recs = walk_records(body)
assert body[recs[-1]["end"] + 4] == STREAM_END_ID
dec = decode_waveform_v2(body)
assert len({len(dec[c]) for c in ("Tran", "Vert", "Long")}) == 1