|
| 1 | +"""P4 regression tests: ripple completion (loop steps 12-13). |
| 2 | +
|
| 3 | +A successfully executed consequential action publishes a ``feed.ripple`` event |
| 4 | +(ripples out to the live feed). Privacy actions never ripple, reads never |
| 5 | +ripple, and an honest failure produces NO ripple — verified by watching the |
| 6 | +broadcaster's published-event counter across each request. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +from aiohttp import web |
| 11 | +from aiohttp.test_utils import TestClient, TestServer |
| 12 | + |
| 13 | +from gateway.service_routes import ServiceRoutes |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +async def env(): |
| 18 | + routes = ServiceRoutes(config={}) |
| 19 | + app = web.Application() |
| 20 | + routes.register_routes(app) |
| 21 | + async with TestClient(TestServer(app)) as c: |
| 22 | + yield routes, c |
| 23 | + |
| 24 | + |
| 25 | +def _published(routes) -> int: |
| 26 | + return routes.broadcaster.snapshot()["published_total"] |
| 27 | + |
| 28 | + |
| 29 | +async def test_executed_action_ripples(env): |
| 30 | + routes, client = env |
| 31 | + before = _published(routes) |
| 32 | + resp = await client.post( |
| 33 | + "/api/v1/groups", json={"creator": "0xabc", "name": "Builders"} |
| 34 | + ) |
| 35 | + assert resp.status == 200, await resp.text() |
| 36 | + assert _published(routes) == before + 1, "an executed action must ripple once" |
| 37 | + |
| 38 | + |
| 39 | +async def test_privacy_action_does_not_ripple(env): |
| 40 | + # /compute/store executes privacy.decentralized_store — a privacy-service |
| 41 | + # action must NEVER ripple (steps 12-13 respect privacy). |
| 42 | + routes, client = env |
| 43 | + before = _published(routes) |
| 44 | + resp = await client.post( |
| 45 | + "/api/v1/compute/store", |
| 46 | + json={"owner": "0xabc", "data": "0xdead", "storage_type": "ipfs"}, |
| 47 | + ) |
| 48 | + assert resp.status == 200, await resp.text() |
| 49 | + assert _published(routes) == before, "privacy actions must not ripple" |
| 50 | + |
| 51 | + |
| 52 | +async def test_read_does_not_ripple(env): |
| 53 | + # A read (get_conversations) changes no state and must not ripple. |
| 54 | + routes, client = env |
| 55 | + before = _published(routes) |
| 56 | + resp = await client.get("/api/v1/messaging/conversations?address=0xabc") |
| 57 | + assert resp.status == 200, await resp.text() |
| 58 | + assert _published(routes) == before, "reads must not ripple" |
| 59 | + |
| 60 | + |
| 61 | +async def test_honest_failure_produces_no_ripple(env): |
| 62 | + # An unsupported oracle pair is an honest 400 — no execution, no ripple. |
| 63 | + routes, client = env |
| 64 | + before = _published(routes) |
| 65 | + resp = await client.get("/api/v1/oracle/price/NOPE-NOPE") |
| 66 | + assert resp.status == 400, await resp.text() |
| 67 | + assert _published(routes) == before, "honest failures must not ripple" |
| 68 | + |
| 69 | + |
| 70 | +async def test_write_with_readish_prefix_still_ripples(env): |
| 71 | + # snapshot_vote is a consequential WRITE whose name begins with a formerly |
| 72 | + # read-classified prefix ("snapshot"). It must ripple — regression guard for |
| 73 | + # the over-broad read-prefix bug the adversarial pass caught. |
| 74 | + routes, client = env |
| 75 | + before = _published(routes) |
| 76 | + resp = await client.post( |
| 77 | + "/api/v1/governance/snapshot/vote", |
| 78 | + json={"voter": "0xabc", "space": "mydao.eth", "proposal_id": "p1", "choice": 1}, |
| 79 | + ) |
| 80 | + if resp.status == 200: |
| 81 | + assert _published(routes) == before + 1, "snapshot_vote (a write) must ripple" |
| 82 | + else: |
| 83 | + # If the backing method can't run here it must at least not be a silent |
| 84 | + # read-skip masquerading as success. |
| 85 | + assert resp.status != 200 or _published(routes) > before |
| 86 | + |
| 87 | + |
| 88 | +async def test_ripple_payload_shape(env): |
| 89 | + # The ripple event carries the actor + action so the feed can render it. |
| 90 | + routes, client = env |
| 91 | + events = [] |
| 92 | + orig = routes.broadcaster.publish_dict |
| 93 | + |
| 94 | + def _spy(type_, payload, **kw): |
| 95 | + events.append((type_, payload)) |
| 96 | + return orig(type_, payload, **kw) |
| 97 | + |
| 98 | + routes.broadcaster.publish_dict = _spy # type: ignore[assignment] |
| 99 | + resp = await client.post( |
| 100 | + "/api/v1/licensing/ip", |
| 101 | + json={"owner": "0xowner", "type": "patent", "name": "Widget"}, |
| 102 | + ) |
| 103 | + assert resp.status in (200, 400), await resp.text() |
| 104 | + if resp.status == 200: |
| 105 | + ripples = [p for t, p in events if t == "feed.ripple"] |
| 106 | + assert ripples, "an executed register_ip must emit feed.ripple" |
| 107 | + assert ripples[0]["actor"] == "0xowner" |
| 108 | + assert ripples[0]["service"] == "ip_royalties" |
0 commit comments